Class ChannelConnection
- All Implemented Interfaces:
SelectorSupport.IOHandler
- Direct Known Subclasses:
TCPConnection
ChannelNetwork connections.
Locking
All access to this class must be with the associated ChannelNetwork instance locked.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final SelectableChannelprotected final SelectionKeyprotected final Loggerprotected final ChannelNetworkprotected final SelectableChannelprotected final SelectionKeyprotected final String -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedChannelConnection(ChannelNetwork network, String peer, SelectableChannel channel) protectedChannelConnection(ChannelNetwork network, String peer, SelectableChannel inputChannel, SelectableChannel outputChannel) -
Method Summary
Modifier and TypeMethodDescriptionvoidInvoked when an exception is thrown byserviceIO().longGet time in milliseconds since last activity.Get the associated input channel.Get the associated output channel.getPeer()Get remote peer's identity.protected booleanoutput(ByteBuffer buf) Enqueue an outgoing message on this connection.protected voidPerform housekeeping.protected voidRestart the idle timer.voidserviceIO(SelectionKey key) Handle ready I/O.toString()protected voidUpdate selected keys.
-
Field Details
-
log
-
network
-
peer
-
inputChannel
-
outputChannel
-
inputSelectionKey
-
outputSelectionKey
-
-
Constructor Details
-
ChannelConnection
protected ChannelConnection(ChannelNetwork network, String peer, SelectableChannel channel) throws IOException - Throws:
IOException
-
ChannelConnection
protected ChannelConnection(ChannelNetwork network, String peer, SelectableChannel inputChannel, SelectableChannel outputChannel) throws IOException - Throws:
IOException
-
-
Method Details
-
getPeer
Get remote peer's identity.- Returns:
- remote peer for this connection
-
getInputChannel
Get the associated input channel.- Returns:
- input channel for this connection; could be same as the output channel
-
getOutputChannel
Get the associated output channel.- Returns:
- input channel for this connection; could be same as the input channel
-
getIdleTime
public long getIdleTime()Get time in milliseconds since last activity.- Returns:
- idle time in milliseconds
-
output
Enqueue an outgoing message on this connection.- Parameters:
buf- outgoing data- Returns:
- true if message was enqueued, false if output buffer was full or connection closed
-
toString
-
serviceIO
Description copied from interface:SelectorSupport.IOHandlerHandle ready I/O.- Specified by:
serviceIOin interfaceSelectorSupport.IOHandler- Parameters:
key- selection key- Throws:
IOException- if an I/O error occurs; this will result inclose()being invoked
-
close
Description copied from interface:SelectorSupport.IOHandlerInvoked when an exception is thrown byserviceIO().This usually indicates the channel has been (or should be) closed. In any case, it allows for this handler to perform any required cleanup.
Typically this method will close the associated channel (if not already closed), which implicitly unregisters the associated
SelectionKeys and causes theserviceIO()methods of other handlers waiting on the same channel to be invoked, where they will then likely throwClosedChannelException, which in turn causes a subsequent invocation of this method. Therefore, if this instance is shared by multiple selection keys selecting on the same channel, it should be idempotent.- Specified by:
closein interfaceSelectorSupport.IOHandler- Parameters:
cause- the error that occurred
-
updateSelection
protected void updateSelection()Update selected keys.The implementation in
ChannelConnectionselects for read if the input queue is not full, and for write if the output queue is non-empty. -
restartIdleTimer
protected void restartIdleTimer()Restart the idle timer. -
performHousekeeping
Perform housekeeping. The implementation inChannelConnectionchecks the max idle time.- Throws:
IOException- if an I/O error occurs (this connection will be closed)
-