public class SimpleCommunicationController extends Object implements CommunicationController
Simple communication controller implementation.
Code using his implementation should call the processTimersAndIO()
from a loop. This method processes all timers that are due and then uses a
Selector
to wait for one of five things to happen:
wakeUp()
method is called.registerChannel(ChannelProcessor, SelectableChannel)
method is
called.registerTimer(TimerProcessor, long)
method is
called.After the selection operation has finished, all processors that have registered channels that are ready now are processed. After that, the method returns.
If one of the processIO()
or
processTimer()
methods throws an
exception, the processing is stopped immediately and the exception is passed
up to the code calling processTimersAndIO()
. This code should catch
the exception, handle it appropriately and then call
processTimersAndIO()
again.
Constructor and Description |
---|
SimpleCommunicationController()
Creates a communication controller using the platform's default
SelectorProvider . |
SimpleCommunicationController(SelectorProvider selectorProvider)
Creates a communication controller that uses the specified selector
provider for opening its
Selector . |
Modifier and Type | Method and Description |
---|---|
boolean |
inCommunicationThread()
Tells whether the current thread is the communication thread.
|
void |
processTimersAndIO()
Processes all timers that are due an then waits for an I/O event to
happen or a timer to expire.
|
SelectionKey |
registerChannel(ChannelProcessor channelProcessor,
SelectableChannel channel)
Registers a channel with the communication controller.
|
void |
registerTimer(TimerProcessor timerProcessor,
long eventTime)
Registers for a timer event.
|
void |
wakeUp()
Wakes up the currently waiting
processTimerEvents() method. |
public SimpleCommunicationController() throws IOException
SelectorProvider
.IOException
- if SelectorProvider.openSelector()
throws such an
exception.public SimpleCommunicationController(SelectorProvider selectorProvider) throws IOException
Selector
.selectorProvider
- selector provider to use for creating this controller's
selector.IOException
- if SelectorProvider.openSelector()
throws such an
exception.public void processTimersAndIO() throws IOException
ChannelProcessor.processIO()
or
TimerProcessor.processTimer()
is not caught by this method, but
passed up the stack.IOException
- if Selector.select()
throws such an exception.public void wakeUp()
Wakes up the currently waiting processTimerEvents()
method. If
this method is currently not waiting, it will return immediately instead
of waiting the next time it is called.
Registering a timer with a start time in the past should be preferred over calling this method. Due to limitations of the Java platform, this method does not establish a happens-before relationship between the code calling this method and the code that is executed in the communication thread after waking up. On many platforms, such a happens-before relationship might exist implicitly, however it is not guaranteed by the API documentation.
Code that wants to trigger execution of a callback within the
communication thread should prefer the
registerTimer(TimerProcessor, long)
method. This guarantees a
happens-before relationship and will also wake up the communication
thread if it is currently sleeping.
public SelectionKey registerChannel(ChannelProcessor channelProcessor, SelectableChannel channel) throws ClosedChannelException
CommunicationController
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.
registerChannel
in interface CommunicationController
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.public void registerTimer(TimerProcessor timerProcessor, long eventTime)
CommunicationController
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.
registerTimer
in interface CommunicationController
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.public boolean inCommunicationThread()
CommunicationController
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.inCommunicationThread
in interface CommunicationController
true
if the current thread is the communication
thread, false
otherwise.Copyright © 2014–2017 aquenos GmbH. All rights reserved.