Packets: Fix VariantParser invalid parser access bug
[senf.git] / Scheduler / ClockService.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief ClockService public header */
25
26 #ifndef HH_SENF_Scheduler_ClockService_
27 #define HH_SENF_Scheduler_ClockService_ 1
28
29 // Custom includes
30 #include <sys/time.h> 
31 #include <boost/utility.hpp>
32 #include <boost/date_time/posix_time/posix_time.hpp>
33 #include <boost/scoped_ptr.hpp>
34 #include <boost/cstdint.hpp>
35 #include "../Utils/singleton.hh"
36 #include "Console/Parse.hh"
37
38 //#include "ClockService.mpp"
39 ///////////////////////////////hh.p////////////////////////////////////////
40
41 namespace senf {
42
43 #ifndef DOXYGEN
44     namespace detail { class ClockServiceTest; }
45 #endif
46
47     // Implementation note: The clock value is represented as a 64bit unsigned integer number of
48     // nanosecods based on the CLOCK_MONOTONIC POSIX clock.
49     //
50     // To allow conversion between clock value and absolute time, the ClockService samples the
51     // absolute current time and the clock value when the conversion is performed. This is done at
52     // most once per second on a if-needed basis.
53     
54     /** \brief Reliable high precision monotonous clock source
55
56         The ClockService provides a highly accurate monotonous clock source based on
57         gettimeofday(). However, it takes additional precautions to detect clock skew.
58
59         \implementation The funny mixture of static and non-static members stems from the old
60             implementation based on interval timers and gettimeofday(). The current implementation
61             usses POSIX clocks and is much simpler and more precise.
62       */
63     class ClockService
64         : singleton<ClockService>
65     {
66     public:
67         ///////////////////////////////////////////////////////////////////////////
68         // Types
69
70         /** \brief ClockService timer data type
71             
72             Unsigned integer type representing scheduler time. Scheduler time is measured in
73             nanoseconds relative to some implementation defined reference time.
74          */
75         typedef boost::int_fast64_t clock_type;
76
77         /** \brief Supplementary integer type
78
79             This type is used to represent varies supplementary values (e.g. number of microseconds)
80          */
81         typedef boost::int_fast64_t int64_type;
82
83         /** \brief Absolute time data type
84
85             Boost.DateTime datatype used to represent absolute date/time values.
86          */
87         typedef boost::posix_time::ptime abstime_type;
88
89         ///////////////////////////////////////////////////////////////////////////
90
91         static clock_type now();  ///< Return current clock value
92         
93         static abstime_type abstime(clock_type clock); ///< Convert clock to absolute time
94                                         /**< This member converts a clock value into an absolute
95                                              Boost.DateTime value.
96                                              \note You should not base timeout calculations on this
97                                                  absolute time value. Clock time is guaranteed to be
98                                                  monotonous, absolute time may be non-monotonous if
99                                                  the system date/time is changed. */
100
101         static clock_type clock(abstime_type time); ///< Convert absolute time to clock value
102                                         /**< This member converst an absolute time value into the
103                                              corresponding clock value.
104                                              \see abstime */
105
106         static clock_type from_time_t(time_t const & time); 
107                                         ///< Convert legacy time_t to clock value
108                                         /**< This member converts an absolute time value 
109                                              represented as a time_t value into a clock value */
110
111         static clock_type from_timeval(timeval const & time); 
112                                         ///< Convert legacy timeval to clock value
113                                         /**< This member converts an absolute time value
114                                              represented as a timeval value into a clock value */
115
116         static clock_type nanoseconds(int64_type v); ///< Convert \a v nanoseconds to clock_type
117         static clock_type microseconds(int64_type v); ///< Convert \a v microseconds to clock_type
118         static clock_type milliseconds(int64_type v); ///< Convert \a v milliseconds to clock_type
119         static clock_type seconds(int64_type v); ///< Convert \a v seconds to clock_type
120         static clock_type minutes(int64_type v); ///< Convert \a v minutes to clock_type
121         static clock_type hours(int64_type v); ///< Convert \a v hours to clock_type
122         static clock_type days(int64_type v); ///< Convert \a v days to clock_type
123
124         static int64_type in_nanoseconds(clock_type v); ///< Convert \a v to nanoseconds
125         static int64_type in_microseconds(clock_type v); ///< Convert \a v to microseconds
126         static int64_type in_milliseconds(clock_type v); ///< Convert \a v to milliseconds
127         static int64_type in_seconds(clock_type v); ///< Convert \a v to seconds
128         static int64_type in_minutes(clock_type v); ///< Convert \a v to minutes
129         static int64_type in_hours(clock_type v); ///< Convert \a v to hours
130         static int64_type in_days(clock_type v); ///< Convert \a v to days
131
132         static void restart(); ///< Force re-syncronisation of abstime and clock
133                                         /**< Calling the member should never be necessary since
134                                              abstime() / clock() automatically call restart() if
135                                              needed */
136
137     private:
138         ClockService();
139
140         abstime_type abstime_m(clock_type clock);
141         clock_type clock_m(abstime_type time);
142         void restart_m();
143
144         boost::posix_time::ptime baseAbstime_;
145         clock_type baseClock_;
146
147         /// Internal: ClockService private data (PIMPL idiom)
148
149 #ifndef DOXYGEN
150         friend class singleton<ClockService>;
151 #endif
152     };
153
154     /** \brief Console argument parser to parse value as time interval
155
156         This parser will parse a time interval specification into a ClockService::clock_type
157         value. The following units are supported:
158
159         <table class="senf fixedcolumn">
160         <tr><td>\c d</td><td>days</td></tr>
161         <tr><td>\c h</td><td>hours</td></tr>
162         <tr><td>\c m</td><td>minutes</td></tr>
163         <tr><td>\c s</td><td>seconds</td></tr>
164         </table>
165
166         Additionally, the unit may be prefixed by an SI scale:
167
168         <table class="senf fixedcolumn">
169         <tr><td>\c m</td><td>milli</td></tr>
170         <tr><td>\c u</td><td>micro</td></tr>
171         <tr><td>\c n</td><td>nano</td></tr>
172         </table>
173
174         An optional decimal point is also supported. A single timer interval may combine any number
175         of these specifications. The following are all valid intervals:
176
177         <table class="senf fixedcolumn">
178         <tr><td><code>10d</code></td><td>10 days</td></tr>
179         <tr><td><code>5d5d</code></td><td>10 days</td></tr>
180         <tr><td><code>1d2h100m3.5s</code></td><td>27 hours, 30 minutes and 3.5 seconds</td></tr>
181         <tr><td><code>1s100ms</code></td><td>1.1 seconds</td></tr>
182         <tr><td><code>1.1s</code></td><td>1.1 seconds</td></tr>
183         <tr><td><code>123.456us</code></td><td>123.456 microseconds</td></tr>
184         <tr><td><code>2md</code></td><td>(very unusual) 2 milli-days</td></tr>
185         </table>
186      */
187     void parseClockServiceInterval(console::ParseCommandInfo::TokensRange const & tokens, 
188                                    ClockService::clock_type & out);
189 }
190
191 ///////////////////////////////hh.e////////////////////////////////////////
192 #include "ClockService.cci"
193 //#include "ClockService.ct"
194 //#include "ClockService.cti"
195 #endif
196
197 \f
198 // Local Variables:
199 // mode: c++
200 // fill-column: 100
201 // comment-column: 40
202 // c-file-style: "senf"
203 // indent-tabs-mode: nil
204 // ispell-local-dictionary: "american"
205 // compile-command: "scons -u test"
206 // End: