edu.udo.cs.ie.cowbots.arch.massim
Class AbstractAgent

Package class diagram package AbstractAgent
java.lang.Object
  extended by edu.udo.cs.ie.cowbots.arch.massim.AbstractAgent
Direct Known Subclasses:
MassimAgent

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 This code is taken from the MASSim software package. We have to implement the + processRequestAction + processSimulationBegin + processSimulationEnd + processLogIn methods properly. modified verison, supports asynchronous action response messages (author: Thomas)


Nested Class Summary
private  class AbstractAgent.SocketClosedException
           
 
Field Summary
(package private)  org.w3c.dom.Document actDoc
           
(package private)  org.w3c.dom.Element actEl
           
(package private)  java.lang.String actID
           
(package private)  java.util.concurrent.locks.ReentrantLock actLock
           
protected  javax.xml.parsers.DocumentBuilderFactory documentbuilderfactory
           
private  java.io.InputStream inputstream
           
protected static java.util.logging.Logger logger
           
private  java.lang.String networkhost
           
private  int networkport
           
private  java.io.OutputStream outputstream
           
private  java.lang.String password
           
private  java.net.Socket socket
           
private  java.net.InetSocketAddress socketaddress
           
private  javax.xml.transform.TransformerFactory transformerfactory
           
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 sendResponseAction(java.lang.String action)
          sends an action 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

networkport

private int networkport

networkhost

private java.lang.String networkhost

socketaddress

private java.net.InetSocketAddress socketaddress

socket

private java.net.Socket socket

inputstream

private java.io.InputStream inputstream

outputstream

private java.io.OutputStream outputstream

username

protected java.lang.String username

password

private java.lang.String password

documentbuilderfactory

protected javax.xml.parsers.DocumentBuilderFactory documentbuilderfactory

transformerfactory

private javax.xml.transform.TransformerFactory transformerfactory

logger

protected static java.util.logging.Logger logger

actLock

java.util.concurrent.locks.ReentrantLock actLock

actID

java.lang.String actID

actDoc

org.w3c.dom.Document actDoc

actEl

org.w3c.dom.Element actEl
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,
                            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.
AbstractAgent.SocketClosedException

receiveDocument

public org.w3c.dom.Document receiveDocument()
                                     throws org.xml.sax.SAXException,
                                            java.io.IOException,
                                            javax.xml.parsers.ParserConfigurationException,
                                            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
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

sendResponseAction

public void sendResponseAction(java.lang.String action)
sends an action to the server

Parameters:
action - action to send

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