The Meta-Environment API

toolbus.communication
Class SocketReadMultiplexer

java.lang.Object
  extended by toolbus.communication.SocketReadMultiplexer
All Implemented Interfaces:
java.lang.Runnable, IReadMultiplexer

public class SocketReadMultiplexer
extends java.lang.Object
implements IReadMultiplexer, java.lang.Runnable

This class handles the multiplexing of socket channels that we are registered for read operations.

Author:
Arnold Lankamp

Field Summary
private  AbstractConnectionHandler connectionHandler
           
private  boolean running
           
private  toolbus.util.concurrency.Latch selectionPreventionLatch
           
private  java.nio.channels.Selector selector
           
 
Constructor Summary
SocketReadMultiplexer(AbstractConnectionHandler connectionHandler)
          Constructor.
 
Method Summary
 void deregisterForRead(java.nio.channels.SelectableChannel channel)
          Deregisters the given channel for reading (if nessecary).
 boolean isRunning()
          Checks if this multiplexer is running or not.
private  void read(java.nio.channels.SelectionKey key)
          Notifies the with the selection key associated I/O handler that we are able to receive data.
 void registerForRead(java.nio.channels.SelectableChannel channel, SocketIOHandler ioHandler)
          Registers the given channel for reading.
 void run()
          The main loop of this multiplexer.
 void stopRunning()
          Stops the execution of this multiplexer after then next iteration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

selectionPreventionLatch

private final toolbus.util.concurrency.Latch selectionPreventionLatch

connectionHandler

private final AbstractConnectionHandler connectionHandler

selector

private final java.nio.channels.Selector selector

running

private volatile boolean running
Constructor Detail

SocketReadMultiplexer

public SocketReadMultiplexer(AbstractConnectionHandler connectionHandler)
Constructor.

Parameters:
connectionHandler - The connection handler this read multiplexer was created by.
Method Detail

isRunning

public boolean isRunning()
Checks if this multiplexer is running or not.

Returns:
True if this handler is running, false otherwise.

stopRunning

public void stopRunning()
Stops the execution of this multiplexer after then next iteration. The running state will be set to false and the selector will be forced to return directly.


run

public void run()
The main loop of this multiplexer. It blocks until there is a registered channel which we are able to read from.

Specified by:
run in interface java.lang.Runnable

read

private void read(java.nio.channels.SelectionKey key)
Notifies the with the selection key associated I/O handler that we are able to receive data.

Parameters:
key - The key associated with the channel that we can read from.

registerForRead

public void registerForRead(java.nio.channels.SelectableChannel channel,
                            SocketIOHandler ioHandler)
Description copied from interface: IReadMultiplexer
Registers the given channel for reading.

Specified by:
registerForRead in interface IReadMultiplexer
Parameters:
channel - The channel that needs to be registered for reading.
ioHandler - The I/O handler that is associated with the given channel.
See Also:
IReadMultiplexer.registerForRead(SelectableChannel, SocketIOHandler)

deregisterForRead

public void deregisterForRead(java.nio.channels.SelectableChannel channel)
Description copied from interface: IReadMultiplexer
Deregisters the given channel for reading (if nessecary).

Specified by:
deregisterForRead in interface IReadMultiplexer
Parameters:
channel - The channel that needs to be deregistered for reading.
See Also:
IReadMultiplexer.deregisterForRead(SelectableChannel)

The Meta-Environment API