Updated license
[anna.git] / source / ldap / TimerManager.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <anna/core/tracing/Logger.hpp>
38
39 #include <anna/app/functions.hpp>
40
41 #include <anna/timex/Engine.hpp>
42
43 #include <anna/ldap/TimerManager.hpp>
44 #include <anna/ldap/internal/Timer.hpp>
45 #include <anna/ldap/Response.hpp>
46 #include <anna/ldap/Session.hpp>
47
48 using namespace std;
49 using namespace anna;
50
51 ldap::TimerManager::TimerManager() :
52   timex::TimeEventObserver("anna::ldap::TimerManager"),
53   a_timeController(NULL) {
54 }
55
56 //-------------------------------------------------------------------------------------------------------
57 // (1) Bloquea el TimerManager el primero para mantener siempre el mismo orden de acceso a la
58 // seccion critica, lo que evita interbloqueos.
59 //-------------------------------------------------------------------------------------------------------
60 ldap::Timer* ldap::TimerManager::createTimer(Response* response)
61 throw(RuntimeException) {
62   Timer* result(NULL);
63
64   if(a_timeController == NULL)
65     a_timeController = app::functions::component <timex::Engine> (ANNA_FILE_LOCATION);
66
67   Guard guard(a_timeController, "anna::ldap::TimerManager::createTimer");              // (1)
68   result = a_timers.create();
69   const ClassCode::_v v = response->getClassCode();
70   result->setId((timex::TimeEvent::Id) response);
71   result->setObserver(this);
72   result->setResponse(response);
73   result->setTimeout(response->getSession()->getTimeout(v));
74   LOGDEBUG(
75     string msg("anna::ldap::TimerManager::createTimer | ");
76     msg += result->asString();
77     Logger::debug(msg, ANNA_FILE_LOCATION);
78   );
79   a_timeController->activate(result);
80   return result;
81 }
82
83 void ldap::TimerManager::cancel(ldap::Timer* timer)
84 throw() {
85   if(timer == NULL)
86     return;
87
88   LOGDEBUG(
89     string msg("anna::ldap::TimerManager::cancel | ");
90     msg += timer->asString();
91     Logger::debug(msg, ANNA_FILE_LOCATION);
92   );
93
94   try {
95     if(a_timeController == NULL)
96       a_timeController = app::functions::component <timex::Engine> (ANNA_FILE_LOCATION);
97
98     a_timeController->cancel(timer);
99   } catch(RuntimeException& ex) {
100     ex.trace();
101   }
102 }
103
104 //------------------------------------------------------------------------------------------
105 // Se invoca automaticamente desde timex::Engine
106 //------------------------------------------------------------------------------------------
107 void ldap::TimerManager::release(timex::TimeEvent* timeEvent)
108 throw() {
109   Timer* timer = static_cast <Timer*>(timeEvent);
110   timer->setResponse(NULL);
111   a_timers.release(timer);
112 }
113