Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
ctimers.h
1 /*
2  * ctimers.h
3  *
4  * Created on: 26.01.2014
5  * Author: andreas
6  */
7 
8 #ifndef CTIMERS_H_
9 #define CTIMERS_H_
10 
11 #include <set>
12 #include <algorithm>
13 
14 #include "rofl/common/ctimer.h"
15 #include "rofl/common/logging.h"
16 #include "rofl/common/thread_helper.h"
17 #include "rofl/common/croflexception.h"
18 
19 namespace rofl {
20 
21 class eTimersBase : public RoflException {};
22 class eTimersNotFound : public eTimersBase {};
23 
29 class ctimers {
30 public:
31 
35  virtual
37  {};
38 
43  {};
44 
49  const rofl::ctimers& timers)
50  { *this = timers; };
51 
52 public:
53 
63  ctimers&
65  const rofl::ctimers& t) {
66  if (this == &t)
67  return *this;
68  RwLock lock(rwlock, RwLock::RWLOCK_WRITE);
69  timers.clear();
70  timers.insert(t.timers.begin(), t.timers.end());
71  return *this;
72  };
73 
76 public:
77 
87  bool
88  empty() {
89  RwLock lock(rwlock, RwLock::RWLOCK_READ);
90  return (timers.empty());
91  };
92 
100  RwLock lock(rwlock, RwLock::RWLOCK_WRITE);
101  if (timers.empty())
102  throw eTimersNotFound();
103  std::multiset<ctimer>::iterator first = timers.begin();
104  ctimer timer(*first);
105  return timer;
106  };
107 
113  const rofl::ctimerid&
115  const rofl::ctimer& t) {
116  RwLock lock(rwlock, RwLock::RWLOCK_WRITE);
117  std::multiset<ctimer>::iterator it = timers.insert(t);
118  return it->get_timer_id();
119  };
120 
133  const rofl::ctimerid&
135  const rofl::ctimerid& timer_id,
136  const rofl::ctimespec& timespec) {
137  RwLock lock(rwlock, RwLock::RWLOCK_WRITE);
138  std::multiset<ctimer>::iterator it;
139  if ((it = find_if(timers.begin(), timers.end(), ctimer::ctimer_find_by_timer_id(timer_id))) == timers.end()) {
140  throw eTimersNotFound();
141  }
142  const ctimerid& timerid = timers.insert(ctimer(it->get_timer_env(), it->get_opaque(), timespec))->get_timer_id();
143  timers.erase(it);
144  return timerid;
145  };
146 
158  RwLock lock(rwlock, RwLock::RWLOCK_WRITE);
159  if (timers.empty()) {
160  throw eTimersNotFound();
161  }
162  ctimer now = ctimer::now();
163 
164  std::multiset<ctimer>::iterator it = timers.begin();
165 
166  ctimer timer = *(it);
167 
168  if (timer > now) {
169  throw eTimersNotFound();
170  }
171 
172  timers.erase(it);
173 
174  return timer;
175  };
176 
183  bool
185  const rofl::ctimerid& timer_id) {
186  RwLock lock(rwlock, RwLock::RWLOCK_READ);
187  std::multiset<ctimer>::iterator it;
188  if ((it = find_if(timers.begin(), timers.end(), ctimer::ctimer_find_by_timer_id(timer_id))) == timers.end()) {
189  return false;
190  }
191  return true;
192  };
193 
199  void
201  const ctimerid& timer_id) {
202  RwLock lock(rwlock, RwLock::RWLOCK_WRITE);
203  std::multiset<ctimer>::iterator it;
204  if ((it = find_if(timers.begin(), timers.end(), ctimer::ctimer_find_by_timer_id(timer_id))) == timers.end()) {
205  return;
206  }
207  timers.erase(it);
208  };
209 
213  void
214  clear() {
215  RwLock lock(rwlock, RwLock::RWLOCK_WRITE);
216  timers.clear();
217  };
218 
221 public:
222 
223  friend std::ostream&
224  operator<< (std::ostream& os, const rofl::ctimers& t) {
225  os << indent(0) << "<ctimers: >" << std::endl;
226  indent i(2);
227  for (std::multiset<ctimer>::const_iterator it = t.timers.begin(); it != t.timers.end(); ++it) {
228  os << (*it);
229  }
230  return os;
231  };
232 
233 private:
234 
235  std::multiset<rofl::ctimer> timers;
236  PthreadRwLock rwlock;
237 };
238 
239 };
240 
241 #endif /* CTIMERS_H_ */
const rofl::ctimerid & reset(const rofl::ctimerid &timer_id, const rofl::ctimespec &timespec)
Resets an existing timer identifier by its handle with a new timeout value.
Definition: ctimers.h:134
ctimers(const rofl::ctimers &timers)
ctimers copy constructor
Definition: ctimers.h:48
void clear()
Removes all timers from this timer list.
Definition: ctimers.h:214
Timer handle used by class rofl::cioloop.
Definition: ctimerid.h:21
Definition: ctimers.h:22
List of timers objects of class rofl::ctimer.
Definition: ctimers.h:29
ctimers()
ctimers default constructor
Definition: ctimers.h:42
Single timer object in rofl-common.
Definition: ctimer.h:45
void cancel(const ctimerid &timer_id)
Removes a timer identified by the given timer handle from this timer list.
Definition: ctimers.h:200
rofl::ctimer get_expired_timer()
Returns the next timer from timer list, when it has already expired.
Definition: ctimers.h:157
bool empty()
Deletes all timers in this timer list.
Definition: ctimers.h:88
const rofl::ctimerid & add_timer(const rofl::ctimer &t)
Inserts a new timer into the timer list.
Definition: ctimers.h:114
bool pending(const rofl::ctimerid &timer_id)
Checks whether a certain timer identified by the given handle is still pending.
Definition: ctimers.h:184
Definition: thread_helper.h:88
virtual ~ctimers()
ctimers destructor
Definition: ctimers.h:36
ctimers & operator=(const rofl::ctimers &t)
Assignment operator.
Definition: ctimers.h:64
static rofl::ctimer now()
Returns a rofl::ctimer object with internal timespec set to current time.
Definition: ctimer.h:55
Definition: logging.h:76
Time specification used by class rofl::ctimer object.
Definition: ctimespec.h:25
rofl::ctimer get_next_timer()
Returns a copy of the next expiring timer in this timer list.
Definition: ctimers.h:99
Definition: ctimers.h:21
Definition: croflexception.h:27