Esempio 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
Esempio n. 2
0
    def connect(self, host, port, teamname, version=11):
        self.__sock = sock.Socket(host, port)

        init_address = self.__sock.address
        init_msg = "(init %s (version %d))"
        self.__sock.send(init_msg % (teamname, version))

        self.__sock.send("move(0 0)")

        while self.__sock.address == init_address:
            time.sleep(0.0001)

        self.__thinking = False
        self.__think_thread = threading.Thread(target=self.__think_loop,
                                               name="think_loop")
        self.__think_thread.daemon = True