de.unkrig.commons.io
Class Multiplexer

java.lang.Object
  extended by de.unkrig.commons.io.Multiplexer
All Implemented Interfaces:
RunnableWhichThrows<java.io.IOException>, Stoppable

public class Multiplexer
extends java.lang.Object
implements RunnableWhichThrows<java.io.IOException>, Stoppable

A thin wrapper for the JDK Selector that also manages timers and multiple threads.


Nested Class Summary
static interface Multiplexer.TimerKey
          An identifier for a created timer.
 
Constructor Summary
Multiplexer()
           
 
Method Summary
 java.nio.channels.SelectionKey register(java.nio.channels.SelectableChannel sc, int ops, RunnableWhichThrows<java.io.IOException> runnable)
          Executes the given runnable exactly once iff the channel becomes acceptable, connected, readable and/or writable.
 void run()
          Handles channels and timers; returns never.
 void stop()
          Causes run() to complete normally soon; if run() is not currently being executed, then calling stop() has no effect.
 Multiplexer.TimerKey timer(long expiry, java.lang.Runnable runnable)
          Registers the given runnable for exactly one execution by this Multiplexer's run() method when the current time is equal to (or slightly greater than) expiry.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Multiplexer

public Multiplexer()
            throws java.io.IOException
Throws:
java.io.IOException
Method Detail

run

public void run()
         throws java.io.IOException
Handles channels and timers; returns never. Must be called by exactly one thread. Will complete normally shortly after stop() was called.

Specified by:
run in interface RunnableWhichThrows<java.io.IOException>
Throws:
java.io.IOException

register

public java.nio.channels.SelectionKey register(java.nio.channels.SelectableChannel sc,
                                               int ops,
                                               RunnableWhichThrows<java.io.IOException> runnable)
                                        throws java.nio.channels.ClosedChannelException
Executes the given runnable exactly once iff the channel becomes acceptable, connected, readable and/or writable.

To cancel the reigstration, invoke SelectionKey.cancel() on the returned object.

Throws:
java.nio.channels.ClosedChannelException

timer

public Multiplexer.TimerKey timer(long expiry,
                                  java.lang.Runnable runnable)
Registers the given runnable for exactly one execution by this Multiplexer's run() method when the current time is equal to (or slightly greater than) expiry.

Registering the same runnable more than once (and even with equal expiry) will lead to the runnable being run that many times.

Registering a runnable for an expiry that lies in the past will lead to the runnable being executed by this Multiplexer's run() method very soon.

Invoking the Multiplexer.TimerKey.cancel() method on the returned object prevents the runnable from being executed (iff its execution has not yet begun).


stop

public void stop()
Causes run() to complete normally soon; if run() is not currently being executed, then calling stop() has no effect.

Specified by:
stop in interface Stoppable