4.8. Reactor Class

Reactor is a synchronous I/O demultiplexor.


#include <assa/Reactor.h>

class Reactor 
    Reactor ();

    bool    registerIOHandler (EventHandler* eh_, int fd_, EventType et_);
    TimerId registerTimerHandler (EventHandler* eh_, TimeVal&amp; tv_);
    bool removeHandler (EventHandler* eh_, EventType et_);
    bool removeTimerHandler (TimerId id_);
    bool removeIOHandler (int fd_);

    void waitForEvents (void);
    void waitForEvents (TimeVal* tv_);

    void stopReactor (void);

4.8.2. USAGE

If Reactor is your main event processor, then calling

Reactor::waitForEvents (void);

is used to process events indefinitely.

Reactor monitors I/O events on file descriptors with select(). It also handles timers and UNIX signals. When an application requests Reactor to attach an EventHandler to a file descriptor or a timer, Reactor automatically notifies the EventHandler of any I/O activities and timer expirations.

Most of the time, however, event processing is shared with some other library, such as X event loop routine or CORBA even loop, or Gtk+ event loop. In this case, waitForEvents() version with a timeout is used instead.

Reactor::waitForEvents (TimeVal* tv_);

This version of waitForEvents() blocks up to tv_ seconds time interval. This time interval is the longest time waitForEvents() would block if there was no events to deliver. If interrupted by an event, waitForEvents() would return with tv_ reduced by the time interval it was blocked.

To insure that we spend entire tv_ time interval processing events, use the following idiom:

TimeVal tv (5.0);   // Wait for 5 secs.

while (tv != TimeVal::zeroTime ()) 
    Reactor::getInstance ()->waitForEvents (&tv);

This loop ensures that the entire time interval is used up for event processing by Reactor.