Updated license
[anna.git] / source / diameter.comm / Response.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 // Local
38 #include <anna/diameter.comm/Response.hpp>
39 #include <anna/diameter.comm/Message.hpp>
40 #include <anna/diameter.comm/TimerManager.hpp>
41 #include <anna/diameter/defines.hpp>
42 #include <anna/diameter/helpers/helpers.hpp>
43
44 #include <anna/core/functions.hpp>
45 #include <anna/core/tracing/Logger.hpp>
46
47
48
49 using namespace std;
50 using namespace anna;
51
52 diameter::comm::Response::response_pool diameter::comm::Response::st_responses;
53
54 //----------------------------------------------------------------------------------------
55 // Se invocan desde diameter::comm::Session
56 //----------------------------------------------------------------------------------------
57 diameter::comm::Response* diameter::comm::Response::instance(const ClassCode::_v & classCode, const HopByHop hopByHop)
58 throw(anna::RuntimeException) {
59   diameter::comm::Response* result = st_responses.create();
60   result->a_classCode = classCode;
61   result->a_hopByHop = hopByHop;
62   result->clear();
63   LOGDEBUG(
64     string msg("anna::diameter::comm::Response::instance | ");
65     msg += result->asString();
66     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
67   );
68   return result;
69 }
70
71 void diameter::comm::Response::release(diameter::comm::Response* response)
72 throw() {
73   try {
74     st_responses.release(response);
75   } catch(anna::RuntimeException& ex) {
76     ex.trace();
77   }
78 }
79
80 diameter::comm::Response::Response() :
81   a_classCode(ClassCode::Undefined),
82   a_hopByHop(0),
83   a_session(NULL),
84   a_timer(NULL),
85   a_request(NULL) {
86 }
87
88 void diameter::comm::Response::clear()
89 throw() {
90   a_resultCode = ResultCode::Undefined;
91   a_session = NULL;
92   a_timer = NULL;
93   a_request = NULL;
94 }
95
96 void diameter::comm::Response::activateTimer()
97 throw(anna::RuntimeException) {
98   a_timer = TimerManager::instantiate().createTimer(this);
99 }
100
101 void diameter::comm::Response::cancelTimer()
102 throw() {
103   if(a_timer != NULL) {
104     try {
105       TimerManager::instantiate().cancelTimer(a_timer);
106     } catch(anna::RuntimeException& ex) {
107       ex.trace();
108     }
109
110     a_timer = NULL;
111   }
112 }
113
114 const char* diameter::comm::Response::asText(const ResultCode::_v rc)
115 throw() {
116   static const char* text [] = { "Undefined", "Success", "Timeout", "DiameterUnavailable" };
117   return text [rc];
118 }
119
120 //bool diameter::comm::Response::isKeepAlive() const
121 //throw() {
122 //   if (a_request) {
123 //      if (a_request->getCommandId() == diameter::helpers::base::COMMANDID__Device_Watchdog_Request)
124 //         return true;
125 //   }
126 //
127 //   return false;
128 //}
129
130 string diameter::comm::Response::asString() const
131 throw() {
132   string result("diameter::comm::Response { ClassCode: ");
133   result += ClassCode::asText(a_classCode);
134   result += anna::functions::asString(" | HopByHop: %u", a_hopByHop);
135   result += " | ResultCode: ";
136   result += asText(a_resultCode);
137   return result += " }";
138 }
139