All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class BaseLayer.BRecvThread

Abstract.Connection
   |
   +----Abstract.ReceiverThread
           |
           +----BaseLayer.BRecvThread

public class BRecvThread
extends ReceiverThread
BRecvThread is a subclass of the Receiver thread abstract class. BRecvThread will take a core role to send/receive String message. The received message will be saved to the MessageBuffer with the type String. Typically,AgentAction or other class will parse the String message and construct Message object.
BRecvThread is based on the design under the assumption :
The constructor with socket parameter will be invoked by ServerThread. This means that the ReceiverThread will be initiated by other agent to this
The constructor with host name and port parameter will be invoked if you initiate socket connection to the host and specified port.
Use accessors to change default data member values such as setEndWith(char) or setMaxMessageSize(int) if necessary.
From beta version, writeMsg(String) and setSecurity(Security) will throw Abstract.ConnectionException if IOException or Security fails.

See Also:
BConnectionTable

Variable Index

 o _client
Socket object from which the Receiver Thread send/receive message
 o _in
Input stream with type BufferedReader, derived from the Socket to receive message
 o _lastTime
Timeout check when idle.
 o _out
Output stream with type BufferedWriter, derived from the socket to send message
 o _role
true if ServerSocket acctept(server) false if I want to connect to other server(client).
 o _serverAddress
ServerAddress, to which I want to connect to
 o HEADER_LEN
Message header reserved for read bit and time stamp, not included in MAX_MSG_LEN
 o MAX_MSG_SIZE
Message buffer size.

Constructor Index

 o BRecvThread()
Default Constructor.
 o BRecvThread(Address, int, Address, ConnectionTable, MessageBuffer, Security, ThreadGroup)
This constructor is called when an agent want to initialize a connection to the other agents, using Address.
 o BRecvThread(Socket, int, Address, ConnectionTable, MessageBuffer, Security)
Constructor, to be called from the serverthread after accepting a connection.
 o BRecvThread(String)
Create ReceiverThread with specific thread name.
 o BRecvThread(String, String, int, Address, ConnectionTable, MessageBuffer, int, ThreadGroup)
This constructor is called when an agent want to initialize a connection to the other agents, using host name and port.
 o BRecvThread(ThreadGroup, String)
Create ReceiverThread with specific thread name and Thread group.

Method Index

 o checkTime()
Check time out.
 o cleanUp()
Remove this thread from the connection table
 o closeSocket()
Close all the connections and stop this thread
 o endConn()
Close the connection.
 o findEndLocation(char[], int, int)
Find endWith location and return the unit message length startPosition will be updated.
 o getCurrentTimeMillis()
 o getDurationTime()
Accessor to get duration time
 o getInputStream()
Accessor to get input stream
 o getlastTime()
Accessor to get last communication time
 o getMaxMessageSize()
Get maximum allowable message length.
 o getOneMessage()
Get one message.
 o getOutputStream()
Accessor to get output stream
 o getRole()
Get Receiver's role.
 o getServerAddress()
Get current Server address
 o getSocket()
Accessor to get client socket
 o processLogoutMessage()
Process logout message.
 o readBuffer(char[], int, int)
Read unblocked stream from the Socket and save it to the buffer.
 o resetTime()
Reset the timer to current time.
 o run()
Override Thread run() method It will first perform the Security check.
 o setDurationTime(int)
Set duration time.
 o setInputStream(BufferedReader)
Set input stream
 o setMaxMessageSize(int)
Set maximum message size in character length.
 o setOutputStream(BufferedWriter)
Set output stream
 o setRole(boolean)
Set role.
 o setSecurity(Security)
Because of synchronization problem, the client side connect, registration will not be working as a separate thread.
 o setServerAddress(Address)
Set the server address to connect
 o setSocket(Socket)
Set client socket.
 o writeMsg(String)
The thread just writes on the connection for the remote side.

Variables

 o _serverAddress
 protected Address _serverAddress
ServerAddress, to which I want to connect to

See Also:
Address
 o _client
 protected Socket _client
Socket object from which the Receiver Thread send/receive message

 o _in
 protected BufferedReader _in
Input stream with type BufferedReader, derived from the Socket to receive message

 o _out
 protected BufferedWriter _out
Output stream with type BufferedWriter, derived from the socket to send message

 o _lastTime
 protected long _lastTime
Timeout check when idle. Record the last sending/receiving time If there's no communication during setting time, this object will be stop by itself. Timer is not set until setDurationTime() method is called.

 o _role
 protected boolean _role
true if ServerSocket acctept(server) false if I want to connect to other server(client). Since this data is automatically set, you do not need to set this member.

 o MAX_MSG_SIZE
 protected int MAX_MSG_SIZE
