Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
ctimer.h
1 /*
2  * ctimer.h
3  *
4  * Created on: 26.01.2014
5  * Author: andreas
6  */
7 
8 #ifndef CTIMER_H_
9 #define CTIMER_H_
10 
11 #include <sys/time.h>
12 #include <time.h>
13 
14 #include <iostream>
15 
16 #include "rofl/common/cmemory.h"
17 #include "rofl/common/logging.h"
18 #include "rofl/common/croflexception.h"
19 #include "rofl/common/ctimerid.h"
20 #include "rofl/common/ctimespec.h"
21 
22 namespace rofl {
23 
27 class ctimer_env {
28 public:
29  virtual
30  ~ctimer_env()
31  {};
32 };
33 
45 class ctimer {
46 public:
47 
53  static
55  now() {
56  ctimer timer;
57  timer.set_timespec() = ctimespec::now();
58  return timer;
59  };
60 
61 public:
62 
66  virtual
68  {};
69 
73  ctimer() :
74  timer_id(),
75  env(0),
76  timespec(0, 0),
77  opaque(0),
78  data(0)
79  {};
80 
90  ctimer_env* env,
91  int opaque,
92  const rofl::ctimespec& timespec,
93  void *data = (void*)0) :
94  env(env),
95  timespec(timespec),
96  opaque(opaque),
97  data(data)
98  { this->timespec += rofl::ctimespec::now(); };
99 
104  const rofl::ctimer& timer)
105  { *this = timer; };
106 
107 public:
108 
118  ctimer&
120  const rofl::ctimer& timer) {
121  if (this == &timer)
122  return *this;
123  timer_id = timer.timer_id;
124  env = timer.env;
125  timespec = timer.timespec;
126  opaque = timer.opaque;
127  data = timer.data;
128  return *this;
129  };
130 
131 #if 0
132 
136  operator+ (
137  const rofl::ctimer& t) {
138  ctimer timer;
139  timer.timespec = timespec + t.timespec;
140  return timer;
141  };
142 
147  operator- (
148  const rofl::ctimer& t) {
149  ctimer timer;
150  timer.timespec = timespec - t.timespec;
151  return timer;
152  };
153 
157  rofl::ctimer&
158  operator+= (
159  const rofl::ctimer& t) {
160  timespec += t.timespec;
161  return *this;
162  };
163 
167  rofl::ctimer&
168  operator-= (
169  const rofl::ctimer& t) {
170  timespec -= t.timespec;
171  return *this;
172  };
173 #endif
174 
178  bool
180  const rofl::ctimer& t) const
181  { return not ((*this < t) && (t < *this)); };
182 
186  bool
188  const rofl::ctimer& t) const
189  { return not (*this == t); };
190 
194  bool
196  const rofl::ctimer& t) const
197  { return (timespec < t.timespec); };
198 
202  bool
204  const rofl::ctimer& t) const
205  { return ((*this < t) || (*this == t)); };
206 
210  bool
212  const rofl::ctimer& t) const
213  { return not (*this <= t); };
214 
218  bool
220  const rofl::ctimer& t) const
221  { return ((*this > t) || (*this == t)); };
222 
225 public:
226 
236  ctimer_env*
238  { return env; };
239 
243  const rofl::ctimerid&
244  get_timer_id() const
245  { return timer_id; };
246 
250  int
251  get_opaque() const
252  { return opaque; };
253 
257  void*
258  get_data() const
259  { return data; };
260 
266  { return timespec; };
267 
271  const rofl::ctimespec&
272  get_timespec() const
273  { return timespec; };
274 
277 public:
278 
280  ctimerid timer_id;
281  public:
282  ctimer_find_by_timer_id(ctimerid timer_id) : timer_id(timer_id) {};
283  bool operator() (const rofl::ctimer& t) {
284  return (timer_id == t.get_timer_id());
285  };
286  bool operator() (ctimer const* t) {
287  return (timer_id == t->get_timer_id());
288  };
289  };
290 
291 public:
292 
293  friend std::ostream&
294  operator<< (std::ostream& os, const rofl::ctimer& timer) {
295  ctimer delta = ctimer(timer);
296  os << indent(0) << "<ctimer ";
297  os << "opaque:" << timer.opaque << " ";
298  os << "data:" << timer.data << " ";
299  os << ">" << std::endl;
300  rofl::indent i(2);
301  os << timer.timer_id;
302  os << timer.timespec;
303  return os;
304  };
305 
306 private:
307 
308  static const long CC_TIMER_ONE_SECOND_S = 1;
309  static const long CC_TIMER_ONE_SECOND_NS = 1e9;
310 
311  rofl::ctimerid timer_id;
312  rofl::ctimer_env* env; // this refers to the ciosrv instance that registered the timer associated with this ctimer instance
313  rofl::ctimespec timespec;
314  int opaque; // can be used as type field by a class deriving from ciosrv => not used by ctimer, ciosrv or cioloop
315  void* data; // can be used as arbitrary pointer by a class deriving from ciosrv => not used by ctimer, ciosrv or cioloop
316 };
317 
318 };
319 
320 #endif /* CTIMER_H_ */
rofl::ctimespec & set_timespec()
Returns reference to rofl::ctimespec instance assigned to this timer.
Definition: ctimer.h:265
ctimer()
ctimer default constructor
Definition: ctimer.h:73
Timer handle used by class rofl::cioloop.
Definition: ctimerid.h:21
virtual ~ctimer()
ctimer destructor
Definition: ctimer.h:67
Single timer object in rofl-common.
Definition: ctimer.h:45
int get_opaque() const
Returns opaque timer type chosen by user.
Definition: ctimer.h:251
Environment expected by an instance of class rofl::ctimer.
Definition: ctimer.h:27
bool operator>(const rofl::ctimer &t) const
Greater operator.
Definition: ctimer.h:211
ctimer_env * get_timer_env() const
Returns pointer to environment of this rofl::ctimer instance.
Definition: ctimer.h:237
bool operator<=(const rofl::ctimer &t) const
Lesser-or-equal operator.
Definition: ctimer.h:203
bool operator!=(const rofl::ctimer &t) const
Inequality operator.
Definition: ctimer.h:187
ctimer(ctimer_env *env, int opaque, const rofl::ctimespec &timespec, void *data=(void *) 0)
ctimer constructor
Definition: ctimer.h:89
ctimer(const rofl::ctimer &timer)
ctimer copy constructor
Definition: ctimer.h:103
ctimer & operator=(const rofl::ctimer &timer)
Assignment operator.
Definition: ctimer.h:119
bool operator<(const rofl::ctimer &t) const
Lesser operator.
Definition: ctimer.h:195
bool operator==(const rofl::ctimer &t) const
Equality operator.
Definition: ctimer.h:179
static rofl::ctimer now()
Returns a rofl::ctimer object with internal timespec set to current time.
Definition: ctimer.h:55
Definition: logging.h:76
void * get_data() const
Returns pointer to opaque data segment chosen by user.
Definition: ctimer.h:258
Time specification used by class rofl::ctimer object.
Definition: ctimespec.h:25
bool operator>=(const rofl::ctimer &t) const
Greater-or-equal operator.
Definition: ctimer.h:219
const rofl::ctimerid & get_timer_id() const
Returns const reference to rofl-common's internal handle assigned to this timer.
Definition: ctimer.h:244
const rofl::ctimespec & get_timespec() const
Returns const reference to rofl::ctimespec instance assigned to this timer.
Definition: ctimer.h:272