ADML is not really an AOTS deployment but an simple ADML one
[anna.git] / example / diameter / rxSimpleTest / MyLocalServer.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 // Project
10 #include <anna/core/core.hpp>
11 #include <anna/diameter/functions.hpp>
12 #include <anna/time/functions.hpp>
13 #include <anna/diameter/codec/Engine.hpp>
14 #include <anna/diameter.comm/Response.hpp>
15 #include <anna/diameter.comm/ClientSession.hpp>
16 #include <anna/diameter.comm/Server.hpp>
17 #include <anna/diameter.comm/OriginHost.hpp>
18
19 // Process
20 #include <MyLocalServer.hpp>
21 #include <MyDiameterEngine.hpp>
22 #include <MyDiameterEntity.hpp>
23 #include <rxSimpleTest.hpp>
24
25
26 void MyLocalServer::eventRequest(anna::diameter::comm::ServerSession *serverSession, const anna::DataBlock &message)
27 throw(anna::RuntimeException) {
28   LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyLocalServer", "eventRequest", ANNA_FILE_LOCATION));
29   // Performance stats:
30   RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
31   anna::diameter::comm::OriginHost *my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
32   anna::diameter::codec::Engine *codecEngine = my_node->getCodecEngine();
33
34   // CommandId:
35   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
36   LOGDEBUG
37   (
38     std::string msg = "Request received: ";
39     msg += anna::diameter::functions::commandIdAsPairString(cid);
40     msg += " | DiameterServer: ";
41     msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
42     msg += " | EventTime: ";
43     msg += anna::time::functions::currentTimeAsString();
44     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
45   );
46
47   anna::diameter::codec::Message codecMsg;
48   anna::diameter::codec::Message codecAnsMsg;
49   anna::diameter::codec::Message *answer_message = (anna::diameter::codec::Message *)&codecAnsMsg;
50   my_app.getCommunicator()->prepareAnswer(answer_message, message);
51
52   anna::diameter::comm::Message *msg;
53   try {
54     msg = my_node->createCommMessage();
55     msg->setBody(answer_message->code());
56     /* response = NULL =*/serverSession->send(msg);
57   } catch(anna::RuntimeException &ex) {
58     ex.trace();
59   }
60
61   // release msg
62   my_node->releaseCommMessage(msg);
63 }
64
65 void MyLocalServer::eventResponse(const anna::diameter::comm::Response &response)
66 throw(anna::RuntimeException) {
67   LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyLocalServer", "eventResponse", ANNA_FILE_LOCATION));
68   RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
69   anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
70   anna::diameter::comm::ClassCode::_v code = response.getClassCode();
71   anna::diameter::comm::Response::ResultCode::_v result = response.getResultCode();
72   anna::diameter::comm::Message* request = const_cast<anna::diameter::comm::Message*>(response.getRequest());
73   const anna::DataBlock* message = response.getMessage();
74   const anna::diameter::comm::ServerSession *serverSession = static_cast<const anna::diameter::comm::ServerSession *>(response.getSession());
75   bool contextExpired = (result == anna::diameter::comm::Response::ResultCode::Timeout);
76   bool isUnavailable = (result == anna::diameter::comm::Response::ResultCode::DiameterUnavailable);
77   bool isOK = (result == anna::diameter::comm::Response::ResultCode::Success);
78
79   // CommandId:
80   anna::diameter::CommandId request_cid = request->getCommandId();
81   LOGDEBUG
82   (
83     std::string msg = "Response received for original diameter request: ";
84     msg += anna::diameter::functions::commandIdAsPairString(request_cid);
85     msg += " | Response: ";
86     msg += response.asString();
87     msg += " | LocalServer: ";
88     msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
89     msg += " | EventTime: ";
90     msg += anna::time::functions::currentTimeAsString();
91     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
92   );
93
94   if(isUnavailable) {
95     //if (isApplicationMessage)
96     LOGWARNING(anna::Logger::warning("Diameter client unavailable for Diameter Request", ANNA_FILE_LOCATION));
97   }
98
99   if(contextExpired) {
100     //if (isApplicationMessage)
101     LOGWARNING(anna::Logger::warning("Context Expired for Diameter Request which was sent to the client", ANNA_FILE_LOCATION));
102   }
103
104   if(isOK) {
105     LOGDEBUG(
106       std::string msg = "Received response for diameter message:  ";
107       msg += anna::diameter::functions::commandIdAsPairString(request_cid);
108       anna::Logger::debug(msg, ANNA_FILE_LOCATION);
109     );
110   }
111 }
112
113 void MyLocalServer::eventUnknownResponse(anna::diameter::comm::ServerSession *serverSession, const anna::DataBlock &message)
114 throw(anna::RuntimeException) {
115   LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyLocalServer", "eventUnknownResponse", ANNA_FILE_LOCATION));
116   // Performance stats:
117   RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
118   anna::diameter::comm::OriginHost *my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
119   // CommandId:
120   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
121   LOGDEBUG
122   (
123     std::string msg = "Out-of-context response received from client: ";
124     msg += anna::diameter::functions::commandIdAsPairString(cid);
125     msg += " | DiameterServer: ";
126     msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
127     msg += " | EventTime: ";
128     msg += anna::time::functions::currentTimeAsString();
129     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
130   );
131 }
132
133 void MyLocalServer::eventDPA(anna::diameter::comm::ServerSession *serverSession, const anna::DataBlock &message)
134 throw(anna::RuntimeException) {
135   LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyLocalServer", "eventDPA", ANNA_FILE_LOCATION));
136   // Performance stats:
137   RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
138   anna::diameter::comm::OriginHost *my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
139   // CommandId:
140   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
141   LOGDEBUG
142   (
143     std::string msg = "Disconnect-Peer-Answer response received from client: ";
144     msg += anna::diameter::functions::commandIdAsPairString(cid);
145     msg += " | DiameterServer: ";
146     msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
147     msg += " | EventTime: ";
148     msg += anna::time::functions::currentTimeAsString();
149     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
150   );
151 }