Rice Pastry API

rice.selector
Class SelectorManager

java.lang.Object
  extended by java.lang.Thread
      extended by rice.selector.SelectorManager
All Implemented Interfaces:
java.lang.Runnable, Destructable, Timer
Direct Known Subclasses:
ProfileSelector, RecordSM, ReplaySM

public class SelectorManager
extends java.lang.Thread
implements Timer, Destructable

This class is the class which handles the selector, and listens for activity. When activity occurs, it figures out who is interested in what has happened, and hands off to that object.

Version:
$Id: SelectorManager.java 4675 2009-03-11 15:54:34Z jeffh $
Author:
Alan Mislove

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  java.util.HashSet<java.nio.channels.SelectionKey> cancelledKeys
           
protected  Environment environment
           
protected  java.lang.String instance
           
protected  java.util.LinkedList<java.lang.Runnable> invocations
           
protected  Logger logger
           
protected  java.util.HashSet<java.nio.channels.SelectionKey> modifyKeys
           
protected  RandomSource random
           
protected  boolean running
           
protected  boolean select
          Can be disabled for the simulator to improve performance, only do this if you know you don't need to select on anything
protected  java.nio.channels.Selector selector
           
protected  int seqCtr
           
protected  java.lang.Object seqLock
          Internal method which adds a task to the task tree, waking up the selector if necessary to recalculate the sleep time
static int TIMEOUT
           
protected  java.util.Queue<TimerTask> timerQueue
           
protected  TimeSource timeSource
           
protected  boolean useLoopListeners
          LoopListeners is used in case you are worried that your process may not get scheduled for a while such as on an overloaded planetlab node, or if you hibernate your laptop, this is not needed for the simulator
protected  long wakeupTime
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SelectorManager(java.lang.String instance, TimeSource timeSource, LogManager log)
           
SelectorManager(java.lang.String instance, TimeSource timeSource, LogManager log, RandomSource random)
          Constructor, which is private since there is only one selector per JVM.
 
Method Summary
 void addLoopObserver(LoopObserver lo)
           
protected  void addTask(TimerTask task)
           
 void cancel(java.nio.channels.SelectionKey key)
          Method which asks the Selector Manager to add the given key to the cancelled set.
 void destroy()
           
protected  void doInvocations()
          Method which invokes all pending invocations.
protected  void doSelections()
           
protected  void executeDueTasks()
          Internal method which finds all due tasks and executes them.
protected  boolean executeTask(TimerTask next)
           
 Environment getEnvironment()
           
protected  java.lang.Runnable getInvocation()
          Method which synchroniously returns the first element off of the invocations list.
 java.nio.channels.SelectionKey getKey(java.nio.channels.SelectableChannel channel)
          Utility method which returns the SelectionKey attached to the given channel, if one exists
protected  java.nio.channels.SelectionKey getModifyKey()
          Method which synchroniously returns on element off of the modifyKeys list
 long getNextTaskExecutionTime()
           
 int getNumInvocations()
          Debug method which returns the number of pending invocations
 java.nio.channels.Selector getSelector()
           
 Timer getTimer()
          Returns the timer associated with this SelectorManager (in this case, it is this).
 void invoke(java.lang.Runnable d)
          This method schedules a runnable task to be done by the selector thread during the next select() call.
 boolean isSelectorThread()
          Returns whether or not this thread of execution is the selector thread
static void main(java.lang.String[] args)
           
 void modifyKey(java.nio.channels.SelectionKey key)
          Adds a selectionkey handler into the list of handlers which wish to change their keys.
protected  void notifyLoopListeners()
           
protected  void onLoop()
          This method is to be implemented by a subclass to do some task each loop.
 java.nio.channels.SelectionKey register(java.nio.channels.SelectableChannel channel, SelectionKeyHandler handler, int ops)
          Registers a new channel with the selector, and attaches the given SelectionKeyHandler as the handler for the newly created key.
 void removeLoopObserver(LoopObserver lo)
           
 void removeTask(TimerTask task)
           
 void run()
          This method starts the socket manager listening for events.
 TimerTask schedule(TimerTask task)
          Method which schedules a task to run after a specified number of millis.
 TimerTask schedule(TimerTask task, long delay)
           
 TimerTask schedule(TimerTask task, long delay, long period)
          Method which schedules a task to run repeatedly after a specified delay and period
 TimerTask scheduleAtFixedRate(TimerTask task, long delay, long period)
          Method which schedules a task to run repeatedly (at a fixed rate) after a specified delay and period
