Benny Prijono | 4766ffe | 2005-11-01 17:56:59 +0000 | [diff] [blame^] | 1 | /* $Id$ |
| 2 | * |
| 3 | */
|
Benny Prijono | 0a749f1 | 2005-10-31 21:02:30 +0000 | [diff] [blame] | 4 | #ifndef __PJPP_TIMER_H__
|
| 5 | #define __PJPP_TIMER_H__
|
| 6 |
|
| 7 | #include <pj/timer.h>
|
| 8 | #include <pj++/types.hpp>
|
| 9 |
|
| 10 | class PJ_Timer_Heap;
|
| 11 |
|
| 12 | class PJ_Timer_Entry : private pj_timer_entry
|
| 13 | {
|
| 14 | friend class PJ_Timer_Heap;
|
| 15 |
|
| 16 | public:
|
| 17 | static void timer_heap_callback(pj_timer_heap_t *, pj_timer_entry *);
|
| 18 |
|
| 19 | PJ_Timer_Entry() { cb = &timer_heap_callback; }
|
| 20 | PJ_Timer_Entry(int arg_id, void *arg_user_data)
|
| 21 | {
|
| 22 | cb = &timer_heap_callback;
|
| 23 | init(arg_id, arg_user_data);
|
| 24 | }
|
| 25 |
|
| 26 | virtual void on_timeout() = 0;
|
| 27 |
|
| 28 | void init(int arg_id, void *arg_user_data)
|
| 29 | {
|
| 30 | id = arg_id;
|
| 31 | user_data = arg_user_data;
|
| 32 | }
|
| 33 |
|
| 34 | int get_id() const
|
| 35 | {
|
| 36 | return id;
|
| 37 | }
|
| 38 |
|
| 39 | void set_id(int arg_id)
|
| 40 | {
|
| 41 | id = arg_id;
|
| 42 | }
|
| 43 |
|
| 44 | void set_user_data(void *arg_user_data)
|
| 45 | {
|
| 46 | user_data = arg_user_data;
|
| 47 | }
|
| 48 |
|
| 49 | void *get_user_data() const
|
| 50 | {
|
| 51 | return user_data;
|
| 52 | }
|
| 53 |
|
| 54 | const PJ_Time_Val &get_timeout() const
|
| 55 | {
|
| 56 | pj_assert(sizeof(PJ_Time_Val) == sizeof(pj_time_val));
|
| 57 | return (PJ_Time_Val&)_timer_value;
|
| 58 | }
|
| 59 | };
|
| 60 |
|
| 61 | class PJ_Timer_Heap
|
| 62 | {
|
| 63 | public:
|
| 64 | PJ_Timer_Heap() {}
|
| 65 |
|
| 66 | bool create(PJ_Pool *pool, pj_size_t initial_count,
|
| 67 | unsigned flag = PJ_TIMER_HEAP_SYNCHRONIZE)
|
| 68 | {
|
| 69 | ht_ = pj_timer_heap_create(pool->pool_(), initial_count, flag);
|
| 70 | return ht_ != NULL;
|
| 71 | }
|
| 72 |
|
| 73 | pj_timer_heap_t *get_timer_heap()
|
| 74 | {
|
| 75 | return ht_;
|
| 76 | }
|
| 77 |
|
| 78 | bool schedule( PJ_Timer_Entry *ent, const PJ_Time_Val &delay)
|
| 79 | {
|
| 80 | return pj_timer_heap_schedule(ht_, ent, &delay) == 0;
|
| 81 | }
|
| 82 |
|
| 83 | bool cancel(PJ_Timer_Entry *ent)
|
| 84 | {
|
| 85 | return pj_timer_heap_cancel(ht_, ent) == 1;
|
| 86 | }
|
| 87 |
|
| 88 | pj_size_t count()
|
| 89 | {
|
| 90 | return pj_timer_heap_count(ht_);
|
| 91 | }
|
| 92 |
|
| 93 | void earliest_time(PJ_Time_Val *t)
|
| 94 | {
|
| 95 | pj_timer_heap_earliest_time(ht_, t);
|
| 96 | }
|
| 97 |
|
| 98 | int poll(PJ_Time_Val *next_delay = NULL)
|
| 99 | {
|
| 100 | return pj_timer_heap_poll(ht_, next_delay);
|
| 101 | }
|
| 102 |
|
| 103 | private:
|
| 104 | pj_timer_heap_t *ht_;
|
| 105 | };
|
| 106 |
|
| 107 | #endif /* __PJPP_TIMER_H__ */
|