The Meta-Environment API

toolbus.communication
Class SocketReadWriteMultiplexer

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

public class SocketReadWriteMultiplexer
extends java.lang.Object
implements IReadMultiplexer, IWriteMultiplexer, java.lang.Runnable

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

Author:
Arnold Lankamp

Field Summary
private  AbstractConnectionHandler connectionHandler
           
private  java.lang.Object registrationLock
           
private  boolean running
           
private  toolbus.util.concurrency.Latch selectionPreventionLatch
           
private  java.nio.channels.Selector selector
           
 
Constructor Summary
SocketReadWriteMultiplexer(AbstractConnectionHandler connectionHandler)
          Constructor.
 
Method Summary
 void deregisterForRead(java.nio.channels.SelectableChannel channel)
          Deregisters the given channel for reading (if nessecary).
 void deregisterForWrite(java.nio.channels.SelectableChannel channel)
          Deregisters the given channel for writing (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 registerForWrite(java.nio.channels.SelectableChannel channel, SocketIOHandler ioHandler)
          Registers the given channel for writing.
 void run()
          The main loop of this multiplexer.
 void stopRunning()
          Stops the execution of this multiplexer after then next iteration.
private  void write(java.nio.channels.SelectionKey key)
          Notifies the with the selection key associated I/O handler that we are able to send data.
 
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

registrationLock

private final java.lang.Object registrationLock

connectionHandler

private final AbstractConnectionHandler connectionHandler

selector

private final java.nio.channels.Selector selector

running

private volatile boolean running
Constructor Detail

SocketReadWriteMultiplexer

public SocketReadWriteMultiplexer(AbstractConnectionHandler connectionHandler)
Constructor.

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

isRunning

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

Returns:
True if this multiplexer 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 or write 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.

write

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

Parameters:
key - The key associated with the channel that we can write to.

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)

registerForWrite

public void registerForWrite(java.nio.channels.SelectableChannel channel,
                             SocketIOHandler ioHandler)
Description copied from interface: IWriteMultiplexer
Registers the given channel for writing.

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

deregisterForWrite

public void deregisterForWrite(java.nio.channels.SelectableChannel channel)
Description copied from interface: IWriteMultiplexer
Deregisters the given channel for writing (if nessecary).

Specified by:
deregisterForWrite in interface IWriteMultiplexer
Parameters:
channel - The channel that needs to be deregistered for writing.
See Also:
IWriteMultiplexer.deregisterForWrite(SelectableChannel)

The Meta-Environment API