from nscldaq.statemanager import StateMonitor
smb = StateMonitor.StateMonitorBase(transitionRequestUri, statePublisherUri, initializer=None)
smb.requestTransition(requestString)
smb.initialState(state)
smb.transition(state)
smb.run()
sm = StateMonitor(transitionRequestUri, statePublisherUri, initializer=None)
sm.register(state, callable, argument)
sm.unregister(state)
sm.initialState(state)
sm.transition(state)
sm.run()
The StateMonitor package provides a pair of classes that interact with the state manager in a way that makes state-aware progams easy to write. The classes are ZeroMQ aware and provide sufficient hooks for you to interact with facilities other than the state manager.
The StateMonitorBase class provides
a base class on which all state monitoring programs can be
implemented. It provides combines with a internal
zmqEventLoop object and
zmq.Context object to intercept data on
zmq.socket objects connected tothe state
manager. A callback is invoked for transition broadcasts as
well as when the state of the system is initially learned.
StateMonitor derives from
StateMonitorBase allowing clients to
register callbacks that are invoked when the system enters
specific states. When using these classes, if the flow of
control the provide seems confining, remember that you can
derive from these classes overiding methods such as
run to get different event loop
behavior and the low level handlers for state and transition
handlers if needed.
Provides a zmqEventLoop and connections to the
state manager in a way that specific operations can be performed
when state transitions occur and when the state of the system
is initially discovered.
Recall that the state manager is not only publishing state transitions. It also periodically publishes the current state. The state is discovered when the current state publication is seen or if a state transition is seen prior to the receipt of a state publication.
The StateMonitorBase class defines the following
attributes that are considered public:
zmqContext
During initialization the object creates a
zmq.Context object which it uses
to generate the zmq.Socket objects
that talk to the state manager.
The StateMonitorBase class has the following methods:
StateMonitor.StateMonitorBase
(transitionRequestUri, statePublisherUri,
initializer=None)
Constructs a StateMonitorBase object.
The transitionRequestUri is
a URI that describes the endpoint on which the state manager
listens for state transition requests. This is of the form
tcp://hostname:portnum the port manager
can be used to determine the port number if you know the
host.
The statePublisherUri parameter
similarly describes the endpoint on which the state manager
publishes both state information and state transitions.
Finally the initializer parameter
provides a callable that is invoked after initialization
of the object is complete. The StateMonitorBase
object is passed as a parameter.
requestTransition(requestString)
Asks the state manager to perform the transition indicated
by requestString. The return value
from this method is the response string from the state
manager. If the state transition could be taken, the
string is simply OK. If the state transtion
is could not be taken, the string is FAIL
followed by a human readable error message.
initialState(state)This method is intended to be overriden in real applications that make use of this class as a base class. It is called when the state of the state manager is initially learned. This happens either when a STATE message is received from the state manager or when a TRANSITION message is received prior to the first STATE message. In either case, there is no known prior state.
The state parameter is the current system
state. The base class method should be called by any override. It
Saves the prior state.
Unsubscribes from STATE messages since the only way the state can now change is due to a TRANSITION message.
transition(state)
Called when a state transition occurs. state
is the new state. This method is intended to be overridden by derived
classes. The base class method:
If the prior state is not known invokes
initialState
If the prior state is known, saves the current state as the prior state for the next transition.
run()
Invokes the poller's
pollForever method.
This class is derived from StateManagerBase.
It adds to that class the ability to register callbacks
when specific states are entered. The StateMonitor
class has the following addtional methods:
register(
state, callable,
argument)
Registers interest in entry into state.
When state is either
discovered to be the initial state,
or when there is a state transition into state
When either of those conditions is met, the callable
is invoked. It gets passed in order, the StateMonitor object,
the prior state (None if this is an initial state discovery),
the state being entered and the argument passed in at
registration time.
unregister(state)
Unregisters any callback associated with state.
A KeyError is raised if you try to unregister
a state that has no callback registered.