Class ChannelConnection

java.lang.Object
org.dellroad.stuff.net.ChannelConnection
All Implemented Interfaces:
SelectorSupport.IOHandler
Direct Known Subclasses:
TCPConnection

public abstract class ChannelConnection extends Object implements SelectorSupport.IOHandler
Support superclass for ChannelNetwork connections.

Locking

All access to this class must be with the associated ChannelNetwork instance locked.

  • Field Details

  • Constructor Details

  • Method Details

    • getPeer

      public String getPeer()
      Get remote peer's identity.
      Returns:
      remote peer for this connection
    • getInputChannel

      public SelectableChannel getInputChannel()
      Get the associated input channel.
      Returns:
      input channel for this connection; could be same as the output channel
    • getOutputChannel

      public SelectableChannel getOutputChannel()
      Get the associated output channel.
      Returns:
      input channel for this connection; could be same as the input channel
    • getIdleTime

      public long getIdleTime()
      Get time in milliseconds since last activity.
      Returns:
      idle time in milliseconds
    • output

      protected boolean output(ByteBuffer buf)
      Enqueue an outgoing message on this connection.
      Parameters:
      buf - outgoing data
      Returns:
      true if message was enqueued, false if output buffer was full or connection closed
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • serviceIO

      public void serviceIO(SelectionKey key) throws IOException
      Description copied from interface: SelectorSupport.IOHandler
      Handle ready I/O.
      Specified by:
      serviceIO in interface SelectorSupport.IOHandler
      Parameters:
      key - selection key
      Throws:
      IOException - if an I/O error occurs; this will result in close() being invoked
    • close

      public void close(Throwable cause)
      Description copied from interface: SelectorSupport.IOHandler
      Invoked when an exception is thrown by serviceIO().

      This usually indicates the channel has been (or should be) closed. In any case, it allows for this handler to perform any required cleanup.

      Typically this method will close the associated channel (if not already closed), which implicitly unregisters the associated SelectionKeys and causes the serviceIO() methods of other handlers waiting on the same channel to be invoked, where they will then likely throw ClosedChannelException, which in turn causes a subsequent invocation of this method. Therefore, if this instance is shared by multiple selection keys selecting on the same channel, it should be idempotent.

      Specified by:
      close in interface SelectorSupport.IOHandler
      Parameters:
      cause - the error that occurred
    • updateSelection

      protected void updateSelection()
      Update selected keys.

      The implementation in ChannelConnection selects for read if the input queue is not full, and for write if the output queue is non-empty.

    • restartIdleTimer

      protected void restartIdleTimer()
      Restart the idle timer.
    • performHousekeeping

      protected void performHousekeeping() throws IOException
      Perform housekeeping. The implementation in ChannelConnection checks the max idle time.
      Throws:
      IOException - if an I/O error occurs (this connection will be closed)