Example #1
0
 def __init__(self, ip_address, logger):
     self.__msg_senders = dict()
     self.__msg_receivers = dict()
     self.__stopSockets = list()
     self.__address = ip_address
     self.__bots = self.get_bots()
     self.__logger = logger
     self.__inbox = Inbox(self.__logger)
     self.__started = False
     for bot in self.__bots:
         self.__logger.debug(ip_address + " added sender and receiver for: " + bot)
         self.__msg_senders[bot] = MessageSender(bot, self.PORT, self.__logger)
         self.__msg_receivers[bot] = MessageReceiver(bot, self.__inbox, self.__logger)
     self.__connection = ConnectionsListener(self.__address, self.PORT, self.__msg_receivers, self.__logger)
Example #2
0
 def __init__(self, ip_address, logger):
     self.__msg_senders = dict()
     self.__msg_receivers = dict()
     self.__stopSockets = list()
     self.__address = ip_address
     self.__bots = self.get_bots()
     self.__logger = logger
     self.__inbox = Inbox(self.__logger)
     self.__started = False
     for bot in self.__bots:
         bot = bot.rstrip(
         )  ## remove '\n' -> should have been done by get_bots(), but appearantly this didnt work
         self.__logger.debug(ip_address +
                             " added sender and receiver for: " + bot)
         self.__msg_senders[bot] = MessageSender(bot, self.PORT,
                                                 self.__logger)
         self.__msg_receivers[bot] = MessageReceiver(
             bot, self.__inbox, self.__logger)
     self.__connection = ConnectionsListener(self.__address, self.PORT,
                                             self.__msg_receivers,
                                             self.__logger)
Example #3
0
 def __init__(self, ip_address, logger):
     self.__msg_senders = dict()
     self.__msg_receivers = dict()
     self.__stopSockets = list()
     self.__address = ip_address
     self.__bots = self.get_bots()
     self.__logger = logger
     self.__inbox = Inbox(self.__logger)
     self.__started = False
     for bot in self.__bots:
         self.__logger.debug(ip_address + " added sender and receiver for: " + bot)
         self.__msg_senders[bot] = MessageSender(bot, self.PORT, self.__logger)
         self.__msg_receivers[bot] = MessageReceiver(bot, self.__inbox, self.__logger)
     self.__connection = ConnectionsListener(self.__address, self.PORT, self.__msg_receivers, self.__logger)
Example #4
0
class Distributor:
    "Distributer class for distributed neat, only used if phys_dis_neat=True in class NEATPopulation"
    PORT = 4242

    def __init__(self, ip_address, logger):
        self.__msg_senders = dict()
        self.__msg_receivers = dict()
        self.__stopSockets = list()
        self.__address = ip_address
        self.__bots = self.get_bots()
        self.__logger = logger
        self.__inbox = Inbox(self.__logger)
        self.__started = False
        for bot in self.__bots:
            bot = bot.rstrip(
            )  ## remove '\n' -> should have been done by get_bots(), but appearantly this didnt work
            self.__logger.debug(ip_address +
                                " added sender and receiver for: " + bot)
            self.__msg_senders[bot] = MessageSender(bot, self.PORT,
                                                    self.__logger)
            self.__msg_receivers[bot] = MessageReceiver(
                bot, self.__inbox, self.__logger)
        self.__connection = ConnectionsListener(self.__address, self.PORT,
                                                self.__msg_receivers,
                                                self.__logger)

    def get_bots(self):
        with open('bots.txt') as bots_file:
            bots = bots_file.readlines()
        bots = filter(lambda b: b != "\n" and not b.startswith(self.__address),
                      bots)
        return [bot.replace("\n", "") for bot in bots]

    """
        Sends the Genotype to all robots listed in te bots.txt file. The function will return True when the sending was
        successful and False otherwise
    """

    def send_genotype(self, genotype):
        self.__logger.debug("Sending genotype to listeners: " + str(genotype))
        if self.__started:
            for addr in self.__msg_senders:
                bot = self.__msg_senders[addr]
                self.__logger.debug("Sending message to " + addr)
                bot.outboxAppend(genotype)
            return True
        else:
            self.__logger.debug(
                "Attempted to send genotype without starting the Distributor")
            return False

    def get_genotypes(self):
        genotypes = self.__inbox.popAll()
        self.__logger.info("Recieved " + str(len(genotypes)) + " genotypes")
        return genotypes

    """
        Start waiting for messages and enable the senders to send messages to the other bots
    """

    def start(self):
        self.__connection.start()

        for addr in self.__msg_senders:
            self.__msg_senders[addr].start()
        for addr in self.__msg_receivers:
            self.__msg_receivers[addr].start()
        self.__started = True

    def stop(self):
        if not self.__started:
            return

        for addr in self.__msg_senders:
            self.__logger.info('Killing Sender ' + addr)
            self.__msg_senders[addr].stop()
            self.__msg_senders[addr].join()
        self.__logger.info('All MessageSenders: KILLED')

        # Stopping connections listener: no more incoming connections
        self.__connection.stop()
        self.__connection.join()
        self.__logger.info('ConnectionsListener: KILLED')

        for addr in self.__msg_receivers:
            self.__logger.info("Killing receiver " + addr)
            self.__msg_receivers[addr].stop()
            self.__msg_receivers[addr].join()

        self.__logger.info("All MessageReceivers KILLED")
        self.__started = False
        self.__logger.close()
