Package org.elasticsearch.nio
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) theSocketChannelContextwill 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 areChannelContext.closeChannel()andsendMessage(Object, BiConsumer).
-
-
Field Summary
Fields Modifier and Type Field Description protected NioSocketChannelchannelprotected InboundChannelBufferchannelBufferprotected java.util.concurrent.atomic.AtomicBooleanisClosing-
Fields inherited from class org.elasticsearch.nio.ChannelContext
rawChannel
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedSocketChannelContext(NioSocketChannel channel, NioSelector selector, java.util.function.Consumer<java.lang.Exception> exceptionHandler, NioChannelHandler readWriteHandler, InboundChannelBuffer channelBuffer)
-
Method Summary
Modifier and Type Method Description voidaddConnectListener(java.util.function.BiConsumer<java.lang.Void,java.lang.Exception> listener)voidcloseFromSelector()This 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 voidcurrentFlushOperationComplete()protected voidcurrentFlushOperationFailed(java.io.IOException e)abstract voidflushChannel()protected intflushToChannel(FlushOperation flushOperation)NioSocketChannelgetChannel()protected FlushOperationgetPendingFlush()NioSelectorgetSelector()protected voidhandleReadBytes()booleanisConnectComplete()voidqueueWriteOperation(WriteOperation writeOperation)abstract intread()protected intreadFromChannel(InboundChannelBuffer channelBuffer)booleanreadyForFlush()protected voidregister()abstract booleanselectorShouldClose()This method indicates if a selector should close this channel.voidsendMessage(java.lang.Object message, java.util.function.BiConsumer<java.lang.Void,java.lang.Exception> listener)protected voidsetCloseNow()-
Methods inherited from class org.elasticsearch.nio.ChannelContext
addCloseListener, closeChannel, handleException, isOpen, setSelectionKey
-
-
-
-
Field Detail
-
channel
protected final NioSocketChannel channel
-
channelBuffer
protected final InboundChannelBuffer channelBuffer
-
isClosing
protected final java.util.concurrent.atomic.AtomicBoolean isClosing
-
-
Constructor Detail
-
SocketChannelContext
protected SocketChannelContext(NioSocketChannel channel, NioSelector selector, java.util.function.Consumer<java.lang.Exception> exceptionHandler, NioChannelHandler readWriteHandler, InboundChannelBuffer channelBuffer)
-
-
Method Detail
-
getSelector
public NioSelector getSelector()
- Specified by:
getSelectorin classChannelContext<java.nio.channels.SocketChannel>
-
getChannel
public NioSocketChannel getChannel()
- Specified by:
getChannelin classChannelContext<java.nio.channels.SocketChannel>
-
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.IOExceptionThis 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()
-
register
protected void register() throws java.io.IOException- Overrides:
registerin classChannelContext<java.nio.channels.SocketChannel>- Throws:
java.io.IOException
-
closeFromSelector
public void closeFromSelector() throws java.io.IOExceptionDescription 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<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
-
-