fulmine.util.io
Class SelectorTasks

java.lang.Object
  extended by fulmine.AbstractLifeCycle
      extended by fulmine.util.io.SelectorTasks
All Implemented Interfaces:
IDestroyable, ILifeCycle

public final class SelectorTasks
extends AbstractLifeCycle
implements ILifeCycle

A collection of ISelectionKeyTask objects that handle the logic for the ready operations raised from registered NIO Channel objects. This class exists to provide a mechanism to manage the tasks that should be run for each channel that is registered against a single Selector. Each instance has an internal Selector that is used to register with the Channel supplied in the register(int, SelectableChannel, ISelectionKeyTask) method.

This object invokes the Selector.select() method and blocks until a channel is available for one of the registered operations. When this happens the appropriate task is located and executed.

  // create and register
  SelectorTasks tasks = new SelectorTasks();
  // create an optional listener to get notified when the selector is opened
  ISelectorTasksStateListener = ...
  tasks.setStateListener(listener);
  
  // OPEN THE CHANNEL - no other operation is valid until this is run
  tasks.openSelector();
  
  SelectableChannel channel = ...;
  ISelectionKeyTask task = ...;
  tasks.register(SelectionKey.OP_READ, channel, task);
  
  // process - this blocks until a ready operation occurs, 
  // in the real-world, this would be called in a separate thread
  while(tasks.process());
 
There should only be 1 thread running the process method. The getRunnable() method returns an Runnable object for a dedicated processing thread. e.g.
  // create and register
  SelectorTasks tasks = new SelectorTasks();
  ...
  // kick off a thread to run the process() method 
  new Thread(tasks.getRunnable()).start();
 
This is not thread aware.

Author:
Ramon Servadei

Constructor Summary
SelectorTasks()
          Standard constructor
 
Method Summary
protected  void doDestroy()
          Overridden in subclasses to perform custom logic on destruction.
protected  void doStart()
          Overridden in subclasses to perform custom logic on activation.
protected  AsyncLog getLog()
          Get the log to use for the object hierarchy
 Runnable getRunnable()
          Get a Runnable to continually execute the process() method.
 void openSelector()
          Open the internal Selector
 boolean process()
          Call Selector.select() and wait for a one of the operations in any of the registered channels to occur.
 void register(int op, SelectableChannel channel, ISelectionKeyTask task)
          Register the internal Selector with the channel and map the supplied task to the SelectionKey that is returned from the SelectableChannel.register(Selector, int) method.
 void setStateListener(ISelectorTasksStateListener listener)
          Set the state listener
 ISelectionKeyTask unregister(SelectionKey key)
          Unregister the selection key and remove the associated ISelectionKeyTask.
 
Methods inherited from class fulmine.AbstractLifeCycle
checkActive, destroy, finalize, isActive, start, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface fulmine.ILifeCycle
isActive, start
 
Methods inherited from interface fulmine.IDestroyable
destroy
 

Constructor Detail

SelectorTasks

public SelectorTasks()
Standard constructor

Method Detail

register

public void register(int op,
                     SelectableChannel channel,
                     ISelectionKeyTask task)
Register the internal Selector with the channel and map the supplied task to the SelectionKey that is returned from the SelectableChannel.register(Selector, int) method.

Parameters:
op - the operation to register for
channel - the channel that will register with the internal selector
task - the task to run when the operation occurs in the channel. This task should be efficient; if it does any blocking this affects processing of other registered ISelectionKeyTask objects.
See Also:
SelectionKey

unregister

public ISelectionKeyTask unregister(SelectionKey key)
Unregister the selection key and remove the associated ISelectionKeyTask. The key is also cancelled via the SelectionKey.cancel() method.

Parameters:
key - the selection key to unregister and cancel
Returns:
the previously registered task

process

public boolean process()
Call Selector.select() and wait for a one of the operations in any of the registered channels to occur. When one does, the appropriate ISelectionKeyTask is run.

Returns:
false if this becomes inactive

openSelector

public void openSelector()
Open the internal Selector


getLog

protected AsyncLog getLog()
Description copied from class: AbstractLifeCycle
Get the log to use for the object hierarchy

Overrides:
getLog in class AbstractLifeCycle
Returns:
the log to use for the object hierarchy

doStart

protected final void doStart()
Description copied from class: AbstractLifeCycle
Overridden in subclasses to perform custom logic on activation. Any exceptions should be thrown as a RuntimeException or subclass thereof. When this method is called, the AbstractLifeCycle.isActive() method will return true.

Specified by:
doStart in class AbstractLifeCycle

doDestroy

protected final void doDestroy()
Description copied from class: AbstractLifeCycle
Overridden in subclasses to perform custom logic on destruction. Any exceptions should be thrown as a RuntimeException or subclass thereof.

Specified by:
doDestroy in class AbstractLifeCycle

setStateListener

public void setStateListener(ISelectorTasksStateListener listener)
Set the state listener

Parameters:
listener -

getRunnable

public Runnable getRunnable()
Get a Runnable to continually execute the process() method.

Returns:
a runnable to execute this


Copyright © 2007-2009. All Rights Reserved.