wiki:Python Quagents Client

Version 1 (modified by xwang, 13 years ago) (diff)

--

Python Quagents Client

Client.py:

This file contains the Client class. Clients connect to the server on port 6000. Each client stores information about it's name, id, commandid, etc. Calling the ready() function spawn a quagent.
NOTE: Sometimes calling ready() too fast will not spawn a quagent and the program will be stuck waiting for a response from the server. Calling setName() before ready() usually avoids this problem.

Command.py

This file contains the Command class and the individual command classes that inherit from the Command class. Each individual command takes in a different number of parameters and has a unique opcode. Each command must be attached to a client which is done by calling the setExecuter() function. The command can then be executed by calling execute().
NOTE: All the execute() function does is send the command to the server and then calls recv to wait for a response. I'm not sure if this is the correct way to do this. Executing multiple commands causes the received data to get screwed up at times so I included a time.sleep(0.05) after executing each command. There is also a bug with the responses from the radar command. The response seems to be broken up into two messages. The first message only contains the first line of the response and the second message contains everything else. The way execute() is implemented now only the first message is received.

Example Code

Writing a script to control a quagent is very simple. See the following example.

# Main.py
import client
import command
import time

agent = client.Client("127.0.0.1")	# Creates a quagent
agent.setName('Quagent')
agent.ready()				# Spawn

# Give commands to quagent by creating the command object and setting the quagent as the executor
fo = command.Follow('n')	
fo.setExecutor(agent)
fo.execute()
pu = command.PickUp('n')
pu.setExecutor(agent)
pu.execute()
ci = command.CheckInventory('n')	
ci.setExecutor(agent)
ci.execute()
pd = command.PutDown(0, 20, 'n')	# The parameters correspond to protocolzero. The last parameter is always the priority
pd.setExecutor(agent)
pd.execute()
ci.execute()