xorp

EventLoop Class Reference

Event Loop. More...

#include <eventloop.hh>

Inheritance diagram for EventLoop:
NONCOPYABLE

List of all members.

Public Member Functions

 EventLoop ()
 Constructor.
virtual ~EventLoop ()
 Destructor.
void run ()
 Invoke all pending callbacks relating to XorpTimer and file descriptor activity.
void set_debug (bool v)
bool is_debug () const
TimerListtimer_list ()
SelectorListselector_list ()
XorpTimer new_oneoff_at (const TimeVal &when, const OneoffTimerCallback &ocb, int priority=XorpTask::PRIORITY_DEFAULT)
 Add a new one-off timer to the EventLoop.
XorpTimer new_oneoff_after (const TimeVal &wait, const OneoffTimerCallback &ocb, int priority=XorpTask::PRIORITY_DEFAULT)
 Add a new one-off timer to the EventLoop.
XorpTimer new_oneoff_after_ms (int ms, const OneoffTimerCallback &ocb, int priority=XorpTask::PRIORITY_DEFAULT)
 Add a new one-off timer to the EventLoop.
void remove_timer (XorpTimer &t)
 Remove timer from timer list.
XorpTimer new_periodic (const TimeVal &wait, const PeriodicTimerCallback &pcb, int priority=XorpTask::PRIORITY_DEFAULT)
 Add periodic timer to the EventLoop.
XorpTimer new_periodic_ms (int ms, const PeriodicTimerCallback &pcb, int priority=XorpTask::PRIORITY_DEFAULT)
 Add periodic timer to the EventLoop.
XorpTimer set_flag_at (const TimeVal &when, bool *flag_ptr, bool to_value=true)
 Add a flag setting timer to the EventLoop.
XorpTimer set_flag_after (const TimeVal &wait, bool *flag_ptr, bool to_value=true)
 Add a flag setting timer to the EventLoop.
XorpTimer set_flag_after_ms (int ms, bool *flag_ptr, bool to_value=true)
 Add a flag setting timer to the EventLoop.
XorpTimer new_timer (const BasicTimerCallback &cb)
 Create a custom timer associated with the EventLoop.
XorpTask new_oneoff_task (const OneoffTaskCallback &cb, int priority=XorpTask::PRIORITY_DEFAULT, int weight=XorpTask::WEIGHT_DEFAULT)
 Create a new one-time task to be scheduled with the timers and file handlers.
XorpTask new_task (const RepeatedTaskCallback &cb, int priority=XorpTask::PRIORITY_DEFAULT, int weight=XorpTask::WEIGHT_DEFAULT)
 Create a new repeated task to be scheduled with the timers and file handlers.
bool add_ioevent_cb (XorpFd fd, IoEventType type, const IoEventCb &cb, int priority=XorpTask::PRIORITY_DEFAULT)
 Add a file descriptor and callback to be invoked when descriptor is ready for input or output.
bool remove_ioevent_cb (XorpFd fd, IoEventType type=IOT_ANY)
 Remove callbacks associated with file descriptor.
bool timers_pending () const
bool events_pending () const
size_t timer_list_length () const
void current_time (TimeVal &now) const
 Get current time according to EventLoop's TimerList.
size_t descriptor_count () const
 Get the count of the descriptors that have been added.
void set_aggressiveness (int num)

Private Member Functions

void do_work ()

Private Attributes

ClockBase_clock
TimerList _timer_list
TaskList _task_list
int _aggressiveness
time_t _last_ev_run
time_t _last_warned
bool _is_debug
bool _last_ev_type [XorpTask::PRIORITY_INFINITY]
SelectorList _selector_list

Detailed Description

Event Loop.

Co-ordinates interactions between a TimerList and a SelectorList for Xorp processes. All XorpTimer and select operations should be co-ordinated through this interface.


Member Function Documentation

bool EventLoop::add_ioevent_cb ( XorpFd  fd,
IoEventType  type,
const IoEventCb &  cb,
int  priority = XorpTask::PRIORITY_DEFAULT 
)

