Class SocketChannelContext

java.lang.Object
org.elasticsearch.nio.ChannelContext<java.nio.channels.SocketChannel>
org.elasticsearch.nio.SocketChannelContext
Direct Known Subclasses:
BytesChannelContext

public abstract class SocketChannelContext
extends ChannelContext<java.nio.channels.SocketChannel>
This context should implement the specific logic for a channel. When a channel receives a notification that it is ready to perform certain operations (read, write, etc) the SocketChannelContext will be called. This context will need to implement all protocol related logic. Additionally, if any special close behavior is required, it should be implemented in this context. The only methods of the context that should ever be called from a non-selector thread are ChannelContext.closeChannel() and sendMessage(Object, BiConsumer).
  • Field Details

  • Constructor Details

  • Method Details

    • getSelector

      public NioSelector getSelector()
      Specified by:
      getSelector in class ChannelContext<java.nio.channels.SocketChannel>
    • getChannel

      public NioSocketChannel getChannel()
      Specified by:
      getChannel in class ChannelContext<java.nio.channels.SocketChannel>
    • register

      protected void register() throws java.io.IOException
      Overrides:
      register in class ChannelContext<java.nio.channels.SocketChannel>
      Throws:
      java.io.IOException
    • addConnectListener

      public void addConnectListener​(java.util.function.BiConsumer<java.lang.Void,​java.lang.Exception> listener)
    • isConnectComplete

      public boolean isConnectComplete()
    • connect

      public boolean connect() throws java.io.IOException
      This method will attempt to complete the connection process for this channel. It should be called for new channels or for a channel that has produced a OP_CONNECT event. If this method returns true then the connection is complete and the channel is ready for reads and writes. If it returns false, the channel is not yet connected and this method should be called again when a OP_CONNECT event is received.
      Returns:
      true if the connection process is complete
      Throws:
      java.io.IOException - if an I/O error occurs
    • sendMessage

      public void sendMessage​(java.lang.Object message, java.util.function.BiConsumer<java.lang.Void,​java.lang.Exception> listener)
    • queueWriteOperation

      public void queueWriteOperation​(WriteOperation writeOperation)
    • read

      public abstract int read() throws java.io.IOException
      Throws:
      java.io.IOException
    • flushChannel

      public abstract void flushChannel() throws java.io.IOException
      Throws:
      java.io.IOException
    • currentFlushOperationFailed

      protected void currentFlushOperationFailed​(java.io.IOException e)
    • currentFlushOperationComplete

      protected void currentFlushOperationComplete()
    • getPendingFlush

      protected FlushOperation getPendingFlush()
    • channelActive

      protected void channelActive() throws java.io.IOException
      Overrides:
      channelActive in class ChannelContext<java.nio.channels.SocketChannel>
      Throws:
      java.io.IOException
    • closeFromSelector

      public void closeFromSelector() throws java.io.IOException
      Description copied from class: ChannelContext
      This method cleans up any context resources that need to be released when a channel is closed. It should only be called by the selector thread.
      Overrides:
      closeFromSelector in class ChannelContext<java.nio.channels.SocketChannel>
      Throws:
      java.io.IOException - during channel / context close
    • handleReadBytes

      protected void handleReadBytes() throws java.io.IOException
      Throws:
      java.io.IOException
    • readyForFlush

      public boolean readyForFlush()
    • selectorShouldClose

      public abstract boolean selectorShouldClose()
      This method indicates if a selector should close this channel.
      Returns:
      a boolean indicating if the selector should close
    • closeNow

      protected boolean closeNow()
    • setCloseNow

      protected void setCloseNow()
    • readFromChannel

      protected int readFromChannel​(InboundChannelBuffer channelBuffer) throws java.io.IOException
      Throws:
      java.io.IOException
    • flushToChannel

      protected int flushToChannel​(FlushOperation flushOperation) throws java.io.IOException
      Throws:
      java.io.IOException