Exemplo n.º 1
0
    def connect(self, host, port, teamname, version='15.5'):
        """
        Gives us a connection to the server as one player on a team.  This
        immediately connects the agent to the server and starts receiving and
        parsing the information it sends.
        """

        # if already connected, raise an error since user may have wanted to
        # connect again to a different server.
        if self.__connected:
            msg = "Cannot connect while already connected, disconnect first."
            raise sp_exceptions.AgentConnectionStateError(msg)

        # the pipe through which all of our communication takes place
        self.__sock = sock.Socket(host, port)

        # our models of the world and our body
        self.wm = WorldModel(handler.ActionHandler(self.__sock, self.teamname))

        # set the team name of the world model to the given name
        self.wm.teamname = teamname

        # handles all messages received from the server
        self.msg_handler = handler.MessageHandler(self.wm, self.teamname)

        self.action_handler = handler.ActionHandler(port, self.teamname)

        # set up our threaded message receiving system
        self.__parsing = True  # tell thread that we're currently running
        self.__msg_thread = threading.Thread(target=self.__message_loop,
                                             name="message_loop")
        self.__msg_thread.daemon = True  # dies when parent thread dies

        # start processing received messages. this will catch the initial server
        # response and all subsequent communication.
        self.__msg_thread.start()

        # send the init message and allow the message handler to handle further
        # responses.
        init_address = self.__sock.address
        init_msg = "( init %s ( version %s ) )"
        self.__sock.send(init_msg % (teamname, version))
        print(init_msg)

        # wait until the socket receives a response from the server and gets its
        # assigned port.
        while self.__sock.address == init_address:
            time.sleep(0.0001)

        # create our thinking thread.  this will perform the actions necessary
        # to play a game of robo-soccer.
        self.__thinking = False
        self.__think_thread = threading.Thread(target=self.__think_loop,
                                               name="think_loop")
        self.__think_thread.daemon = True

        # set connected state.  done last to prevent state inconsistency if
        # something goes wrong beforehand.
        self.__connected = True
Exemplo n.º 2
0
async def on_message(message):
    if message.content.startswith('!'):
        try:
            message_handler = handler.MessageHandler(message, client)
        except ValueError:
            raise ValueError('Could not initialise the Handler')

        await message_handler.fill_payload()
        await message_handler.post_message()
Exemplo n.º 3
0
    def stream_messages(self):
        '''
        This function creates a stream of messages at different
        intervals (for testing), and sends them to a message handler.
        The message handler will return (by being called) sychronized
        messages when enough time has passed, or if the max number of
        messages have been streamed.

        This function will create a message handler object to read and
        save synched messages.  It will then call helper functions
        to save and display the synched messages.

        :return: nothing
        '''

        print('Creating stream of messages...')  # for testing

        # Assumed parameters for testing
        N = 10  # number of possible different sources for the messages
        time_interval = 5  # secs, criteria for when to send synchronized messages back
        delta = time_interval / 2  # seconds, window of messages to capture
        max_messages = 6  #  also limit number of messages streamed before getting a syched message back

        # variables for when to synchronize messages
        msg_handler = handler.MessageHandler(
            delta, time_interval, max_messages)  # create a Message_handler
        current_msg = 0  # counter

        # infinite loop to create a stream of messages, and also when to synchronize messages
        while (True):
            # create a message (will be made at random intervals)
            source = random.randint(0, N)  # assign random source
            body = 'This is the body of the message, all the same'
            timestamp = time.time()  # in seconds, float
            message = handler.Message(timestamp, body,
                                      source)  # create a message obj
            current_msg += 1  # update number of messages

            # for visualization
            print("Raw message {} created at time {}".format(
                current_msg, message.timestamp))

            #  send message to msg_handler (alternatively, this could be run
            #  independently with multithreading
            sync_msg = msg_handler.read_stream(message)

            # if response returned, save and display
            if sync_msg:
                self.synched_list.append(sync_msg)
                self.display_synched_messages(sync_msg)

            # sleep sporadic time between .1 and 2 seconds (to create a stream of random intervals as inputs)
            rand_int = random.randint(100, 2000)
            time.sleep(rand_int / 1000)