The Meta-Environment API

toolbus.communication
Class SocketWriteMultiplexer

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

public class SocketWriteMultiplexer
extends java.lang.Object
implements IWriteMultiplexer, java.lang.Runnable

This class handles the multiplexing of socket channels that we are registered for write 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
SocketWriteMultiplexer(AbstractConnectionHandler connectionHandler)
          Constructor.
 
Method Summary
 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.
 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

connectionHandler

private final AbstractConnectionHandler connectionHandler

selector

private final java.nio.channels.Selector selector

running

private volatile boolean running
Constructor Detail

SocketWriteMultiplexer

public SocketWriteMultiplexer(AbstractConnectionHandler connectionHandler)
Constructor.

Parameters:
connectionHandler - The connection handler this write 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 write to.

Specified by:
run in interface java.lang.Runnable

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.

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