<commit_before>import threading, time, ast
import udpserver, tcpfilesocket, udpbroadcaster, messages
from constants import *

groupManager = None

class GroupManager():
    def __init__(self, config):
        self.groupName = config["group"]
        self.memberCount = 0
        self.groupMasterName = config["group_master_name"]
        self.groupMaster = config["group_master"]
        self.actions = config["actions"]
        self.memberHosts = []

        self.masterHost = ""

        if self.groupMaster:
            # init action handler thread
            self.actionHandler = GroupActionHandler(self.actions)
            self.actionHandler.daemon = True
            self.actionHandler.start()
            time.sleep(2)
            self.GroupMasterRoutine()
        else:
            # player is a group member --> broadcast acknowledge in case master is already online
            msgData = messages.getMessage(GROUP_MEMBER_ACKNOWLEDGE, ["-s", str(self.groupName)])
            udpbroadcaster.sendBroadcast(msgData)


    def GroupMasterRoutine(self):
        # send member request broadcast
        msgData = messages.getMessage(GROUP_MEMBER_REQUEST, ["-s", str(self.groupName)])
        udpbroadcaster.sendBroadcast(msgData, True)

    def HandleGroupMemberRequest(self, reqGroupName, masterIP):
        if not self.groupMaster and reqGroupName == self.groupName:
            self.masterHost = masterIP
            # member of requested group --> send acknowledge
            msgData = messages.getMessage(GROUP_MEMBER_ACKNOWLEDGE, ["-s", str(self.groupName)])
            udpbroadcaster.sendMessage(msgData, self.masterHost)

    def HandleGroupMemberAcknowledge(self, ackGroupName, memberIP):
        if self.groupMaster and ackGroupName == self.groupName:
	    print "MEMBER ACKNOWLEDGED: ", memberIP
            if not memberIP in self.memberHosts:
                self.memberHosts.append(memberIP)
                self.memberCount += 1
            self.actionHandler.AddHost(memberIP)

    def ScheduleActions(self, startUp):
        if self.groupMaster:
            # start thread if not already alive
            if not self.actionHandler.isAlive():
                self.actionHandler.start()
            self.actionHandler.startUp = startUp
            # set runevent to trigger action scheduling
            self.actionHandler.runevent.set()

    def StopActionHandling(self):
        if self.groupMaster:
            self.actionHandler.runevent.clear()
            # set update event --> causes handler to proceed and exit loop
            self.actionHandler.updateevent.set()



class GroupActionHandler(threading.Thread):
    def __init__(self, actions):
        self.actions = actions
        self.runevent = threading.Event()
        self.updateevent = threading.Event()
        self.actionThreads = []
        self.hosts = []
        self.running = True
        # bool flag to indicate we're in startup phase
        self.startUp = True

        threading.Thread.__init__(self, name="GroupActionHandler_Thread")

    def AddHost(self, host):
        if not host in self.hosts:
            self.hosts.append(host)
        # check if actions are defined that should be processed when a new group host came online
        for action in self.actions:
            # convert action to dict if needed
            try:
                actionDict = ast.literal_eval(action)
                action = actionDict
            except:
                pass
            if "type" in action and int(action['type']) == ACTION_EVENT_NEW_PLAYER:
                print "New Player found --> triggering action ", action
                # action found, handled like a startup action using the defined delay
                t = threading.Thread(target=self.__ProcessStartupAction, args=[action])
                t.daemon = True
                t.start()


    def run(self):
        # wait to get started
        self.runevent.wait()
        startupActions = []
        update = False
        while self.runevent.is_set():
            if update:
                # TODO: update run of loop
                # --> check for new actions and schedule them
                # --> check for removed actions and stop them
                pass

            for action in self.actions:
                # convert action to dict if needed
                try:
                    actionDict = ast.literal_eval(action)
                    action = actionDict
                except:
                    pass
                if "type" in action:<commit_msg>Fixes in type and event check in action handler, added localhost to be used in member list on group master player to pass commands to master itself as well, testing actions now.<commit_after>import threading, time, ast
import udpserver, tcpfilesocket, udpbroadcaster, messages
from constants import *

groupManager = None

