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
Modifier and TypeFieldDescriptionprotected final SelectableChannel
protected final SelectionKey
protected final Logger
protected final ChannelNetwork
protected final SelectableChannel
protected final SelectionKey
protected final String
-
Constructor Summary
ModifierConstructorDescriptionprotected
ChannelConnection
(ChannelNetwork network, String peer, SelectableChannel channel) protected
ChannelConnection
(ChannelNetwork network, String peer, SelectableChannel inputChannel, SelectableChannel outputChannel) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Invoked when an exception is thrown byserviceIO()
.long
Get time in milliseconds since last activity.Get the associated input channel.Get the associated output channel.getPeer()
Get remote peer's identity.protected boolean
output
(ByteBuffer buf) Enqueue an outgoing message on this connection.protected void
Perform housekeeping.protected void
Restart the idle timer.void
serviceIO
(SelectionKey key) Handle ready I/O.toString()
protected void
Update 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.IOHandler
Handle ready I/O.- Specified by:
serviceIO
in 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.IOHandler
Invoked 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
SelectionKey
s 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:
close
in interfaceSelectorSupport.IOHandler
- Parameters:
cause
- the error that occurred
-
updateSelection
protected void updateSelection()Update selected keys.The implementation in
ChannelConnection
selects 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 inChannelConnection
checks the max idle time.- Throws:
IOException
- if an I/O error occurs (this connection will be closed)
-