Message buffer size. Default is 16K

 o HEADER_LEN
 protected int HEADER_LEN
Message header reserved for read bit and time stamp, not included in MAX_MSG_LEN

Constructors

 o BRecvThread
 public BRecvThread()
Default Constructor. You need member-wise initialization for all data members using accessors - setSocket(Socket), setConnectionTable(ConnectionTable), setMessageBuffer(MessageBuffer), setSecurity(Security). Optionally you can use setEndWith(char) and setDurationTime(int minutes).

 o BRecvThread
 public BRecvThread(String name)
Create ReceiverThread with specific thread name. You need member-wise initialization for all data members using accessors - setSocket(Socket), setConnectionTable(ConnectionTable), setMessageBuffer(MessageBuffer), setSecurity(Security). Optionally you can use setEndWith(char) and setDurationTime(int minutes).

Parameters:
name - Agent name
 o BRecvThread
 public BRecvThread(ThreadGroup tg,
                    String name)
Create ReceiverThread with specific thread name and Thread group. Passing thread group is essential when you will create the receiver thread for an applet because of security issue. You need member-wise initialization for all data members using accessors - setSocket(Socket), setConnectionTable(ConnectionTable), setMessageBuffer(MessageBuffer), setSecurity(Security). Optionally you can use setEndWith(char) and setDurationTime(int minutes).

Parameters:
tg - Thread group to which this thread will be owned to
name - Agent name
 o BRecvThread
 public BRecvThread(Socket client_socket,
                    int priority,
                    Address address,
                    ConnectionTable connections,
                    MessageBuffer queue,
                    Security security) throws ConnectionException
Constructor, to be called from the serverthread after accepting a connection. Note default endWith is '\004'. You can change endWith character by setEndWith(char) method. Note that the timer will not be set. To set timer, use setDurationTime(int minutes) method. Do not use this thread when you want to initiate(conect) to other server. If you use this constructor the role will be set as a server. If there is a special reason you should use this constructor even if your agent role is a server, use setRole(false) method to set the role as a client.

Parameters:
client_scoket - The socket for the remote client;
priority - the priority of the thread.
address - My address.
connections - the connection table is passed as an argument.
queue - the Message Queue.
security - Security for checking this connection security
Throws: ConnectionException
if error occurs
 o BRecvThread
 public BRecvThread(String serverid,
                    String host,
                    int priority,
                    Address myaddress,
                    ConnectionTable connections,
                    MessageBuffer queue,
                    int port,
                    ThreadGroup tg) throws ConnectionException
This constructor is called when an agent want to initialize a connection to the other agents, using host name and port. This constructor is will be useful if you do not want to use any security check._security data member will be set as null. Do not use this constructor if you want security check.

Parameters:
serverid - Agent name to shich the this receiver thread will connect
host - the name of the remote host.
priority - the priority of the thread.
connections - the connection table is passed as an argument.
queue - the Message Queue.
port - the port of the remote host.
tg - Thread group to which this thread will be owned to. Necessary to evade applet security.
Throws: ConnectionException
if error occurs
 o BRecvThread
 public BRecvThread(Address serveraddress,
                    int priority,
                    Address myaddress,
                    ConnectionTable connections,
                    MessageBuffer queue,
                    Security security,
                    ThreadGroup tg) throws ConnectionException
This constructor is called when an agent want to initialize a connection to the other agents, using Address. Use this constructor if you want security check.

Parameters:
serveraddress - The address of the remote host.
priority - The priority of the thread.
myaddress - My addres
connections - the connection table is passed as an argument.
queue - the Message Queue.
security - The security object
Throws: ConnectionException
if error occurs

Methods

 o setSocket
 public void setSocket(Socket socket) throws ConnectionException
Set client socket. This method will generate input(BufferedReader), output(BufferedWriter) stream

Parameters:
socket - client socket
 o setMaxMessageSize
 public void setMaxMessageSize(int newMaxLen)
Set maximum message size in character length. Default is 16K

Parameters:
newMaxLen - Maximum unit message length
 o getMaxMessageSize
 public int getMaxMessageSize()
Get maximum allowable message length. Default is 16K

Returns:
Maximum unit message size
 o setInputStream
 public void setInputStream(BufferedReader input)
Set input stream

Parameters:
input - BufferedReader
 o setOutputStream
 public void setOutputStream(BufferedWriter output)
Set output stream

Parameters:
output - DataOutputStream
 o setDurationTime
 public void setDurationTime(int minutes)
Set duration time. Note that the duration time is not passed to the constructor. You can use setDurationTime(int) after you construct this object. Default is no time set.

