Remove dynamic exceptions
[anna.git] / source / timex / internal / TickProducer.cpp
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 #include <unistd.h>
10 #include <signal.h>
11
12 #include <anna/core/functions.hpp>
13 #include <anna/core/tracing/Logger.hpp>
14
15 #include <anna/app/functions.hpp>
16 #include <anna/app/Application.hpp>
17
18 #include <anna/comm/Communicator.hpp>
19
20 #include <anna/timex/internal/TickProducer.hpp>
21 #include <anna/timex/Engine.hpp>
22
23 using namespace std;
24 using namespace anna;
25
26 //static
27 void* anna::timex::TickProducer::exec(void* arg)
28 {
29   sigset_t mask;
30   sigemptyset(&mask);
31   pthread_sigmask(SIG_SETMASK, &mask, NULL);
32   timex::TickProducer* _this = reinterpret_cast <timex::TickProducer*>(arg);
33
34   try {
35     comm::Communicator* communicator = app::functions::component <comm::Communicator> (ANNA_FILE_LOCATION);
36
37     while(communicator->isServing() == false)
38       functions::sleep(Millisecond(100));
39
40     while(_this->a_requestedStop == false) {
41       functions::sleep(_this->calculeSlice(functions::millisecond()));
42       _this->tick();
43     }
44   } catch(RuntimeException& ex) {
45     Logger::write(Logger::Critical, ex.getText(), ANNA_FILE_LOCATION);
46     exit(-1);
47   }
48
49   return NULL;
50 }
51
52 anna::timex::TickProducer::TickProducer(timex::Engine* timeController, const int fdWrite) :
53   a_timeController(*timeController),
54   a_fdWrite(fdWrite),
55   a_expectedTime(0),
56   a_isInPause(false),
57   a_requestedStop(false)
58 {;}
59
60 void anna::timex::TickProducer::tick()
61 {
62   static char onebyte = 0xff;
63   static int errorCount = 0;
64
65   if(a_isInPause == true)
66     return;
67
68   if(write(a_fdWrite, &onebyte, sizeof(onebyte)) < 0) {
69     Logger::write(Logger::Critical, "Cannot generate tick", strerror(errno), ANNA_FILE_LOCATION);
70
71     if(++ errorCount == 10) {
72       Logger::write(Logger::Critical, "Terminating application due to error at tick generation", ANNA_FILE_LOCATION);
73       app::functions::getApp().eventAbnormalTermination("timex::TickProducer");
74       _exit(-1);
75     }
76   }
77 }
78
79 //--------------------------------------------------------------------------------------------
80 // Calcula la duracion del proximo tick de reloj. Tiene en cuenta la deriva del anterior
81 // tick y la corrige en este.
82 // (2) Si es mayor que cero => El reloj del sistema se adelanta
83 //     Si es menor que cero => El reloj del sistema se atrasa.
84 // (3) Si la correcin aplicada es mayor del 5% de la resolucin volvemos a calcularla ya
85 //     que podemos considerar que normalmente no desviar�tanto tiempo.
86 //--------------------------------------------------------------------------------------------
87 Millisecond anna::timex::TickProducer::calculeSlice(const Millisecond & msnow)
88 {
89   Millisecond result(a_timeController.getResolution());
90   int correction = (a_expectedTime == 0) ? 0 : (a_expectedTime - msnow);           // (2)
91
92   if(correction != 0) {
93     if(((abs(correction) * 100) / result) > 5)                                       // (3)
94       correction = 0;
95
96     result += Millisecond(correction);
97   }
98
99   a_expectedTime = msnow + result;
100   return result;
101 }
102
103