org.jcomponent.netserve.selector.impl
Class DefaultSelectorManager

java.lang.Object
  extended byorg.jcomponent.netserve.selector.impl.DefaultSelectorManager
All Implemented Interfaces:
Runnable, SelectorManager

public class DefaultSelectorManager
extends Object
implements SelectorManager, Runnable

The SelectorManager makes it easy to start a selector in a thread and receive events on selection.


Constructor Summary
DefaultSelectorManager()
           
 
Method Summary
protected  SelectorMonitor getMonitor()
          Return the monitor associated with manager.
protected  java.nio.channels.Selector getSelector()
          Return the selector associated with reactor.
protected  Object getSelectorLock()
          Return the lock used to synchronize access to selector.
protected  String getThreadName()
          Return the name of thread that Selector will run in.
 boolean isRunning()
          Return true if the selector is manager is running.
 java.nio.channels.SelectionKey registerChannel(java.nio.channels.SelectableChannel channel, int ops, SelectorEventHandler handler, Object userData)
          Register a channel with selector.
 void run()
          This is the main connection accepting loop.
 void setInactive()
          Set a flag to indicate reactor is inactive.
 void setMonitor(SelectorMonitor monitor)
          Set the SelectorMonitor that receives events when changes occur.
protected  void setRunning(boolean running)
          Set the flag to specify whether th Reactor is running.
protected  void setSelector(java.nio.channels.Selector selector)
          Set the selector associated with reactor.
 void setTimeout(int timeout)
          Set the timeout on the selector.
 void shutdown()
          Method to shutdown the SelectorManager.
protected  void shutdownSelector()
          Shutdown the selector and any associated acceptors.
protected  void startThread()
          Start the thread to accept connections.
 void startup()
          Initialize the selector to monitor accept attempts.
protected  void waitForThreadToComplete()
          Wait for selector thread to complete.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultSelectorManager

public DefaultSelectorManager()
Method Detail

setMonitor

public void setMonitor(SelectorMonitor monitor)
Set the SelectorMonitor that receives events when changes occur.

Parameters:
monitor - the SelectorMonitor.

setTimeout

public void setTimeout(int timeout)
Set the timeout on the selector.

Parameters:
timeout - the timeout.

startup

public void startup()
             throws IOException
Initialize the selector to monitor accept attempts.

Throws:
IOException - if unable to initialize selector

shutdown

public void shutdown()
Method to shutdown the SelectorManager.


startThread

protected void startThread()
Start the thread to accept connections.


shutdownSelector

protected void shutdownSelector()
Shutdown the selector and any associated acceptors.


waitForThreadToComplete

protected void waitForThreadToComplete()
Wait for selector thread to complete.


setInactive

public void setInactive()
Set a flag to indicate reactor is inactive. Will eventually cause the selector thread to close. Note that shutdownSelector() should be called after this method to make sure all resources are deallocated.


isRunning

public boolean isRunning()
Return true if the selector is manager is running.

Returns:
true if the selector is manager is running.

registerChannel

public java.nio.channels.SelectionKey registerChannel(java.nio.channels.SelectableChannel channel,
                                                      int ops,
                                                      SelectorEventHandler handler,
                                                      Object userData)
                                               throws IOException
Description copied from interface: SelectorManager
Register a channel with selector. Note the user MUST NOT modify the returned SelectionKeys attachment.

Specified by:
registerChannel in interface SelectorManager
Parameters:
channel - the channel
ops - the operations to register
handler - the handler that will be notified on event
userData - the data passed back into the handler
Returns:
the SelectionKey
Throws:
IOException - if channel can not be registered
See Also:
SelectorManager.registerChannel(java.nio.channels.SelectableChannel, int, org.jcomponent.netserve.selector.SelectorEventHandler, java.lang.Object)

run

public void run()
This is the main connection accepting loop.

Specified by:
run in interface Runnable

getSelectorLock

protected Object getSelectorLock()
Return the lock used to synchronize access to selector.

Returns:
the lock used to synchronize access to selector.

setSelector

protected void setSelector(java.nio.channels.Selector selector)
Set the selector associated with reactor.

Parameters:
selector - the selector associated with reactor.

getSelector

protected java.nio.channels.Selector getSelector()
Return the selector associated with reactor.

Returns:
the selector associated with reactor.

getMonitor

protected SelectorMonitor getMonitor()
Return the monitor associated with manager.

Returns:
the monitor associated with manager.

setRunning

protected void setRunning(boolean running)
Set the flag to specify whether th Reactor is running.

Parameters:
running - the flag to specify whether th Reactor is running

getThreadName

protected String getThreadName()
Return the name of thread that Selector will run in.

Returns:
the name of thread that Selector will run in.


Copyright © 1999-2003 The Spice Group. All Rights Reserved.