fr.dyade.aaa.agent
Class PoolNetwork.NetSession

java.lang.Object
  extended by fr.dyade.aaa.agent.PoolNetwork.NetSession
All Implemented Interfaces:
java.lang.Runnable
Enclosing class:
PoolNetwork

final class PoolNetwork.NetSession
extends java.lang.Object
implements java.lang.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
(package private)  class PoolNetwork.NetSession.NetworkInputStream
          Class used to read messages through a stream.
(package private)  class PoolNetwork.NetSession.NetworkOutputStream
          Class used to send messages through a stream.
 
Field Summary
private  boolean canStop
          True if the sessions can be stopped, false otherwise.
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.
(package private)  long lastReceived
          Time in milliseconds of last message received.
private  boolean local
          True if a "local" connection is in progress, a local connection is initiated from this server to the remote one (defined by the server descriptor.
(package private)  int maxMessageInFlow
          Maximum number of message sent and non acknowledged.
private  java.lang.String name
          The session's name.
(package private)  int nbAckSent
          The number of acknowledge sent since last reboot.
(package private)  int nbMessageReceived
          The number of messages received since last reboot.
(package private)  int nbMessageSent
          The number of messages sent since last reboot.
(package private)  PoolNetwork.NetSession.NetworkInputStream nis
           
(package private)  PoolNetwork.NetSession.NetworkOutputStream nos
           
private  boolean running
          Boolean variable used to stop the daemon properly.
(package private)  PoolNetwork.Sender sender
          Sender component needed to send messages.
(package private)  MessageSoftList sendList
          List of all sent messages waiting for an acknowledge from the remote server.
private  ServerDesc server
          The description of the remote server handled by this network session.
private  short sid
          Destination server id
private  java.net.Socket sock
          The communication socket.
private  java.lang.Thread thread
          The thread.
 
Constructor Summary
PoolNetwork.NetSession(java.lang.String name, short sid)
           
 
Method Summary
private  void ack(int stamp)
           
(package private)  void close()
           
private  void doAck(int ack)
          Removes the acknowledged notification from waiting list.
(package private)  long getLastReceived()
          Returns the time in milliseconds of last message received.
(package private)  int getMaxMessageInFlow()
          Gets the maximum number of message sent and non acknowledged.
 java.lang.String getName()
          Returns this session's name.
(package private)  int getNbAckSent()
          Returns the number of acknowledge sent since last reboot.
(package private)  int getNbBufferingMessageToSent()
          Returns the number of buffering messages to sent since last reboot.
(package private)  int getNbMessageReceived()
          Returns the number of messages received since last reboot.
(package private)  int getNbMessageSent()
          Returns the number of messages sent since last reboot.
(package private)  int getNbWaitingMessages()
          Gets the number of waiting messages to send for this session.
 short getRemoteSID()
          Returns the server identification of remote server.
(package private)  void init()
           
 boolean isRunning()
          Tests if the session is connected.
(package private)  boolean localStart()
          This method is called by start(long) in order to initiate a connection from the local server.
(package private)  boolean remoteStart(java.net.Socket sock, int boot)
          Its method is called by start(Socket, int) in order to reply to a connection request from a remote server.
 void run()
           
(package private)  void send()
          Try to send the first message of the list to the corresponding remote server.
(package private)  void setMaxMessageInFlow(int maxMessageInFlow)
          Sets the maximum number of message sent and non acknowledged.
 void shutdown()
           
(package private)  void start(long currentTimeMillis)
          Starts the session opening the connection with the remote server.
(package private)  void start(java.net.Socket sock, int boot)
           
private  void startEnd()
          The session is well initialized, we can start the server thread that "listen" the connected socket.
(package private)  void stop()
           
 java.lang.String toString()
           
 java.lang.StringBuffer toString(java.lang.StringBuffer strbuf)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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, 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 java.lang.Thread thread
The thread.


name

private java.lang.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 the server descriptor. 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 java.net.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.


nis

PoolNetwork.NetSession.NetworkInputStream nis

nos

PoolNetwork.NetSession.NetworkOutputStream nos

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.


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

PoolNetwork.NetSession

PoolNetwork.NetSession(java.lang.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 java.lang.String toString()
Overrides:
toString in class java.lang.Object

toString

public java.lang.StringBuffer toString(java.lang.StringBuffer strbuf)

init

void init()
    throws UnknownServerException
Throws:
UnknownServerException

getName

public final java.lang.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(java.net.Socket sock,
           int boot)

localStart

boolean localStart()
This method is called by start(long) in order to initiate a connection from the local server. The corresponding code on remote server is the method remoteStart(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:

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(java.net.Socket sock,
                    int boot)
Its method is called by start(Socket, int) in order to reply to a connection request from a remote server. The corresponding code on remote server is the method localStart().

Parameters:
sock - the connected socket
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 java.lang.Exception
Removes 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:
java.lang.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).


ack

private final void ack(int stamp)
                throws java.lang.Exception
Throws:
java.lang.Exception

run

public void run()
Specified by:
run in interface java.lang.Runnable
See Also:
Runnable.run()


Copyright © 2010 ScalAgent D.T.. All Rights Reserved.