public interface CommunicationController
Interface for a component that manages I/O activities. A communication
controller typically controls one or more ChannelProcessor
s and
TimerProcessor
s calling one of their process methods at the right
time.
A communication controller allows communication processors to register for
I/O events for channels and for timer events. The controller will call the
respective processor's processIO()
or
TimerProcessor.processTimer()
method each time such an event occurs.
This way, one thread can handle I/O and timer events for multiple components.
A communication controller must be thread safe, meaning that a communication processor must be able to call the controller's methods at any time, not just from one of the methods called by the controller.
On the other hand, a communication controller may never call more than one
processor's process
method in parallel. This is important,
because processor's might not be thread-safe and their might be hidden
dependencies between processors registered with the same communication
controller.
Modifier and Type | Method and Description |
---|---|
boolean |
inCommunicationThread()
Tells whether the current thread is the communication thread.
|
SelectionKey |
registerChannel(ChannelProcessor channelProcessor,
SelectableChannel channel)
Registers a channel with the communication controller.
|
void |
registerTimer(TimerProcessor timerProcessor,
long eventTime)
Registers for a timer event.
|
SelectionKey registerChannel(ChannelProcessor channelProcessor, SelectableChannel channel) throws ClosedChannelException
Registers a channel with the communication controller. This tells the
communication controller that the channel processor is interested in I/O
events for this channel. The channel processor can exactly define which
events it is interested in through the SelectionKey
returned by
this method.
When this communication controller detects that one of the I/O events the
channelProcessor
has registered for has occurred, it calls
the channelProcessor
's processIO()
method.
The selection key returned by this method may be a wrapper that only
provides a subset of the operations defined in the SelectionKey
interface. In particular, its selector()
methods may throw an UnsupportedOperationException
. This should
not be a problem because a processor does not have good reasons to call
this method anyway.
A typical implementation of this method will call the
channel
's
SelectableChannel.register(java.nio.channels.Selector, int, Object)
method and will throw any exception thrown by this method.
channelProcessor
- the channel processor that registers the channel. Each channel
may only be registered for a single channel processor.channel
- the channel that shall be registered. Each channel may only be
registered for a single channel processor.Selector
. This selection key might not be the original
selection key but a wrapper that does not implement the
selector()
and attach(Object)
methods.ClosedChannelException
- if the channel
channel is closed.IllegalBlockingModeException
- if the channel
is in blocking mode.IllegalSelectorException
- if the channel
was not created by the same
provider as this communication controller's selector.CancelledKeyException
- if the channel
is currently registered with this
communication controller's selector but the corresponding key
has already been cancelled.void registerTimer(TimerProcessor timerProcessor, long eventTime)
Registers for a timer event. When the time specified by
eventTime
has come or passed, this communication controller
calls the processor's processTimer()
method.
Multiple timer registrations for the same timerProcessor
may
result in a single call to processTimer()
if the
eventTime
of all of these registrations has passed.
The general contract is that a processor's processTimer()
method may be called an indefinite amount of time after, but never before
the eventTime
has passed.
timerProcessor
- the processor that is registering for a timer event.eventTime
- Java time-stamp (as returned by
System.currentTimeMillis()
) that specifies the point
in time when the timer event should occur. The processor's
process method may be called later, but never before this
time.boolean inCommunicationThread()
ChannelProcessor
s and
TimerProcessor
s. This method can be used by processors that are
not thread safe to detect whether code is run in the correct thread.true
if the current thread is the communication
thread, false
otherwise.Copyright © 2014–2016 aquenos GmbH. All rights reserved.