massim.agent
Class AbstractAgent

java.lang.Object
  extended by massim.agent.AbstractAgent
Direct Known Subclasses:
DemoControlledAgent, DemoGridAgent, ExampleAgent

public abstract class AbstractAgent
extends java.lang.Object

This class provides a very simple foundation to agents. It will only connect once (no automatic reconnection). It will authenticate itself and wait for any messages. You can send ping using "sendPing" whenever


Field Summary
protected  javax.xml.parsers.DocumentBuilderFactory documentbuilderfactory
           
protected static java.util.logging.Logger logger
           
protected  java.lang.String username
           
 
Constructor Summary
AbstractAgent()
           
 
Method Summary
 void agentThread()
          Is the main agent's thread.
 boolean doAuthentication(java.lang.String username, java.lang.String password)
          Unifies the authentication process.
static java.lang.String getDate()
           
 java.lang.String getHost()
           
 java.lang.String getPassword()
           
 int getPort()
           
 java.lang.String getUsername()
           
 void processLogIn()
           
 boolean processMessage(org.w3c.dom.Element el_message)
          This method parses the message received from the server and selects the right action to do next.
 void processRequestAction(org.w3c.dom.Element perception, org.w3c.dom.Element target, long currenttime, long deadline)
           
 void processSimulationEnd(org.w3c.dom.Element perception, long currenttime)
           
 void processSimulationStart(org.w3c.dom.Element perception, long currenttime)
           
 boolean receiveAuthenticationResult()
          Waits for an authentication response from the server.
 org.w3c.dom.Document receiveDocument()
          Receives a packet from the server using the receivePacket method and converts the received data to a XML Document object.
 byte[] receivePacket()
          This method manages the reception of a packet from the server.
 void sendAuthentication(java.lang.String username, java.lang.String password)
          Provides a easy way for the authentication against a server.
 void sendDocument(org.w3c.dom.Document doc)
          Sends an specified XML Document to the server.
 void setHost(java.lang.String host)
           
 void setPassword(java.lang.String password)
           
 void setPort(int port)
           
 void setUsername(java.lang.String username)
           
 void start()
          Starts the agent main thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

username

protected java.lang.String username

documentbuilderfactory

protected javax.xml.parsers.DocumentBuilderFactory documentbuilderfactory

logger

protected static java.util.logging.Logger logger
Constructor Detail

AbstractAgent

public AbstractAgent()
Method Detail

getDate

public static java.lang.String getDate()

getHost

public java.lang.String getHost()

setHost

public void setHost(java.lang.String host)

getPort

public int getPort()

setPort

public void setPort(int port)

getUsername

public java.lang.String getUsername()

setUsername

public void setUsername(java.lang.String username)

getPassword

public java.lang.String getPassword()

setPassword

public void setPassword(java.lang.String password)

start

public void start()
Starts the agent main thread.

See Also:
agentThread

sendAuthentication

public void sendAuthentication(java.lang.String username,
                               java.lang.String password)
                        throws java.io.IOException
Provides a easy way for the authentication against a server. It must be called before the agent is bind to the server and the outputstream is initialized.

Parameters:
username - Username of the actual agent.
password - Password associated with the username.
Throws:
java.io.IOException - When the conection have not been initialized.

receiveAuthenticationResult

public boolean receiveAuthenticationResult()
                                    throws java.io.IOException
Waits for an authentication response from the server. It must be called after the sendAuthentication method call.

Returns:
true when the authentication hat been succesful, false othercase.
Throws:
java.io.IOException - When the conection have not been initialized.

doAuthentication

public boolean doAuthentication(java.lang.String username,
                                java.lang.String password)
                         throws java.io.IOException
Unifies the authentication process. It sends the authentication and then waits for the response from the server.

Parameters:
username - Username of the actual agent.
password - Password associated with the username.
Returns:
true when the authentication hat been succesful, false othercase.
Throws:
java.io.IOException - When the conection have not been initialized.
See Also:
sendAuthentication, receiveAuthenticationResult

receivePacket

public byte[] receivePacket()
                     throws java.io.IOException,
                            massim.agent.AbstractAgent.SocketClosedException
This method manages the reception of a packet from the server. It takes no parameters and suposes the authentication is done and hat succeed. It also writes to stderr the contents of the package.

Returns:
a byte array with the response from the server.
Throws:
java.io.IOException - When the conection have not been initialized.
SocketClosedException
massim.agent.AbstractAgent.SocketClosedException

receiveDocument

public org.w3c.dom.Document receiveDocument()
                                     throws org.xml.sax.SAXException,
                                            java.io.IOException,
                                            javax.xml.parsers.ParserConfigurationException,
                                            massim.agent.AbstractAgent.SocketClosedException
Receives a packet from the server using the receivePacket method and converts the received data to a XML Document object.

Returns:
A valid XML Document object.
Throws:
org.xml.sax.SAXException - When the received data is not wellformed.
java.io.IOException - When the conection have not been initialized.
javax.xml.parsers.ParserConfigurationException
SocketClosedException
massim.agent.AbstractAgent.SocketClosedException
See Also:
receivePacket

agentThread

public void agentThread()
Is the main agent's thread. It makes all the agent's work. First it manages the authentication, if it is not successful it will end. Then calls the processLogin method that is an user specified method. And next it remains in an infininte loop receiving and processing messages from the server. The messages must start with the message element. If it encounters any problem with the reception it ends execution.

See Also:
doAuthentication, processLogIn, receiveDocument, processMessage

processMessage

public boolean processMessage(org.w3c.dom.Element el_message)
This method parses the message received from the server and selects the right action to do next. The messages must be of the type:
  1. request-action
  2. sim-start
  3. sim-end
If the type is one of the first three, it builds a valid response envelop and calls the method related with the actual request wich will build the correct response content for the server. The responsible of sending such response is this method also, after it is builded.

Parameters:
el_message - XML Element object containing the message to process.
Returns:
true always
See Also:
processRequestAction, processSimulationStart, processSimulationEnd, sendDocument

processRequestAction

public void processRequestAction(org.w3c.dom.Element perception,
                                 org.w3c.dom.Element target,
                                 long currenttime,
                                 long deadline)

processSimulationEnd

public void processSimulationEnd(org.w3c.dom.Element perception,
                                 long currenttime)

processSimulationStart

public void processSimulationStart(org.w3c.dom.Element perception,
                                   long currenttime)

processLogIn

public void processLogIn()

sendDocument

public void sendDocument(org.w3c.dom.Document doc)
                  throws java.io.IOException
Sends an specified XML Document to the server.

Parameters:
doc - An XML Document object containing the message to send.
Throws:
java.io.IOException


Copyright © 2012. All Rights Reserved.