All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class watch.WatchManager

java.lang.Object
   |
   +----watch.WatchManager

public class WatchManager
extends Object
This object manages the relationship between a Watchable object and the various Watchers that watch it. The Watchable object registers several of its input variables, and the WatchManager informs each Watcher when a variable it is interested in changes, or every nth time it changes. The WatchManager should be in the same thread as the Watchable object, but each Watcher may be in a separater thread, with an update() method.

This code is (c) 1996 Leemon Baird <leemon@cs.cmu.edu>, http://www.cs.cmu.edu/~baird
The source and object code may be redistributed freely. If the code is modified, please state so in the comments.

Version:
1.0, 3 June 96
Author:
Leemon Baird

Variable Index

 o changes
The number of times each watched variable has changed since its Watcher was last called
 o freq
The frequency of each watched variable
 o name
The name of each watched variable
 o numUpdates
number of calls to update() since last yield and sleep
 o prevVar
The previous value of each watched variable
 o profilePeriod
The profiling is done for periods of this many seconds
 o regName
The name of each registered variable
 o regPtr
The pointer to each registered variable
 o regWatchable
The Watchable object containing each registered variable
 o sleepFreq
sleep for sleepTime milliseconds and yield every sleepFreq calls to update()
 o sleepTime
sleep for sleepTime milliseconds and yield every sleepFreq calls to update()
 o startTime
The time (in milliseconds) when this class first started.
 o updatePercent
The percentage of time spent in updates so far
 o updateTime
The total number of milliseconds spent on all updates so far
 o var
The variable to be watched for each watched variable
 o watched
The Watchable object containing each watched variable
 o watcher
The Watcher for each watched variable

Constructor Index

 o WatchManager()
default constructor sleeps 20 milliseconds every 1000 calls to update().
 o WatchManager(int, int)
watchManager will yield and sleep(time) every freq calls to update().

Method Index

 o findVar(String)
A Watcher calls this to get a Pointer to a watchable variable given its name.
 o getAllVars(String)
Return all watchable variables with prefix allPrefix.
 o getVars(String)
Given the group, return a list of all its watchable variables.
 o registerParameters(Parsable, String)
register all the parameters for a Watcher object as both watchable variables and as watches.
 o registerVar(String, Pointer, Watchable)
Watchable objects call this to register a variable that's a pointer to an object.
 o registerWatch(String, PInt, Watcher)
A Watcher calls this to be notified when variable name changes.
 o unregisterVar(String)
Watchable objects call this to unregister a variable.
 o unregisterWatch(String, Watcher)
A Watcher calls this if it no longer cares about this variable.
 o unregisterWatchable(Watchable)
unregister every variable assocaited with this watchable
 o unregisterWatcher(Watcher)
unregister every watch associated with this watcher
 o update()
Watchable objects call this when ready for their variables to be seen.

Variables

 o regName
 protected Vector regName
The name of each registered variable

 o regWatchable
 protected Vector regWatchable
The Watchable object containing each registered variable

 o regPtr
 protected Vector regPtr
The pointer to each registered variable

 o name
 protected String name[]
The name of each watched variable

 o watcher
 protected Watcher watcher[]
The Watcher for each watched variable

 o watched
 protected Watchable watched[]
The Watchable object containing each watched variable

 o var
 protected Pointer var[]
The variable to be watched for each watched variable

 o prevVar
 protected Pointer prevVar[]
The previous value of each watched variable

 o freq
 protected PInt freq[]
The frequency of each watched variable

 o changes
 protected int changes[]
The number of times each watched variable has changed since its Watcher was last called

 o startTime
 protected long startTime
The time (in milliseconds) when this class first started.

 o updateTime
 protected long updateTime
The total number of milliseconds spent on all updates so far

 o updatePercent
 protected PDouble updatePercent
The percentage of time spent in updates so far

 o profilePeriod
 protected PDouble profilePeriod
