Fawkes API  Fawkes Development Version
clock.cpp
1 
2 /***************************************************************************
3  * clock.cpp - A central clock
4  *
5  * Created: Sun Jun 03 00:23:59 2007
6  * Copyright 2007 Daniel Beck
7  * 2007-2008 Tim Niemueller [www.niemueller.de]
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #include <core/exception.h>
26 #include <utils/time/clock.h>
27 #include <utils/time/timesource.h>
28 
29 #include <cstdlib>
30 
31 namespace fawkes {
32 
33 /** @class Clock clock.h <utils/time/clock.h>
34  * This is supposed to be the central clock in Fawkes.
35  * It is implemented as a singleton to ensure that there is only
36  * one object. So-called TimeSources can be registered at the Clock
37  * their current time can be retrieved through the Clock.
38  * @author Daniel Beck, Tim Niemueller
39  */
40 
41 /** initialize static members */
42 Clock *Clock::_instance = NULL;
43 
44 /** Constructor. */
45 Clock::Clock()
46 {
47  ext_timesource = 0;
48  ext_default = false;
49 }
50 
51 /** Destructor. */
53 {
54  delete ext_timesource;
55 }
56 
57 /** Clock initializer.
58  * This one is static and has to be called to instantiate a Clock object.
59  * In further calls it just returns a pointer to the Clock object.
60  * @return a pointer to the Clock object
61  */
62 Clock *
64 {
65  if (NULL == _instance) {
66  _instance = new Clock();
67  }
68 
69  return _instance;
70 }
71 
72 /** Finalize. */
73 void
75 {
76  delete _instance;
77  _instance = NULL;
78 }
79 
80 /** Register an external time source.
81  *
82  * @param ts a pointer to the external time source
83  * @param make_default if true, this time source is made the default
84  * timesource which means that for every call of get_time() the time
85  * of the external time source is returned
86  */
87 void
89 {
90  ext_timesource = ts;
91 
92  if (make_default) {
93  ext_default = true;
94  }
95 }
96 
97 /** Remove external time source.
98  * If an external timesource is currently set it is removed. The time source
99  * will not be deleted but only the reference to it is removed.
100  * @param ts only remove time source if it equals ts, if NULL remove no matter what.
101  */
102 void
104 {
105  if ((ts == NULL) || (ext_timesource == ts)) {
106  ext_timesource = NULL;
107  ext_default = false;
108  } else {
109  throw Exception("Time sources do not match. Not removing.");
110  }
111 }
112 
113 /** Set/unset the external time source as the default time source.
114  * @param ext_is_default true to make external time source the default,
115  * false to disable it as the default.
116  */
117 void
119 {
120  if (ext_is_default) {
121  if (NULL != ext_timesource) {
122  ext_default = true;
123  } else {
124  throw Exception("Trying to make the external timesource the default timesource but there is "
125  "no external timesource");
126  }
127  } else {
128  ext_default = false;
129  }
130 }
131 
132 /** Checks whether the external time source is the default time soucre.
133  * @return true if external time source is default time source
134  */
135 bool
137 {
138  return ext_default;
139 }
140 
141 /** Returns the time of the selected time source.
142  * @param tv pointer to a timeval struct where the time is written to
143  * @param sel allows to select the time source
144  */
145 void
146 Clock::get_time(struct timeval *tv, TimesourceSelector sel) const
147 {
148  if ((DEFAULT == sel && !ext_default) || REALTIME == sel) {
149  gettimeofday(tv, 0);
150  } else if ((EXTERNAL == sel) && (NULL == ext_timesource)) {
151  throw Exception("No external time source registered");
152  } else {
153  ext_timesource->get_time(tv);
154  }
155 }
156 
157 /** Returns the time of the selected time source.
158  * @param tv pointer to a timeval struct where the time is written to
159  */
160 void
161 Clock::get_time(struct timeval *tv) const
162 {
163  if (ext_default) {
164  if (NULL == ext_timesource) {
165  throw Exception("No external time source registered");
166  }
167  ext_timesource->get_time(tv);
168  } else {
169  gettimeofday(tv, NULL);
170  }
171 }
172 
173 /** Returns the time of the selected time source.
174  * @param time reference to a time where the result is stored
175  */
176 void
177 Clock::get_time(Time &time) const
178 {
179  get_time(&(time.time_));
180 }
181 
182 /** Returns the time of the selected time source.
183  * @param time reference to a time where the result is stored
184  * @param sel allows to select the time source
185  */
186 void
188 {
189  get_time(&(time.time_), sel);
190 }
191 
192 /** Returns the time of the selected time source.
193  * @param time pointer to a Time instance
194  */
195 void
196 Clock::get_time(Time *time) const
197 {
198  get_time(&(time->time_));
199 }
200 
201 /** Returns the time of the selected time source.
202  * @param time pointer to a Time instance where the time is stored
203  * @param sel allows to select the time source
204  */
205 void
207 {
208  get_time(&(time->time_), sel);
209 }
210 
211 /** Returns the system time.
212  * @param tv pointer to a timeval struct where the time is written to
213  */
214 void
215 Clock::get_systime(struct timeval *tv) const
216 {
217  gettimeofday(tv, 0);
218 }
219 
220 /** Returns the time of the selected time source.
221  * @param time reference to Time instance where the time is stored
222  */
223 void
225 {
226  gettimeofday(&(time.time_), 0);
227 }
228 
229 /** Returns the time of the selected time source.
230  * @param time pointer to Time instance where the time is stored
231  */
232 void
234 {
235  gettimeofday(&(time->time_), 0);
236 }
237 
238 /** Get the current time.
239  * @return current time
240  */
241 Time
242 Clock::now() const
243 {
244  Time t(_instance);
245  return t.stamp();
246 }
247 
248 /** How much time has elapsed since t?
249  * Calculated as "now - t" in seconds.
250  * @param t time
251  * @return elapsed seconds
252  */
253 float
255 {
256  Time nowt(_instance);
257  return nowt - t;
258 }
259 
260 /** How much system time has elapsed since t?
261  * Use only for system time criteria like timeouts.
262  * @param t time
263  * @return elapsed system seconds
264  */
265 float
267 {
268  struct timeval nowt;
269  gettimeofday(&nowt, NULL);
270  return time_diff_sec(nowt, t->time_);
271 }
272 
273 /** Convert a time given w.r.t. the external time source into the system time.
274  * @param t the time that is converted to the system time
275  * @return the time in system time
276  */
277 Time
279 {
280  timeval tv;
281  Time ret(t);
282 
283  if (NULL != ext_timesource) {
284  tv = ext_timesource->conv_to_realtime(t.get_timeval());
285  ret.set_time(&tv);
286  }
287 
288  return ret;
289 }
290 
291 /** Convert some native time to a Fawkes time.
292  * If communicating with some entity using some native time format (e.g.
293  * a simulation giving offset timestamps from the simulation start) it
294  * is necessary to convert them to a time that the time is comparable
295  * to other times generated using the clock (and hence external timesource).
296  * @param t the time that is to be converted to the a Fawkes time
297  * @return the time in Fawkes time comparable to other times queried
298  * from this lock. Identity of the given time if no external time source
299  * has been registered.
300  */
301 Time
303 {
304  timeval tv;
305  Time ret(t);
306 
307  if (NULL != ext_timesource) {
308  tv = ext_timesource->conv_native_to_exttime(t.get_timeval());
309  ret.set_time(&tv);
310  }
311 
312  return ret;
313 }
314 
315 /** Check whether an external time source is registered.
316  * @return true if an external time source is registered
317  */
318 bool
320 {
321  if (0 != ext_timesource) {
322  return true;
323  } else {
324  return false;
325  }
326 }
327 
328 } // end namespace fawkes
This is supposed to be the central clock in Fawkes.
Definition: clock.h:35
void remove_ext_timesource(TimeSource *ts=0)
Remove external time source.
Definition: clock.cpp:103
TimesourceSelector
Select the time source.
Definition: clock.h:38
@ DEFAULT
select the default time source
Definition: clock.h:39
@ REALTIME
select the system time source
Definition: clock.h:40
@ EXTERNAL
select the external time source
Definition: clock.h:41
Time native_to_time(const Time &t)
Convert some native time to a Fawkes time.
Definition: clock.cpp:302
static void finalize()
Finalize.
Definition: clock.cpp:74
float sys_elapsed(Time *t) const
How much system time has elapsed since t? Use only for system time criteria like timeouts.
Definition: clock.cpp:266
void register_ext_timesource(TimeSource *ts, bool make_default=false)
Register an external time source.
Definition: clock.cpp:88
bool is_ext_default_timesource() const
Checks whether the external time source is the default time soucre.
Definition: clock.cpp:136
virtual ~Clock()
Destructor.
Definition: clock.cpp:52
Time now() const
Get the current time.
Definition: clock.cpp:242
void get_time(struct timeval *tv) const
Returns the time of the selected time source.
Definition: clock.cpp:161
bool has_ext_timesource() const
Check whether an external time source is registered.
Definition: clock.cpp:319
static Clock * instance()
Clock initializer.
Definition: clock.cpp:63
void get_systime(struct timeval *tv) const
Returns the system time.
Definition: clock.cpp:215
void set_ext_default_timesource(bool ext_is_default)
Set/unset the external time source as the default time source.
Definition: clock.cpp:118
Time ext_to_realtime(const Time &t)
Convert a time given w.r.t.
Definition: clock.cpp:278
float elapsed(Time *t) const
How much time has elapsed since t? Calculated as "now - t" in seconds.
Definition: clock.cpp:254
Base class for exceptions in Fawkes.
Definition: exception.h:36
TimeSource interface.
Definition: timesource.h:37
virtual timeval conv_native_to_exttime(const timeval *tv) const =0
Convert a native time to the external time.
virtual timeval conv_to_realtime(const timeval *tv) const =0
Convert a time given w.r.t.
virtual void get_time(timeval *tv) const =0
Get the current time.
A class for handling time.
Definition: time.h:93
Time & stamp()
Set this time to the current time.
Definition: time.cpp:704
const timeval * get_timeval() const
Obtain the timeval where the time is stored.
Definition: time.h:112
void set_time(const timeval *tv)
Sets the time.
Definition: time.cpp:246
Fawkes library namespace.
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.
Definition: time.h:41