blob: 8357a1989568279013a61731980b7ae18d2ee377 [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
2 *
3 */
Benny Prijono0a749f12005-10-31 21:02:30 +00004#ifndef __PJPP_TIMER_H__
5#define __PJPP_TIMER_H__
6
7#include <pj/timer.h>
8#include <pj++/types.hpp>
9
10class PJ_Timer_Heap;
11
12class PJ_Timer_Entry : private pj_timer_entry
13{
14 friend class PJ_Timer_Heap;
15
16public:
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
61class PJ_Timer_Heap
62{
63public:
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
103private:
104 pj_timer_heap_t *ht_;
105};
106
107#endif /* __PJPP_TIMER_H__ */