CZMQEventLoop

Name

CZMQEventLoop -- Event loop for fd's and zmq sockets

Synopsis


#include <CStateMonitor.h>
            CZMQEventLoop
           
  CZMQEventLoop()
          throws ;
           
   void Register(zmq::socket_t&  sock, int mask, Callback cb, void*  param = 0)
          throws ;
               void Register(int fd, int  mask)
          throws ;
               void unregister(zmq::socket_t& sock)
          throws ;
               void unregister(int fd)
          throws ;
               void poll(int timeout)
          throws ;
               void pollForever(int timeout, IdleCallback callback=0)
          throws ;
  
        

DESCRIPTION

This class provides an event loop that is capable of reacting to events on both ZeroMQ sockets and file descriptors. An event, in this context, is an object becoming readable or writable. Events get callbacks associated with them, and those callbacks are invoked from the event loop whenever the associated object has an event of interest.

The event loop can be interleaved with other work either by using the poll method, which waits for events until either one occurs or a timeout occurs, or by providing a callback to the pollForever method. Event interest is declared using the Register method and interest is removed via the unregister methods.

METHODS

void Register(zmq::socket_t& sock, int mask, Callback cb, void* param = 0) throws ;

Registers interest in one or more events on a ZeroMQ socket; sock. mask defines which events are of interest and is a bitwise or of the masks: ZMQ_POLLIN and ZMQ_POLLOUT.

When one of the events of interest occurs while the event loop is running, the callback cb is invoked. See TYPES below for more information about the Callback function...

The cb parameter, among others is passed to the cb function without any interpretation.

Note that a socket can only have a single callback registered. If you register a callback on a socket that already has one defined, the new definition replaces the previous definition.

void Register(int fd, int mask) throws ;

This method is identical to the previous Register method, however the socket is replaced by a fd which is a file descriptor for which we'd like to receive events.

void unregister(zmq::socket_t& sock) throws ;

Unregisters interest in events from the sock ZeroMQ Socket. Once this is called, any callback establisedh on events from this socket are no longer delivered.

void unregister(int fd) throws ;

Unregisters events on the file descriptor fd.

void poll(int timeout) throws ;

Blocks until either an event occurs or the timeout number of microseconds have passed. If an event occurs for which a callback has been established, that callback will be invoked prior to returning.

void pollForever(int timeout, IdleCallback callback=0) throws ;

Invokes poll witht he timeout parameter supplied. On return, the callback is invoked, if non null. If the callback returns false, pollForever returns, otherwise it loops to the poll call.

If callback is null, the poll loop does not exit until the program exits. It is this behavior that inpsires the methodname. For more information about the IdlCallback type, see TYPES below.

TYPES

Callback

This type is a pointer to a function that is suitable for use as the cb parameter to the Register functions. It's form is as follows:

typedef void (*Callback)( CZMQEventLoop* pEventLoop , zmq::pollitem_t* object , void* param );

The pEventLoop parameter is a pointer to the event loop that is invoking this callback. The object describes the object that cause the callback to be invoked. See the ZeroMQ documentation for more information about its structure. param is the additional parameter passed in during the Register call that established this callback.

IdleCallback

This is a pointer to a function that is appropriate to use as a the idler callback parameter in a call to the pollForevr method.

The form of this function is:

typedef bool (*IdleCallback)( CZMQEventLoop* pEventLoop );

Where pEventLoop is a pointer to the event loop object that is running.