public class AgentManager extends Object implements IAgentMonitorListener
Manages agent instances that are connected to the test framework.
Handles agents that are launched externally after they connect to the test framework,
and can also launch agents locally itself. Agents are represented as AgentJim
objects.
A new agent is launched by calling the getAgent(int, java.lang.String, boolean)
method with a uniform number
and team name that aren't taken yet, with the blocking parameter set to true.
Agents that are launched locally have the advantage of having all their standard output redirected to the test framework, where it can be handled by the GUI
Modifier and Type | Field and Description |
---|---|
private static AgentManager |
agentManager |
private long |
agentWaitTime |
private Logger |
logger |
private List<IAgentManagerListener> |
managerListeners |
private List<AgentJim> |
orderedAgents |
private List<Integer> |
queuedScenePlayers |
private Map<String,AgentJim> |
runningAgents
Maps the agents' unique names to their agent objects.
|
private Map<String,Process> |
runningProcesses
Maps the agents' unique names to their process objects.
|
private Map<Integer,Player> |
scenePlayerMap |
Modifier | Constructor and Description |
---|---|
private |
AgentManager() |
Modifier and Type | Method and Description |
---|---|
void |
addAgentManagerListener(IAgentManagerListener listener) |
void |
enqueueScenePlayer(Player p) |
AgentJim |
getAgent(int uniform,
String team,
boolean blocking)
Returns the object of an agent determined by its uniform number and team name.
|
AgentJim |
getAgentByOrder(int order)
Returns an agent determined by its position in the internal structures, or null
if no such agent exists.
|
int |
getAgentCount()
Returns the number of currently managed agents
|
int |
getFreeTFTPPort(String ip)
Returns the lowest expected free port for a TFTP server on a certain IP address
|
int |
getFreeUniform(String team)
Returns the lowest expected free uniform number in the specified team.
|
static AgentManager |
getManager()
Returns the instance of this singleton class
|
private void |
readForever(String agentName,
BufferedReader stream)
Handles the standard output of an agent process started by the test framework.
|
void |
receivedMessage(int uniform,
String team,
AgentMonitorMessage message)
Handles a message received from the agent
|
void |
removeAgent(AgentJim agent)
Removes an agent that was started by the test framework and terminates its process.
|
void |
removeAgentManagerListener(IAgentManagerListener listener) |
void |
setAgentWaitTime(long waitTime) |
void |
shutDownAgents()
Destroys all agent processes launched by the test framework
|
void |
startAgent(int uniform,
String team) |
void |
startAgent(int uniform,
String team,
boolean tftp_enabled,
int tftp_port)
Starts a new agent with the specified parameters
|
private Logger logger
private static AgentManager agentManager
private Map<String,Process> runningProcesses
private Map<String,AgentJim> runningAgents
Maps the agents' unique names to their agent objects. The unique name consists of the agent's uniform number followed by its team name (no spaces).
Unlike runningProcesses, this also contains agents that were launched externally.private long agentWaitTime
private List<IAgentManagerListener> managerListeners
public static AgentManager getManager()
public AgentJim getAgent(int uniform, String team, boolean blocking)
Returns the object of an agent determined by its uniform number and team name.
Can also launch a new agent when blocking is set to true. That is a blocking operation which takes quite a long time (roughly 10 seconds).
uniform
- uniform number of the agent, must be unique within a teamteam
- name of the team that the agent plays forblocking
- if true, a new agent will be started locally in case an agent with the given uniform number and team doesn't existpublic int getAgentCount()
public AgentJim getAgentByOrder(int order)
public void receivedMessage(int uniform, String team, AgentMonitorMessage message)
receivedMessage
in interface IAgentMonitorListener
uniform
- the uniform number of the agent who sent the messageteam
- the team of the agent who sent the messagemessage
- the message's objectpublic void startAgent(int uniform, String team)
public void startAgent(int uniform, String team, boolean tftp_enabled, int tftp_port)
uniform
- uniform number of the agentteam
- name of the agent's teamtftp_enabled
- whether to enable TFTP server on the agenttftp_port
- port on which the agent's TFTP server should listenprivate void readForever(String agentName, BufferedReader stream)
Handles the standard output of an agent process started by the test framework.
Notifies all the manager listeners about each new line of output.
agentName
- unique identifier of the agent, which is its uniform number followed by its team name (no spaces)stream
- input stream connected to the agent's standard outputpublic void removeAgent(AgentJim agent)
Removes an agent that was started by the test framework and terminates its process. Does nothing for agents that were not launched by the test framework.
TODO: send an exit command to agents not launched by the test framework
agent
- the agent to removepublic void shutDownAgents()
public int getFreeTFTPPort(String ip)
public int getFreeUniform(String team)
public void addAgentManagerListener(IAgentManagerListener listener)
public void removeAgentManagerListener(IAgentManagerListener listener)
public void enqueueScenePlayer(Player p)
public void setAgentWaitTime(long waitTime)