1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
9 #include <anna/core/functions.hpp>
10 #include <anna/diameter/defines.hpp>
11 #include <anna/diameter/functions.hpp>
12 #include <anna/diameter/helpers/helpers.hpp>
13 #include <anna/diameter/codec/functions.hpp>
14 #include <anna/diameter/codec/Message.hpp>
15 #include <anna/diameter/helpers/base/functions.hpp>
16 #include <anna/time/functions.hpp>
19 #include <anna/diameter.comm/ServerSession.hpp>
20 #include <anna/diameter.comm/Engine.hpp>
21 #include <anna/diameter.comm/Entity.hpp>
22 #include <anna/diameter.comm/Response.hpp>
23 #include <anna/diameter.comm/Message.hpp>
24 #include <anna/diameter.comm/OamModule.hpp>
25 #include <anna/diameter.comm/ApplicationMessageOamModule.hpp>
26 #include <anna/diameter.comm/TimerManager.hpp>
27 #include <anna/diameter.comm/Timer.hpp>
28 #include <anna/diameter.comm/LocalServer.hpp>
29 #include <anna/diameter.comm/ServerSessionReceiver.hpp>
30 #include <anna/diameter.comm/ReceiverFactoryImpl.hpp>
32 #include <anna/app/functions.hpp>
33 #include <anna/comm/ClientSocket.hpp>
34 #include <anna/core/functions.hpp>
35 #include <anna/core/DataBlock.hpp>
36 #include <anna/core/tracing/Logger.hpp>
37 #include <anna/core/tracing/TraceMethod.hpp>
38 #include <anna/xml/Node.hpp>
39 #include <anna/timex/Engine.hpp>
42 #include <stdlib.h> // rand()
48 using namespace anna::diameter;
49 using namespace anna::diameter::comm;
52 const anna::Millisecond ServerSession::DefaultAllowedInactivityTime(90000); // Inactivity timeout
55 ServerSession::ServerSession() : Session("diameter::comm::ServerSession", "Diameter Inactivity Detection Timer"),
56 a_receiverFactory(this),
57 a_cer(ClassCode::Bind),
58 a_dwr(ClassCode::ApplicationMessage) // not actually needed; Message is application type by default
61 void ServerSession::initialize() throw() {
62 Session::initialize();
64 a_clientSocket = NULL;
65 a_deprecated = false; // selected for remove (server session was lost due to event break connection)
68 //ServerSession::~ServerSession() {;}
70 void ServerSession::setClientSocket(anna::comm::ClientSocket *clientSocket) throw() {
71 a_clientSocket = clientSocket;
72 a_clientSocket->setReceiverFactory(a_receiverFactory);
76 const std::string& ServerSession::getAddress() const throw() {
77 return a_parent->getKey().first;
80 int ServerSession::getPort() const throw() {
81 return a_parent->getKey().second;
84 const Response* ServerSession::send(const Message* message) throw(anna::RuntimeException) {
85 LOGMETHOD(anna::TraceMethod traceMethod(a_className, "send", ANNA_FILE_LOCATION));
88 throw anna::RuntimeException("Cannot send a NULL message", ANNA_FILE_LOCATION);
92 diameter::CommandId cid = message->getCommandId(isRequest);
93 diameter::ApplicationId aid = message->getApplicationId();
96 std::string msg = "Sending diameter message: ";
97 msg += anna::diameter::functions::commandIdAsPairString(cid);
98 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
103 string msg(asString());
104 msg += " | ServerSession is deprecated and will be erased";
105 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
108 if((a_state == State::Closed) && (cid != helpers::base::COMMANDID__Capabilities_Exchange_Answer)) {
109 string msg(asString());
110 msg += " | ServerSession is not bound";
111 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
114 // CER/DWR are not sent from server-side:
115 if(cid == helpers::base::COMMANDID__Capabilities_Exchange_Request) {
116 string msg(asString());
117 msg += " | Trying to send CER: unexpected message from server-side";
118 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
121 if(cid == helpers::base::COMMANDID__Device_Watchdog_Request) {
122 string msg(asString());
123 msg += " | Trying to send DWR: unexpected message from server-side";
124 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
128 /*if (cid == helpers::base::COMMANDID__Capabilities_Exchange_Answer) {
129 if (a_state != State::Closed) {
130 string msg(asString());
131 msg += " | Discarding CEA on not closed state";
132 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
135 } else*/ if(cid == helpers::base::COMMANDID__Device_Watchdog_Answer) {
136 if(a_state == State::WaitingDPA) {
137 string msg(asString());
138 msg += " | DWA is not sent on 'WaitingDPA' state";
139 //throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
140 LOGDEBUG(anna::Logger::debug(msg, ANNA_FILE_LOCATION));
144 if(a_state == State::Disconnecting) {
145 string msg(asString());
146 msg += " | DWA is not sent on 'Disconnecting' state";
147 //throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
148 LOGDEBUG(anna::Logger::debug(msg, ANNA_FILE_LOCATION));
151 } else if(cid == helpers::base::COMMANDID__Disconnect_Peer_Request) {
152 if(a_state == State::WaitingDPA) {
153 string msg(asString());
154 msg += " | Still waiting for DPR ack (DPA)";
155 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
158 if(a_state == State::Disconnecting) {
159 string msg(asString());
160 msg += " | Server disconnection has already been initiated";
161 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
164 if((a_state == State::WaitingDPA) || (a_state == State::Disconnecting)) {
165 if(cid != helpers::base::COMMANDID__Disconnect_Peer_Answer) {
167 string msg("diameter::comm::ServerSession::send | ");
169 msg += " | Sents (request or answer) blocked to diameter client (disconnection in progress). Discarding ...";
170 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
177 // Trace send operation:
179 string msg("diameter::comm::ServerSession::send | ");
182 msg += message->asString();
183 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
185 bool fixed = false; // answers cannot be fixed
186 Message * message_nc = const_cast<Message*>(message);
190 fixed = message_nc->fixRequestSequence(a_nextHopByHop, a_nextEndToEnd);
191 message_nc->updateRequestTimestampMs(); // statistics purposes (processing time for request type)
196 message->send(*this);
198 // Next hop by hop & end to end identifiers:
199 if(isRequest) generateNextSequences();
202 // The Diameter protocol requires that agents maintain transaction
203 // state, which is used for failover purposes. Transaction state
204 // implies that upon forwarding a request, the Hop-by-Hop identifier
205 // is saved; the field is replaced with a locally unique identifier,
206 // which is restored to its original value when the corresponding
207 // answer is received. The request's state is released upon receipt
208 // of the answer. A stateless agent is one that only maintains
209 // transaction state.
211 updateOutgoingActivityTime();
213 countSendings(cid, aid, true /* send ok */);
214 // Trace non-application messages:
217 if( (cid == helpers::base::COMMANDID__Device_Watchdog_Request) ||
218 (cid == helpers::base::COMMANDID__Disconnect_Peer_Request)) {
219 anna::Logger::debug("Sent DataBlock to XML representation:", ANNA_FILE_LOCATION);
221 anna::diameter::codec::Message msg(a_engine->getBaseProtocolCodecEngine()); msg.decode(message->getBody()); /* decode to be traced */
223 catch(anna::RuntimeException &ex) {
224 std::string msg = ex.getText();
225 msg += " | Use diameter::comm::Engine::setBaseProtocolCodecEngine() to allow internal base protocol messages full tracing";
226 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
231 // Restore sequences:
232 if(fixed) message_nc->restoreSequencesAfterFix(); // restore to application sequences after fix
233 } catch(anna::RuntimeException&) {
234 if(fixed) message_nc->restoreSequencesAfterFix(); // restore to application sequences after fix
237 countSendings(cid, aid, false /* send no ok */);
242 /*if (cid == helpers::base::COMMANDID__Capabilities_Exchange_Answer) {
243 setState(State::Bound); // Done at sendCEA if proceed
245 } else */if(cid == helpers::base::COMMANDID__Disconnect_Peer_Request) {
246 LOGWARNING(anna::Logger::warning("DPR has been sent to the peer (diameter client)", ANNA_FILE_LOCATION));
247 setState(State::WaitingDPA);
250 // Answers are not temporized:
251 if(!isRequest) return NULL;
253 // Request will have context responses:
254 Response* result(NULL);
255 result = Response::instance(message->getClassCode(), a_nextHopByHop - 1 /* current request sent to client */);
256 result->setRequest(message);
257 response_add(result);
263 bool ServerSession::unbind(bool forceDisconnect) throw(anna::RuntimeException) {
264 LOGMETHOD(anna::TraceMethod traceMethod(a_className, "unbind", ANNA_FILE_LOCATION));
266 if(a_state == State::Closed)
270 anna::comm::ClientSocket * cs = a_clientSocket;
271 //anna::comm::ClientSocket * cs = const_cast<anna::comm::ClientSocket*>(a_clientSocket);
273 if(forceDisconnect) {
274 LOGDEBUG(anna::Logger::debug("Immediate disconnection (forceDisconnect)", ANNA_FILE_LOCATION));
276 if(cs) cs->requestClose(); // this will invoke finalize()
281 // if (a_state == State::Disconnecting) {
283 // string msg("diameter::comm::ServerSession::unbind | ");
284 // msg += asString();
285 // msg += " | Disconnection already in progress !";
286 // anna::Logger::debug(msg, ANNA_FILE_LOCATION);
292 // if (a_onDisconnect == OnDisconnect::IgnorePendings) {
293 // LOGDEBUG(anna::Logger::debug("Immediate disconnection (IgnorePendings)", ANNA_FILE_LOCATION));
295 // if (cs) cs->requestClose(); // this will invoke finalize()
300 // if (getOTARequests() == 0) { // No pendings
301 // LOGDEBUG(anna::Logger::debug("No pending answers. Perform client-session close.", ANNA_FILE_LOCATION));
303 // if (cs) cs->requestClose(); // this will invoke finalize()
310 void ServerSession::eventPeerShutdown() throw() {
311 // Inform father server:
312 a_parent->eventPeerShutdown(this);
315 void ServerSession::eventRequestRetransmission(Message *request) throw() {
318 OamModule &oamModule = OamModule::instantiate();
319 oamModule.count(OamModule::Counter::RequestRetransmitted);
320 oamModule.count(OamModule::Counter::RequestRetransmittedOnServerSession);
322 // Inform father server:
323 a_parent->eventRequestRetransmission(this, request);
326 void ServerSession::eventResponse(const Response& response) throw(anna::RuntimeException) {
327 // Inform father server:
328 a_parent->eventResponse(response);
331 void ServerSession::eventRequest(const anna::DataBlock &request) throw(anna::RuntimeException) {
332 // Inform father server:
333 a_parent->eventRequest(this, request);
336 void ServerSession::eventUnknownResponse(const anna::DataBlock& response) throw(anna::RuntimeException) {
337 // Inform father server:
338 a_parent->eventUnknownResponse(this, response);
341 void ServerSession::eventDPA(const anna::DataBlock& response) throw(anna::RuntimeException) {
342 // Inform father server:
343 a_parent->eventDPA(this, response);
346 //------------------------------------------------------------------------------------------
347 // Se invoca desde el diameter::comm::Receiver
348 //------------------------------------------------------------------------------------------
349 void ServerSession::receive(const anna::comm::Message& message)
350 throw(anna::RuntimeException) {
351 LOGMETHOD(anna::TraceMethod traceMethod(a_className, "receive", ANNA_FILE_LOCATION));
353 updateIncomingActivityTime();
356 const anna::DataBlock & db = message.getBody();
357 diameter::CommandId cid = codec::functions::getCommandId(db);
358 bool isRequest = cid.second;
360 std::string msg = "Received diameter message: ";
361 msg += anna::diameter::functions::commandIdAsPairString(cid);
362 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
364 if( (cid == helpers::base::COMMANDID__Capabilities_Exchange_Request) ||
365 (cid.first == helpers::base::COMMANDID__Device_Watchdog_Request.first)) {
367 anna::diameter::codec::Message dmsg(a_engine->getBaseProtocolCodecEngine()); dmsg.decode(db); /* decode to be traced */
369 catch(anna::RuntimeException &ex) {
370 std::string msg = ex.getText();
371 msg += " | Use diameter::comm::Engine::setBaseProtocolCodecEngine() to allow internal base protocol messages full tracing";
372 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
377 OamModule &oamModule = OamModule::instantiate();
378 oamModule.count(isRequest ? OamModule::Counter::RequestReceived : OamModule::Counter::AnswerReceived);
379 oamModule.count(isRequest ? OamModule::Counter::RequestReceivedOnServerSession : OamModule::Counter::AnswerReceivedOnServerSession);
381 a_parent->updateReceivedMessageSizeStatisticConcept(message.getSize(), cid); // only on reception (application could manage sent sizes)
384 // Si recibo un request, el message solo tiene fiable el DataBlock. Como por defecto se construye como ApplicationMessage,
385 // el unico caso que no cuadraria seria la recepcion de un CER. Lo que hacemos es NO PROGRESAR NUNCA un CER (*).
386 // El DWR sin embargo, si podriamos progresarlo al ser de aplicacion, pero no les sirve para nada (**).
387 /////////////////////////////
388 // Here received a request //
389 /////////////////////////////
392 if(cid == helpers::base::COMMANDID__Capabilities_Exchange_Request) {
393 oamModule.count(OamModule::Counter::CERReceived);
395 if(a_state == State::Bound) {
396 LOGWARNING(anna::Logger::warning("Received another CER over already bound connection. Anyway, will be replied with CEA", ANNA_FILE_LOCATION));
401 getParent()->getEngine()->manageDrDhServerSession(this, true /* register */);
404 //activateTimer(); // Ya se invoca al inicio de este metodo ::receive
405 //bool changes = a_parent->refreshAvailability();
409 else if(cid == helpers::base::COMMANDID__Device_Watchdog_Request) {
410 oamModule.count(OamModule::Counter::DWRReceived);
416 else if(cid == helpers::base::COMMANDID__Disconnect_Peer_Request) {
417 oamModule.count(OamModule::Counter::DPRReceived);
419 if(a_state == State::Bound) {
421 setState(State::Disconnecting);
422 LOGWARNING(anna::Logger::warning("DPR has been received from peer (diameter client)", ANNA_FILE_LOCATION));
423 // Ignore pending on server sessions:
424 /*if (getOTARequests() == 0) */sendDPA();
425 return; // DPR won't be informed because virtual readDPA is available for this
430 // application message counters
431 ApplicationMessageOamModule::instantiate().count(cid.first, -1 /* no result code */, anna::diameter::codec::functions::getApplicationId(db), ApplicationMessageOamModule::Counter::Request_Received_AsServer);
434 } catch(anna::RuntimeException& ex) {
441 /////////////////////////////
442 // Here received an answer //
443 /////////////////////////////
444 bool doUnbind = false;
448 resultCode = helpers::base::functions::getResultCode(db);
449 } catch(anna::RuntimeException& ex) {
454 if(cid == helpers::base::COMMANDID__Disconnect_Peer_Answer) {
455 oamModule.count(OamModule::Counter::DPAReceived);
457 if(a_state == State::WaitingDPA) {
458 if(resultCode != helpers::base::AVPVALUES__Result_Code::DIAMETER_SUCCESS) {
459 LOGWARNING(anna::Logger::warning("Received DPA with non-success Result-Code. Ignoring and recovering Bound state", ANNA_FILE_LOCATION));
460 setState(State::Bound);
462 LOGWARNING(anna::Logger::warning("Received DPA With Result-Code = DIAMETER_SUCCESS. Disconnect now.", ANNA_FILE_LOCATION));
469 } else if(cid == helpers::base::COMMANDID__Device_Watchdog_Answer) { // non usual (server should not send DWR's)
470 oamModule.count(OamModule::Counter::DWAReceived);
473 HopByHop hopByHop = codec::functions::getHopByHop(db); // context identification
474 Response* response = response_find(hopByHop);
476 // Out-of-context responses:
479 oamModule.count(OamModule::Counter::AnswerReceivedUnknown);
480 oamModule.count(OamModule::Counter::AnswerReceivedOnServerSessionUnknown);
481 oamModule.activateAlarm(OamModule::Alarm::AnswerReceivedOnServerSessionUnknown);
483 // application message counters
484 ApplicationMessageOamModule::instantiate().count(cid.first, resultCode, anna::diameter::codec::functions::getApplicationId(db), ApplicationMessageOamModule::Counter::Answer_UnknownReceived_AsServer);
486 eventUnknownResponse(db);
488 string msg(asString());
489 msg += anna::functions::asString(" | Response received from client, for non registered context (HopByHop: %u)", hopByHop);
490 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
493 response->setResultCode(Response::ResultCode::Success);
494 response->cancelTimer();
496 string msg("diameter::comm::ServerSession::receive | ");
498 msg += " | Received answer";
499 msg += response->asString();
500 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
503 anna::Millisecond current = (anna::Millisecond)anna::functions::millisecond();
504 anna::Millisecond request = response->getRequest()->getRequestTimestampMs();
505 anna::Millisecond timeToAnswerMs = current - request;
506 a_parent->updateProcessingTimeStatisticConcept(timeToAnswerMs, cid);
509 // std::string msg = "This diameter request context lasted ";
510 // msg += anna::functions::asString(timeToAnswerMs);
511 // msg += " milliseconds at diameter client (included network time)";
512 // anna::Logger::debug(msg, ANNA_FILE_LOCATION);
514 // Progress origin for tracking purposes on asyncronous boxes with both diameter interfaces (entities and clients)
515 Message * requestMessage = const_cast<Message*>(response->getRequest());
516 requestMessage->setRequestClientSessionKey(response->getRequest()->getRequestClientSessionKey()); // "" means unkown/unset
518 if(cid != helpers::base::COMMANDID__Disconnect_Peer_Answer) {
519 // don't progress DPA: unbind is automatically performed and not open to any application decision
521 response->setMessage(&db);
522 // Restore received datablock
524 string msg("diameter::comm::ClientSession::receive | Restore answer to original request sequences (hop-by-hop = ");
525 msg += anna::functions::asString(response->getRequest()->getRequestHopByHop());
526 msg += ", end-to-end = ";
527 msg += anna::functions::asString(response->getRequest()->getRequestEndToEnd());
529 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
531 diameter::codec::functions::setHopByHop((anna::DataBlock&)db, response->getRequest()->getRequestHopByHop());
532 diameter::codec::functions::setEndToEnd((anna::DataBlock&)db, response->getRequest()->getRequestEndToEnd());
534 // application message counters
535 ApplicationMessageOamModule::instantiate().count(cid.first, resultCode, anna::diameter::codec::functions::getApplicationId(db), ApplicationMessageOamModule::Counter::Answer_Received_AsServer);
537 eventResponse(*response);
539 } catch(anna::RuntimeException& ex) {
544 response_erase(response);
548 unbind(true /* always immediate */);
551 void ServerSession::finalize() throw() {
552 LOGMETHOD(anna::TraceMethod traceMethod(a_className, "finalize", ANNA_FILE_LOCATION));
553 // Configuration overiddings
554 setOnDisconnect(OnDisconnect::IgnorePendings);
555 notifyOrphansOnExpiration(false);
556 // session will be idle after 'Session::finalize'
557 // (anyway at 99% of the cases, server session would be idle because is not usual to send request from diameter servers)
558 // Closing state won't be used on server-sessions
559 Session::finalize(); // sets closed state (unbind at closeServerSession won't repeat client socket close, because returns at the beginning)
562 // "delete this" indirecto (seria mas elegante borrar las server sessions deprecated mediante un temporizador de purgado)
564 if(idle()) getParent()->closeServerSession(this); // http://www.parashift.com/c++-faq-lite/delete-this.html
565 } catch(anna::RuntimeException& ex) {
569 // Inform father local server (availability changes):
570 getParent()->refreshAvailability();
572 bool multipleConnections = (getParent()->getMaxConnections() > 1);
573 std::string socket = anna::functions::socketLiteralAsString(getAddress(), getPort());
574 OamModule &oamModule = OamModule::instantiate();
576 if(multipleConnections) {
577 oamModule.activateAlarm(OamModule::Alarm::LostConnectionForServerSessionAtLocalServer__s__ServerSessionId__d__, socket.c_str(), getSocketId());
578 oamModule.count(OamModule::Counter::LostConnectionForServerSession);
580 oamModule.activateAlarm(OamModule::Alarm::LostConnectionForServerSessionAtLocalServer__s__, socket.c_str());
581 oamModule.count(OamModule::Counter::LostConnectionForServerSession);
587 void ServerSession::sendCEA()
588 throw(anna::RuntimeException) {
589 LOGMETHOD(anna::TraceMethod traceMethod(a_className, "sendCEA", ANNA_FILE_LOCATION));
590 anna::DataBlock cea(true);
591 a_engine->readCEA(cea, a_cer.getBody()); // Asume that CEA is valid ...
592 // If one peer sends a CER message to another Peer and receiver does not have support for
594 // 1) any common application then it must return the CEA with Result-Code Avp set to DIAMETER_NO_COMMON_APPLICATION
595 // and should disconnect the transport layer connection (automatically done by diameter::comm module).
596 // 2) no common security mechanism then it must return the CEA with Result-Code Avp set to DIAMETER_NO_COMMON_SECURITY
597 // and should disconnect the transport layer connection (automatically done by diameter::comm module).
598 // 3) if CER is received from any unknown peer then receiver should discard the message, or send the CEA with the
599 // Result-Code Avp set to DIAMETER_UNKNOWN_PEER.
602 LOGDEBUG(anna::Logger::debug("Empty CEA message. Remote client never will bound this connection at application level", ANNA_FILE_LOCATION));
603 LOGWARNING(anna::Logger::warning("Discarding received CER: cannot send empty CEA (consider to send CEA with Result-Code DIAMETER_UNKNOWN_PEER)", ANNA_FILE_LOCATION));
610 // Here, CEA has been sent (no exception). Analize CEA Result-Code chosen:
614 resultCode = helpers::base::functions::getResultCode(cea);
615 } catch(anna::RuntimeException& ex) {
620 case helpers::base::AVPVALUES__Result_Code::DIAMETER_SUCCESS:
621 setState(State::Bound);
623 case helpers::base::AVPVALUES__Result_Code::DIAMETER_NO_COMMON_APPLICATION:
624 LOGWARNING(anna::Logger::warning("DIAMETER_NO_COMMON_APPLICATION CEA Result-Code implies unbinding connection", ANNA_FILE_LOCATION));
625 unbind(true /* always immediate */); // no delegamos en un planning o similar
626 // Realmente el cliente diameter tambien deberia cerrar la conexion al recibir este Result-Code
628 case helpers::base::AVPVALUES__Result_Code::DIAMETER_NO_COMMON_SECURITY:
629 LOGWARNING(anna::Logger::warning("DIAMETER_NO_COMMON_SECURITY CEA Result-Code implies unbinding connection", ANNA_FILE_LOCATION));
630 unbind(true /* always immediate */); // no delegamos en un planning o similar
631 // Realmente el cliente diameter tambien deberia cerrar la conexion al recibir este Result-Code
638 void ServerSession::sendDWA()
639 throw(anna::RuntimeException) {
640 LOGMETHOD(anna::TraceMethod traceMethod(a_className, "sendDWA", ANNA_FILE_LOCATION));
641 anna::DataBlock dwa(true);
642 a_engine->readDWA(dwa, a_dwr.getBody()); // Asume that DWA is valid ...
645 throw anna::RuntimeException("This diameter agent defines an empty DWA message. Remote client never will validate this connection health", ANNA_FILE_LOCATION);
653 //-------------------------------------------------------------------------
654 // Se invoca desde diameter::comm::Timer
655 //-------------------------------------------------------------------------
656 void ServerSession::expireResponse(diameter::comm::Response* response)
658 LOGMETHOD(anna::TraceMethod traceMethod(a_className, "expireResponse", ANNA_FILE_LOCATION));
659 Session::expireResponse(response);
661 OamModule &oamModule = OamModule::instantiate();
662 oamModule.count(OamModule::Counter::RequestSentExpired);
663 oamModule.count(OamModule::Counter::RequestSentOnServerSessionExpired);
664 oamModule.activateAlarm(OamModule::Alarm::RequestSentOnServerSessionExpired);
665 // // "delete this" indirecto (seria mas elegante borrar las server sessions deprecated mediante un temporizador de purgado)
666 // if (idle()) a_parent->eraseServerSession(*a_clientSocket); // http://www.parashift.com/c++-faq-lite/delete-this.html
669 std::string ServerSession::asString() const
671 string result = Session::asString();
672 result += " | Parent Local Server: ";
673 result += anna::functions::socketLiteralAsString(getAddress(), getPort());
674 result += " | Client Socket: ";
675 result += a_clientSocket->asString();
676 // Diferente del timeout de ApplicationMessage:
677 result += " | Allowed inactivity time: ";
678 result += getTimeout().asString();
679 result += " | Deprecated: ";
680 result += (a_deprecated ? "yes" : "no");
681 return result += " }";
684 anna::xml::Node* ServerSession::asXML(anna::xml::Node* parent) const
686 anna::xml::Node* result = Session::asXML(parent);
687 parent->createChild("diameter.comm.ServerSession");
688 result->createAttribute("ParentLocalServer", anna::functions::socketLiteralAsString(getAddress(), getPort()));
689 result->createAttribute("ClientSocket", a_clientSocket->asString());
690 // Diferente del timeout de ApplicationMessage:
691 result->createAttribute("AllowedInactivityTime", getTimeout().asString());
692 result->createAttribute("Deprecated", a_deprecated ? "yes" : "no");
697 //------------------------------------------------------------------------------
698 //------------------------------------------------------ ServerSession::expire()
699 //------------------------------------------------------------------------------
700 void ServerSession::expire(anna::timex::Engine *timeController) throw(anna::RuntimeException) {
701 LOGMETHOD(anna::TraceMethod traceMethod(a_className, "expire (inactivity check timer)", ANNA_FILE_LOCATION));
702 LOGWARNING(anna::Logger::warning("Detecting anomaly (too inactivity time) over server session. Resetting", ANNA_FILE_LOCATION));
704 bool multipleConnections = (getParent()->getMaxConnections() > 1);
705 std::string socket = anna::functions::socketLiteralAsString(getAddress(), getPort());
706 OamModule &oamModule = OamModule::instantiate();
708 if(multipleConnections) {
709 oamModule.activateAlarm(OamModule::Alarm::UnbindConnectionForServerSessionAtLocalServer__s__ServerSessionId__d__DueToInactivityTimeAnomaly, socket.c_str(), getSocketId());
710 oamModule.count(OamModule::Counter::UnbindConnectionForServerSessionDueToInactivityTimeAnomaly);
712 oamModule.activateAlarm(OamModule::Alarm::UnbindConnectionForServerSessionAtLocalServer__s__DueToInactivityTimeAnomaly, socket.c_str());
713 oamModule.count(OamModule::Counter::UnbindConnectionForServerSessionDueToInactivityTimeAnomaly);
716 unbind(true /* always immediate */); // no delegamos en un planning o similar
719 void ServerSession::setAllowedInactivityTime(const anna::Millisecond & allowedInactivityTime) throw() {
720 setTimeout(allowedInactivityTime);
723 //------------------------------------------------------------------------------
724 //---------------------------------- ServerSession::updateIncomingActivityTime()
725 //------------------------------------------------------------------------------
726 void ServerSession::updateIncomingActivityTime() throw() {
727 Session::updateIncomingActivityTime();
728 a_parent->updateIncomingActivityTime();
732 //------------------------------------------------------------------------------
733 //---------------------------------- ServerSession::updateOutgoingActivityTime()
734 //------------------------------------------------------------------------------
735 void ServerSession::updateOutgoingActivityTime(void) throw() {
736 Session::updateOutgoingActivityTime();
737 a_parent->updateOutgoingActivityTime();
741 //------------------------------------------------------------------------------
742 //----------------------------------------------- ServerSession::countSendings()
743 //------------------------------------------------------------------------------
744 void ServerSession::countSendings(const diameter::CommandId & cid, unsigned int aid, bool ok)throw() {
745 OamModule &oamModule = OamModule::instantiate();
746 ApplicationMessageOamModule &appMsgOamModule = ApplicationMessageOamModule::instantiate();
748 bool isRequest = cid.second;
752 oamModule.count(isRequest ? OamModule::Counter::RequestSentOK : OamModule::Counter::AnswerSentOK);
753 oamModule.count(isRequest ? OamModule::Counter::RequestSentOnServerSessionOK : OamModule::Counter::AnswerSentOnServerSessionOK);
755 if(cid == helpers::base::COMMANDID__Capabilities_Exchange_Answer) oamModule.count(OamModule::Counter::CEASentOK);
756 else if(cid == helpers::base::COMMANDID__Device_Watchdog_Answer) oamModule.count(OamModule::Counter::DWASentOK);
757 else if(cid == helpers::base::COMMANDID__Device_Watchdog_Request) oamModule.count(OamModule::Counter::DWRSentOK); // not usual
758 else if(cid == helpers::base::COMMANDID__Disconnect_Peer_Answer) oamModule.count(OamModule::Counter::DPASentOK);
759 else if(cid == helpers::base::COMMANDID__Disconnect_Peer_Request) oamModule.count(OamModule::Counter::DPRSentOK);
760 // Application messages:
762 appMsgOamModule.count(cid.first, -1 /* no result code */, aid, isRequest ? ApplicationMessageOamModule::Counter::Request_SentOK_AsServer : ApplicationMessageOamModule::Counter::Answer_SentOK_AsServer);
766 oamModule.count(isRequest ? OamModule::Counter::RequestSentNOK : OamModule::Counter::AnswerSentNOK);
767 oamModule.count(isRequest ? OamModule::Counter::RequestSentOnServerSessionNOK : OamModule::Counter::RequestSentOnServerSessionNOK);
769 if(cid == helpers::base::COMMANDID__Capabilities_Exchange_Answer) oamModule.count(OamModule::Counter::CEASentNOK);
770 else if(cid == helpers::base::COMMANDID__Device_Watchdog_Answer) oamModule.count(OamModule::Counter::DWASentNOK);
771 else if(cid == helpers::base::COMMANDID__Device_Watchdog_Request) oamModule.count(OamModule::Counter::DWRSentNOK); // not usual
772 else if(cid == helpers::base::COMMANDID__Disconnect_Peer_Answer) oamModule.count(OamModule::Counter::DPASentNOK);
773 else if(cid == helpers::base::COMMANDID__Disconnect_Peer_Request) oamModule.count(OamModule::Counter::DPRSentNOK);
774 // Application messages:
776 appMsgOamModule.count(cid.first, -1 /* no result code */, aid, isRequest ? ApplicationMessageOamModule::Counter::Request_SentNOK_AsServer : ApplicationMessageOamModule::Counter::Answer_SentNOK_AsServer);