Class Agent
java.lang.Object
com.sgware.serialsoc.SerialSocket
edu.uky.cs.nil.tt.Agent
- All Implemented Interfaces:
Named, Closeable, AutoCloseable
An agent represents an individual connection to a
server that
can server as one of the roles in a session.
Every agent is assigned a unique, sequential ID number when it is
created. These ID numbers have no special meaning; they exist mainly to
identify an agent in system log messages. ID numbers start over at 0 each
time the server starts, and an agent's ID number is not preserved in places
like the database or session log.
An agent also has a name, which identifies the type of
agent it is. It is possible that multiple agents with the same name can be
connected to the server at the same time (if all of those agents are of the
same type). To identify a specific agent on the server, use its ID number.
- Author:
- Stephen G. Ware
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal intA unique, sequential ID number assigned to this agentfinal ServerThe server which manages this agentFields inherited from class com.sgware.serialsoc.SerialSocket
socket -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the current list of turns this agent can take in their session (which may be empty), or null if this agent has not yet been matched.booleanReturns true if this agent is still being managed in some way by the server.getGM()Returns thegame masterfor this agent's session (which might be this agent or their partner), or null if this agent has not yet been matched.getName()Returns this object's unique name that distinguishes it from other objects of the same type in the same context.Returns the other agent in this agent's session, or null if this agent has not yet been matched.Returns theplayerfor this agent's session (which might be this agent or their partner), or null if this agent has not yet been matched.getRole()Returns the role this agent is playing in its session, or null if the agent has not yet been matched.Returns the session this agent is participating in, or null if this agent has not yet been matched.getState()Returns the current state of the story world for this agent's session, as perceived by this agent, or null if this agent has not yet been matched.Returns the current status of the story for this agent's session, or null if this agent has not yet been matched.booleanChecks whether this agent has been sent astopmessage yet.getWorld()Returns the story world model in which this agent's session takes place, or null if the agent has not yet been matched.protected voidprotected voidonClose()protected voidprotected voidprotected voidThis method is called each time ajoinmessage is received to establish the agent's identity and preferences.protected voidThis method is called each time areportmessage is received to record the agent's responses.protected voidprotected voidThis method is called either when astopmessage is received or when a session is stopped by an agent's partner or the server.protected voidThis method is called each time a new turn is taken in the this agent's session.protected voidvoidEncodes a message as JSON and sends it to the client via this agent's socket.protected voidtick()This method is called regularly by the Server to check things that are time-sensative but not triggered by events, such as disconnecting agents who have taken too long to send a message.toString()Methods inherited from class com.sgware.serialsoc.SerialSocket
close, send
-
Field Details
-
server
The server which manages this agent -
id
public final int idA unique, sequential ID number assigned to this agent
-
-
Constructor Details
-
Agent
Constructs an agent.- Parameters:
server- the server that manages this agentsocket- the socket this agent uses to send and receive messagesid- a unique ID number assigned to the agent by the server- Throws:
Exception- if a problem occurs when creating the agent
-
-
Method Details
-
toString
-
getName
-
getConnected
public boolean getConnected()Returns true if this agent is still being managed in some way by the server. This method may continue to return true even after the agent's socket has been closed if the agent's information might still be needed by the server. When this method returns false, it means this agent's socket has been closed and its information is no longer needed.- Returns:
- true if the agent is still being used in some way by the server
-
getSession
Returns the session this agent is participating in, or null if this agent has not yet been matched.- Returns:
- this agent's session, or null if the agent has not been matched
-
getWorld
Returns the story world model in which this agent's session takes place, or null if the agent has not yet been matched.- Returns:
- this agent's story world model, or null if the agent has not yet been matched
-
getRole
Returns the role this agent is playing in its session, or null if the agent has not yet been matched.- Returns:
- the agent's role, or null if the agent has not yet been matched
-
getPartner
Returns the other agent in this agent's session, or null if this agent has not yet been matched.- Returns:
- this agent's partner, or null if this agent has not yet been matched
-
getGM
Returns thegame masterfor this agent's session (which might be this agent or their partner), or null if this agent has not yet been matched.- Returns:
- the game master of this agent's session, or null if this agent has not yet been matched
-
getPlayer
-
getStatus
Returns the current status of the story for this agent's session, or null if this agent has not yet been matched.- Returns:
- the status of the story for this agent's session, or null if this agent has not yet been matched
-
getState
Returns the current state of the story world for this agent's session, as perceived by this agent, or null if this agent has not yet been matched. If this agent is the session's game master, this method returns the actual state of the story world. If this agent is the session's player, this method returns the state the player perceives.- Returns:
- the state of the story world for this agent's session, or null if this agent has not yet been matched
-
getChoices
-
getStopped
public boolean getStopped()Checks whether this agent has been sent astopmessage yet.- Returns:
- true if this agent has been sent a stop message
-
onConnect
-
send
-
receive
-
onJoin
This method is called each time ajoinmessage is received to establish the agent's identity and preferences. Typically, an agent should send exactly one join message. If there is a problem processing the join message, the agent may be notified of the problem, allowing them to send another. Once an agent has successfully established their identity, they should not send any further join messages. This method sets this agent's name. If the agent is requesting a password-protected name, the password is verified. If everything is in order, this methodnotifies the serverthat a new agent has joined.- Parameters:
join- the join message containing the agent's identity and preferences- Throws:
Exception- if a problem occurs while establishing this agent's identify or preferencesIllegalStateException- if the agent has previously established its identityIllegalArgumentException- if the agent failed to provide the correct password when a password is needed
-
onStart
This method is called when theserverstarts a newsessionthat includes this agent. Typically, this should only happen once per session because agents are disconnected after a session ends. This method establishes things like the story world, this agent's partner, etc.- Parameters:
session- the new session this agent is participating ingm- the agent who is playing the game master role in the sessionplayer- the agent who is playing the player role in the session- Throws:
Exception- if a problem occurs while the agent is joining the sessionIllegalStateException- if the agent has not yetjoinedor if the agent is already part of a different session
-
onChoice
This method is called each time achoicemessage is received to take the chosenturnin the agent's session. Choices can only be made after a session starts, before it stops, and when it is this agent's turn. Once a turn is taken, both agent in the session arenotified.- Parameters:
choice- the choice that specifies which turn the agent wants to take- Throws:
Exception- if a problem occurs when taking the chosen turnIllegalStateException- if the agent has not yet begun a session, if that session has ended, if it is not this agent's turn to act, or if the index of the choice is out of bounds for the list of choices
-
onTurn
This method is called each time a new turn is taken in the this agent's session. The agent updates their current story status and sends the relevant information over the socket.- Parameters:
turn- the turn to be taken in the story- Throws:
Exception- if a problem occurs updating the story statusIllegalStateException- if the agent's session has not yet started
-
onReport
This method is called each time areportmessage is received to record the agent's responses. Reports can only be received after a session starts and before this agent either sends astopmessage or causes the session to stop.- Parameters:
report- the report message containing the details of the item and reported value- Throws:
IllegalStateException- if the agent's session has not yet started or has already stopped
-
tick
-
onStop
This method is called either when astopmessage is received or when a session is stopped by an agent's partner or the server. A session can only stop after it has started. Stop messages may be sent to the agent when the story reaches one of its pre-defined endings, but this method is not called then. This method is only called when the agent sends a stop message to the server, when the agent disconnects, or when the server stops a session. After this method is called, the agent cannot take more turns or send morereports. So an agent may send reports after they receive a stop message because the story reached a pre-defined ending, but they cannot send reports after they send a stop message to the server. Once both agents have either acknowledged that the session has stopped or disconnected, this methodalerts the serverthat the session has ended.- Parameters:
stop- the stop message containing the reason for stopping the session- Throws:
Exception- if a problem occurs while ending the sessionIllegalStateException- if the agent's session has not yet started
-
onClose
-
onDisconnect
-