Interface IOSession

  • All Superinterfaces:
    java.lang.AutoCloseable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.io.Closeable, Identifiable, ModalCloseable, java.nio.channels.ReadableByteChannel, SocketModalCloseable, java.nio.channels.WritableByteChannel
    All Known Subinterfaces:
    ProtocolIOSession
    All Known Implementing Classes:
    SSLIOSession

    @Internal
    public interface IOSession
    extends java.nio.channels.ByteChannel, SocketModalCloseable, Identifiable
    IOSession interface represents a sequence of logically related data exchanges between two end points.

    The channel associated with implementations of this interface can be used to read data from and write data to the session.

    I/O sessions are not bound to an execution thread, therefore one cannot use the context of the thread to store a session's state. All details about a particular session must be stored within the session itself, usually using execution context associated with it.

    Implementations of this interface are expected to be threading safe.

    Since:
    4.0
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  IOSession.Status
      This enum represents a set of states I/O session transitions through during its life-span.
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      java.nio.channels.ByteChannel channel()
      Returns the underlying I/O channel associated with this session.
      void clearEvent​(int op)
      Clears interest in a particular I/O event type by updating the event mask associated with the session.
      void close()
      Terminates the session gracefully and closes the underlying I/O channel.
      void enqueue​(Command command, Command.Priority priority)
      Inserts Command at the end of the command queue.
      int getEventMask()
      Returns mask of I/O evens this session declared interest in.
      IOEventHandler getHandler()
      Returns event handler associated with the session.
      default long getInitiationTime()
      Returns the absolute millisecond timestamp of the session initiation.
      long getLastEventTime()
      Returns the absolute millisecond timestamp of the last I/O event including socket timeout reset.
      long getLastReadTime()
      Returns the absolute millisecond timestamp of the last read event.
      long getLastWriteTime()
      Returns the absolute millisecond timestamp of the last write event.
      java.net.SocketAddress getLocalAddress()
      Returns local address.
      java.util.concurrent.locks.Lock getLock()
      Returns session lock that should be used by I/O event handlers to synchronize access to the session.
      default int getPendingCommandCount()
      Returns the number of pending commands enqueued for execution, or -1 if unknown.
      java.net.SocketAddress getRemoteAddress()
      Returns address of the remote peer.
      Timeout getSocketTimeout()
      Returns value of the socket timeout in milliseconds.
      IOSession.Status getStatus()
      Returns status of the session:
      boolean hasCommands()
      Tests if there enqueued commands pending execution.
      Command poll()
      Removes first Command from the command queue if available.
      void setEvent​(int op)
      Declares interest in a particular I/O event type by updating the event mask associated with the session.
      void setEventMask​(int ops)
      Declares interest in I/O event notifications by setting the event mask associated with the session
      void setSocketTimeout​(Timeout timeout)
      Sets value of the socket timeout in milliseconds.
      void updateReadTime()
      Updates the timestamp of the last read event
      void updateWriteTime()
      Updates the timestamp of the last write event
      void upgrade​(IOEventHandler handler)
      Upgrades event handler associated with the session.
      • Methods inherited from interface java.nio.channels.Channel

        isOpen
      • Methods inherited from interface java.nio.channels.ReadableByteChannel

        read
      • Methods inherited from interface java.nio.channels.WritableByteChannel

        write
    • Method Detail

      • getHandler

        IOEventHandler getHandler()
        Returns event handler associated with the session.
        Returns:
        the event handler, or null if no handler is associated with the session.
        Since:
        5.0
      • upgrade

        void upgrade​(IOEventHandler handler)
        Upgrades event handler associated with the session.
        Since:
        5.0
      • getLock

        java.util.concurrent.locks.Lock getLock()
        Returns session lock that should be used by I/O event handlers to synchronize access to the session.
        Since:
        5.0
      • hasCommands

        boolean hasCommands()
        Tests if there enqueued commands pending execution.
        Since:
        5.0
      • poll

        Command poll()
        Removes first Command from the command queue if available.
        Returns:
        the first command, or null if the queue is empty.
        Since:
        5.0
      • channel

        java.nio.channels.ByteChannel channel()
        Returns the underlying I/O channel associated with this session.
        Returns:
        the I/O channel.
      • getRemoteAddress

        java.net.SocketAddress getRemoteAddress()
        Returns address of the remote peer.
        Returns:
        socket address, or null if unavailable.
      • getLocalAddress

        java.net.SocketAddress getLocalAddress()
        Returns local address.
        Returns:
        socket address, or null if unavailable.
      • getEventMask

        int getEventMask()
        Returns mask of I/O evens this session declared interest in.
        Returns:
        I/O event mask.
      • setEventMask

        void setEventMask​(int ops)
        Declares interest in I/O event notifications by setting the event mask associated with the session
        Parameters:
        ops - new I/O event mask.
      • setEvent

        void setEvent​(int op)
        Declares interest in a particular I/O event type by updating the event mask associated with the session.
        Parameters:
        op - I/O event type.
      • clearEvent

        void clearEvent​(int op)
        Clears interest in a particular I/O event type by updating the event mask associated with the session.
        Parameters:
        op - I/O event type.
      • close

        void close()
        Terminates the session gracefully and closes the underlying I/O channel. This method ensures that session termination handshake, such as the one used by the SSL/TLS protocol, is correctly carried out.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
      • getSocketTimeout

        Timeout getSocketTimeout()
        Returns value of the socket timeout in milliseconds. The value of 0 signifies the session cannot time out.
        Specified by:
        getSocketTimeout in interface SocketModalCloseable
        Returns:
        socket timeout.
      • setSocketTimeout

        void setSocketTimeout​(Timeout timeout)
        Sets value of the socket timeout in milliseconds. The value of 0 signifies the session cannot time out.

        Please note this operation may affect the last event time.

        Specified by:
        setSocketTimeout in interface SocketModalCloseable
        Parameters:
        timeout - socket timeout.
        See Also:
        getLastEventTime()
      • getInitiationTime

        default long getInitiationTime()
        Returns the absolute millisecond timestamp of the session initiation.
        Since:
        5.5
      • getLastReadTime

        long getLastReadTime()
        Returns the absolute millisecond timestamp of the last read event.
        Returns:
        timestamp in milliseconds, compatible with System.currentTimeMillis().
      • getLastWriteTime

        long getLastWriteTime()
        Returns the absolute millisecond timestamp of the last write event.
        Returns:
        timestamp in milliseconds, compatible with System.currentTimeMillis().
      • getLastEventTime

        long getLastEventTime()
        Returns the absolute millisecond timestamp of the last I/O event including socket timeout reset.
        Returns:
        timestamp in milliseconds, compatible with System.currentTimeMillis().
        See Also:
        getSocketTimeout()
      • updateReadTime

        void updateReadTime()
        Updates the timestamp of the last read event
      • updateWriteTime

        void updateWriteTime()
        Updates the timestamp of the last write event
      • getPendingCommandCount

        default int getPendingCommandCount()
        Returns the number of pending commands enqueued for execution, or -1 if unknown.
        Since:
        5.5