State manager utilities

Name

nscldaq.statemanager.Utilities.getPort, nscldaq.statemanager.Utilities.connectRequestPort, nscldaq.statemanager.Utilities.subscribe, nscldaq.statemanager.Utilities.checkRequest, nscldaq.statemanager.Utilities.ZmqFileEventLoop -- State Manager utilities

Synopsis

from nscldaq.statemanager import Utilities

ipPort = Utilities.getPort(host, service, user)

zmqSocket = Utilities.connectRequestPort( zmqContext, host='localhost', service='StateRequest', user=None)

zmqSocket = Utilities.subscribe( zmqContext, wantTransitions, wantState, host='localhost', service='StatePublish', user=None)

gotSomething = Utilities.checkRequest(zmqSocket, pollTimeout, maxPolls, callback, cbArg)

eventLoop = Utilities.zmqEventLoop()

eventLoop.register(ioItem, events, handler)

eventLoop.unregister(ioItem)

eventLoop.poll(timeout)

eventLoop.pollForever(timeout, idler=None)

DESCRIPTION

The Utilities package provides several convenience functions and an event loop class for the state manager. These can be considered mostly low level functions, and the event loop too is a pretty low level item.

The state manager and its clients use the zeromq communications software. This means that some parameters are actually zeromq objects. Where appropriate, this is pointed out.

FUNCTIONS

ipPort = Utilities.getPort( host service user );

Looks up a service using the NSCL DAQ port manager. host is the host on which the service is believed to be advertised. service is the name of the service the server is advertising. user is the username qualifying the service.

In order to support multiple acquisition runs in a single computer, services can be run by a specific user. The user that runs the service is part of the information provided about services by the port manager query subsystem.

The function returns the port on which the server is listening for connections for that service. If there is no match or if the port manager on host cannot be contacted, the function will raise a RuntimeError

Utilities.connectRequestPort( zmqContext host='localhost' service='StateRequest' user=None );

Connects to the request port of the state manager. zmqContext is the zeromq context object that must have been created by the caller as part of the initialization of zeromq. host, which defaults to the localhost indicates which system the state manager is running in. service which defaults to StateRequest, the service normally used by the state manager, is the service name on which the state manager is allowing state transition request connections. user, which defaults to None, indicating the running user should be used, is the username that is running the state manager.

On successful completion a zmq::socket object is returned. Note that zmq::socket is not interchangeable with the sockets produced by the python socket constructor.

zmqEventLoop

zmqEventLoop (ZeroMQ Event loop) provides the ability to run an event loop based on a zmq.poller object. The event loop is built to allow clients to register interest in either zmq.socket events or events on any Python object that has a fileno method. When events of interest occur, callbacks associated with those events are invoked. The event loop can be entered for some designated time period or it can be entered "forever" with a user callback determining if/when the loop exits.

zmqEventLoop()

Constructs and returns an instance of a zmqEventLoop. No parameters are required or accepted.

register( ioItem, events, handler)

Registers a callback. ioItem is either a zmq.socket or any Python object with a fileno method that returns a file descriptor. events is a set of flags bitwised OR'd together to specify the events of interest. The legal flags are zmq.POLLIN or zmq.POLLOUT. handler is a callable that will be invoked when the ioItem has an event of interest.

handler is invoked with the following parameters in order: The event loop object that invoked the handler, the ioItem that has the desired event and a mask of events that were fired by the object.

Note that if an I/O item already has a callback register it is silently replaced by the new one.

unregister(ioItem)

Unregisters any callback registered for the ioItem. If the ioItem is not registered, the underlying zmq.Poller object will raise a KeyError exception.

poll(timeout)

Runs the event loop until the next events are declared or for timeout microseconds, whichever is first. Any event that occured will be dispatched prior to return. This method provides for a main program loop that follows the model:


e = Utilities.zmqEventLoop()
...
while True:
   e.poll(maxLatencyInMicroseconds)
   doOtherStuff()
                    

pollForever(timeout, idler=None)

Repeatedly invokes the poll method. In this case, the timeout specifies the maximum time the poll call can block in milliseconds (not microseconds).

After each call to poll the idler callable, if supplied is called with the event loop object as a parameter. The idler method is assumed to return a boolean True if the event loop should make another pass or False fi pollForever should return. The poll method will always be called at least once.