Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TimeoutManager.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * TimeoutManager.h
17  * Manages timeout events.
18  * Copyright (C) 2013 Simon Newton
19  */
20 
21 #ifndef COMMON_IO_TIMEOUTMANAGER_H_
22 #define COMMON_IO_TIMEOUTMANAGER_H_
23 
24 #include <queue>
25 #include <set>
26 #include <vector>
27 
28 #include "ola/Callback.h"
29 #include "ola/Clock.h"
30 #include "ola/ExportMap.h"
31 #include "ola/base/Macro.h"
32 #include "ola/thread/SchedulerInterface.h"
33 
34 namespace ola {
35 namespace io {
36 
37 
46  public :
52  TimeoutManager(ola::ExportMap *export_map, Clock *clock);
53 
54  ~TimeoutManager();
55 
66  const ola::TimeInterval &interval,
67  ola::Callback0<bool> *closure);
68 
77  const ola::TimeInterval &interval,
79 
85 
91  bool EventsPending() const {
92  return !m_events.empty();
93  }
94 
102 
103  static const char K_TIMER_VAR[];
104 
105  private :
106  class Event {
107  public:
108  explicit Event(const TimeInterval &interval, const Clock *clock)
109  : m_interval(interval) {
110  TimeStamp now;
111  clock->CurrentTime(&now);
112  m_next = now + m_interval;
113  }
114  virtual ~Event() {}
115  virtual bool Trigger() = 0;
116 
117  void UpdateTime(const TimeStamp &now) {
118  m_next = now + m_interval;
119  }
120 
121  TimeStamp NextTime() const { return m_next; }
122 
123  private:
124  TimeInterval m_interval;
125  TimeStamp m_next;
126  };
127 
128  // An event that only happens once
129  class SingleEvent: public Event {
130  public:
131  SingleEvent(const TimeInterval &interval,
132  const Clock *clock,
133  ola::BaseCallback0<void> *closure):
134  Event(interval, clock),
135  m_closure(closure) {
136  }
137 
138  virtual ~SingleEvent() {
139  if (m_closure)
140  delete m_closure;
141  }
142 
143  bool Trigger() {
144  if (m_closure) {
145  m_closure->Run();
146  // it's deleted itself at this point
147  m_closure = NULL;
148  }
149  return false;
150  }
151 
152  private:
153  ola::BaseCallback0<void> *m_closure;
154  };
155 
156  /*
157  * An event that occurs more than once. The closure can return false to
158  * indicate that it should not be called again.
159  */
160  class RepeatingEvent: public Event {
161  public:
162  RepeatingEvent(const TimeInterval &interval,
163  const Clock *clock,
164  ola::BaseCallback0<bool> *closure):
165  Event(interval, clock),
166  m_closure(closure) {
167  }
168  ~RepeatingEvent() {
169  delete m_closure;
170  }
171  bool Trigger() {
172  if (!m_closure)
173  return false;
174  return m_closure->Run();
175  }
176 
177  private:
178  ola::BaseCallback0<bool> *m_closure;
179  };
180 
181  struct ltevent {
182  bool operator()(Event *e1, Event *e2) const {
183  return e1->NextTime() > e2->NextTime();
184  }
185  };
186 
187  typedef std::priority_queue<Event*, std::vector<Event*>, ltevent>
188  event_queue_t;
189 
190  ola::ExportMap *m_export_map;
191  Clock *m_clock;
192 
193  event_queue_t m_events;
194  std::set<ola::thread::timeout_id> m_removed_timeouts;
195 
196  DISALLOW_COPY_AND_ASSIGN(TimeoutManager);
197 };
198 } // namespace io
199 } // namespace ola
200 #endif // COMMON_IO_TIMEOUTMANAGER_H_
Definition: Clock.h:118
A 0 arg, single use callback that returns void.
Definition: Callback.h:157
Manages timer events.
Definition: TimeoutManager.h:45
A container for the exported variables.
Definition: ExportMap.h:324
A 0 argument callback which can be called multiple times.
Definition: Callback.h:129
Export variables on the http server.
void * timeout_id
A timeout handle which can later be used to cancel a timeout.
Definition: SchedulerInterface.h:34
ola::thread::timeout_id RegisterRepeatingTimeout(const ola::TimeInterval &interval, ola::Callback0< bool > *closure)
Register a repeating timeout. Returning false from the Callback will cancel this timer.
Definition: TimeoutManager.cpp:57
Used to get the current time.
Definition: Clock.h:221
Helper macros.
ola::thread::timeout_id RegisterSingleTimeout(const ola::TimeInterval &interval, ola::SingleUseCallback0< void > *closure)
Register a single use timeout function.
Definition: TimeoutManager.cpp:71
void CancelTimeout(ola::thread::timeout_id id)
Cancel a timeout.
Definition: TimeoutManager.cpp:85
TimeInterval ExecuteTimeouts(TimeStamp *now)
Execute any expired timeouts.
Definition: TimeoutManager.cpp:95
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
bool EventsPending() const
Check if there are any events in the queue. Events remain in the queue even if they have been cancell...
Definition: TimeoutManager.h:91
TimeoutManager(ola::ExportMap *export_map, Clock *clock)
Create a new TimeoutManager.
Definition: TimeoutManager.cpp:39
Definition: Clock.h:171