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
-
changes
- The number of times each watched variable has changed since its Watcher was last called
-
freq
- The frequency of each watched variable
-
name
- The name of each watched variable
-
numUpdates
- number of calls to update() since last yield and sleep
-
prevVar
- The previous value of each watched variable
-
profilePeriod
- The profiling is done for periods of this many seconds
-
regName
- The name of each registered variable
-
regPtr
- The pointer to each registered variable
-
regWatchable
- The Watchable object containing each registered variable
-
sleepFreq
- sleep for sleepTime milliseconds and yield every sleepFreq calls to update()
-
sleepTime
- sleep for sleepTime milliseconds and yield every sleepFreq calls to update()
-
startTime
- The time (in milliseconds) when this class first started.
-
updatePercent
- The percentage of time spent in updates so far
-
updateTime
- The total number of milliseconds spent on all updates so far
-
var
- The variable to be watched for each watched variable
-
watched
- The Watchable object containing each watched variable
-
watcher
- The Watcher for each watched variable
-
WatchManager()
- default constructor sleeps 20 milliseconds every 1000 calls to update().
-
WatchManager(int, int)
- watchManager will yield and sleep(time) every freq calls to update().
-
findVar(String)
- A Watcher calls this to get a Pointer to a watchable variable
given its name.
-
getAllVars(String)
- Return all watchable variables with prefix allPrefix.
-
getVars(String)
- Given the group, return a list of all its watchable variables.
-
registerParameters(Parsable, String)
- register all the parameters for a Watcher object as both
watchable variables and as watches.
-
registerVar(String, Pointer, Watchable)
- Watchable objects call this to register a variable that's a pointer to an object.
-
registerWatch(String, PInt, Watcher)
- A Watcher calls this to be notified when variable name changes.
-
unregisterVar(String)
- Watchable objects call this to unregister a variable.
-
unregisterWatch(String, Watcher)
- A Watcher calls this if it no longer cares about this variable.
-
unregisterWatchable(Watchable)
- unregister every variable assocaited with this watchable
-
unregisterWatcher(Watcher)
- unregister every watch associated with this watcher
-
update()
- Watchable objects call this when ready for their variables to be seen.
regName
protected Vector regName
- The name of each registered variable
regWatchable
protected Vector regWatchable
- The Watchable object containing each registered variable
regPtr
protected Vector regPtr
- The pointer to each registered variable
name
protected String name[]
- The name of each watched variable
watcher
protected Watcher watcher[]
- The Watcher for each watched variable
watched
protected Watchable watched[]
- The Watchable object containing each watched variable
var
protected Pointer var[]
- The variable to be watched for each watched variable
prevVar
protected Pointer prevVar[]
- The previous value of each watched variable
freq
protected PInt freq[]
- The frequency of each watched variable
changes
protected int changes[]
- The number of times each watched variable has changed since its Watcher was last called
startTime
protected long startTime
- The time (in milliseconds) when this class first started.
updateTime
protected long updateTime
- The total number of milliseconds spent on all updates so far
updatePercent
protected PDouble updatePercent
- The percentage of time spent in updates so far
profilePeriod
protected PDouble profilePeriod
- The profiling is done for periods of this many seconds
sleepFreq
protected PInt sleepFreq
- sleep for sleepTime milliseconds and yield every sleepFreq calls to update()
sleepTime
protected PInt sleepTime
- sleep for sleepTime milliseconds and yield every sleepFreq calls to update()
numUpdates
protected int numUpdates
- number of calls to update() since last yield and sleep
WatchManager
public WatchManager()
- default constructor sleeps 20 milliseconds every 1000 calls to update().
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.
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.
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".
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/".
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).
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.
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.
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.
unregisterVar
public synchronized void unregisterVar(String varName)
- Watchable objects call this to unregister a variable.
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.
unregisterWatcher
public synchronized void unregisterWatcher(Watcher wtchr)
- unregister every watch associated with this watcher
unregisterWatchable
public synchronized void unregisterWatchable(Watchable watchable)
- unregister every variable assocaited with this watchable
All Packages Class Hierarchy This Package Previous Next Index