IBR-DTNSuite  0.12
Clock.h
Go to the documentation of this file.
1 /*
2  * Clock.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef CLOCK_H_
23 #define CLOCK_H_
24 
25 #include <sys/time.h>
26 
27 #include "ibrdtn/data/Number.h"
28 #include "ibrdtn/data/Bundle.h"
29 #include "ibrdtn/data/BundleID.h"
30 
32 
33 #ifdef __WIN32__
34 
37 #ifndef timerclear
38 #define timerclear(a) \
39  (a)->tv_set = 0; (a)->tv_usec = 0
40 #endif
41 
42 #ifndef timeradd
43 #define timeradd(a, b, result) \
44  do { \
45  (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
46  (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
47  if ((result)->tv_usec >= 1000000L) { \
48  ++(result)->tv_sec; \
49  (result)->tv_usec -= 1000000L; \
50  } \
51  } while (0)
52 #endif
53 
54 #ifndef timersub
55 #define timersub(a, b, result) \
56  do { \
57  (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
58  (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
59  if ((result)->tv_usec < 0) { \
60  --(result)->tv_sec; \
61  (result)->tv_usec += 1000000L; \
62  } \
63  } while (0)
64 #endif
65 #endif
66 
67 namespace dtn
68 {
69  namespace utils
70  {
71  class Clock
72  {
73  public:
79 
84 
89  static bool isExpired(const dtn::data::Timestamp &timestamp, const dtn::data::Number &lifetime);
90 
95  static bool isExpired(const dtn::data::Bundle &b);
96 
101  static bool isExpired(const dtn::data::MetaBundle &m);
102 
107 
112  static dtn::data::Timestamp getExpireTime(const dtn::data::Timestamp &timestamp, const dtn::data::Number &lifetime) __attribute__ ((deprecated));
113 
120  static dtn::data::Timestamp getExpireTime(const dtn::data::Number &lifetime);
121 
125  static void settimeofday(struct timeval *tv);
126 
132  static void gettimeofday(struct timeval *tv);
133 
140  static void getdtntimeofday(struct timeval *tv);
141 
146  static void setOffset(const struct timeval &tv);
147 
152  static const struct timeval& getOffset();
153 
155 
161  static double getRating();
162 
166  static void setRating(double val);
167 
172  static bool shouldModifyClock();
173 
177  static void setModifyClock(bool val);
178 
182  static double toDouble(const timeval &val);
183 
188 
189  private:
195  static double _rating;
196 
201  static bool _modify_clock;
202 
203  Clock();
204  virtual ~Clock();
205 
206  static dtn::data::Timestamp __getExpireTime(const dtn::data::Timestamp &timestamp, const dtn::data::Number &lifetime);
207 
208  static struct timeval _offset;
209  static bool _offset_init;
210 
211  static ibrcommon::MonotonicClock _monotonic_clock;
212  };
213  }
214 }
215 
216 #endif /* CLOCK_H_ */