The profiling is done for periods of this many seconds

 o sleepFreq
 protected PInt sleepFreq
sleep for sleepTime milliseconds and yield every sleepFreq calls to update()

 o sleepTime
 protected PInt sleepTime
sleep for sleepTime milliseconds and yield every sleepFreq calls to update()

 o numUpdates
 protected int numUpdates
number of calls to update() since last yield and sleep

Constructors

 o WatchManager
 public WatchManager()
default constructor sleeps 20 milliseconds every 1000 calls to update().

 o WatchManager
 public WatchManager(int freq,
                     int time)
watchManager will yield and sleep(time) every freq calls to update(). If freq=-1, then it never yields or sleeps. If time=-1, then it never sleeps.

Methods

 o registerParameters
 public void registerParameters(Parsable obj,
                                String prefix)
register all the parameters for a Watcher object as both watchable variables and as watches. This lets other objects see those variables, the user can change them at runtime, and if any of them change, the object's update() method is automatically called so it can respond. The object passed in must be both Parsable and a Watcher or this will do nothing.

 o getAllVars
 public synchronized String[] getAllVars(String allPrefix)
Return all watchable variables with prefix allPrefix. If variable foo is in group "/a/b/c/" then the list will include "/a/b/c/foo".

 o getVars
 public synchronized String[] getVars(String group)
Given the group, return a list of all its watchable variables. If variable foo is in group /a/b/c/, then getVars("/a/b/c/") returns a list including "/a/b/c/foo". If /a/b/c/d/ is also a group, then the list will include "/a/b/c/d/" but will not include any other string starting with "/a/b/c/d/".

 o update
 public synchronized void update()
Watchable objects call this when ready for their variables to be seen. update() should only be called when all the Watchable objects that have registered with this object are ready to be seen. update() in turn calls the update() method of the Watcher objects when the appropriate variables have changed (or changed for the nth time).

 o findVar
 public synchronized Pointer findVar(String varName)
A Watcher calls this to get a Pointer to a watchable variable given its name. The Watchable object must have already registered the variable. This returns the pointer if successful, and null if it couldn't find the requested variable.

 o registerWatch
 public synchronized Pointer registerWatch(String varName,
                                           PInt varFreq,
                                           Watcher varWatcher)
A Watcher calls this to be notified when variable name changes. It is notified by having its update() method called. If freq>1 then it is called at the beginning, and once every freq changes thereafter. If freq<1, then update() is never called, but unregister() is still called when the variable is unregistered. This returns the pointer if successful, and null if it couldn't find the requested variable.

 o unregisterWatch
 public synchronized void unregisterWatch(String nm,
                                          Watcher wtchr)
A Watcher calls this if it no longer cares about this variable. It will subsequently not be called when the variable changes or is unregistered by the Watchable object.

 o unregisterVar
 public synchronized void unregisterVar(String varName)
Watchable objects call this to unregister a variable.

 o registerVar
 public synchronized void registerVar(String name,
                                      Pointer v,
                                      Watchable obj)
Watchable objects call this to register a variable that's a pointer to an object. Note: watchers are given a pointer to the variable. Typically, watchers only watch (hence the name), but in some cases a watcher can follow the pointer and change the value of the variable. If a watchable object has a variable X that should be watchable but not changeable by a watcher, then it should declare X and PX internally (the latter of type Pointer), register PX instead of X, and then update it with PX.val=x periodically (perhaps right before each call to update()). That ensures that no watcher can change X, but they'll get to see a copy of its value. This trick can also allow the watchable object to access its own watchable variables in an inner loop slightly faster (saving one pointer dereference), since it can work with X rather than PX.val.

 o unregisterWatcher
 public synchronized void unregisterWatcher(Watcher wtchr)
unregister every watch associated with this watcher

 o unregisterWatchable
 public synchronized void unregisterWatchable(Watchable watchable)
unregister every variable assocaited with this watchable


All Packages  Class Hierarchy  This Package  Previous  Next  Index