ANNA Suite  2020b
Multipurpose development suite for Telco applications
Engine.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_timex_Engine_hpp
10 #define anna_timex_Engine_hpp
11 
12 #include <pthread.h>
13 
14 #include <vector>
15 #include <map>
16 
17 #include <anna/app/Component.hpp>
18 
19 #include <anna/timex/TimeEvent.hpp>
20 
21 namespace anna {
22 
23 class Thread;
24 
25 namespace timex {
26 
27 class TickProducer;
28 class TickConsumer;
29 
30 namespace st {
31 class TickProducer;
32 }
33 
34 namespace mt {
35 class TickProducer;
36 }
37 
62 class Engine : public app::Component {
63 public:
67  static const Millisecond minResolution;
68 
78  Engine(const Millisecond & maxTimeout, const Millisecond & resolution);
79 
83  virtual ~Engine();
84 
89  const Millisecond & getResolution() const { return a_resolution; }
90 
95  const Millisecond & getMaxTimeout() const { return a_maxTimeout; }
96 
103  void pause() noexcept(false);
104 
108  void play() noexcept(false);
109 
117  void activate(TimeEvent* timeEvent) noexcept(false);
118 
127  void activate(TimeEvent& timeEvent) noexcept(false) { activate(&timeEvent); }
128 
136  void cancel(TimeEvent* timeEvent) noexcept(false);
137 
144  void cancel(TimeEvent& timeEvent) noexcept(false) { cancel(&timeEvent); }
145 
154  TimeEvent* getTimeEvent(const TimeEvent::Id eventTimeId) ;
155 
160  virtual void eventBeginQuantum() noexcept(false) {;}
161 
168  virtual void eventEndQuantum(const int quantumSize) noexcept(false) {;}
169 
174  virtual std::string asString() const ;
175 
181  virtual xml::Node* asXML(xml::Node* parent) const ;
182 
188  static const char* getClassName() { return "anna::timex::Engine"; }
189 
190 private:
191  typedef std::vector <TimeEvent*> Quantum;
192  typedef Quantum::iterator quantum_iterator;
193  typedef std::map <TimeEvent::Id, Quantum*> Directory;
194 
195  Quantum* a_timeTable;
196  Engine::Directory a_directory;
197  int a_currentQuantum;
198  int a_maxQuantum;
199  Millisecond a_resolution;
200  Millisecond a_maxTimeout;
201  bool a_isActive;
202  TickProducer* a_tickProducer;
203  TickConsumer* a_tickConsumer;
204  Thread* a_thread;
205  Quantum* a_expiredQuantum;
206  Quantum a_delayedQuantum;
207  pthread_t a_threadProducer;
208 
209  void do_initialize() noexcept(false);
210  void do_stop() ;
211  void kill() ;
212 
213  void tick() noexcept(false);
214  int getQuantum(const int timeout) const { return (a_currentQuantum + (timeout / a_resolution)) % a_maxQuantum; }
215  void calibrate() ;
216 
217  // Reimplementado de app::Component
218  void do_cloneParent() ;
219  void do_cloneChild() noexcept(false);
220 
221  static void notifyRelease(TimeEvent* timeEvent) ;
222  static TimeEvent* timeEvent(quantum_iterator& ii) { return *ii; }
223 
224  friend class TickProducer;
225  friend class TickConsumer;
226  friend class st::TickProducer;
227  friend class mt::TickProducer;
228 };
229 
230 }
231 }
232 
233 #endif
234 
235 
anna::ptrnumber Id
Definition: TimeEvent.hpp:34
Definition: Thread.hpp:31
const Millisecond & getMaxTimeout() const
Definition: Engine.hpp:95
Definition: Millisecond.hpp:24
static const Millisecond minResolution
Definition: Engine.hpp:67
virtual void eventEndQuantum(const int quantumSize) noexcept(false)
Definition: Engine.hpp:168
Definition: Node.hpp:56
Definition: TickProducer.hpp:24
Definition: TimeEvent.hpp:26
void cancel(TimeEvent &timeEvent) noexcept(false)
Definition: Engine.hpp:144
void activate(TimeEvent &timeEvent) noexcept(false)
Definition: Engine.hpp:127
const Millisecond & getResolution() const
Definition: Engine.hpp:89
virtual void eventBeginQuantum() noexcept(false)
Definition: Engine.hpp:160
Definition: app.hpp:12
Definition: Component.hpp:44
Definition: Engine.hpp:62
Definition: TickConsumer.hpp:25
static const char * getClassName()
Definition: Engine.hpp:188