Add a file descriptor and callback to be invoked when descriptor is ready for input or output.

An IoEventType determines what type of I/O event will cause the callback to be invoked.

Only one callback may be associated with each event type, e.g. one callback for read pending, one callback for write pending.

If multiple event types in are associated with the same callback, the callback is only invoked once, but the mask argument passed to the callback shows multiple event types.

Parameters:
fdthe file descriptor.
typethe IoEventType of the event.
cbobject to be invoked when file descriptor has I/O pending.
Returns:
true on success, false if any error occurred.
size_t EventLoop::descriptor_count ( ) const

Get the count of the descriptors that have been added.

Returns:
the count of the descriptors that have been added.
bool EventLoop::events_pending ( ) const [inline]
Returns:
true if any XorpTimers are present on EventLoop's TimerList or any XorpTasks are present on the TaskList.
XorpTimer EventLoop::new_oneoff_after ( const TimeVal wait,
const OneoffTimerCallback &  ocb,
int  priority = XorpTask::PRIORITY_DEFAULT 
) [inline]

Add a new one-off timer to the EventLoop.

Parameters:
waitthe relative time when the timer expires.
ocbcallback object that is invoked when timer expires.
Returns:
a XorpTimer object that must be assigned to remain scheduled.
XorpTimer EventLoop::new_oneoff_after_ms ( int  ms,
const OneoffTimerCallback &  ocb,
int  priority = XorpTask::PRIORITY_DEFAULT 
) [inline]

Add a new one-off timer to the EventLoop.

Parameters:
msthe relative time in milliseconds when the timer expires.
ocbcallback object that is invoked when timer expires.
Returns:
a XorpTimer object that must be assigned to remain scheduled.
XorpTimer EventLoop::new_oneoff_at ( const TimeVal when,
const OneoffTimerCallback &  ocb,
int  priority = XorpTask::PRIORITY_DEFAULT 
) [inline]

Add a new one-off timer to the EventLoop.

Parameters:
whenthe absolute time when the timer expires.
ocbcallback object that is invoked when timer expires.
Returns:
a XorpTimer object that must be assigned to remain scheduled.
XorpTask EventLoop::new_oneoff_task ( const OneoffTaskCallback &  cb,
int  priority = XorpTask::PRIORITY_DEFAULT,
int  weight = XorpTask::WEIGHT_DEFAULT 
)

Create a new one-time task to be scheduled with the timers and file handlers.

Parameters:
cbcallback object that is invoked when task is run.
prioritythe scheduling priority for the task.
scheduler_classthe scheduling class within the priority level.
Returns:
a XorpTask object that must be assigned to remain scheduled.
XorpTimer EventLoop::new_periodic ( const TimeVal wait,
const PeriodicTimerCallback &  pcb,
int  priority = XorpTask::PRIORITY_DEFAULT 
) [inline]

Add periodic timer to the EventLoop.

Parameters:
waitthe period when the timer expires.
pcbuser callback object that is invoked when timer expires. If the callback returns false the periodic XorpTimer is unscheduled.
Returns:
a XorpTimer object that must be assigned to remain scheduled.
XorpTimer EventLoop::new_periodic_ms ( int  ms,
const PeriodicTimerCallback &  pcb,
int  priority = XorpTask::PRIORITY_DEFAULT 
) [inline]

Add periodic timer to the EventLoop.

Parameters:
msthe period in milliseconds when the timer expires.
pcbuser callback object that is invoked when timer expires. If the callback returns false the periodic XorpTimer is unscheduled.
Returns:
a XorpTimer object that must be assigned to remain scheduled.
XorpTask EventLoop::new_task ( const RepeatedTaskCallback &  cb,
int  priority = XorpTask::PRIORITY_DEFAULT,
int  weight = XorpTask::WEIGHT_DEFAULT 
)

Create a new repeated task to be scheduled with the timers and file handlers.

Parameters:
cbcallback object that is invoked when task is run. If the callback returns true, the task will continue to run, otherwise it will be unscheduled.
prioritythe scheduling priority for the task.
scheduler_classthe scheduling class within the priority level.
Returns:
a XorpTask object that must be assigned to remain scheduled.
XorpTimer EventLoop::new_timer ( const BasicTimerCallback &  cb) [inline]

