Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / source / diameter.comm / Message.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 // Local
10 #include <anna/diameter.comm/Message.hpp>
11 #include <anna/diameter.comm/ClientSession.hpp>
12 #include <anna/diameter.comm/ServerSession.hpp>
13 #include <anna/diameter/codec/functions.hpp>
14 #include <anna/diameter/defines.hpp>
15
16 #include <anna/xml/Node.hpp>
17 #include <anna/core/functions.hpp>
18 #include <anna/core/RuntimeException.hpp>
19 #include <anna/comm/ClientSocket.hpp>
20 #include <anna/core/tracing/Logger.hpp>
21
22 using namespace std;
23 using namespace anna::diameter;
24 using namespace anna::diameter::comm;
25
26
27
28 const char* Message::asText(const OnExpiry::_v rc)
29 throw() {
30   static const char* text [] = { "Abandon", "Ignore" };
31   return text [rc];
32 }
33
34 string Message::asString() const
35 throw() {
36   string result("diameter::comm::Message { ");
37   result += "ClassCode: ";
38   result += ClassCode::asText(a_classCode);
39   result += " | OnExpiry: ";
40   result += asText(a_onExpiry);
41   result += " | Retries: ";
42   result += anna::functions::asString(a_retries);
43   return result += " }";
44 }
45
46 anna::xml::Node* Message::asXML(anna::xml::Node* parent) const
47 throw() {
48   anna::xml::Node* result = parent->createChild("diameter.comm.Message");
49   result->createAttribute("ClassCode", ClassCode::asText(a_classCode));
50   result->createAttribute("OnExpiry", asText(a_onExpiry));
51   result->createAttribute("Retries", anna::functions::asString(a_retries));
52   //isRequest ...
53   return result;
54 }
55
56 //void Message::clear ()
57 //   throw ()
58 //{
59 //   ::clear();
60 ////   a_classCode = ClassCode::Undefined;
61 ////   a_onExpiry = OnExpiry::Ignore;
62 //}
63
64 bool Message::fixRequestSequence(HopByHop hbh, EndToEnd ete, bool freezeEndToEnd) throw() {
65   setRequestHopByHop(getHopByHop()); // original request hop-by-hop (backup)
66   setRequestEndToEnd(getEndToEnd()); // original request end-to-end (backup)
67   bool result = false;
68
69   if(hbh != getRequestHopByHop()) {
70     codec::functions::setHopByHop((anna::DataBlock&)getBody(), hbh);
71     result = true;
72   }
73
74   if(!freezeEndToEnd) {
75     if(ete != getRequestEndToEnd()) {
76       codec::functions::setEndToEnd((anna::DataBlock&)getBody(), ete);
77       result = true;
78     }
79   }
80
81   LOGDEBUG(
82     string msg("diameter::comm::fixRequestSequence { ");
83     msg += "Hop by hop: ";
84     msg += anna::functions::asString(getRequestHopByHop());
85     msg += " (original) -> ";
86     msg += anna::functions::asString(hbh);
87     msg += " (session)";
88     msg += freezeEndToEnd ? " | End to end [freezed]: " : " | End to end: ";
89     msg += anna::functions::asString(getRequestEndToEnd());
90     msg += " (original) -> ";
91     msg += anna::functions::asString(ete);
92     msg += " (session)";
93     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
94   );
95   return result;
96 }
97
98
99
100 void Message::restoreSequencesAfterFix() throw() {
101   LOGDEBUG(
102     string msg("diameter::comm::restoreSequencesAfterFix { ");
103     msg += "Hop by hop: ";
104     msg += anna::functions::asString(getHopByHop());
105     msg += " (session) -> ";
106     msg += anna::functions::asString(getRequestHopByHop());
107     msg += " (original)";
108     msg += " | End to end: ";
109     msg += anna::functions::asString(getEndToEnd());
110     msg += " (session) -> ";
111     msg += anna::functions::asString(getRequestEndToEnd());
112     msg += " (original)";
113     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
114   );
115   diameter::codec::functions::setHopByHop((anna::DataBlock&)getBody(), a_requestHopByHop);
116   diameter::codec::functions::setEndToEnd((anna::DataBlock&)getBody(), a_requestEndToEnd);
117 }
118
119
120 void Message::send(ClientSession& clientSession) const
121 throw(anna::RuntimeException) {
122   try {
123     clientSession.getServer()->send((Message *)this);
124   } catch(anna::RuntimeException&) {
125     throw;
126   }
127 }
128
129 void Message::send(ServerSession& serverSession) const
130 throw(anna::RuntimeException) {
131   try {
132     serverSession.getClientSocket()->send((Message *)this);
133   } catch(anna::RuntimeException&) {
134     throw;
135   }
136 }
137
138
139
140 anna::diameter::CommandId Message::getCommandId(bool &isRequest) const throw() {
141   diameter::CommandId result = diameter::codec::functions::getCommandId(getBody());
142   isRequest = result.second; // diameter::codec::functions::isRequest(result);
143   return result;
144 }
145
146 HopByHop Message::getHopByHop() const throw() {
147   return (diameter::codec::functions::getHopByHop(getBody()));
148 }
149
150 EndToEnd Message::getEndToEnd() const throw() {
151   return (diameter::codec::functions::getEndToEnd(getBody()));
152 }