protected  int select(int time)
          Selects on the selector, and returns the result.
protected  java.nio.channels.SelectionKey[] selectedKeys()
          Selects all of the currently selected keys on the selector and returns the result as an array of keys.
 void setEnvironment(Environment env)
          Can only be called once.
 void setLogLevel(int level)
           
 void setSelect(boolean b)
           
 void useLoopListeners(boolean val)
          Set this to false when using the simulator, because you don't need to notify loop observers.
 void wakeup()
          Note, should hold the selector's (this) lock to call this.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TIMEOUT

public static int TIMEOUT

selector

protected java.nio.channels.Selector selector

invocations

protected java.util.LinkedList<java.lang.Runnable> invocations

modifyKeys

protected java.util.HashSet<java.nio.channels.SelectionKey> modifyKeys

cancelledKeys

protected java.util.HashSet<java.nio.channels.SelectionKey> cancelledKeys

timerQueue

protected java.util.Queue<TimerTask> timerQueue

wakeupTime

protected long wakeupTime

timeSource

protected TimeSource timeSource

logger

protected Logger logger

instance

protected java.lang.String instance

running

protected boolean running

select

protected boolean select
Can be disabled for the simulator to improve performance, only do this if you know you don't need to select on anything


random

protected RandomSource random

environment

protected Environment environment

useLoopListeners

protected boolean useLoopListeners
LoopListeners is used in case you are worried that your process may not get scheduled for a while such as on an overloaded planetlab node, or if you hibernate your laptop, this is not needed for the simulator


seqLock

protected java.lang.Object seqLock
Internal method which adds a task to the task tree, waking up the selector if necessary to recalculate the sleep time


seqCtr

protected int seqCtr
Constructor Detail

SelectorManager

public SelectorManager(java.lang.String instance,
                       TimeSource timeSource,
                       LogManager log,
                       RandomSource random)
Constructor, which is private since there is only one selector per JVM.


SelectorManager

public SelectorManager(java.lang.String instance,
                       TimeSource timeSource,
                       LogManager log)
Method Detail

setEnvironment

public void setEnvironment(Environment env)
Can only be called once. The rest of the time the argument is rejected. This is needed so that when the NodeFactory clones the Environment, it doesn't get restarted/have multiple different environments etc...

Parameters:
env -

cancel

public void cancel(java.nio.channels.SelectionKey key)
Method which asks the Selector Manager to add the given key to the cancelled set. If no one calls register on this key during the rest of this select() operation, the key will be cancelled. Otherwise, it will be returned as a result of the register operation.

Parameters:
key - The key to cancel

getKey

public java.nio.channels.SelectionKey getKey(java.nio.channels.SelectableChannel channel)
Utility method which returns the SelectionKey attached to the given channel, if one exists

Parameters:
channel - The channel to return the key for
Returns:
The key

register

public java.nio.channels.SelectionKey register(java.nio.channels.SelectableChannel channel,
                                               SelectionKeyHandler handler,
                                               int ops)
                                        throws java.io.IOException
Registers a new channel with the selector, and attaches the given SelectionKeyHandler as the handler for the newly created key. Operations which the hanlder is interested in will be called as available.

Parameters:
channel - The channel to regster with the selector
handler - The handler to use for the callbacks
ops - The initial interest operations
Returns:
The SelectionKey which uniquely identifies this channel
Throws:
java.io.IOException

invoke

public void invoke(java.lang.Runnable d)
This method schedules a runnable task to be done by the selector thread during the next select() call. All operations which modify the selector should be done using this method, as they must be done in the selector thread.

Parameters:
d - The runnable task to invoke

getNumInvocations

public int getNumInvocations()
Debug method which returns the number of pending invocations

