Remove dynamic exceptions
[anna.git] / source / ldap / TimerManager.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 <anna/core/tracing/Logger.hpp>
10
11 #include <anna/app/functions.hpp>
12
13 #include <anna/timex/Engine.hpp>
14
15 #include <anna/ldap/TimerManager.hpp>
16 #include <anna/ldap/internal/Timer.hpp>
17 #include <anna/ldap/Response.hpp>
18 #include <anna/ldap/Session.hpp>
19
20 using namespace std;
21 using namespace anna;
22
23 ldap::TimerManager::TimerManager() :
24   timex::TimeEventObserver("anna::ldap::TimerManager"),
25   a_timeController(NULL) {
26 }
27
28 //-------------------------------------------------------------------------------------------------------
29 // (1) Bloquea el TimerManager el primero para mantener siempre el mismo orden de acceso a la
30 // seccion critica, lo que evita interbloqueos.
31 //-------------------------------------------------------------------------------------------------------
32 ldap::Timer* ldap::TimerManager::createTimer(Response* response)
33 noexcept(false) {
34   Timer* result(NULL);
35
36   if(a_timeController == NULL)
37     a_timeController = app::functions::component <timex::Engine> (ANNA_FILE_LOCATION);
38
39   Guard guard(a_timeController, "anna::ldap::TimerManager::createTimer");              // (1)
40   result = a_timers.create();
41   const ClassCode::_v v = response->getClassCode();
42   result->setId((timex::TimeEvent::Id) response);
43   result->setObserver(this);
44   result->setResponse(response);
45   result->setTimeout(response->getSession()->getTimeout(v));
46   LOGDEBUG(
47     string msg("anna::ldap::TimerManager::createTimer | ");
48     msg += result->asString();
49     Logger::debug(msg, ANNA_FILE_LOCATION);
50   );
51   a_timeController->activate(result);
52   return result;
53 }
54
55 void ldap::TimerManager::cancel(ldap::Timer* timer)
56 {
57   if(timer == NULL)
58     return;
59
60   LOGDEBUG(
61     string msg("anna::ldap::TimerManager::cancel | ");
62     msg += timer->asString();
63     Logger::debug(msg, ANNA_FILE_LOCATION);
64   );
65
66   try {
67     if(a_timeController == NULL)
68       a_timeController = app::functions::component <timex::Engine> (ANNA_FILE_LOCATION);
69
70     a_timeController->cancel(timer);
71   } catch(RuntimeException& ex) {
72     ex.trace();
73   }
74 }
75
76 //------------------------------------------------------------------------------------------
77 // Se invoca automaticamente desde timex::Engine
78 //------------------------------------------------------------------------------------------
79 void ldap::TimerManager::release(timex::TimeEvent* timeEvent)
80 {
81   Timer* timer = static_cast <Timer*>(timeEvent);
82   timer->setResponse(NULL);
83   a_timers.release(timer);
84 }
85