Example #5
0
class Distributor:
    PORT = 4242

    def __init__(self, ip_address, logger):
        self.__msg_senders = dict()
        self.__msg_receivers = dict()
        self.__stopSockets = list()
        self.__address = ip_address
        self.__bots = self.get_bots()
        self.__logger = logger
        self.__inbox = Inbox(self.__logger)
        self.__started = False
        for bot in self.__bots:
            self.__logger.debug(ip_address + " added sender and receiver for: " + bot)
            self.__msg_senders[bot] = MessageSender(bot, self.PORT, self.__logger)
            self.__msg_receivers[bot] = MessageReceiver(bot, self.__inbox, self.__logger)
        self.__connection = ConnectionsListener(self.__address, self.PORT, self.__msg_receivers, self.__logger)

    def get_bots(self):
        with open('bots.txt') as bots_file:
            bots = bots_file.readlines()
        bots = filter(lambda b: b != "\n" and not b.startswith(self.__address), bots)
        return [bot.replace("\n", "") for bot in bots]

    """
        Sends the Genotype to all robots listed in te bots.txt file. The function will return True when the sending was
        successful and False otherwise
    """
    def send_genotype(self, genotype):
        self.__logger.debug("Sending genotype to listeners: " + str(genotype))
        if self.__started:
            for addr in self.__msg_senders:
                bot = self.__msg_senders[addr]
                self.__logger.debug("Sending message to " + addr)
                bot.outboxAppend(genotype)
            return True
        else:
            self.__logger.debug("Attempted to send genotype without starting the Distributor")
            return False

    def get_genotypes(self):
        genotypes = self.__inbox.popAll()
        self.__logger.info("Recieved " + str(len(genotypes)) +  " genotypes")
        return genotypes

    """
        Start waiting for messages and enable the senders to send messages to the other bots
    """
    def start(self):
        self.__connection.start()

        # for i in range(0, len(self.__msg_receivers)):
        #     stop_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #     stop_socket.connect(('127.0.0.1', '1337'))
        #     self.__stopSockets.append(stop_socket)

        for addr in self.__msg_senders:
            self.__msg_senders[addr].start()
        for addr in self.__msg_receivers:
            self.__msg_receivers[addr].start()
        self.__started = True

    def stop(self):
        if not self.__started:
            return

        for addr in self.__msg_senders:
            self.__logger.info('Killing Sender ' + addr)
            self.__msg_senders[addr].stop()
            self.__msg_senders[addr].join()
        self.__logger.info('All MessageSenders: KILLED')

        # Stopping connections listener: no more incoming connections
        self.__connection.stop()
        self.__connection.join()
        self.__logger.info('ConnectionsListener: KILLED')

        for addr in self.__msg_receivers:
            self.__logger.info("Killing receiver " + addr)
            self.__msg_receivers[addr].stop()
            self.__msg_receivers[addr].join()

        self.__logger.info("All MessageReceivers KILLED")
        self.__started = False
        self.__logger.close()
Example #6
0
class Distributor:
    PORT = 4242

    def __init__(self, ip_address, logger):
        self.__msg_senders = dict()
        self.__msg_receivers = dict()
        self.__stopSockets = list()
        self.__address = ip_address
        self.__bots = self.get_bots()
        self.__logger = logger
        self.__inbox = Inbox(self.__logger)
        self.__started = False
        for bot in self.__bots:
            self.__logger.debug(ip_address + " added sender and receiver for: " + bot)
            self.__msg_senders[bot] = MessageSender(bot, self.PORT, self.__logger)
            self.__msg_receivers[bot] = MessageReceiver(bot, self.__inbox, self.__logger)
        self.__connection = ConnectionsListener(self.__address, self.PORT, self.__msg_receivers, self.__logger)

    def get_bots(self):
        with open('bots.txt') as bots_file:
            bots = bots_file.readlines()
        bots = filter(lambda b: b != "\n" and not b.startswith(self.__address), bots)
        return [bot.replace("\n", "") for bot in bots]

    """
        Sends the Genotype to all robots listed in te bots.txt file. The function will return True when the sending was
        successful and False otherwise
    """
    def send_genotype(self, genotype):
        self.__logger.debug("Sending genotype to listeners: " + str(genotype))
        if self.__started:
            for addr in self.__msg_senders:
                bot = self.__msg_senders[addr]
                self.__logger.debug("Sending message to " + addr)
                bot.outboxAppend(genotype)
            return True
        else:
            self.__logger.debug("Attempted to send genotype without starting the Distributor")
            return False

    def get_genotypes(self):
        genotypes = self.__inbox.popAll()
        self.__logger.info("Recieved " + str(len(genotypes)) +  " genotypes")
        return genotypes

    """
        Start waiting for messages and enable the senders to send messages to the other bots
    """
    def start(self):
        self.__connection.start()

        # for i in range(0, len(self.__msg_receivers)):
        #     stop_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #     stop_socket.connect(('127.0.0.1', '1337'))
        #     self.__stopSockets.append(stop_socket)

        for addr in self.__msg_senders:
            self.__msg_senders[addr].start()
        for addr in self.__msg_receivers:
            self.__msg_receivers[addr].start()
        self.__started = True

    def stop(self):
        if not self.__started:
            return

        for addr in self.__msg_senders:
            self.__logger.info('Killing Sender ' + addr)
            self.__msg_senders[addr].stop()
            self.__msg_senders[addr].join()
        self.__logger.info('All MessageSenders: KILLED')

        # Stopping connections listener: no more incoming connections
        self.__connection.stop()
        self.__connection.join()
        self.__logger.info('ConnectionsListener: KILLED')

        for addr in self.__msg_receivers:
            self.__logger.info("Killing receiver " + addr)
            self.__msg_receivers[addr].stop()
            self.__msg_receivers[addr].join()

        self.__logger.info("All MessageReceivers KILLED")
        self.__started = False
        self.__logger.close()