public class NioConnection extends Object implements SocketConnection, EnumStatus<NioConnection.NioConnectionStatus>, Source<NioEvent<?>,SourceType>, Future<NioConnection>
NioManager
. Instantiating classes must implement
callbacks for receipt of data, connecting, accepting, and error handling.Modifier and Type | Class and Description |
---|---|
static class |
NioConnection.NioConnectionStatus
An enumeration of connection states.
|
protected static interface |
NioConnection.WriteHandler
Defines a handler for outgoing data.
|
Connection.OperationType
Modifier and Type | Field and Description |
---|---|
protected NioConnection.WriteHandler |
bufferedHandler
A
NioConnection.WriteHandler used when writes are deferred. |
protected Runnable |
closingHandler
An internal handler for connection closure.
|
protected Runnable |
deferredHandler
An internal handler for deferred writes.
|
protected static int |
FLAG_BOUND
A bit flag indicating that the connection has been bound.
|
protected static int |
FLAG_CLOSED
A bit flag indicating that the connection has been closed.
|
protected NioConnection.WriteHandler |
nullHandler
A
NioConnection.WriteHandler that forgets all data offered to it. |
protected NioConnection.WriteHandler |
writeThroughHandler
A
NioConnection.WriteHandler used when bytes can be written through directly to the underlying transport. |
Modifier | Constructor and Description |
---|---|
protected |
NioConnection(ConnectionHandler<? super NioConnection> handler,
int bufferSize,
NioManagerThread thread)
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
cancel(boolean mayInterruptIfRunning)
Does nothing.
|
void |
close()
Closes this connection.
|
protected NioEvent<Integer> |
createOpEvent(int mask,
boolean enabled)
Creates an operation interest change
NioEvent that originates from this connection. |
protected void |
deregisterKey()
NioManagerThread call -- Deregisters the underlying SelectionKey . |
protected void |
doBind()
NioManagerThread call -- Finishes connecting/accepting. |
protected void |
doClose()
NioManagerThread call -- Closes this connection. |
protected void |
doClosing(ConnectionHandler.ClosingType type)
NioManagerThread call -- Initiates connection closure. |
protected void |
doError(Throwable exception)
NioManagerThread call -- Sets the exception that occurred. |
protected void |
doOp(int mask,
boolean enabled)
NioManagerThread call -- Does an operation interest change. |
protected void |
doRead()
NioManagerThread call -- Does a ready read. |
protected void |
doWrite()
NioManagerThread call -- Does a ready write. |
NioConnection |
get()
Waits until this connection has been initialized.
|
NioConnection |
get(long timeout,
TimeUnit unit)
Waits until this connection has been initialized.
|
Throwable |
getException()
Gets the exception that occurred.
|
Handler<NioEvent<?>> |
getHandler()
|
protected SelectionKey |
getKey()
Gets the
SelectionKey . |
InetSocketAddress |
getLocalAddress()
Gets the local address.
|
Object |
getLock()
Gets the monitor for enforcing mutual exclusion over operations on this connection.
|
InetSocketAddress |
getRemoteAddress()
Gets the remote address.
|
NioConnection.NioConnectionStatus |
getStatus()
Gets the status.
|
protected NioManagerThread |
getThread()
Gets the
NioManagerThread currently servicing this connection. |
SourceType |
getType()
Gets the type.
|
<V> Future<V> |
invoke(Callable<V> c)
Executes the given code snippet on this connection's
ConnectionManager thread. |
boolean |
isCancelled()
Does nothing.
|
boolean |
isDone()
Gets whether this connection has been initialized.
|
boolean |
isManagerThread()
Gets whether this connection's
ConnectionManager thread is the current thread. |
void |
onLocal(NioEvent<?> evt)
Delivers an
Event to the local manifestation. |
void |
onRemote(NioEvent<?> evt)
Delivers an
Event to the remote manifestation. |
protected void |
registerKey(Selector selector,
int initialOps)
NioManagerThread call -- Registers the underlying SelectionKey . |
int |
send(ByteBuffer bb)
Sends data to the remote host.
|
protected void |
setBufferedHandler()
Sets
bufferedHandler . |
void |
setEnabled(Connection.OperationType type,
boolean enabled)
Enables/disables various managed operations:
Connection.OperationType.READ -- The ConnectionHandler.onReceive(ByteBuffer) callback is
enabled/disabled. |
void |
setException(Throwable exception)
Sets the exception and triggers connection closure.
|
void |
setHandler(Handler<NioEvent<?>> handler)
|
protected void |
setNullHandler()
Sets
nullHandler . |
void |
setStatus(NioConnection.NioConnectionStatus status)
Sets the status.
|
protected void |
setThread(NioManagerThread thread)
Sets the
NioManagerThread currently servicing this connection. |
protected void |
setup(SocketChannel channel)
NioManagerThread call -- Sets up the SocketChannel behind this connection. |
protected void |
setWriteThroughHandler()
Sets
writeThroughHandler . |
String |
toString()
Creates a human-readable representation of this connection.
|
protected static final int FLAG_BOUND
protected static final int FLAG_CLOSED
protected final NioConnection.WriteHandler bufferedHandler
NioConnection.WriteHandler
used when writes are deferred. Enqueues data without further action.protected final NioConnection.WriteHandler writeThroughHandler
NioConnection.WriteHandler
used when bytes can be written through directly to the underlying transport.protected final NioConnection.WriteHandler nullHandler
NioConnection.WriteHandler
that forgets all data offered to it.protected final Runnable deferredHandler
protected final Runnable closingHandler
protected NioConnection(ConnectionHandler<? super NioConnection> handler, int bufferSize, NioManagerThread thread)
handler
- the ConnectionHandler
for callbacks.bufferSize
- the network buffer size.thread
- the NioManagerThread
with which this connection will be registered.public int send(ByteBuffer bb)
Connection
send
in interface Connection
bb
- the ByteBuffer
containing data. It must be in ready-to-read mode.byte
s remaining in this connection's write buffer; is greater than 0
if and
only if not all byte
s could be written out to the network, and have to be stored locally for the
time being.public void setEnabled(Connection.OperationType type, boolean enabled)
Connection
Connection.OperationType.READ
-- The ConnectionHandler.onReceive(ByteBuffer)
callback is
enabled/disabled. With argument true
, this method shall guarantee that
ConnectionHandler.onReceive(ByteBuffer)
will subsequently be called at least once.Connection.OperationType.WRITE
-- Managed writes of data buffered by the Connection.send(ByteBuffer)
method are
enabled/disabled.setEnabled
in interface Connection
type
- the Connection.OperationType
.enabled
- whether the given operation is enabled or disabled.Connection.OperationType
public Throwable getException()
Connection
getException
in interface Connection
public void setException(Throwable exception)
Connection
setException
in interface Connection
exception
- the exception.ConnectionHandler.ClosingType.ERROR
public Object getLock()
Connection
getLock
in interface Connection
public boolean isManagerThread()
Connection
ConnectionManager
thread is the current thread.isManagerThread
in interface Connection
true
if and only if this connection's ConnectionManager
thread is the current thread.
Note: All code where this test returns true
are thread-safe with respect to callbacks.Thread.currentThread()
public <V> Future<V> invoke(Callable<V> c)
Connection
ConnectionManager
thread.invoke
in interface Connection
V
- the result type.c
- the code snippet to execute.Future
for retrieving the Callable
result.public InetSocketAddress getLocalAddress()
SocketConnection
getLocalAddress
in interface SocketConnection
public InetSocketAddress getRemoteAddress()
SocketConnection
getRemoteAddress
in interface SocketConnection
public NioConnection.NioConnectionStatus getStatus()
EnumStatus
getStatus
in interface EnumStatus<NioConnection.NioConnectionStatus>
public void setStatus(NioConnection.NioConnectionStatus status)
EnumStatus
setStatus
in interface EnumStatus<NioConnection.NioConnectionStatus>
public void onLocal(NioEvent<?> evt)
SourceLocal
Event
to the local manifestation.onLocal
in interface SourceLocal<NioEvent<?>>
public NioConnection get() throws InterruptedException, ExecutionException
get
in interface Future<NioConnection>
InterruptedException
- when this operation is interrupted.ExecutionException
- when something goes awry.public NioConnection get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
get
in interface Future<NioConnection>
InterruptedException
- when this operation is interrupted.ExecutionException
- when something goes awry.TimeoutException
- when this operation has timed out.public boolean isDone()
isDone
in interface Future<NioConnection>
public void close()
Connection
close
in interface Closeable
close
in interface AutoCloseable
close
in interface Source<NioEvent<?>,SourceType>
close
in interface Connection
public String toString()
Connection
toString
in interface Connection
toString
in class Object
protected void setNullHandler()
nullHandler
.protected void setBufferedHandler()
bufferedHandler
.protected void setWriteThroughHandler()
writeThroughHandler
.protected NioManagerThread getThread()
NioManagerThread
currently servicing this connection.protected void setThread(NioManagerThread thread)
NioManagerThread
currently servicing this connection.protected NioEvent<Integer> createOpEvent(int mask, boolean enabled)
NioEvent
that originates from this connection.protected SelectionKey getKey()
SelectionKey
.protected void setup(SocketChannel channel) throws IOException
NioManagerThread
call -- Sets up the SocketChannel
behind this connection.IOException
- when something goes awry.protected void registerKey(Selector selector, int initialOps) throws IOException
NioManagerThread
call -- Registers the underlying SelectionKey
.IOException
- when something goes awry.protected void deregisterKey()
NioManagerThread
call -- Deregisters the underlying SelectionKey
.protected void doBind()
NioManagerThread
call -- Finishes connecting/accepting. Does not do own exception handling.protected void doWrite()
NioManagerThread
call -- Does a ready write. Does own exception handling.protected void doRead()
NioManagerThread
call -- Does a ready read. Does own exception handling.protected void doOp(int mask, boolean enabled)
NioManagerThread
call -- Does an operation interest change. Does not do own exception handling.protected void doClosing(ConnectionHandler.ClosingType type)
NioManagerThread
call -- Initiates connection closure. Does not do own exception handling.protected void doError(Throwable exception)
NioManagerThread
call -- Sets the exception that occurred. Does not do own exception handling.protected void doClose()
NioManagerThread
call -- Closes this connection. Does not do own exception handling.public boolean cancel(boolean mayInterruptIfRunning)
cancel
in interface Future<NioConnection>
false
, always.public boolean isCancelled()
isCancelled
in interface Future<NioConnection>
false
, always.public SourceType getType()
EnumType
getType
in interface EnumType<SourceType>
public Handler<NioEvent<?>> getHandler()
Source
getHandler
in interface Source<NioEvent<?>,SourceType>
public void setHandler(Handler<NioEvent<?>> handler)
Source
setHandler
in interface Source<NioEvent<?>,SourceType>
public void onRemote(NioEvent<?> evt)
SourceRemote
Event
to the remote manifestation.onRemote
in interface SourceRemote<NioEvent<?>>