Returns:
The number of pending invocations

modifyKey

public void modifyKey(java.nio.channels.SelectionKey key)
Adds a selectionkey handler into the list of handlers which wish to change their keys. Thus, modifyKeys() will be called on the next selection operation

Parameters:
key - The key which is to be changed

onLoop

protected void onLoop()
This method is to be implemented by a subclass to do some task each loop.


run

public void run()
This method starts the socket manager listening for events. It is designed to be started when this thread's start() method is invoked.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

destroy

public void destroy()
Specified by:
destroy in interface Destructable
Overrides:
destroy in class java.lang.Thread

useLoopListeners

public void useLoopListeners(boolean val)
Set this to false when using the simulator, because you don't need to notify loop observers.

Parameters:
val -

notifyLoopListeners

protected void notifyLoopListeners()

addLoopObserver

public void addLoopObserver(LoopObserver lo)

removeLoopObserver

public void removeLoopObserver(LoopObserver lo)

doSelections

protected void doSelections()
                     throws java.io.IOException
Throws:
java.io.IOException

doInvocations

protected void doInvocations()
Method which invokes all pending invocations. This method should *only* be called by the selector thread.


getInvocation

protected java.lang.Runnable getInvocation()
Method which synchroniously returns the first element off of the invocations list.

Returns:
An item from the invocations list

getModifyKey

protected java.nio.channels.SelectionKey getModifyKey()
Method which synchroniously returns on element off of the modifyKeys list

Returns:
An item from the invocations list

select

protected int select(int time)
              throws java.io.IOException
Selects on the selector, and returns the result. Also properly synchronizes around the selector

Returns:
DESCRIBE THE RETURN VALUE
Throws:
java.io.IOException - DESCRIBE THE EXCEPTION

selectedKeys

protected java.nio.channels.SelectionKey[] selectedKeys()
                                                 throws java.io.IOException
Selects all of the currently selected keys on the selector and returns the result as an array of keys.

Returns:
The array of keys
Throws:
java.io.IOException - DESCRIBE THE EXCEPTION

isSelectorThread

public boolean isSelectorThread()
Returns whether or not this thread of execution is the selector thread

Returns:
Whether or not this is the selector thread

schedule

public TimerTask schedule(TimerTask task)
Method which schedules a task to run after a specified number of millis. The task must have a proper nextExecutionTime set

Specified by:
schedule in interface Timer
Parameters:
task - The task to run

schedule

public TimerTask schedule(TimerTask task,
                          long delay)
Specified by:
schedule in interface Timer

schedule

public TimerTask schedule(TimerTask task,
                          long delay,
                          long period)
Method which schedules a task to run repeatedly after a specified delay and period

Specified by:
schedule in interface Timer
Parameters:
task - The task to run
delay - The delay before first running, in milliseconds
period - The period with which to run in milliseconds

scheduleAtFixedRate

public TimerTask scheduleAtFixedRate(TimerTask task,
                                     long delay,
                                     long period)
Method which schedules a task to run repeatedly (at a fixed rate) after a specified delay and period

Specified by:
scheduleAtFixedRate in interface Timer
Parameters:
task - The task to run
delay - The delay before first running in milliseconds
period - The period with which to run in milliseconds

addTask

protected void addTask(TimerTask task)

removeTask

public void removeTask(TimerTask task)

wakeup

public void wakeup()
Note, should hold the selector's (this) lock to call this.


getNextTaskExecutionTime

public long getNextTaskExecutionTime()

executeDueTasks

protected void executeDueTasks()
Internal method which finds all due tasks and executes them.


executeTask

protected boolean executeTask(TimerTask next)

getTimer

public Timer getTimer()
Returns the timer associated with this SelectorManager (in this case, it is this).

Returns:
The associated timer

getSelector

public java.nio.channels.Selector getSelector()

setSelect

public void setSelect(boolean b)

getEnvironment

public Environment getEnvironment()

setLogLevel

public void setLogLevel(int level)

main

public static void main(java.lang.String[] args)

Rice Pastry API

Copyright © 2001-2005 - Rice Pastry.


Imprint-Dataprotection