class GroupManager():
    def __init__(self, config):
        self.groupName = config["group"]
        self.memberCount = 0
        self.groupMasterName = config["group_master_name"]
        self.groupMaster = config["group_master"]
        self.actions = config["actions"]
        self.memberHosts = []

        self.masterHost = ""

        if self.groupMaster:
            # init action handler thread
            self.actionHandler = GroupActionHandler(self.actions)
            self.actionHandler.daemon = True
            self.actionHandler.start()
            time.sleep(2)
            self.GroupMasterRoutine()
        else:
            # player is a group member --> broadcast acknowledge in case master is already online
            msgData = messages.getMessage(GROUP_MEMBER_ACKNOWLEDGE, ["-s", str(self.groupName)])
            udpbroadcaster.sendBroadcast(msgData)


    def GroupMasterRoutine(self):
	# add localhost to host list to receive commands on master player as well
	self.memberHosts.append('127.0.0.1')
	self.actionHandler.AddHost('127.0.0.1')
        # send member request broadcast
        msgData = messages.getMessage(GROUP_MEMBER_REQUEST, ["-s", str(self.groupName)])
        udpbroadcaster.sendBroadcast(msgData, True)

    def HandleGroupMemberRequest(self, reqGroupName, masterIP):
        if not self.groupMaster and reqGroupName == self.groupName:
            self.masterHost = masterIP
            # member of requested group --> send acknowledge
            msgData = messages.getMessage(GROUP_MEMBER_ACKNOWLEDGE, ["-s", str(self.groupName)])
            udpbroadcaster.sendMessage(msgData, self.masterHost)

    def HandleGroupMemberAcknowledge(self, ackGroupName, memberIP):
        if self.groupMaster and ackGroupName == self.groupName:
	    print "MEMBER ACKNOWLEDGED: ", memberIP
            if not memberIP in self.memberHosts:
                self.memberHosts.append(memberIP)
                self.memberCount += 1
            self.actionHandler.AddHost(memberIP)

    def ScheduleActions(self, startUp):
        if self.groupMaster:
            # start thread if not already alive
            if not self.actionHandler.isAlive():
                self.actionHandler.start()
            self.actionHandler.startUp = startUp
            # set runevent to trigger action scheduling
            self.actionHandler.runevent.set()

    def StopActionHandling(self):
        if self.groupMaster:
            self.actionHandler.runevent.clear()
            # set update event --> causes handler to proceed and exit loop
            self.actionHandler.updateevent.set()



class GroupActionHandler(threading.Thread):
    def __init__(self, actions):
        self.actions = actions
        self.runevent = threading.Event()
        self.updateevent = threading.Event()
        self.actionThreads = []
        self.hosts = []
        self.running = True
        # bool flag to indicate we're in startup phase
        self.startUp = True

        threading.Thread.__init__(self, name="GroupActionHandler_Thread")

    def AddHost(self, host):
        if not host in self.hosts:
            self.hosts.append(host)
        # check if actions are defined that should be processed when a new group host came online
        for action in self.actions:
            # convert action to dict if needed
            try:
                actionDict = ast.literal_eval(action)
                action = actionDict
            except:
                pass
            if "type" in action and int(action['type']) == ACTION_TYPE_ONETIME and int(action['event']) == ACTION_EVENT_NEW_PLAYER:
                print "New Player found --> triggering action ", action
                # action found, handled like a startup action using the defined delay
                t = threading.Thread(target=self.__ProcessStartupAction, args=[action])
                t.daemon = True
                t.start()


    def run(self):
        # wait to get started
        self.runevent.wait()
        startupActions = []
        update = False
        while self.runevent.is_set():
            if update:
                # TODO: update run of loop
                # --> check for new actions and schedule them
                # --> check for removed actions and stop them
                pass

            for action in self.actions:
                # convert action to dict if needed
                try:
                    actionDict = ast.literal_eval(action)
                    action = actionDict
                except:
                    pass
                if "type" in action:
{'fraction_non_alphanumeric': 0.07933149989422467, 'fraction_numerical': 0.001903955997461392, 'mean_word_length': 2.040192926045016, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5315140', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2327, 'n_words': 680}