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 |
| TimerList & | timer_list () |
| SelectorList & | selector_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 |
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.
| 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:
-
| fd | the file descriptor. |
| type | the IoEventType of the event. |
| cb | object to be invoked when file descriptor has I/O pending. |
- Returns:
- true on success, false if any error occurred.
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 initializationAdd 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
}
}