00001 // $Id: ClockService.hh 1742 2010-11-04 14:51:56Z g0dil $ 00002 // 00003 // Copyright (C) 2007 00004 // Fraunhofer (FOKUS) 00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY 00006 // Stefan Bund <g0dil@berlios.de> 00007 // 00008 // This program is free software; you can redistribute it and/or modify 00009 // it under the terms of the GNU General Public License as published by 00010 // the Free Software Foundation; either version 2 of the License, or 00011 // (at your option) any later version. 00012 // 00013 // This program is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License 00019 // along with this program; if not, write to the 00020 // Free Software Foundation, Inc., 00021 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 00026 #ifndef HH_SENF_Scheduler_ClockService_ 00027 #define HH_SENF_Scheduler_ClockService_ 1 00028 00029 // Custom includes 00030 #include <sys/time.h> 00031 #include <boost/utility.hpp> 00032 #include <boost/date_time/posix_time/posix_time.hpp> 00033 #include <boost/scoped_ptr.hpp> 00034 #include <boost/cstdint.hpp> 00035 #include <senf/config.hh> 00036 #include <senf/Utils/singleton.hh> 00037 #include <senf/Utils/Console/Parse.hh> 00038 00039 //#include "ClockService.mpp" 00040 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00041 00042 namespace senf { 00043 00044 #ifndef DOXYGEN 00045 namespace detail { class ClockServiceTest; } 00046 #endif 00047 00048 // Implementation note: The clock value is represented as a 64bit unsigned integer number of 00049 // nanoseconds based on the CLOCK_MONOTONIC POSIX clock. 00050 // 00051 // To allow conversion between clock value and absolute time, the ClockService samples the 00052 // absolute current time and the clock value when the conversion is performed. This is done at 00053 // most once per second on a if-needed basis. 00054 00064 class ClockService 00065 : singleton<ClockService> 00066 { 00067 public: 00068 //-//////////////////////////////////////////////////////////////////////// 00069 // Types 00070 00076 typedef config::time_type clock_type; 00077 00082 typedef boost::int_fast64_t int64_type; 00083 00089 typedef boost::posix_time::ptime abstime_type; 00090 00096 typedef boost::posix_time::time_duration reltime_type; 00097 00098 //-//////////////////////////////////////////////////////////////////////// 00099 00100 static clock_type now(); 00101 00102 static abstime_type abstime(clock_type clock); 00103 00111 static reltime_type reltime(clock_type clock); 00112 00118 static clock_type clock(abstime_type time); 00119 00123 static clock_type from_time_t(time_t const & time); 00125 00128 static clock_type from_timeval(timeval const & time); 00130 00133 static clock_type nanoseconds(int64_type v); 00134 static clock_type microseconds(int64_type v); 00135 static clock_type milliseconds(int64_type v); 00136 static clock_type seconds(int64_type v); 00137 static clock_type minutes(int64_type v); 00138 static clock_type hours(int64_type v); 00139 static clock_type days(int64_type v); 00140 00141 static int64_type in_nanoseconds(clock_type v); 00142 static int64_type in_microseconds(clock_type v); 00143 static int64_type in_milliseconds(clock_type v); 00144 static int64_type in_seconds(clock_type v); 00145 static int64_type in_minutes(clock_type v); 00146 static int64_type in_hours(clock_type v); 00147 static int64_type in_days(clock_type v); 00148 00149 static void restart(); 00150 00154 private: 00155 ClockService(); 00156 00157 abstime_type abstime_m(clock_type clock); 00158 clock_type clock_m(abstime_type time); 00159 void restart_m(); 00160 00161 boost::posix_time::ptime baseAbstime_; 00162 clock_type baseClock_; 00163 00165 00166 #ifndef DOXYGEN 00167 friend class singleton<ClockService>; 00168 #endif 00169 }; 00170 00204 void parseClockServiceInterval(console::ParseCommandInfo::TokensRange const & tokens, 00205 ClockService::clock_type & out); 00206 00207 void formatClockServiceInterval(ClockService::clock_type interval, std::ostream & os); 00208 } 00209 00210 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00211 #include "ClockService.cci" 00212 //#include "ClockService.ct" 00213 //#include "ClockService.cti" 00214 #endif 00215 00216 00217 // Local Variables: 00218 // mode: c++ 00219 // fill-column: 100 00220 // comment-column: 40 00221 // c-file-style: "senf" 00222 // indent-tabs-mode: nil 00223 // ispell-local-dictionary: "american" 00224 // compile-command: "scons -u test" 00225 // End: