All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class RouterLayer.Router.RouterRecvThread

Abstract.Connection
   |
   +----Abstract.ReceiverThread
           |
           +----BaseLayer.BRecvThread
                   |
                   +----KQMLLayer.KQMLRecvThread
                           |
                           +----RouterLayer.Router.RouterRecvThread

public class RouterRecvThread
extends KQMLRecvThread
RouterRecvThread is a subclass of the BReceiverThread class. RouterRecvThread action is:
Run(while true) The RouterRecvThread will retain its own MessageBuffer, which does not related to the RouterAction. The Router utilities will be delegated to the RouterRecvThread. For message reservation, the RouterRecvThread will create Reservation object and add it to the ReservationTable, cretated and maintained by the RouterAction. For delete-message operation, the delete-messages can be buffered. You can set the _maxDeleteSize to change the buffer size. Default is 1, this means that the message numbers will be updated every time for delete operation is performed. The message numbering should be identical to the RotuerClientAction. Therefore, if you set the delete-message buffer size, the RouterClientAction delete-message buffer size should be changed, too.

See Also:
RouterServerThread, RouterClientAgentAction, RouterAction

Variable Index

 o _currentFileSize
Current incoming file size
 o _currentFPTSize
Current message size in the MessageBuffer
 o _deleteFPT
Vector to contains deleted message number.
 o _deleteFPTSize
Current delete-message size
 o _disconnectTable
Table, which contains the disconnected agent list.
 o _incomingFile
Incoming file
 o _maxDeleteSize
Maximun buffer size for deleted messages
 o _messageBoxPath
Message box file path By giving messageBoxPath, I am assuming that the file extension as
incoming file - agentname.incoming
message file pointer file - agentname.fpt
reserved message file - agentname.reserved.timestamp.
 o _msgFPTStream
Cache file pointer of the messages.
 o _offLineList
Agent name array which contains the agent names those are off-line intentionally.
 o _reservedBook
Hashtable to record reserved messages.
 o CREATEFILEFAILED
File creation for the incoming file box failed
 o EMAILNOTSPECIFIED
Can not find email address for receiver
 o INTERNALERROR
Internal error
 o MESSAGESIZETOOBIG
Message size is over MAX_MSG_LEN.
 o NAMENOTSPECIFIED
The receiver name is not specified in the KMQL message.
 o NOTREGISTEREDUSER
The agent is not an registered user.
 o NOTVALIDMESSAGEMETHOD
Message method is not understandable
 o PARSEERROR
Parse error will be immediately returned
 o PASSWORDMISSING
Agent password is not passed during the Security process
 o RECEIVERMISSING
Receiver is not specified
 o UNREGISTERFAILED
Unregister is not authorized
 o WRONGKQMLFORMAT
Wrong format KQML message will be immediately returned

Constructor Index

 o RouterRecvThread()
Default Constructor.
 o RouterRecvThread(Address, Address, ConnectionTable, String, Security, OffLineAgentTable)
Router receiver thread to send off-line agent's messages back to the senders with error.
 o RouterRecvThread(Address, int, Address, ConnectionTable, MessageBuffer, Security, String, Hashtable, OffLineAgentTable, Vector, ThreadGroup)
This constructor is called when an agent want to initialize a connection to the other agents, using Address
 o RouterRecvThread(Socket, int, Address, ConnectionTable, MessageBuffer, Security, String, Hashtable, OffLineAgentTable, Vector)
Constructor, to be called from the serverthread after accepting a connection.
 o RouterRecvThread(String)
Constructor with the Agent name.

Method Index

 o checkTime()
Override BRecvThread's checkTime method to add the agent name to the off line agent list
 o cleanUp()
Clean up the files and remove connection from the connection table
 o closeSocket()
Close socket and stop this thread
 o deleteMessages()
Does not necessarily be synchronized.
 o endConn()
If there are messages to be deleted, deleteMessages() will be invoked Send 'disconnect' message to the agent and Close all the connections.
 o forwardMessage(String)
New messages are received from the socket.
 o getMsgHeader()
Return message header, 'N 86472389 ' where 86472389 represent current time
 o initialize()
Send queued messages to the agent.
 o openFiles()
Send messages to the receiver by retriving file and open necessary files.
 o processDefaultProtocol(KQMLmessage)
Processing default protocols : disconnect-agent, reserve-message, list-agent, request-address, and unregister-agent.
 o receiveMessage()
Message received to the queue.
 o reserveMessages(KQMLmessage)
Message reservation on a specific time Generate reserved message file.
 o run()
The run() method will performed the Security check at first.
 o saveMessageToFile(String, String)
This method is invoked under the assumption that the receiver is off-line and the Router can not connect to the receiver
 o saveMessageToIncomingFile(String)
 o sendErrorMessage(int, String)
Send error message to the agent is error occurs.
 o sendMessage(String, String)
Send message to the intended receiver.
 o sendOffLineAgentErrorMessages()
 o sendReservedMessages(String)
send reserved messages, invoked from the RouterAction The reasoning is that when the RouterAction want to send reserved message and the receiver thread is running, it does not need to open a new connection to send.
 o setDisconnectTable(Vector)
Set the disconnect table
 o setOffLineList(OffLineAgentTable)
Set the off line agent name list.
 o setReservedBook(Hashtable)
Set the ReservationTable
 o writeMsg(String)
The thread just writes on the connection for the remote side.

Variables

 o NOTREGISTEREDUSER
 protected static final int NOTREGISTEREDUSER
The agent is not an registered user. The error message will be sent when an agent want to ANS service, but the agent is not on the registered user list

 o NAMENOTSPECIFIED
 protected static final int NAMENOTSPECIFIED
The receiver name is not specified in the KMQL message. Error message will be sent

 o CREATEFILEFAILED
 protected static final int CREATEFILEFAILED
File creation for the incoming file box failed

 o PASSWORDMISSING
 protected static final int PASSWORDMISSING
Agent password is not passed during the Security process

 o NOTVALIDMESSAGEMETHOD
 protected static final int NOTVALIDMESSAGEMETHOD
Message method is not understandable

 o RECEIVERMISSING
 protected static final int RECEIVERMISSING
Receiver is not specified

 o INTERNALERROR
 protected static final int INTERNALERROR
Internal error

 o UNREGISTERFAILED
 protected static final int UNREGISTERFAILED
Unregister is not authorized

 o PARSEERROR
 protected static final int PARSEERROR
Parse error will be immediately returned

 o WRONGKQMLFORMAT
 protected static final int WRONGKQMLFORMAT
Wrong format KQML message will be immediately returned

 o EMAILNOTSPECIFIED
 protected static final int EMAILNOTSPECIFIED
Can not find email address for receiver

 o MESSAGESIZETOOBIG
 protected static final int MESSAGESIZETOOBIG
Message size is over MAX_MSG_LEN. Will be disconnected from the router

 o _reservedBook
 protected Hashtable _reservedBook
Hashtable to record reserved messages. The key is time stamp and the field value is reservation object

 o _offLineList
 protected OffLineAgentTable _offLineList
Agent name array which contains the agent names those are off-line intentionally. If a message receiver is in the off-line list, the RouterRecvThread will not send the message to the receiver. The message will be saved to the file. If the receiver is reconnect, the received messages will be sent.

 o _disconnectTable
 protected Vector _disconnectTable
Table, which contains the disconnected agent list. If the agent send 'disconnect-agent' message to the router, the router will add the agent to this table. Until the agent will reconnect, the router will not send message to the agent.

 o _messageBoxPath
 protected String _messageBoxPath
Message box file path By giving messageBoxPath, I am assuming that the file extension as
incoming file - agentname.incoming
message file pointer file - agentname.fpt
reserved message file - agentname.reserved.timestamp. Time stamp is 12 digit numbers

 o _deleteFPT
 protected int _deleteFPT[]
Vector to contains deleted message number. Delete operation will be performed either when the receiver thread is disconnected or number of delete-message is greater than _maxDeleteSize

 o _currentFPTSize
 protected int _currentFPTSize
Current message size in the MessageBuffer

 o _currentFileSize
 protected long _currentFileSize
Current incoming file size

 o _deleteFPTSize
 protected int _deleteFPTSize
Current delete-message size

 o _maxDeleteSize
 protected int _maxDeleteSize
