Co-ordinates interactions between a TimerList and a SelectorList for Xorp processes. All XorpTimer and select operations should be co-ordinated through this interface.
EventLoop ()
| EventLoop |
~EventLoop ()
| ~EventLoop |
void run ()
| 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, and the descriptor_count method can be used to see if there are any select'able file descriptors.
EventLoop e; ... while(e.timers_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 } }
TimerList& timer_list ()
| timer_list |
Returns: reference to the TimerList used by the EventLoop instance.
SelectorList& selector_list ()
| selector_list |
Returns: reference to the SelectorList used by the EventLoop instance. XXX: Deprecated.
XorpTimer new_oneoff_at (const TimeVal& when,
const OneoffTimerCallback& ocb)
| new_oneoff_at |
Add a new one-off timer to the EventLoop.
Parameters:
when | the absolute time when the timer expires. |
ocb | callback object that is invoked when timer expires. |
Returns: a XorpTimer object that must be assigned to remain scheduled.
XorpTimer new_oneoff_after (const TimeVal& wait,
const OneoffTimerCallback& ocb)
| new_oneoff_after |
Add a new one-off timer to the EventLoop.
Parameters:
wait | the relative time when the timer expires. |
ocb | callback object that is invoked when timer expires. |
Returns: a XorpTimer object that must be assigned to remain scheduled.
XorpTimer new_oneoff_after_ms (int ms, const OneoffTimerCallback& ocb)
| new_oneoff_after_ms |
Add a new one-off timer to the EventLoop.
Parameters:
ms | the relative time in milliseconds when the timer expires. |
ocb | callback object that is invoked when timer expires. |
Returns: a XorpTimer object that must be assigned to remain scheduled.
XorpTimer new_periodic (const TimeVal& wait,
const PeriodicTimerCallback& pcb)
| new_periodic |
Add periodic timer to the EventLoop.
Parameters:
wait | the period when the timer expires. |
pcb | user 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 new_periodic_ms (int ms, const PeriodicTimerCallback& pcb)
| new_periodic_ms |
Add periodic timer to the EventLoop.
Parameters:
ms | the period in milliseconds when the timer expires. |
pcb | user 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 set_flag_at (const TimeVal& when,
bool* flag_ptr,
bool to_value = true)
| set_flag_at |
Add a flag setting timer to the EventLoop.
Parameters:
when | the absolute time when the timer expires. |
flag_ptr | pointer to a boolean variable that is to be set when the timer expires. |
to_value | value to set the boolean variable to. Default value is true. |
Returns: a XorpTimer object that must be assigned to remain scheduled.
XorpTimer set_flag_after (const TimeVal& wait,
bool* flag_ptr,
bool to_value = true)
| set_flag_after |
Add a flag setting timer to the EventLoop.
Parameters:
wait | the relative time when the timer expires. |
flag_ptr | pointer to a boolean variable that is to be set when the timer expires. |
to_value | value to set the boolean variable to. Default value is true. |
Returns: a XorpTimer object that must be assigned to remain scheduled.
XorpTimer set_flag_after_ms (int ms, bool* flag_ptr, bool to_value = true)
| set_flag_after_ms |
Add a flag setting timer to the EventLoop.
Parameters:
ms | the relative time in millisecond when the timer expires. |
flag_ptr | pointer 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_ptr | pointer to a boolean variable that is to be set when the timer expires. |
to_value | value to set the boolean variable to. Default value is true. |
Returns: a XorpTimer object that must be assigned to remain scheduled.
XorpTimer new_timer (const BasicTimerCallback& cb)
| new_timer |
Create a custom timer associated with the EventLoop.
The XorpTimer object created needs to be explicitly scheduled with the available XorpTimer methods.
Parameters:
cb | user callback object that is invoked when timer expires. |
Returns: a XorpTimer object that must be assigned to remain scheduled.
bool add_ioevent_cb (XorpFd fd, IoEventType type, const IoEventCb& cb)
| add_ioevent_cb |
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.
bool remove_ioevent_cb (XorpFd fd, IoEventType type = IOT_ANY)
| remove_ioevent_cb |
Remove callbacks associated with file descriptor.
Parameters:
fd | the file descriptor. |
type | the event type to clear. The special value IOT_ANY means clear any kind of callback. |
Returns: true on success, false if any error occurred.
bool timers_pending ()
| timers_pending |
[const]
Returns: true if any XorpTimers are present on EventLoop's TimerList.
size_t timer_list_length ()
| timer_list_length |
[const]
Returns: the number of XorpTimers present on EventLoop's TimerList.
inline void current_time (TimeVal& now)
| current_time |
[const]
Get current time according to EventLoop's TimerList
size_t descriptor_count ()
| descriptor_count |
[const]
Get the count of the descriptors that have been added.
Returns: the count of the descriptors that have been added.