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 <utils/time/clock.h>
26 #include <utils/time/timesource.h>
27 #include <core/exception.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 
52 /** Destructor. */
54 {
55  delete ext_timesource;
56 }
57 
58 
59 /** Clock initializer.
60  * This one is static and has to be called to instantiate a Clock object.
61  * In further calls it just returns a pointer to the Clock object.
62  * @return a pointer to the Clock object
63  */
64 Clock *
66 {
67  if (NULL == _instance) {
68  _instance = new Clock();
69  }
70 
71  return _instance;
72 }
73 
74 
75 /** Finalize. */
76 void
78 {
79  delete _instance;
80  _instance = NULL;
81 }
82 
83 
84 /** Register an external time source.
85  *
86  * @param ts a pointer to the external time source
87  * @param make_default if true, this time source is made the default
88  * timesource which means that for every call of get_time() the time
89  * of the external time source is returned
90  */
91 void
93 {
94  ext_timesource = ts;
95 
96  if (make_default) {
97  ext_default = true;
98  }
99 }
100 
101 
102 /** Remove external time source.
103  * If an external timesource is currently set it is removed. The time source
104  * will not be deleted but only the reference to it is removed.
105  * @param ts only remove time source if it equals ts, if NULL remove no matter what.
106  */
107 void
109 {
110  if ( (ts == NULL) || (ext_timesource == ts) ) {
111  ext_timesource = NULL;
112  ext_default = false;
113  } else {
114  throw Exception("Time sources do not match. Not removing.");
115  }
116 }
117 
118 
119 /** Set/unset the external time source as the default time source.
120  * @param ext_is_default true to make external time source the default,
121  * false to disable it as the default.
122  */
123 void
125 {
126  if ( ext_is_default ) {
127  if (NULL != ext_timesource) {
128  ext_default = true;
129  } else {
130  throw Exception("Trying to make the external timesource the default timesource but there is no external timesource");
131  }
132  } else {
133  ext_default = false;
134  }
135 }
136 
137 
138 /** Checks whether the external time source is the default time soucre.
139  * @return true if external time source is default time source
140  */
141 bool
143 {
144  return ext_default;
145 }
146 
147 
148 /** Returns the time of the selected time source.
149  * @param tv pointer to a timeval struct where the time is written to
150  * @param sel allows to select the time source
151  */
152 void
153 Clock::get_time(struct timeval* tv, TimesourceSelector sel) const
154 {
155  if ( (DEFAULT == sel && !ext_default) ||
156  REALTIME == sel)
157  {
158  gettimeofday(tv, 0);
159  }
160  else if ( (EXTERNAL == sel) &&
161  (NULL == ext_timesource) )
162  {
163  throw Exception("No external time source registered");
164  }
165  else
166  {
167  ext_timesource->get_time(tv);
168  }
169 }
170 
171 
172 /** Returns the time of the selected time source.
173  * @param tv pointer to a timeval struct where the time is written to
174  */
175 void
176 Clock::get_time(struct timeval* tv) const
177 {
178  if ( ext_default ) {
179  if ( NULL == ext_timesource ) {
180  throw Exception("No external time source registered");
181  }
182  ext_timesource->get_time(tv);
183  } else {
184  gettimeofday(tv, NULL);
185  }
186 }
187 
188 
189 /** Returns the time of the selected time source.
190  * @param time reference to a time where the result is stored
191  */
192 void
193 Clock::get_time(Time &time) const
194 {
195  get_time(&(time.__time));
196 }
197 
198 
199 
200 
201 /** Returns the time of the selected time source.
202  * @param time reference to a time where the result is stored
203  * @param sel allows to select the time source
204  */
205 void
207 {
208  get_time(&(time.__time), sel);
209 }
210 
211 
212 /** Returns the time of the selected time source.
213  * @param time pointer to a Time instance
214  */
215 void
216 Clock::get_time(Time *time) const
217 {
218  get_time(&(time->__time));
219 }
220 
221 
222 
223 
224 /** Returns the time of the selected time source.
225  * @param time pointer to a Time instance where the time is stored
226  * @param sel allows to select the time source
227  */
228 void
230 {
231  get_time(&(time->__time), sel);
232 }
233 
234 
235 /** Returns the system time.
236  * @param tv pointer to a timeval struct where the time is written to
237  */
238 void
239 Clock::get_systime(struct timeval* tv) const
240 {
241  gettimeofday(tv, 0);
242 }
243 
244 
245 /** Returns the time of the selected time source.
246  * @param time reference to Time instance where the time is stored
247  */
248 void
250 {
251  gettimeofday(&(time.__time), 0);
252 }
253 
254 
255 /** Returns the time of the selected time source.
256  * @param time pointer to Time instance where the time is stored
257  */
258 void
260 {
261  gettimeofday(&(time->__time), 0);
262 }
263 
264 
265 /** Get the current time.
266  * @return current time
267  */
268 Time
269 Clock::now() const
270 {
271  Time t(_instance);
272  return t.stamp();
273 }
274 
275 
276 /** How much time has elapsed since t?
277  * Calculated as "now - t" in seconds.
278  * @param t time
279  * @return elapsed seconds
280  */
281 float
283 {
284  Time nowt(_instance);
285  return nowt - t;
286 }
287 
288 
289 /** How much system time has elapsed since t?
290  * Use only for system time criteria like timeouts.
291  * @param t time
292  * @return elapsed system seconds
293  */
294 float
296 {
297  struct timeval nowt;
298  gettimeofday(&nowt, NULL);
299  return time_diff_sec(nowt, t->__time);
300 }
301 
302 
303 /** Convert a time given w.r.t. the external time source into the system time.
304  * @param t the time that is converted to the system time
305  * @return the time in system time
306  */
307 Time
309 {
310  timeval tv;
311  Time ret(t);
312 
313  if (NULL != ext_timesource) {
314  tv = ext_timesource->conv_to_realtime(t.get_timeval());
315  ret.set_time(&tv);
316  }
317 
318  return ret;
319 }
320 
321 
322 /** Convert some native time to a Fawkes time.
323  * If communicating with some entity using some native time format (e.g.
324  * a simulation giving offset timestamps from the simulation start) it
325  * is necessary to convert them to a time that the time is comparable
326  * to other times generated using the clock (and hence external timesource).
327  * @param t the time that is to be converted to the a Fawkes time
328  * @return the time in Fawkes time comparable to other times queried
329  * from this lock. Identity of the given time if no external time source
330  * has been registered.
331  */
332 Time
334 {
335  timeval tv;
336  Time ret(t);
337 
338  if (NULL != ext_timesource) {
339  tv = ext_timesource->conv_native_to_exttime(t.get_timeval());
340  ret.set_time(&tv);
341  }
342 
343  return ret;
344 }
345 
346 /** Check whether an external time source is registered.
347  * @return true if an external time source is registered
348  */
349 bool
351 {
352  if (0 != ext_timesource) {
353  return true;
354  } else {
355  return false;
356  }
357 }
358 
359 } // end namespace fawkes
TimesourceSelector
Select the time source.
Definition: clock.h:39
const timeval * get_timeval() const
Obtain the timeval where the time is stored.
Definition: time.h:109
static Clock * instance()
Clock initializer.
Definition: clock.cpp:65
void get_time(struct timeval *tv) const
Returns the time of the selected time source.
Definition: clock.cpp:176
Fawkes library namespace.
void get_systime(struct timeval *tv) const
Returns the system time.
Definition: clock.cpp:239
This is supposed to be the central clock in Fawkes.
Definition: clock.h:34
bool is_ext_default_timesource() const
Checks whether the external time source is the default time soucre.
Definition: clock.cpp:142
Time native_to_time(const Time &t)
Convert some native time to a Fawkes time.
Definition: clock.cpp:333
float elapsed(Time *t) const
How much time has elapsed since t? Calculated as "now - t" in seconds.
Definition: clock.cpp:282
A class for handling time.
Definition: time.h:91
TimeSource interface.
Definition: timesource.h:36
bool has_ext_timesource() const
Check whether an external time source is registered.
Definition: clock.cpp:350
virtual ~Clock()
Destructor.
Definition: clock.cpp:53
void set_ext_default_timesource(bool ext_is_default)
Set/unset the external time source as the default time source.
Definition: clock.cpp:124
Time now() const
Get the current time.
Definition: clock.cpp:269
select the external time source
Definition: clock.h:42
Base class for exceptions in Fawkes.
Definition: exception.h:36
Time ext_to_realtime(const Time &t)
Convert a time given w.r.t.
Definition: clock.cpp:308
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.
Definition: time.h:40
virtual timeval conv_to_realtime(const timeval *tv) const =0
Convert a time given w.r.t.
select the system time source
Definition: clock.h:41
void register_ext_timesource(TimeSource *ts, bool make_default=false)
Register an external time source.
Definition: clock.cpp:92
static void finalize()
Finalize.
Definition: clock.cpp:77
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:295
virtual void get_time(timeval *tv) const =0
Get the current time.
void set_time(const timeval *tv)
Sets the time.
Definition: time.cpp:262
virtual timeval conv_native_to_exttime(const timeval *tv) const =0
Convert a native time to the external time.
Time & stamp()
Set this time to the current time.
Definition: time.cpp:783
void remove_ext_timesource(TimeSource *ts=0)
Remove external time source.
Definition: clock.cpp:108
select the default time source
Definition: clock.h:40