Maximun buffer size for deleted messages

 o _msgFPTStream
 protected RandomAccessFile _msgFPTStream
Cache file pointer of the messages.

 o _incomingFile
 protected RandomAccessFile _incomingFile
Incoming file

Constructors

 o RouterRecvThread
 public RouterRecvThread()
Default Constructor.

 o RouterRecvThread
 public RouterRecvThread(String name)
Constructor with the Agent name. If you use this constructor, you should use set methods to set data members

Parameters:
name - Agent name
 o RouterRecvThread
 public RouterRecvThread(Socket client_socket,
                         int priority,
                         Address myaddress,
                         ConnectionTable connections,
                         MessageBuffer queue,
                         Security security,
                         String messageboxpath,
                         Hashtable reservedBook,
                         OffLineAgentTable offlinelist,
                         Vector disconnectlist) throws ConnectionException
Constructor, to be called from the serverthread after accepting a connection.

Parameters:
client_scoket - The socket for the remote client;
priority - The priority of the thread.
myaddress - My address to be sent to the Router to check security
connections - The connection table is passed as an argument.
queue - The Message Queue. Normally null. It will be created by this if null
security - The RouterSecurity
messageboxpath - The Message incoming box directory
reservedBook - The reservation table, passed from the RouterAction
offlinelist - The off line agent name list.
disconnectlist - The disconnected agent name list.
 o RouterRecvThread
 public RouterRecvThread(Address serveraddress,
                         int priority,
                         Address myaddress,
                         ConnectionTable connections,
                         MessageBuffer queue,
                         Security security,
                         String messageboxpath,
                         Hashtable reservedBook,
                         OffLineAgentTable offlinelist,
                         Vector disconnectlist,
                         ThreadGroup tg) throws ConnectionException
This constructor is called when an agent want to initialize a connection to the other agents, using Address

Parameters:
serveraddress - The Router address
priority - The priority of the thread.
myaddress - My address to be sent to the Router to check security
connections - The connection table is passed as an argument.
queue - The Message Queue. Normally null. It will be created by this if null
security - The RouterSecurity
messageboxpath - The Message incoming box directory
reservedBook - The reservation table, passed from the RouterAction
offlinelist - The off line agent name list.
disconnectlist - The disconnected agent name list.
tg - The thread group to which this receiver thread will be owned to
 o RouterRecvThread
 public RouterRecvThread(Address serveraddress,
                         Address myaddress,
                         ConnectionTable connections,
                         String messageboxpath,
                         Security security,
                         OffLineAgentTable offlinelist)
Router receiver thread to send off-line agent's messages back to the senders with error.

Parameters:
serveraddress - Router address
myaddress - Router address to be sent to the Router to check security
connections - The connection table is passed as an argument.
security - The RouterSecurity
messageboxpath - The Message incoming box directory
offlinelist - The off line agent name list.

Methods

 o setReservedBook
 public void setReservedBook(Hashtable reservedBook)
Set the ReservationTable

Parameters:
reservedBook - ReservationTable from the RouterAction
 o setOffLineList
 public void setOffLineList(OffLineAgentTable offlinelist)
Set the off line agent name list. The off-line means that an agent intentionally wants not to receive message before it reconnect to the Router.

Parameters:
offlinelist - Off line agent name list.
 o setDisconnectTable
 public void setDisconnectTable(Vector disconnectlist)
Set the disconnect table

Parameters:
disconnectlist - Disconnected agent list
 o initialize
 protected void initialize()
Send queued messages to the agent. Update off-line/disconnect table

 o openFiles
 public synchronized void openFiles()
Send messages to the receiver by retriving file and open necessary files. This method is invoked after security check has been passed.

 o run
 public void run()
The run() method will performed the Security check at first. If the security check has been passed, the Security will update the ConnectionTable. Then the run() method will send the incoming messages and start to receive messages

Overrides:
run in class BRecvThread
 o checkTime
 protected void checkTime()
Override BRecvThread's checkTime method to add the agent name to the off line agent list

Overrides:
checkTime in class BRecvThread
 o forwardMessage
 protected void forwardMessage(String msg)
New messages are received from the socket. The RouterRecvThread parse the message to find out the receiver. If the receiver is the Router itself, it will provide necessary services. If the receiver is another agent, it will pass the message to other agent after adding time stamp. For Router service, refer to RouterAction class.

