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 :
- Use message queue which might be centralized to AgentAction or distributed
to each BRecvThread
- Message will be stored to MessageQueue with type String. Appropriate
object will be constructed lately. This approach separates Message class
from BRecvThread
- Use ConnectionTable to control running threads
- Use timer. The timer will be set if you invoke setDurationTime() method.
Default timer is no setting(so that this thread will running until it is killed
by other object). Duration time unit is a minute.
- ConnectionException will be thrown if error occur.
- The ReceiverThread name will be set as the agent name
- Default Maximum length of a message is 16K
- End message character will be set by setEndWith(char). Default is '\004'.
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
-
_client
- Socket object from which the Receiver Thread send/receive message
-
_in
- Input stream with type BufferedReader, derived from the Socket to receive message
-
_lastTime
- Timeout check when idle.
-
_out
- Output stream with type BufferedWriter, derived from the socket to send message
-
_role
- true if ServerSocket acctept(server) false if I want to connect to other
server(client).
-
_serverAddress
- ServerAddress, to which I want to connect to
-
HEADER_LEN
- Message header reserved for read bit and time stamp, not included in MAX_MSG_LEN
-
MAX_MSG_SIZE
- Message buffer size.
-
BRecvThread()
- Default Constructor.
-
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.
-
BRecvThread(Socket, int, Address, ConnectionTable, MessageBuffer, Security)
- Constructor, to be called from the serverthread after accepting
a connection.
-
BRecvThread(String)
- Create ReceiverThread with specific thread name.
-
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.
-
BRecvThread(ThreadGroup, String)
- Create ReceiverThread with specific thread name and Thread group.
-
checkTime()
- Check time out.
-
cleanUp()
- Remove this thread from the connection table
-
closeSocket()
- Close all the connections and stop this thread
-
endConn()
- Close the connection.
-
findEndLocation(char[], int, int)
- Find endWith location and return the unit message length
startPosition will be updated.
-
getCurrentTimeMillis()
-
-
getDurationTime()
- Accessor to get duration time
-
getInputStream()
- Accessor to get input stream
-
getlastTime()
- Accessor to get last communication time
-
getMaxMessageSize()
- Get maximum allowable message length.
-
getOneMessage()
- Get one message.
-
getOutputStream()
- Accessor to get output stream
-
getRole()
- Get Receiver's role.
-
getServerAddress()
- Get current Server address
-
getSocket()
- Accessor to get client socket
-
processLogoutMessage()
- Process logout message.
-
readBuffer(char[], int, int)
- Read unblocked stream from the Socket and save it to the buffer.
-
resetTime()
- Reset the timer to current time.
-
run()
- Override Thread run() method
It will first perform the Security check.
-
setDurationTime(int)
- Set duration time.
-
setInputStream(BufferedReader)
- Set input stream
-
setMaxMessageSize(int)
- Set maximum message size in character length.
-
setOutputStream(BufferedWriter)
- Set output stream
-
setRole(boolean)
- Set role.
-
setSecurity(Security)
- Because of synchronization problem, the client side connect, registration
will not be working as a separate thread.
-
setServerAddress(Address)
- Set the server address to connect
-
setSocket(Socket)
- Set client socket.
-
writeMsg(String)
- The thread just writes on the connection for the remote side.
_serverAddress
protected Address _serverAddress
- ServerAddress, to which I want to connect to
- See Also:
- Address
_client
protected Socket _client
- Socket object from which the Receiver Thread send/receive message
_in
protected BufferedReader _in
- Input stream with type BufferedReader, derived from the Socket to receive message
_out
protected BufferedWriter _out
- Output stream with type BufferedWriter, derived from the socket to send message
_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.
_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.
MAX_MSG_SIZE
protected int MAX_MSG_SIZE
- Message buffer size. Default is 16K
HEADER_LEN
protected int HEADER_LEN
- Message header reserved for read bit and time stamp, not included in MAX_MSG_LEN
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).
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
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
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
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
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
setSocket
public void setSocket(Socket socket) throws ConnectionException
- Set client socket. This method will generate input(BufferedReader), output(BufferedWriter) stream
- Parameters:
- socket - client socket
setMaxMessageSize
public void setMaxMessageSize(int newMaxLen)
- Set maximum message size in character length. Default is 16K
- Parameters:
- newMaxLen - Maximum unit message length
getMaxMessageSize
public int getMaxMessageSize()
- Get maximum allowable message length. Default is 16K
- Returns:
- Maximum unit message size
setInputStream
public void setInputStream(BufferedReader input)
- Set input stream
- Parameters:
- input - BufferedReader
setOutputStream
public void setOutputStream(BufferedWriter output)
- Set output stream
- Parameters:
- output - DataOutputStream
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
getSocket
public Socket getSocket()
- Accessor to get client socket
- Returns:
- Receiving socket
getInputStream
public BufferedReader getInputStream()
- Accessor to get input stream
- Returns:
- input stream
getOutputStream
public BufferedWriter getOutputStream()
- Accessor to get output stream
- Returns:
- output stream
getDurationTime
public long getDurationTime()
- Accessor to get duration time
- Returns:
- Maximum idle time.
getlastTime
public long getlastTime()
- Accessor to get last communication time
- Returns:
- Most recent time, sending/receiving messages
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
getRole
public boolean getRole()
- Get Receiver's role. Return true if the role is a server
- Returns:
- True if the role is a server
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
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.
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
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
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
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
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
resetTime
protected void resetTime()
- Reset the timer to current time.
setServerAddress
public void setServerAddress(Address addr)
- Set the server address to connect
- Parameters:
- addr - Server address(other agent address)
getServerAddress
public Address getServerAddress()
- Get current Server address
- Returns:
- Server addnress to which this receiver thread is connected
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
cleanUp
protected void cleanUp()
- Remove this thread from the connection table
- Overrides:
- cleanUp in class ReceiverThread
closeSocket
protected void closeSocket()
- Close all the connections and stop this thread
- Overrides:
- closeSocket in class ReceiverThread
processLogoutMessage
public void processLogoutMessage()
- Process logout message. Send disconnect message to the other end connection
The disconnect message will be sent only once.
getCurrentTimeMillis
public long getCurrentTimeMillis()
All Packages Class Hierarchy This Package Previous Next Index