Class AbstractIoSession

    • Field Detail

      • CLOSE_REQUEST

        public static final WriteRequest CLOSE_REQUEST
        An internal write request object that triggers session close.
      • MESSAGE_SENT_REQUEST

        public static final WriteRequest MESSAGE_SENT_REQUEST
        An internal write request object that triggers message sent events.
    • Constructor Detail

      • AbstractIoSession

        protected AbstractIoSession​(IoService service)
        Create a Session for a service
        Parameters:
        service - the Service for this session
    • Method Detail

      • getId

        public final long getId()
        We use an AtomicLong to guarantee that the session ID are unique.
        Specified by:
        getId in interface IoSession
        Returns:
        a unique identifier for this session. Every session has its own ID which is different from each other. TODO : The way it's implemented does not guarantee that the contract is respected. It uses the HashCode() method which don't guarantee the key unicity.
      • getProcessor

        public abstract IoProcessor getProcessor()
        Returns:
        The associated IoProcessor for this session
      • isConnected

        public final boolean isConnected()
        Specified by:
        isConnected in interface IoSession
        Returns:
        true if this session is connected with remote peer.
      • isActive

        public boolean isActive()
        Specified by:
        isActive in interface IoSession
        Returns:
        true if this session is active.
      • isClosing

        public final boolean isClosing()
        Specified by:
        isClosing in interface IoSession
        Returns:
        true if and only if this session is being closed (but not disconnected yet) or is closed.
      • isSecured

        public boolean isSecured()
        Specified by:
        isSecured in interface IoSession
        Returns:
        true if the session has started and initialized a SslEngine, false if the session is not yet secured (the handshake is not completed) or if SSL is not set for this session, or if SSL is not even an option.
      • isScheduledForFlush

        public final boolean isScheduledForFlush()
        Tells if the session is scheduled for flushed
        Returns:
        true if the session is scheduled for flush
      • scheduledForFlush

        public final void scheduledForFlush()
        Schedule the session for flushed
      • unscheduledForFlush

        public final void unscheduledForFlush()
        Change the session's status : it's not anymore scheduled for flush
      • setScheduledForFlush

        public final boolean setScheduledForFlush​(boolean schedule)
        Set the scheduledForFLush flag. As we may have concurrent access to this flag, we compare and set it in one call.
        Parameters:
        schedule - the new value to set if not already set.
        Returns:
        true if the session flag has been set, and if it wasn't set already.
      • close

        public final CloseFuture close​(boolean rightNow)
        Closes this session immediately or after all queued write requests are flushed. This operation is asynchronous. Wait for the returned CloseFuture if you want to wait for the session actually closed.
        Specified by:
        close in interface IoSession
        Parameters:
        rightNow - true to close this session immediately . The pending write requests will simply be discarded. false to close this session after all queued write requests are flushed.
        Returns:
        The associated CloseFuture
      • close

        public final CloseFuture close()
        Closes this session after all queued write requests are flushed. This operation is asynchronous. Wait for the returned CloseFuture if you want to wait for the session actually closed.
        Specified by:
        close in interface IoSession
        Returns:
        The associated CloseFuture
      • closeOnFlush

        public final CloseFuture closeOnFlush()
        Closes this session after all queued write requests are flushed. This operation is asynchronous. Wait for the returned CloseFuture if you want to wait for the session actually closed.
        Specified by:
        closeOnFlush in interface IoSession
        Returns:
        The associated CloseFuture
      • closeNow

        public final CloseFuture closeNow()
        Closes this session immediately. This operation is asynchronous, it returns a CloseFuture.
        Specified by:
        closeNow in interface IoSession
        Returns:
        The CloseFuture that can be use to wait for the completion of this operation
      • destroy

        protected void destroy()
        Destroy the session
      • read

        public final ReadFuture read()
        TODO This javadoc is wrong. The return tag should be short.
        Specified by:
        read in interface IoSession
        Returns:
        a ReadFuture which is notified when a new message is received, the connection is closed or an exception is caught. This operation is especially useful when you implement a client application. TODO : Describe here how we enable this feature. However, please note that this operation is disabled by default and throw IllegalStateException because all received events must be queued somewhere to support this operation, possibly leading to memory leak. This means you have to keep calling IoSession.read() once you enabled this operation. To enable this operation, please call IoSessionConfig.setUseReadOperation(boolean) with true.
      • offerReadFuture

        public final void offerReadFuture​(Object message)
        Associates a message to a ReadFuture
        Parameters:
        message - the message to associate to the ReadFuture
      • offerFailedReadFuture

        public final void offerFailedReadFuture​(Throwable exception)
        Associates a failure to a ReadFuture
        Parameters:
        exception - the exception to associate to the ReadFuture
      • offerClosedReadFuture

        public final void offerClosedReadFuture()
        Inform the ReadFuture that the session has been closed
      • write

        public WriteFuture write​(Object message)
        Writes the specified message to remote peer. This operation is asynchronous; IoHandler.messageSent(IoSession,Object) will be invoked when the message is actually sent to remote peer. You can also wait for the returned WriteFuture if you want to wait for the message actually written.
        Specified by:
        write in interface IoSession
        Parameters:
        message - The message to write
        Returns:
        The associated WriteFuture
      • write

        public WriteFuture write​(Object message,
                                 SocketAddress remoteAddress)
        (Optional) Writes the specified message to the specified destination. This operation is asynchronous; IoHandler.messageSent(IoSession, Object) will be invoked when the message is actually sent to remote peer. You can also wait for the returned WriteFuture if you want to wait for the message actually written.

        When you implement a client that receives a broadcast message from a server such as DHCP server, the client might need to send a response message for the broadcast message the server sent. Because the remote address of the session is not the address of the server in case of broadcasting, there should be a way to specify the destination when you write the response message. This interface provides IoSession.write(Object, SocketAddress) method so you can specify the destination.

        Specified by:
        write in interface IoSession
        Parameters:
        message - The message to write
        remoteAddress - null if you want the message sent to the default remote address
        Returns:
        The associated WriteFuture
      • getAttachment

        public final Object getAttachment()
        Returns an attachment of this session. This method is identical with getAttribute( "" ).
        Specified by:
        getAttachment in interface IoSession
        Returns:
        The attachment
      • setAttachment

        public final Object setAttachment​(Object attachment)
        Sets an attachment of this session. This method is identical with setAttribute( "", attachment ).
        Specified by:
        setAttachment in interface IoSession
        Parameters:
        attachment - The attachment
        Returns:
        Old attachment. null if it is new.
      • getAttribute

        public final Object getAttribute​(Object key)
        Returns the value of the user-defined attribute of this session.
        Specified by:
        getAttribute in interface IoSession
        Parameters:
        key - the key of the attribute
        Returns:
        null if there is no attribute with the specified key
      • getAttribute

        public final Object getAttribute​(Object key,
                                         Object defaultValue)
        Returns the value of user defined attribute associated with the specified key. If there's no such attribute, the specified default value is associated with the specified key, and the default value is returned. This method is same with the following code except that the operation is performed atomically.
         if (containsAttribute(key)) {
             return getAttribute(key);
         } else {
             setAttribute(key, defaultValue);
             return defaultValue;
         }
         
        Specified by:
        getAttribute in interface IoSession
        Parameters:
        key - the key of the attribute we want to retreive
        defaultValue - the default value of the attribute
        Returns:
        The retrieved attribute or null if not found
      • setAttribute

        public final Object setAttribute​(Object key,
                                         Object value)
        Sets a user-defined attribute.
        Specified by:
        setAttribute in interface IoSession
        Parameters:
        key - the key of the attribute
        value - the value of the attribute
        Returns:
        The old value of the attribute. null if it is new.
      • setAttribute

        public final Object setAttribute​(Object key)
        Sets a user defined attribute without a value. This is useful when you just want to put a 'mark' attribute. Its value is set to Boolean.TRUE.
        Specified by:
        setAttribute in interface IoSession
        Parameters:
        key - the key of the attribute
        Returns:
        The old value of the attribute. null if it is new.
      • setAttributeIfAbsent

        public final Object setAttributeIfAbsent​(Object key,
                                                 Object value)
        Sets a user defined attribute if the attribute with the specified key is not set yet. This method is same with the following code except that the operation is performed atomically.
         if (containsAttribute(key)) {
             return getAttribute(key);
         } else {
             return setAttribute(key, value);
         }
         
        Specified by:
        setAttributeIfAbsent in interface IoSession
        Parameters:
        key - The key of the attribute we want to set
        value - The value we want to set
        Returns:
        The old value of the attribute. null if not found.
      • setAttributeIfAbsent

        public final Object setAttributeIfAbsent​(Object key)
        Sets a user defined attribute without a value if the attribute with the specified key is not set yet. This is useful when you just want to put a 'mark' attribute. Its value is set to Boolean.TRUE. This method is same with the following code except that the operation is performed atomically.
         if (containsAttribute(key)) {
             return getAttribute(key);  // might not always be Boolean.TRUE.
         } else {
             return setAttribute(key);
         }
         
        Specified by:
        setAttributeIfAbsent in interface IoSession
        Parameters:
        key - The key of the attribute we want to set
        Returns:
        The old value of the attribute. null if not found.
      • removeAttribute

        public final Object removeAttribute​(Object key)
        Removes a user-defined attribute with the specified key.
        Specified by:
        removeAttribute in interface IoSession
        Parameters:
        key - The key of the attribute we want to remove
        Returns:
        The old value of the attribute. null if not found.
      • removeAttribute

        public final boolean removeAttribute​(Object key,
                                             Object value)
        Removes a user defined attribute with the specified key if the current attribute value is equal to the specified value. This method is same with the following code except that the operation is performed atomically.
         if (containsAttribute(key) && getAttribute(key).equals(value)) {
             removeAttribute(key);
             return true;
         } else {
             return false;
         }
         
        Specified by:
        removeAttribute in interface IoSession
        Parameters:
        key - The key we want to remove
        value - The value we want to remove
        Returns:
        true if the removal was successful
      • replaceAttribute

        public final boolean replaceAttribute​(Object key,
                                              Object oldValue,
                                              Object newValue)
        Replaces a user defined attribute with the specified key if the value of the attribute is equals to the specified old value. This method is same with the following code except that the operation is performed atomically.
         if (containsAttribute(key) && getAttribute(key).equals(oldValue)) {
             setAttribute(key, newValue);
             return true;
         } else {
             return false;
         }
         
        Specified by:
        replaceAttribute in interface IoSession
        Parameters:
        key - The key we want to replace
        oldValue - The previous value
        newValue - The new value
        Returns:
        true if the replacement was successful
      • containsAttribute

        public final boolean containsAttribute​(Object key)
        Specified by:
        containsAttribute in interface IoSession
        Parameters:
        key - The key of the attribute we are looking for in the session
        Returns:
        true if this session contains the attribute with the specified key.
      • getAttributeKeys

        public final Set<Object> getAttributeKeys()
        Specified by:
        getAttributeKeys in interface IoSession
        Returns:
        the set of keys of all user-defined attributes.
      • getAttributeMap

        public final IoSessionAttributeMap getAttributeMap()
        Returns:
        The map of attributes associated with the session
      • setAttributeMap

        public final void setAttributeMap​(IoSessionAttributeMap attributes)
        Set the map of attributes associated with the session
        Parameters:
        attributes - The Map of attributes
      • setWriteRequestQueue

        public final void setWriteRequestQueue​(WriteRequestQueue writeRequestQueue)
        Create a new close aware write queue, based on the given write queue.
        Parameters:
        writeRequestQueue - The write request queue
      • suspendRead

        public final void suspendRead()
        Suspends read operations for this session.
        Specified by:
        suspendRead in interface IoSession
      • suspendWrite

        public final void suspendWrite()
        Suspends write operations for this session.
        Specified by:
        suspendWrite in interface IoSession
      • resumeRead

        public final void resumeRead()
        Resumes read operations for this session.
        Specified by:
        resumeRead in interface IoSession
      • resumeWrite

        public final void resumeWrite()
        Resumes write operations for this session.
        Specified by:
        resumeWrite in interface IoSession
      • isReadSuspended

        public boolean isReadSuspended()
        Is read operation is suspended for this session.
        Specified by:
        isReadSuspended in interface IoSession
        Returns:
        true if suspended
      • isWriteSuspended

        public boolean isWriteSuspended()
        Is write operation is suspended for this session.
        Specified by:
        isWriteSuspended in interface IoSession
        Returns:
        true if suspended
      • getReadBytes

        public final long getReadBytes()
        Specified by:
        getReadBytes in interface IoSession
        Returns:
        the total number of bytes which were read from this session.
      • getWrittenBytes

        public final long getWrittenBytes()
        Specified by:
        getWrittenBytes in interface IoSession
        Returns:
        the total number of bytes which were written to this session.
      • getReadMessages

        public final long getReadMessages()
        Specified by:
        getReadMessages in interface IoSession
        Returns:
        the total number of messages which were read and decoded from this session.
      • getWrittenMessages

        public final long getWrittenMessages()
        Specified by:
        getWrittenMessages in interface IoSession
        Returns:
        the total number of messages which were written and encoded by this session.
      • getReadBytesThroughput

        public final double getReadBytesThroughput()
        Specified by:
        getReadBytesThroughput in interface IoSession
        Returns:
        the number of read bytes per second.
      • getWrittenBytesThroughput

        public final double getWrittenBytesThroughput()
        Specified by:
        getWrittenBytesThroughput in interface IoSession
        Returns:
        the number of written bytes per second.
      • getReadMessagesThroughput

        public final double getReadMessagesThroughput()
        Specified by:
        getReadMessagesThroughput in interface IoSession
        Returns:
        the number of read messages per second.
      • getWrittenMessagesThroughput

        public final double getWrittenMessagesThroughput()
        Specified by:
        getWrittenMessagesThroughput in interface IoSession
        Returns:
        the number of written messages per second.
      • updateThroughput

        public final void updateThroughput​(long currentTime,
                                           boolean force)
        Update all statistical properties related with throughput assuming the specified time is the current time. By default this method returns silently without updating the throughput properties if they were calculated already within last calculation interval. If, however, force is specified as true, this method updates the throughput properties immediately.
        Specified by:
        updateThroughput in interface IoSession
        Parameters:
        currentTime - the current time in milliseconds
        force - Force the update if true
      • getScheduledWriteBytes

        public final long getScheduledWriteBytes()
        Specified by:
        getScheduledWriteBytes in interface IoSession
        Returns:
        the number of bytes which are scheduled to be written to this session.
      • getScheduledWriteMessages

        public final int getScheduledWriteMessages()
        Specified by:
        getScheduledWriteMessages in interface IoSession
        Returns:
        the number of messages which are scheduled to be written to this session.
      • setScheduledWriteBytes

        protected void setScheduledWriteBytes​(int byteCount)
        Set the number of scheduled write bytes
        Parameters:
        byteCount - The number of scheduled bytes for write
      • setScheduledWriteMessages

        protected void setScheduledWriteMessages​(int messages)
        Set the number of scheduled write messages
        Parameters:
        messages - The number of scheduled messages for write
      • increaseReadBytes

        public final void increaseReadBytes​(long increment,
                                            long currentTime)
        Increase the number of read bytes
        Parameters:
        increment - The number of read bytes
        currentTime - The current time
      • increaseReadMessages

        public final void increaseReadMessages​(long currentTime)
        Increase the number of read messages
        Parameters:
        currentTime - The current time
      • increaseWrittenBytes

        public final void increaseWrittenBytes​(int increment,
                                               long currentTime)
        Increase the number of written bytes
        Parameters:
        increment - The number of written bytes
        currentTime - The current time
      • increaseWrittenMessages

        public final void increaseWrittenMessages​(WriteRequest request,
                                                  long currentTime)
        Increase the number of written messages
        Parameters:
        request - The written message
        currentTime - The current tile
      • increaseScheduledWriteBytes

        public final void increaseScheduledWriteBytes​(int increment)
        Increase the number of scheduled write bytes for the session
        Parameters:
        increment - The number of newly added bytes to write
      • increaseScheduledWriteMessages

        public final void increaseScheduledWriteMessages()
        Increase the number of scheduled message to write
      • decreaseScheduledBytesAndMessages

        public final void decreaseScheduledBytesAndMessages​(WriteRequest request)
        Decrease the counters of written messages and written bytes when a message has been written
        Parameters:
        request - The written message
      • getWriteRequestQueue

        public final WriteRequestQueue getWriteRequestQueue()
        Get the queue that contains the message waiting for being written. As the reader might not be ready, it's frequent that the messages aren't written completely, or that some older messages are waiting to be written when a new message arrives. This queue is used to manage the backlog of messages.
        Specified by:
        getWriteRequestQueue in interface IoSession
        Returns:
        The queue containing the pending messages.
      • getCurrentWriteMessage

        public final Object getCurrentWriteMessage()
        Returns the message which is being written by IoService.
        Specified by:
        getCurrentWriteMessage in interface IoSession
        Returns:
        null if and if only no message is being written
      • setCurrentWriteRequest

        public final void setCurrentWriteRequest​(WriteRequest currentWriteRequest)
        Associate the current write request with the session
        Specified by:
        setCurrentWriteRequest in interface IoSession
        Parameters:
        currentWriteRequest - the current write request to associate
      • increaseReadBufferSize

        public final void increaseReadBufferSize()
        Increase the ReadBuffer size (it will double)
      • decreaseReadBufferSize

        public final void decreaseReadBufferSize()
        Decrease the ReadBuffer size (it will be divided by a factor 2)
      • getCreationTime

        public final long getCreationTime()
        Specified by:
        getCreationTime in interface IoSession
        Returns:
        the session's creation time in milliseconds
      • getLastIoTime

        public final long getLastIoTime()
        Specified by:
        getLastIoTime in interface IoSession
        Returns:
        the time in millis when I/O occurred lastly.
      • getLastReadTime

        public final long getLastReadTime()
        Specified by:
        getLastReadTime in interface IoSession
        Returns:
        the time in millis when read operation occurred lastly.
      • getLastWriteTime

        public final long getLastWriteTime()
        Specified by:
        getLastWriteTime in interface IoSession
        Returns:
        the time in millis when write operation occurred lastly.
      • isIdle

        public final boolean isIdle​(IdleStatus status)
        Specified by:
        isIdle in interface IoSession
        Parameters:
        status - The researched idle status
        Returns:
        true if this session is idle for the specified IdleStatus.
      • getIdleCount

        public final int getIdleCount​(IdleStatus status)
        Specified by:
        getIdleCount in interface IoSession
        Parameters:
        status - The researched idle status
        Returns:
        the number of the fired continuous sessionIdle events for the specified IdleStatus.

        If sessionIdle event is fired first after some time after I/O, idleCount becomes 1. idleCount resets to 0 if any I/O occurs again, otherwise it increases to 2 and so on if sessionIdle event is fired again without any I/O between two (or more) sessionIdle events.

      • getLastIdleTime

        public final long getLastIdleTime​(IdleStatus status)
        Specified by:
        getLastIdleTime in interface IoSession
        Parameters:
        status - The researched idle status
        Returns:
        the time in milliseconds when the last sessionIdle event is fired for the specified IdleStatus.
      • increaseIdleCount

        public final void increaseIdleCount​(IdleStatus status,
                                            long currentTime)
        Increase the count of the various Idle counter
        Parameters:
        status - The current status
        currentTime - The current time
      • hashCode

        public final int hashCode()
        Overrides:
        hashCode in class Object
      • equals

        public final boolean equals​(Object o)
        TODO This is a ridiculous implementation. Need to be replaced.
        Overrides:
        equals in class Object
      • notifyIdleSession

        public static void notifyIdleSession​(IoSession session,
                                             long currentTime)
        Fires a IoEventType.SESSION_IDLE event if applicable for the specified session.
        Parameters:
        session - The session that is notified
        currentTime - the current time (i.e. System.currentTimeMillis())