source: source/ariba/utility/transport/tcpip/protlib/timer_module.h@ 9323

Last change on this file since 9323 was 6922, checked in by mies, 15 years ago

replaced deprecated hash_map, hash_set classes with boost
added unsigned serialization
fixed more warnings
included -Wall to Makefile.am

File size: 7.6 KB
RevLine 
[5284]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
[6922]39#include <boost/unordered_map.hpp>
[5284]40
41#include "messages.h"
42#include "threads.h"
43#include "llhashers.h"
44#include "timer.h"
45
[6922]46using boost::unordered_map;
47
[5284]48namespace protlib {
49
50/** @addtogroup timermodule Timer Module
51 * @{
52 */
53
54 static const char *const action_t_str[]=
55 {
56 "ignore",
57 "start",
58 "restart",
59 "stop",
60 "stop all",
61 "elapsed",
62 "unkown"
63 };
64
65/// timer message
66/** This message class is used to control timers managed by the timer
67 * module and for replies when a timer goes off.
68 * Timers are identified by the message ID.
69 */
70class TimerMsg : public message
71{
72public:
73 /// timer module actions
74 /** Tell the timer module what to do or indicate that a timer elapsed. */
75 enum action_t
76 {
77 ac_ignore = 0,
78 ac_start = 1,
79 ac_restart = 2,
80 ac_stop = 3,
81 ac_stop_all = 4,
82 ac_elapsed = 5
83 }; // end action_t
84
85 /// timer parameter
86 /** This parameter corresponds to the timer parameters in timer.h and
87 * helps managing timers.
88 */
89 typedef timer_callback_param_t param_t;
90 /// constructor
91 TimerMsg(qaddr_t s = qaddr_unknown, bool s_err = false);
92 /// destructor
93 virtual ~TimerMsg();
94 /// get alarm
95 void get_time(int32& sec, int32& msec);
96 /// get action
97 action_t get_action() const;
98 /// get action str
99 const char* get_action_str() const;
100 /// set result
101 bool set_ok(bool r);
102 /// get result
103 bool is_ok();
104 /// start absolute timer
105 bool start_absolute(int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
106 /// start relative timer
107 bool start_relative(int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
108 /// start timer
109 bool start(bool rel, int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
110 /// restart absolute timer
111 bool restart_absolute(id_t id, int32 sec, int32 msec = 0);
112 /// restart relative timer
113 bool restart_relative(id_t id, int32 sec, int32 msec = 0);
114 /// restart timer
115 bool restart(bool rel, id_t id, int32 sec, int32 msec = 0);
116 /// stop timer
117 bool stop(id_t id);
118 /// stop all timers
119 bool stop_all();
120 /// set to elapsed
121 bool set_elapsed();
122 /// get send_error flag
123 bool get_send_error() const;
124 /// set send_error flag
125 bool set_send_error(bool f);
126 /// get timer parameter #1
127 param_t get_param1() const;
128 /// get timer parameter #2
129 param_t get_param2() const;
130 /// get timer parameters
131 void get_params(param_t& p1, param_t& p2) const;
132 /// Is it an absolute or relative timer?
133 bool is_absolute() const;
134 /// Is it an absolute or relative timer?
135 bool is_relative() const;
136 private:
137 int32 time_sec;
138 int32 time_msec;
139 action_t action;
140 param_t param1;
141 param_t param2;
142 bool ok;
143 bool send_error;
144 bool relative;
145}; // end class TimerMsg
146
147/** Get alarm time. Please check message action to see if this is an
148 * absolute or relative time.
149 * If this is a expiration notification, the time is absolute.
150 */
151inline
152void
153TimerMsg::get_time(int32& sec, int32& msec)
154{
155 sec = time_sec;
156 msec = time_msec;
157} // end get_alarm
158
159/** Get timer message action. There is no way to set the action value, this
160 * is done by start_relative or start_absolute...
161 */
162inline
163TimerMsg::action_t
164TimerMsg::get_action() const
165{
166 return action;
167} // end get_action
168
169inline
170const char*
171TimerMsg::get_action_str() const
172{
173 return action_t_str[action];
174} // end get_action_str
175
176/** Get result flag. */
177bool
178inline
179TimerMsg::is_ok()
180{
181 return ok;
182} // end is_ok
183
184
185/// timer module parameters
186/** This holds the message queue and the default sleep time. */
187struct TimerModuleParam : public ThreadParam
188{
189 TimerModuleParam(uint32 sleep_time = ThreadParam::default_sleep_time,
190 bool sua = false, bool ser = true, bool sre = true);
191 /// send messages until abort
192 const bool send_until_abort;
193 const message::qaddr_t source;
194 const bool send_error_expedited;
195 const bool send_reply_expedited;
196}; // end TimerModuleParam
197
198/// timer module class
199/** This is the timer module. */
200class TimerModule : public Thread, public TimerCallback
201{
202 /***** TimerCallback *****/
203 public:
204 /// callback member function
205 /** @param timer timer ID
206 * @param callback_param additional callback parameter.
207 */
208 virtual void timer_expired(timer_id_t timer, timer_callback_param_t callback_param);
209 public:
210 /// constructor
211 TimerModule(const TimerModuleParam& p);
212 /// destructor
213 virtual ~TimerModule();
214 /// timer module main loop
215 virtual void main_loop(uint32 nr);
216 private:
217 /// wait for incoming messages
218 void process_queue();
219 /// check timers and send messages
220 void process_elapsed_timers();
221 /// start a timer
222 bool start_timer(TimerMsg* m);
223 /// restart a timer
224 bool restart_timer(TimerMsg* m);
225 /// stop a timer
226 bool stop_timer(TimerMsg* m);
227 /// stop all timers
228 bool stop_all_timers();
229 /// send back error
230 void send_error_or_dispose(TimerMsg* m, bool ok);
231 /// timer module parameters
232 TimerManager tm;
233 /// module parameters
234 const TimerModuleParam timerparam;
235 /// timer map
236 /** This stores timer IDs and the corresponding message IDs. */
237 class TimerMap
238 {
239 public:
240 bool insert(timer_id_t tid, TimerMsg* m);
241 /// lookup message ID
242 message::id_t lookup_mid(timer_id_t tid) const;
243 /// lookup timer ID
244 timer_id_t lookup_tid(message::id_t mid) const;
245 /// lookup message
246 TimerMsg* lookup_msg(timer_id_t tid) const;
247 /// erase record of timer ID and message ID
248 void erase(timer_id_t tid, message::id_t mid, bool dispose = true);
249 /// clear all
250 void clear(bool dispose = true);
251 private:
[6922]252 typedef unordered_map<message::id_t,timer_id_t> mid2tid_t;
[5284]253 typedef mid2tid_t::const_iterator const_mid2tid_it_t;
[6922]254 typedef unordered_map<timer_id_t,message::id_t> tid2mid_t;
[5284]255 typedef tid2mid_t::const_iterator const_tid2mid_it_t;
[6922]256 typedef unordered_map<timer_id_t,TimerMsg*> tid2msg_t;
[5284]257 typedef tid2msg_t::const_iterator const_tid2msg_it_t;
258 mid2tid_t mid2tid;
259 tid2mid_t tid2mid;
260 tid2msg_t tid2msg;
261 } tmap; // end class TimerMap
262}; // end class TimerModule
263
264//@}
265
266} // end namespace protlib
267
268#endif
Note: See TracBrowser for help on using the repository browser.