source: trash/old-modules/transport/protlib/timer_module.h@ 5887

Last change on this file since 5887 was 5641, checked in by Christoph Mayer, 15 years ago
File size: 7.5 KB
Line 
1/// ----------------------------------------*- mode: C++; -*--
2/// @file timer_module.h
3/// Timer module (thread) that provides timer management functions
4/// ----------------------------------------------------------
5/// $Id: timer_module.h 2549 2007-04-02 22:17:37Z bless $
6/// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/timer_module.h $
7// ===========================================================
8//
9// Copyright (C) 2005-2007, all rights reserved by
10// - Institute of Telematics, Universitaet Karlsruhe (TH)
11//
12// More information and contact:
13// https://projekte.tm.uka.de/trac/NSIS
14//
15// This program is free software; you can redistribute it and/or modify
16// it under the terms of the GNU General Public License as published by
17// the Free Software Foundation; version 2 of the License
18//
19// This program is distributed in the hope that it will be useful,
20// but WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22// GNU General Public License for more details.
23//
24// You should have received a copy of the GNU General Public License along
25// with this program; if not, write to the Free Software Foundation, Inc.,
26// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27//
28// ===========================================================
29
30/** @ingroup timermodule
31 * The timer module provides a way for other modules to set timers via a
32 * message and receive a message back on their queue when the timer goes
33 * off.
34 */
35
36#ifndef _PROTLIB__TIMER_MODULE_H
37#define _PROTLIB__TIMER_MODULE_H
38
39#include <ext/hash_map>
40
41#include "messages.h"
42#include "threads.h"
43#include "llhashers.h"
44#include "timer.h"
45
46namespace protlib {
47
48/** @addtogroup timermodule Timer Module
49 * @{
50 */
51
52 static const char *const action_t_str[]=
53 {
54 "ignore",
55 "start",
56 "restart",
57 "stop",
58 "stop all",
59 "elapsed",
60 "unkown"
61 };
62
63/// timer message
64/** This message class is used to control timers managed by the timer
65 * module and for replies when a timer goes off.
66 * Timers are identified by the message ID.
67 */
68class TimerMsg : public message
69{
70public:
71 /// timer module actions
72 /** Tell the timer module what to do or indicate that a timer elapsed. */
73 enum action_t
74 {
75 ac_ignore = 0,
76 ac_start = 1,
77 ac_restart = 2,
78 ac_stop = 3,
79 ac_stop_all = 4,
80 ac_elapsed = 5
81 }; // end action_t
82
83 /// timer parameter
84 /** This parameter corresponds to the timer parameters in timer.h and
85 * helps managing timers.
86 */
87 typedef timer_callback_param_t param_t;
88 /// constructor
89 TimerMsg(qaddr_t s = qaddr_unknown, bool s_err = false);
90 /// destructor
91 virtual ~TimerMsg();
92 /// get alarm
93 void get_time(int32& sec, int32& msec);
94 /// get action
95 action_t get_action() const;
96 /// get action str
97 const char* get_action_str() const;
98 /// set result
99 bool set_ok(bool r);
100 /// get result
101 bool is_ok();
102 /// start absolute timer
103 bool start_absolute(int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
104 /// start relative timer
105 bool start_relative(int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
106 /// start timer
107 bool start(bool rel, int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
108 /// restart absolute timer
109 bool restart_absolute(id_t id, int32 sec, int32 msec = 0);
110 /// restart relative timer
111 bool restart_relative(id_t id, int32 sec, int32 msec = 0);
112 /// restart timer
113 bool restart(bool rel, id_t id, int32 sec, int32 msec = 0);
114 /// stop timer
115 bool stop(id_t id);
116 /// stop all timers
117 bool stop_all();
118 /// set to elapsed
119 bool set_elapsed();
120 /// get send_error flag
121 bool get_send_error() const;
122 /// set send_error flag
123 bool set_send_error(bool f);
124 /// get timer parameter #1
125 param_t get_param1() const;
126 /// get timer parameter #2
127 param_t get_param2() const;
128 /// get timer parameters
129 void get_params(param_t& p1, param_t& p2) const;
130 /// Is it an absolute or relative timer?
131 bool is_absolute() const;
132 /// Is it an absolute or relative timer?
133 bool is_relative() const;
134 private:
135 int32 time_sec;
136 int32 time_msec;
137 action_t action;
138 param_t param1;
139 param_t param2;
140 bool ok;
141 bool send_error;
142 bool relative;
143}; // end class TimerMsg
144
145/** Get alarm time. Please check message action to see if this is an
146 * absolute or relative time.
147 * If this is a expiration notification, the time is absolute.
148 */
149inline
150void
151TimerMsg::get_time(int32& sec, int32& msec)
152{
153 sec = time_sec;
154 msec = time_msec;
155} // end get_alarm
156
157/** Get timer message action. There is no way to set the action value, this
158 * is done by start_relative or start_absolute...
159 */
160inline
161TimerMsg::action_t
162TimerMsg::get_action() const
163{
164 return action;
165} // end get_action
166
167inline
168const char*
169TimerMsg::get_action_str() const
170{
171 return action_t_str[action];
172} // end get_action_str
173
174/** Get result flag. */
175bool
176inline
177TimerMsg::is_ok()
178{
179 return ok;
180} // end is_ok
181
182
183/// timer module parameters
184/** This holds the message queue and the default sleep time. */
185struct TimerModuleParam : public ThreadParam
186{
187 TimerModuleParam(uint32 sleep_time = ThreadParam::default_sleep_time,
188 bool sua = false, bool ser = true, bool sre = true);
189 /// send messages until abort
190 const bool send_until_abort;
191 const message::qaddr_t source;
192 const bool send_error_expedited;
193 const bool send_reply_expedited;
194}; // end TimerModuleParam
195
196/// timer module class
197/** This is the timer module. */
198class TimerModule : public Thread, public TimerCallback
199{
200 /***** TimerCallback *****/
201 public:
202 /// callback member function
203 /** @param timer timer ID
204 * @param callback_param additional callback parameter.
205 */
206 virtual void timer_expired(timer_id_t timer, timer_callback_param_t callback_param);
207 public:
208 /// constructor
209 TimerModule(const TimerModuleParam& p);
210 /// destructor
211 virtual ~TimerModule();
212 /// timer module main loop
213 virtual void main_loop(uint32 nr);
214 private:
215 /// wait for incoming messages
216 void process_queue();
217 /// check timers and send messages
218 void process_elapsed_timers();
219 /// start a timer
220 bool start_timer(TimerMsg* m);
221 /// restart a timer
222 bool restart_timer(TimerMsg* m);
223 /// stop a timer
224 bool stop_timer(TimerMsg* m);
225 /// stop all timers
226 bool stop_all_timers();
227 /// send back error
228 void send_error_or_dispose(TimerMsg* m, bool ok);
229 /// timer module parameters
230 TimerManager tm;
231 /// module parameters
232 const TimerModuleParam timerparam;
233 /// timer map
234 /** This stores timer IDs and the corresponding message IDs. */
235 class TimerMap
236 {
237 public:
238 bool insert(timer_id_t tid, TimerMsg* m);
239 /// lookup message ID
240 message::id_t lookup_mid(timer_id_t tid) const;
241 /// lookup timer ID
242 timer_id_t lookup_tid(message::id_t mid) const;
243 /// lookup message
244 TimerMsg* lookup_msg(timer_id_t tid) const;
245 /// erase record of timer ID and message ID
246 void erase(timer_id_t tid, message::id_t mid, bool dispose = true);
247 /// clear all
248 void clear(bool dispose = true);
249 private:
250 typedef hash_map<message::id_t,timer_id_t> mid2tid_t;
251 typedef mid2tid_t::const_iterator const_mid2tid_it_t;
252 typedef hash_map<timer_id_t,message::id_t> tid2mid_t;
253 typedef tid2mid_t::const_iterator const_tid2mid_it_t;
254 typedef hash_map<timer_id_t,TimerMsg*> tid2msg_t;
255 typedef tid2msg_t::const_iterator const_tid2msg_it_t;
256 mid2tid_t mid2tid;
257 tid2mid_t tid2mid;
258 tid2msg_t tid2msg;
259 } tmap; // end class TimerMap
260}; // end class TimerModule
261
262//@}
263
264} // end namespace protlib
265
266#endif
Note: See TracBrowser for help on using the repository browser.