M - message typeB - message buffer typepublic abstract class IOLoop<M extends Message,B extends MessageBuffer<M>> extends SelectLoop
Loosely based on I/O loop algorithm authored by David Erickson.
DEFAULT_TIMEOUT, log, selector, stopped, timeout| Constructor and Description |
|---|
IOLoop()
Creates an IO loop with default timeout.
|
IOLoop(long timeout)
Creates an IO loop with the specified timeout.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
connect(SelectionKey key)
Processes a connection request represented by the supplied select key.
|
protected abstract B |
createBuffer(ByteChannel ch,
SSLContext sslContext)
Creates a new message buffer backed by the given socket channel and
driven by the specified loop.
|
protected void |
discardBuffer(MessageBuffer<M> b)
Discards the specified message buffer from the list of registered
buffers.
|
protected void |
handleMessageProcessingException(B b,
RuntimeException e)
Invoked if a runtime exception is thrown from
processMessages(com.h3c.nbio.MessageBuffer<M>, java.util.List<M>). |
protected void |
loop()
I/O event loop.
|
protected void |
processIO(SelectionKey key)
Processes a single IO request represented by the supplied select key.
|
protected abstract void |
processMessages(MessageBuffer<M> b,
List<M> messages)
Processes the list of messages received from the specified message
buffer.
|
void |
prune()
Prunes the registered buffers by discarding any stale ones.
|
void |
registerAccept(SocketChannel ch,
SSLContext sslContext)
Registers a new socket channel by creating a new message buffer backed
by the socket channel and driven by this loop.
|
void |
registerConnect(SocketChannel ch,
SSLContext sslContext)
Registers a new client-side socket channel by creating a new
message buffer backed by the socket channel and driven by this loop.
|
cease, getError, openSelector, run, signalStart, waitForFinish, waitForStartpublic IOLoop()
throws IOException
IOException - if unable to open selectorpublic IOLoop(long timeout)
throws IOException
timeout - select timeout in millisecondsIOException - if unable to open selectorprotected abstract B createBuffer(ByteChannel ch, SSLContext sslContext)
MessageBuffer.ch - backing byte channelsslContext - optional SSL contextprotected void discardBuffer(MessageBuffer<M> b)
One should not invoke this method directly as this is merely a hook
invoked by the default implementation of
MessageBuffer.discard() method, which is what should be
used directly instead.
b - message bufferprotected abstract void processMessages(MessageBuffer<M> b, List<M> messages)
b - source message buffermessages - list of received messages; guaranteed not to be null
and not emptyprotected void connect(SelectionKey key)
interestedOps to
SelectionKey.OP_READ and then attempts to finish the connection
on the key socket channel.key - selection key holding the pending connect operation.protected void processIO(SelectionKey key)
key - selection key holding the pending I/O operation.protected void handleMessageProcessingException(B b, RuntimeException e)
processMessages(com.h3c.nbio.MessageBuffer<M>, java.util.List<M>).
This default implementation simply rethrows the exception.
b - the buffer that was being processede - the runtime exception that was thrownpublic void prune()
public void registerAccept(SocketChannel ch, SSLContext sslContext)
MessageBuffer.ch - socket channelsslContext - optional SSL contextpublic void registerConnect(SocketChannel ch, SSLContext sslContext)
MessageBuffer.ch - socket channelsslContext - optional SSL contextprotected void loop()
throws IOException
SelectLoopSelectLoop.stopped becomes true via SelectLoop.cease() invocation.
Also, implementations are expected to SelectLoop.signalStart() when
ready.loop in class SelectLoopIOException - if unable to properly select channels or an I/O
error is propagated by an event handler.Copyright © 2014. All Rights Reserved.