Create a custom timer associated with the EventLoop.

The XorpTimer object created needs to be explicitly scheduled with the available XorpTimer methods.

Parameters:
cbuser callback object that is invoked when timer expires.
Returns:
a XorpTimer object that must be assigned to remain scheduled.
bool EventLoop::remove_ioevent_cb ( XorpFd  fd,
IoEventType  type = IOT_ANY 
)

Remove callbacks associated with file descriptor.

Parameters:
fdthe file descriptor.
typethe event type to clear. The special value IOT_ANY means clear any kind of callback.
Returns:
true on success, false if any error occurred.
void EventLoop::remove_timer ( XorpTimer t)

Remove timer from timer list.

void EventLoop::run ( )

Invoke all pending callbacks relating to XorpTimer and file descriptor activity.

This function may block if there are no selectors ready. It may block forever if there are no timers pending. The timers_pending method can be used to detect whether there are timers pending, while the events_pending method can be used to detect whether there any events pending. An event can be either timer or task. The descriptor_count method can be used to see if there are any select'able file descriptors.

     EventLoop e;
     ...
     while(e.events_pending() || e.descriptor_count() > 0) {
         e.run();
     }
 

Non-xorp processes which use Xorp code should create a periodic timer to prevent the run() function from blocking indefinitely when there are no pending XorpTimer or SelectList objects. The period of the timer will depend on the application's existing needs.

     static bool wakeup_hook(int n) {
        static int count = 0;
	count += n;
	printf("count = %d\n", n);
     	return true;
     }
     int main() {
     	... // Program initialization
Add a Xorp EventLoop
	EventLoop e;
	XorpTimer wakeywakey = e.new_periodic_ms(100, callback(wakeup_hook, 1));
Program's main loop
	for(;;) {
		... do what program does in its main loop ...
		e.run(); // process events
	}
     }
 
SelectorList& EventLoop::selector_list ( ) [inline]
Returns:
reference to the SelectorList used by the EventLoop instance. XXX: Deprecated.
XorpTimer EventLoop::set_flag_after ( const TimeVal wait,
bool *  flag_ptr,
bool  to_value = true 
) [inline]

Add a flag setting timer to the EventLoop.

Parameters:
waitthe relative time when the timer expires.
flag_ptrpointer to a boolean variable that is to be set when the timer expires.
to_valuevalue to set the boolean variable to. Default value is true.
Returns:
a XorpTimer object that must be assigned to remain scheduled.
XorpTimer EventLoop::set_flag_after_ms ( int  ms,
bool *  flag_ptr,
bool  to_value = true 
) [inline]

Add a flag setting timer to the EventLoop.

Parameters:
msthe relative time in millisecond when the timer expires.
flag_ptrpointer to a boolean variable that is set to false when this function is called and will be set to true when the XorpTimer expires.
flag_ptrpointer to a boolean variable that is to be set when the timer expires.
to_valuevalue to set the boolean variable to. Default value is true.
Returns:
a XorpTimer object that must be assigned to remain scheduled.
XorpTimer EventLoop::set_flag_at ( const TimeVal when,
bool *  flag_ptr,
bool  to_value = true 
) [inline]

Add a flag setting timer to the EventLoop.

Parameters:
whenthe absolute time when the timer expires.
flag_ptrpointer to a boolean variable that is to be set when the timer expires.
to_valuevalue to set the boolean variable to. Default value is true.
Returns:
a XorpTimer object that must be assigned to remain scheduled.
TimerList& EventLoop::timer_list ( ) [inline]
Returns:
reference to the TimerList used by the EventLoop instance.
size_t EventLoop::timer_list_length ( ) const [inline]
Returns:
the number of XorpTimers present on EventLoop's TimerList.
bool EventLoop::timers_pending ( ) const [inline]
Returns:
true if any XorpTimers are present on EventLoop's TimerList.

The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations