Package org.elasticsearch.nio
Class SocketChannelContext
- Direct Known Subclasses:
BytesChannelContext
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 Summary
FieldsModifier and TypeFieldDescriptionprotected final NioSocketChannelprotected final InboundChannelBufferprotected final AtomicBooleanFields inherited from class org.elasticsearch.nio.ChannelContext
rawChannel -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSocketChannelContext(NioSocketChannel channel, NioSelector selector, Config.Socket socketConfig, Consumer<Exception> exceptionHandler, NioChannelHandler channelHandler, InboundChannelBuffer channelBuffer) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddConnectListener(BiConsumer<Void, Exception> listener) protected voidvoidThis method cleans up any context resources that need to be released when a channel is closed.protected booleancloseNow()booleanconnect()This method will attempt to complete the connection process for this channel.protected voidprotected voidabstract voidprotected intflushToChannel(FlushOperation flushOperation) protected FlushOperationprotected voidbooleanvoidqueueWriteOperation(WriteOperation writeOperation) abstract intread()protected intreadFromChannel(InboundChannelBuffer inboundChannelBuffer) booleanprotected voidregister()abstract booleanThis method indicates if a selector should close this channel.voidsendMessage(Object message, BiConsumer<Void, Exception> listener) protected voidMethods inherited from class org.elasticsearch.nio.ChannelContext
addCloseListener, closeChannel, getSelectionKey, handleException, isOpen, setSelectionKey
-
Field Details
-
channel
-
channelBuffer
-
isClosing
-
-
Constructor Details
-
SocketChannelContext
protected SocketChannelContext(NioSocketChannel channel, NioSelector selector, Config.Socket socketConfig, Consumer<Exception> exceptionHandler, NioChannelHandler channelHandler, InboundChannelBuffer channelBuffer)
-
-
Method Details
-
getSelector
- Specified by:
getSelectorin classChannelContext<SocketChannel>
-
getChannel
- Specified by:
getChannelin classChannelContext<SocketChannel>
-
register
- Overrides:
registerin classChannelContext<SocketChannel>- Throws:
IOException
-
addConnectListener
-
isConnectComplete
public boolean isConnectComplete() -
connect
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:
IOException- if an I/O error occurs
-
sendMessage
-
queueWriteOperation
-
read
- Throws:
IOException
-
flushChannel
- Throws:
IOException
-
currentFlushOperationFailed
-
currentFlushOperationComplete
protected void currentFlushOperationComplete() -
getPendingFlush
-
channelActive
- Overrides:
channelActivein classChannelContext<SocketChannel>- Throws:
IOException
-
closeFromSelector
Description copied from class:ChannelContextThis 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:
closeFromSelectorin classChannelContext<SocketChannel>- Throws:
IOException- during channel / context close
-
handleReadBytes
- Throws:
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
- Throws:
IOException
-
flushToChannel
- Throws:
IOException
-