Package fr.dyade.aaa.agent
Class PoolNetwork.NetSession
- java.lang.Object
-
- fr.dyade.aaa.agent.PoolNetwork.NetSession
-
- All Implemented Interfaces:
Runnable
- Enclosing class:
- PoolNetwork
final class PoolNetwork.NetSession extends Object implements Runnable
The class NetSession controls the connection with a particular server.Each NetSession object is dedicated to the handling of the connection with a server, at a time a session can be either active (with a TCP connection and a receiver thread) or passive. Currently the number of active NetSession is limited, this number can be adjust through the nbMaxCnx property.
The message sending is handled by dynamically Sender components.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classPoolNetwork.NetSession.NetworkInputStreamClass used to read messages through a stream.(package private) classPoolNetwork.NetSession.NetworkOutputStreamClass used to send messages through a stream.
-
Field Summary
Fields Modifier and Type Field Description private booleancanStopTrue if the sessions can be stopped, false otherwise.private ReentrantLockinterruptLockprivate longlastTime in milliseconds of last use of this session, this attribute is set during connection, then updated at each sending or receiving.(package private) longlastReceivedTime in milliseconds of last message received.private booleanlocalTrue if a "local" connection is in progress, a local connection is initiated from this server to the remote one (defined by theserverdescriptor.(package private) intmaxMessageInFlowMaximum number of message sent and non acknowledged.private StringnameThe session's name.(package private) intnbAckSentThe number of acknowledge sent since last reboot.(package private) intnbMessageReceivedThe number of messages received since last reboot.(package private) intnbMessageSentThe number of messages sent since last reboot.(package private) PoolNetwork.NetSession.NetworkInputStreamnis(package private) PoolNetwork.NetSession.NetworkOutputStreamnosprivate booleanrunningBoolean variable used to stop the daemon properly.(package private) static intSEND_IN_PROGRESS(package private) static intSEND_NONE(package private) intsendActivitythe sendActivity is defined to avoid a connection close, that can arrive by a inactivity of the reader when the SO_TIMEOUT is set.(package private) PoolNetwork.SendersenderSender component needed to send messages.(package private) MessageSoftListsendListList of all sent messages waiting for an acknowledge from the remote server.private ServerDescserverThe description of the remote server handled by this network session.private shortsidDestination server idprivate SocketsockThe communication socket.private ThreadthreadThe thread.
-
Constructor Summary
Constructors Constructor Description NetSession(String name, short sid)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidack(int stamp)(package private) voidclose()private voiddoAck(int ack)Removes the acknowledged notification from waiting list.(package private) longgetLastReceived()Returns the time in milliseconds of last message received.(package private) intgetMaxMessageInFlow()Gets the maximum number of message sent and non acknowledged.StringgetName()Returns this session's name.(package private) intgetNbAckSent()Returns the number of acknowledge sent since last reboot.(package private) intgetNbBufferingMessageToSent()Returns the number of buffering messages to sent since last reboot.(package private) intgetNbMessageReceived()Returns the number of messages received since last reboot.(package private) intgetNbMessageSent()Returns the number of messages sent since last reboot.(package private) intgetNbWaitingMessages()Gets the number of waiting messages to send for this session.shortgetRemoteSID()Returns the server identification of remote server.(package private) voidinit()booleanisRunning()Tests if the session is connected.(package private) booleanlocalStart()This method is called bystart(long)in order to initiate a connection from the local server.(package private) booleanremoteStart(Socket sock, int boot)Its method is called bystart(Socket, int)in order to reply to a connection request from a remote server.voidrun()(package private) voidsend()Try to send the first message of the list to the corresponding remote server.(package private) voidsetMaxMessageInFlow(int maxMessageInFlow)Sets the maximum number of message sent and non acknowledged.voidshutdown()(package private) voidstart(long currentTimeMillis)Starts the session opening the connection with the remote server.(package private) voidstart(Socket sock, int boot)private voidstartEnd()The session is well initialized, we can start the server thread that "listen" the connected socket.(package private) voidstop()StringtoString()StringBuildertoString(StringBuilder strbuf)
-
-
-
Field Detail
-
sid
private short sid
Destination server id
-
running
private volatile boolean running
Boolean variable used to stop the daemon properly. The daemon tests this variable between each request, and stops if it is false.- See Also:
start(long),stop()
-
canStop
private boolean canStop
True if the sessions can be stopped, false otherwise. A session can be stopped if it is waiting.
-
thread
private Thread thread
The thread.
-
interruptLock
private ReentrantLock interruptLock
-
name
private String name
The session's name.
-
local
private volatile boolean local
True if a "local" connection is in progress, a local connection is initiated from this server to the remote one (defined by theserverdescriptor. This attribute is used to synchronize local and remote attempts to make connections.
-
server
private ServerDesc server
The description of the remote server handled by this network session.
-
sock
private volatile Socket sock
The communication socket.
-
sender
PoolNetwork.Sender sender
Sender component needed to send messages.
-
maxMessageInFlow
int maxMessageInFlow
Maximum number of message sent and non acknowledged.
-
sendList
MessageSoftList sendList
List of all sent messages waiting for an acknowledge from the remote server. These messages are sent anew after a reconnection.
-
last
private long last
Time in milliseconds of last use of this session, this attribute is set during connection, then updated at each sending or receiving. It is used to chose unused sessions.
-
SEND_NONE
static final int SEND_NONE
- See Also:
- Constant Field Values
-
SEND_IN_PROGRESS
static final int SEND_IN_PROGRESS
- See Also:
- Constant Field Values
-
sendActivity
int sendActivity
the sendActivity is defined to avoid a connection close, that can arrive by a inactivity of the reader when the SO_TIMEOUT is set.
-
nbMessageSent
int nbMessageSent
The number of messages sent since last reboot.
-
nbMessageReceived
int nbMessageReceived
The number of messages received since last reboot.
-
nbAckSent
int nbAckSent
The number of acknowledge sent since last reboot.
-
lastReceived
long lastReceived
Time in milliseconds of last message received.
-
-
Constructor Detail
-
NetSession
NetSession(String name, short sid)
-
-
Method Detail
-
getMaxMessageInFlow
int getMaxMessageInFlow()
Gets the maximum number of message sent and non acknowledged.- Returns:
- the maximum number of message sent and non acknowledged.
-
setMaxMessageInFlow
void setMaxMessageInFlow(int maxMessageInFlow)
Sets the maximum number of message sent and non acknowledged.- Parameters:
maxMessageInFlow- the maximum number of message sent and non acknowledged.
-
toString
public StringBuilder toString(StringBuilder strbuf)
-
init
void init() throws UnknownServerException
- Throws:
UnknownServerException
-
getName
public final String getName()
Returns this session's name.- Returns:
- this session's name.
-
getRemoteSID
public short getRemoteSID()
Returns the server identification of remote server.- Returns:
- the server identification of remote server.
-
isRunning
public boolean isRunning()
Tests if the session is connected.- Returns:
- true if this session is connected; false otherwise.
-
getNbWaitingMessages
int getNbWaitingMessages()
Gets the number of waiting messages to send for this session.- Returns:
- the number of waiting messages.
-
getNbMessageSent
int getNbMessageSent()
Returns the number of messages sent since last reboot.- Returns:
- the number of messages sent since last reboot.
-
getNbMessageReceived
int getNbMessageReceived()
Returns the number of messages received since last reboot.- Returns:
- the number of messages received since last reboot.
-
getNbAckSent
int getNbAckSent()
Returns the number of acknowledge sent since last reboot.- Returns:
- the number of acknowledge sent since last reboot.
-
getLastReceived
long getLastReceived()
Returns the time in milliseconds of last message received.- Returns:
- the time in milliseconds of last message received.
-
getNbBufferingMessageToSent
int getNbBufferingMessageToSent()
Returns the number of buffering messages to sent since last reboot.- Returns:
- the number of buffering messages to sent since last reboot.
-
start
void start(long currentTimeMillis)
Starts the session opening the connection with the remote server.The protocol is synchronized in order to avoid a 'double' connection between the two servers.
- Parameters:
currentTimeMillis- The current time in milliseconds- See Also:
localStart()
-
start
void start(Socket sock, int boot)
-
localStart
boolean localStart()
This method is called bystart(long)in order to initiate a connection from the local server. The corresponding code on remote server is the methodremoteStart(Socket, int). Its method creates the socket, initiates the network connection, and negotiates with remote server.
Its method can be overridden in order to change the connection protocol (introduces authentication by example, or uses SSL), but must respect some conditions:- send a Boot object after the initialization of object streams (it is waiting by the wakeOnConnection thread),
- wait for an acknowledge,
- set the sock, ois and oos attributes at the end if the connection is correct.
In order to override the protocol, we have to implements its method, with the remoteStart and the transmit methods.
- Returns:
- true if the connection is established, false otherwise.
-
remoteStart
boolean remoteStart(Socket sock, int boot)
Its method is called bystart(Socket, int)in order to reply to a connection request from a remote server. The corresponding code on remote server is the methodlocalStart().- Parameters:
sock- the connected socketboot- the boot stamp- Returns:
- true if the connection is established, false otherwise.
-
startEnd
private void startEnd()
The session is well initialized, we can start the server thread that "listen" the connected socket. If the maximum number of connections is reached, one connection from the pool is closed.
-
stop
void stop()
-
shutdown
public void shutdown()
-
close
void close()
-
doAck
private final void doAck(int ack) throws ExceptionRemoves the acknowledged notification from waiting list. This method should not be synchronized, it scans the list from begin to end, and it removes always the first element. Other methods using sendList just adds element at the end.- Parameters:
ack- The stamp of acknowledged message to remove.- Throws:
Exception- Error during the message deletion.
-
send
final void send()
Try to send the first message of the list to the corresponding remote server. Be careful, this method should not be synchronized (in that case, the overall synchronization of the connection -method start- can dead-lock).
-
run
public void run()
- Specified by:
runin interfaceRunnable- See Also:
Runnable.run()
-
-