Parameters:
minutes - Maximum duration time to be set(unit is minute)
Overrides:
setDurationTime in class Connection
 o getSocket
 public Socket getSocket()
Accessor to get client socket

Returns:
Receiving socket
 o getInputStream
 public BufferedReader getInputStream()
Accessor to get input stream

Returns:
input stream
 o getOutputStream
 public BufferedWriter getOutputStream()
Accessor to get output stream

Returns:
output stream
 o getDurationTime
 public long getDurationTime()
Accessor to get duration time

Returns:
Maximum idle time.
 o getlastTime
 public long getlastTime()
Accessor to get last communication time

Returns:
Most recent time, sending/receiving messages
 o setRole
 public void setRole(boolean role)
Set role. If true is passed, its role will be a server. The default is that if you initiate the connection to the other server, its role is a client(false).

Parameters:
role - Boolean value to set role. If true, role is a server
 o getRole
 public boolean getRole()
Get Receiver's role. Return true if the role is a server

Returns:
True if the role is a server
 o setSecurity
 public synchronized void setSecurity(Security security) throws ConnectionException
Because of synchronization problem, the client side connect, registration will not be working as a separate thread. Therefore if the receiver thread is created as a client side, security check will be performed at this method. Fail on security check will close socket and its input/output streams.

Parameters:
security. - Security object
Throws: ConnectionException
If security check fails, Connection exception will be thrown
Overrides:
setSecurity in class Connection
 o run
 public void run()
Override Thread run() method It will first perform the Security check. If passed, it will started and receive messages. If fails, the Security will kill this thread. Receive a message and store it to MessageBuffer Rather than read a message character by character(this need read() call repeatedly and conversion from int to char or byte to char), use buffer to read full available input. Since the full read input may have several messages, it will be divided into several messages and saved to the message queue as String object. The assumption here is that the maximum message length is less than 16K.

 o readBuffer
 protected synchronized int readBuffer(char buffer[],
                                       int start,
                                       int max)
Read unblocked stream from the Socket and save it to the buffer.

Parameters:
buffer - Socket buffer
start - Starting point to read
max - Maximum length to read
Returns:
Message length. -1 if the current buffer does not end with endWith character
 o getOneMessage
 public String getOneMessage()
Get one message. This method is useful to setup initial connection. But we are suggesting that do not use this method actual, heavy loaded message passing since the method is not efficient. Instead to add messages to message queue, it will return the received message. This method is used only in the Security check stage.

Returns:
Received message
Overrides:
getOneMessage in class ReceiverThread
 o findEndLocation
 protected int findEndLocation(char buffer[],
                               int startPosition,
                               int maxLen)
Find endWith location and return the unit message length startPosition will be updated.

Parameters:
buffer - message buffer, read from the socket
startPosition - start position to search end character
maxLen - maximum allowable message length. Full length of the stream
Returns:
unit message length. If the message is not ended with _endWith, it will return -1
 o writeMsg
 public synchronized void writeMsg(String writebuffer) throws ConnectionException
The thread just writes on the connection for the remote side. Close socket and kill this thread not performed here. After catching the exception, you need to invoke endConn() method to clean up.

Parameters:
writebuffer: - the message as a normal ASCII string.
Throws: ConnectionException
if IO error occurs.
Overrides:
writeMsg in class ReceiverThread
 o checkTime
 protected void checkTime()
Check time out. If the timer is not set(no timer setting is default), no action, If idle time is longer than the setting time, this thread will be stop

 o resetTime
 protected void resetTime()
Reset the timer to current time.

 o setServerAddress
 public void setServerAddress(Address addr)
Set the server address to connect

Parameters:
addr - Server address(other agent address)
 o getServerAddress
 public Address getServerAddress()
Get current Server address

Returns:
Server addnress to which this receiver thread is connected
 o endConn
 public synchronized void endConn()
Close the connection. Automatically update the connectionTable. Processing the logout message to the other end connection. Remove myself from the ConnectionTable Close socket,input stream and output stream. Stop this thread.

Overrides:
endConn in class Connection
 o cleanUp
 protected void cleanUp()
Remove this thread from the connection table

Overrides:
cleanUp in class ReceiverThread
 o closeSocket
 protected void closeSocket()
Close all the connections and stop this thread

Overrides:
closeSocket in class ReceiverThread
 o processLogoutMessage
 public void processLogoutMessage()
Process logout message. Send disconnect message to the other end connection The disconnect message will be sent only once.

 o getCurrentTimeMillis
 public long getCurrentTimeMillis()

All Packages  Class Hierarchy  This Package  Previous  Next  Index