public class SimpleCommunicationController extends Object implements CommunicationController
Simple communication controller implementation.
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
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
|Constructor and Description|
Creates a communication controller using the platform's default
Creates a communication controller that uses the specified selector provider for opening its
|Modifier and Type||Method and Description|
Tells whether the current thread is the communication thread.
Processes all timers that are due an then waits for an I/O event to happen or a timer to expire.
Registers a channel with the communication controller.
Registers for a timer event.
Wakes up the currently waiting
public SimpleCommunicationController() throws IOException
public SimpleCommunicationController(SelectorProvider selectorProvider) throws IOException
public void processTimersAndIO() throws IOException
TimerProcessor.processTimer()is not caught by this method, but passed up the stack.
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
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
When this communication controller detects that one of the I/O events the
channelProcessor has registered for has occurred, it calls
The selection key returned by this method may be a wrapper that only
provides a subset of the operations defined in the
interface. In particular, its
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
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
ClosedChannelException- if the
channelchannel is closed.
public 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
Multiple timer registrations for the same
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
method may be called an indefinite amount of time after, but never before
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.
public boolean inCommunicationThread()
TimerProcessors. This method can be used by processors that are not thread safe to detect whether code is run in the correct thread.
Copyright © 2014–2016 aquenos GmbH. All rights reserved.