blob: b547f6d4b09554f71782722818bc38285376bb8f [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJPP_TIMER_HPP__
20#define __PJPP_TIMER_HPP__
21
22#include <pj/timer.h>
23#include <pj++/types.hpp>
24#include <pj/assert.h>
25#include <pj++/lock.hpp>
26
27class Pj_Timer_Heap;
28
29//////////////////////////////////////////////////////////////////////////////
30// Timer entry.
31//
32// How to use:
33// Derive class from Pj_Timer_Entry and override on_timeout().
34// Scheduler timer in Pj_Timer_Heap.
35//
36class Pj_Timer_Entry : public Pj_Object
37{
38 friend class Pj_Timer_Heap;
39
40public:
41 //
42 // Default constructor.
43 //
44 Pj_Timer_Entry()
45 {
46 entry_.user_data = this;
47 entry_.cb = &timer_heap_callback;
48 }
49
50 //
51 // Destructor, do nothing.
52 //
53 ~Pj_Timer_Entry()
54 {
55 }
56
57 //
58 // Override this to get the timeout notification.
59 //
60 virtual void on_timeout(int id) = 0;
61
62private:
63 pj_timer_entry entry_;
64
Benny Prijonoac9d1422006-01-18 23:32:27 +000065 static void timer_heap_callback(pj_timer_heap_t*, pj_timer_entry *e)
Benny Prijono5dcb38d2005-11-21 01:55:47 +000066 {
67 Pj_Timer_Entry *entry = (Pj_Timer_Entry*) e->user_data;
68 entry->on_timeout(e->id);
69 }
70
71};
72
73//////////////////////////////////////////////////////////////////////////////
74// Timer heap.
75//
76class Pj_Timer_Heap : public Pj_Object
77{
78public:
79 //
80 // Default constructor.
81 //
82 Pj_Timer_Heap()
83 : ht_(NULL)
84 {
85 }
86
87 //
88 // Construct timer heap.
89 //
90 Pj_Timer_Heap(Pj_Pool *pool, pj_size_t initial_count)
91 : ht_(NULL)
92 {
93 create(pool, initial_count);
94 }
95
96 //
97 // Destructor.
98 //
99 ~Pj_Timer_Heap()
100 {
101 destroy();
102 }
103
104 //
105 // Create
106 //
107 pj_status_t create(Pj_Pool *pool, pj_size_t initial_count)
108 {
109 destroy();
110 return pj_timer_heap_create(pool->pool_(), initial_count, &ht_);
111 }
112
113 //
114 // Destroy
115 //
116 void destroy()
117 {
118 if (ht_) {
119 pj_timer_heap_destroy(ht_);
120 ht_ = NULL;
121 }
122 }
123
124 //
125 // Get pjlib compatible timer heap object.
126 //
127 pj_timer_heap_t *get_timer_heap()
128 {
129 return ht_;
130 }
131
132 //
133 // Set the lock object.
134 //
135 void set_lock( Pj_Lock *lock, bool auto_delete )
136 {
137 pj_timer_heap_set_lock( ht_, lock->pj_lock_t_(), auto_delete);
138 }
139
140 //
141 // Set maximum number of timed out entries to be processed per poll.
142 //
143 unsigned set_max_timed_out_per_poll(unsigned count)
144 {
145 return pj_timer_heap_set_max_timed_out_per_poll(ht_, count);
146 }
147
148 //
149 // Schedule a timer.
150 //
151 bool schedule( Pj_Timer_Entry *ent, const Pj_Time_Val &delay,
152 int id)
153 {
154 ent->entry_.id = id;
155 return pj_timer_heap_schedule(ht_, &ent->entry_, &delay) == 0;
156 }
157
158 //
159 // Cancel a timer.
160 //
161 bool cancel(Pj_Timer_Entry *ent)
162 {
163 return pj_timer_heap_cancel(ht_, &ent->entry_) == 1;
164 }
165
166 //
167 // Get current number of timers
168 //
169 pj_size_t count()
170 {
171 return pj_timer_heap_count(ht_);
172 }
173
174 //
175 // Get the earliest time.
176 // Return false if no timer is found.
177 //
178 bool earliest_time(Pj_Time_Val *t)
179 {
180 return pj_timer_heap_earliest_time(ht_, t) == PJ_SUCCESS;
181 }
182
183 //
184 // Poll the timer.
185 // Return number of timed out entries has been called.
186 //
187 unsigned poll(Pj_Time_Val *next_delay = NULL)
188 {
189 return pj_timer_heap_poll(ht_, next_delay);
190 }
191
192private:
193 pj_timer_heap_t *ht_;
194};
195
196#endif /* __PJPP_TIMER_HPP__ */
197