Class 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.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean canStop
      True if the sessions can be stopped, false otherwise.
      private ReentrantLock interruptLock  
      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 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) static int SEND_IN_PROGRESS  
      (package private) static int SEND_NONE  
      (package private) 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.
      (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 Socket sock
      The communication socket.
      private Thread thread
      The 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 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.
      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​(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​(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()  
      String toString()  
      StringBuilder toString​(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.
      • 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 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 Socket sock
        The communication socket.
      • 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.
      • 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.
      • 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 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:
        • 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 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
        boot - 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 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:
        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).