Class IOSessionImpl

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel, java.nio.channels.WritableByteChannel, SocketModalCloseable, ModalCloseable, IOSession, Identifiable

    class IOSessionImpl
    extends java.lang.Object
    implements IOSession
    • Constructor Summary

      Constructors 
      Constructor Description
      IOSessionImpl​(java.lang.String type, java.nio.channels.SelectionKey key, java.nio.channels.SocketChannel socketChannel)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete 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 close​(CloseMode closeMode)
      Closes this process or endpoint and releases any system resources associated with it.
      void enqueue​(Command command, Command.Priority priority)
      Inserts Command at the end of the command queue.
      private static void formatOps​(java.lang.StringBuilder buffer, int ops)  
      int getEventMask()
      Returns mask of I/O evens this session declared interest in.
      IOEventHandler getHandler()
      Returns event handler associated with the session.
      java.lang.String getId()  
      long getLastEventTime()
      Returns timestamp of the last I/O event including socket timeout reset.
      long getLastReadTime()
      Returns timestamp of the last read event.
      long getLastWriteTime()
      Returns 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.
      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.
      boolean isOpen()  
      private boolean isStatusClosed()  
      Command poll()
      Removes first Command from the command queue if available.
      int read​(java.nio.ByteBuffer dst)  
      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 newValue)
      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.
      java.lang.String toString()  
      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.
      int write​(java.nio.ByteBuffer src)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • COUNT

        private static final java.util.concurrent.atomic.AtomicLong COUNT
        Counts instances created.
      • key

        private final java.nio.channels.SelectionKey key
      • channel

        private final java.nio.channels.SocketChannel channel
      • commandQueue

        private final java.util.Deque<Command> commandQueue
      • lock

        private final java.util.concurrent.locks.Lock lock
      • id

        private final java.lang.String id
      • handlerRef

        private final java.util.concurrent.atomic.AtomicReference<IOEventHandler> handlerRef
      • status

        private final java.util.concurrent.atomic.AtomicReference<IOSession.Status> status
      • socketTimeout

        private volatile Timeout socketTimeout
      • lastReadTime

        private volatile long lastReadTime
      • lastWriteTime

        private volatile long lastWriteTime
      • lastEventTime

        private volatile long lastEventTime
    • Constructor Detail

      • IOSessionImpl

        public IOSessionImpl​(java.lang.String type,
                             java.nio.channels.SelectionKey key,
                             java.nio.channels.SocketChannel socketChannel)
    • Method Detail

      • getId

        public java.lang.String getId()
        Specified by:
        getId in interface Identifiable
      • upgrade

        public void upgrade​(IOEventHandler handler)
        Description copied from interface: IOSession
        Upgrades event handler associated with the session.
        Specified by:
        upgrade in interface IOSession
      • getLock

        public java.util.concurrent.locks.Lock getLock()
        Description copied from interface: IOSession
        Returns session lock that should be used by I/O event handlers to synchronize access to the session.
        Specified by:
        getLock in interface IOSession
      • hasCommands

        public boolean hasCommands()
        Description copied from interface: IOSession
        Tests if there enqueued commands pending execution.
        Specified by:
        hasCommands in interface IOSession
      • channel

        public java.nio.channels.ByteChannel channel()
        Description copied from interface: IOSession
        Returns the underlying I/O channel associated with this session.
        Specified by:
        channel in interface IOSession
        Returns:
        the I/O channel.
      • getLocalAddress

        public java.net.SocketAddress getLocalAddress()
        Description copied from interface: IOSession
        Returns local address.
        Specified by:
        getLocalAddress in interface IOSession
        Returns:
        socket address.
      • getRemoteAddress

        public java.net.SocketAddress getRemoteAddress()
        Description copied from interface: IOSession
        Returns address of the remote peer.
        Specified by:
        getRemoteAddress in interface IOSession
        Returns:
        socket address.
      • getEventMask

        public int getEventMask()
        Description copied from interface: IOSession
        Returns mask of I/O evens this session declared interest in.
        Specified by:
        getEventMask in interface IOSession
        Returns:
        I/O event mask.
      • setEventMask

        public void setEventMask​(int newValue)
        Description copied from interface: IOSession
        Declares interest in I/O event notifications by setting the event mask associated with the session
        Specified by:
        setEventMask in interface IOSession
        Parameters:
        newValue - new I/O event mask.
      • setEvent

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

        public void clearEvent​(int op)
        Description copied from interface: IOSession
        Clears interest in a particular I/O event type by updating the event mask associated with the session.
        Specified by:
        clearEvent in interface IOSession
        Parameters:
        op - I/O event type.
      • read

        public int read​(java.nio.ByteBuffer dst)
                 throws java.io.IOException
        Specified by:
        read in interface java.nio.channels.ReadableByteChannel
        Throws:
        java.io.IOException
      • write

        public int write​(java.nio.ByteBuffer src)
                  throws java.io.IOException
        Specified by:
        write in interface java.nio.channels.WritableByteChannel
        Throws:
        java.io.IOException
      • updateReadTime

        public void updateReadTime()
        Description copied from interface: IOSession
        Updates the timestamp of the last read event
        Specified by:
        updateReadTime in interface IOSession
      • updateWriteTime

        public void updateWriteTime()
        Description copied from interface: IOSession
        Updates the timestamp of the last write event
        Specified by:
        updateWriteTime in interface IOSession
      • getLastReadTime

        public long getLastReadTime()
        Description copied from interface: IOSession
        Returns timestamp of the last read event.
        Specified by:
        getLastReadTime in interface IOSession
        Returns:
        timestamp.
      • getLastWriteTime

        public long getLastWriteTime()
        Description copied from interface: IOSession
        Returns timestamp of the last write event.
        Specified by:
        getLastWriteTime in interface IOSession
        Returns:
        timestamp.
      • isStatusClosed

        private boolean isStatusClosed()
      • isOpen

        public boolean isOpen()
        Specified by:
        isOpen in interface java.nio.channels.Channel
      • close

        public void close()
        Description copied from interface: IOSession
        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
        Specified by:
        close in interface IOSession
      • close

        public void close​(CloseMode closeMode)
        Description copied from interface: ModalCloseable
        Closes this process or endpoint and releases any system resources associated with it. If the endpoint or the process is already closed then invoking this method has no effect.
        Specified by:
        close in interface ModalCloseable
        Parameters:
        closeMode - How to close the receiver.
      • formatOps

        private static void formatOps​(java.lang.StringBuilder buffer,
                                      int ops)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object