Parameters:
msg - String, received from the buffer
 o processDefaultProtocol
 protected synchronized void processDefaultProtocol(KQMLmessage kqml)
Processing default protocols : disconnect-agent, reserve-message, list-agent, request-address, and unregister-agent. You need to extend this method if your router has extended capability other than message routing(e.g. to support another internet protocol such as SMTP, FTP,etc.)

Parameters:
kqml - Received KQML message
 o sendMessage
 protected synchronized void sendMessage(String receiver,
                                         String msg)
Send message to the intended receiver. If the receiver is on-line, add message to the receiver message buffer. If the receiver is off-line, try to connect to the receiver If connected, add message to the receiver buffer. If fails, save message to the receiver incoming file.

Parameters:
receiver - The receiver agent
stampmsg - The stamped message
 o reserveMessages
 protected synchronized void reserveMessages(KQMLmessage kqml)
Message reservation on a specific time Generate reserved message file. Extension is reserved time. To evade confusion, delete messages first if exist. The received KQML will look like
(reserve-message :sender A :receiver A :time 123456789012 :content (2 5 7) :address (receiver-address :name B :host cdr.stanford.edu :port 1234 :message-method RouterRecvThread :description (agent-info :password xyz))
The delete slot is optional but it would be better to set as 'y' to delete message after you reserve
The address slot is optional. If you want to receive at a specific location other than where you are working now, you will need to set the location. The description slot is optional. However, if the Server side need password check, you should provide password to the Router to connect safely.

Parameters:
kqml - KQMLmessage, which contains the message reservation info.
 o sendReservedMessages
 public synchronized void sendReservedMessages(String time)
send reserved messages, invoked from the RouterAction The reasoning is that when the RouterAction want to send reserved message and the receiver thread is running, it does not need to open a new connection to send. The messages will be saved to my message queue rather than sending directly, since if you send the message directly, you will be in trouble to update the incoming file, FPT file - the agent will received the messages which is not registered to the Router and it will cause confusion. No clean up after sending the reserved file.

Parameters:
time - String, reserved time
 o saveMessageToFile
 protected synchronized void saveMessageToFile(String receiver,
                                               String msg)
This method is invoked under the assumption that the receiver is off-line and the Router can not connect to the receiver

Parameters:
receiver - The receiver agent
msg - Time stamped message
 o receiveMessage
 protected synchronized void receiveMessage()
Message received to the queue. send message and save it to the file Time stamp is already attatched by other receiver threads Should be synchronized, not to add messages to the queue while writing.

 o saveMessageToIncomingFile
 protected void saveMessageToIncomingFile(String msg)
 o deleteMessages
 protected void deleteMessages()
Does not necessarily be synchronized. All the other threads knows that this thread is running, therefore they will try to add received messages to this thread to the message queue. This method will be invoked when the agent wants to disconnect or the _maxDeleteSize is reached.

 o sendOffLineAgentErrorMessages
 public void sendOffLineAgentErrorMessages()
 o writeMsg
 public synchronized void writeMsg(String writebuffer) throws ConnectionException
The thread just writes on the connection for the remote side.

Parameters:
writebuffer: - the message as a normal ASCII string.
Throws: ConnectionException
If IOError occurs, IOException will be thrown
Overrides:
writeMsg in class BRecvThread
 o endConn
 public synchronized void endConn()
If there are messages to be deleted, deleteMessages() will be invoked Send 'disconnect' message to the agent and Close all the connections.

Overrides:
endConn in class BRecvThread
 o cleanUp
 protected void cleanUp()
Clean up the files and remove connection from the connection table

Overrides:
cleanUp in class BRecvThread
 o closeSocket
 protected void closeSocket()
Close socket and stop this thread

Overrides:
closeSocket in class BRecvThread
 o sendErrorMessage
 public void sendErrorMessage(int code,
                              String content)
Send error message to the agent is error occurs.

Parameters:
code - Error code
content - Error content
 o getMsgHeader
 protected String getMsgHeader()
Return message header, 'N 86472389 ' where 86472389 represent current time


All Packages  Class Hierarchy  This Package  Previous  Next  Index