From b9cb59210ce2a02d8246f1a9a1acfcfdcd892f3a Mon Sep 17 00:00:00 2001 From: Eduardo Ramos Testillano Date: Mon, 13 Apr 2015 18:04:47 +0200 Subject: [PATCH] Programming answers in double ended queue. Fix word "Coding" by "Encoding" --- .../deployments/basic/xml_examples/aaa.xml | 11 + example/diameter/launcher/main.cpp | 203 +++++++++++------- include/anna/diameter/codec/EngineImpl.hpp | 4 +- source/diameter/codec/EngineImpl.cpp | 6 +- source/diameter/codec/Message.cpp | 4 +- 5 files changed, 138 insertions(+), 90 deletions(-) create mode 100644 example/diameter/launcher/deployments/basic/xml_examples/aaa.xml diff --git a/example/diameter/launcher/deployments/basic/xml_examples/aaa.xml b/example/diameter/launcher/deployments/basic/xml_examples/aaa.xml new file mode 100644 index 0000000..4892138 --- /dev/null +++ b/example/diameter/launcher/deployments/basic/xml_examples/aaa.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/example/diameter/launcher/main.cpp b/example/diameter/launcher/main.cpp index 301ab34..38a6b1d 100644 --- a/example/diameter/launcher/main.cpp +++ b/example/diameter/launcher/main.cpp @@ -42,6 +42,7 @@ #include #include +#include #include @@ -101,12 +102,78 @@ anna::diameter::comm::Message G_commMsg; anna::diameter::codec::Message G_codecMsg, G_codecAnsMsg; anna::Recycler G_commMessages; // create on requests forwards without programmed answer / release in answers forward - // Auxiliary resources for answers programming -typedef std::map < int /* message code */, anna::diameter::codec::Message* > reacting_answers_container; -typedef std::map < int /* message code */, anna::diameter::codec::Message* >::iterator reacting_answers_iterator; -typedef std::map < int /* message code */, anna::diameter::codec::Message* >::const_iterator reacting_answers_const_iterator; -reacting_answers_container G_reactingAnswers2C, G_reactingAnswers2E; +class ProgrammedAnswers { + +typedef std::deque codec_messages_deque; +typedef std::deque::iterator codec_messages_deque_iterator; +typedef std::deque::const_iterator codec_messages_deque_const_iterator; +typedef std::map < int /* message code */, codec_messages_deque* > reacting_answers_container; +typedef std::map < int /* message code */, codec_messages_deque* >::iterator reacting_answers_iterator; +typedef std::map < int /* message code */, codec_messages_deque* >::const_iterator reacting_answers_const_iterator; + + reacting_answers_container a_deques; + + public: + ProgrammedAnswers() {}; + ~ProgrammedAnswers() { + for (reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) { + anna::diameter::codec::Engine *engine = anna::functions::component (ANNA_FILE_LOCATION); + engine->releaseMessage(*(it->second->begin())); + delete(it->second); + } + } + + void addMessage(int code, anna::diameter::codec::Message *message) throw() { + reacting_answers_const_iterator it = a_deques.find(code); + if (it != a_deques.end()) { + it->second->push_back(message); + } + else { + codec_messages_deque *deque = new codec_messages_deque; + a_deques[code] = deque; + deque->push_back(message); + } + } + + anna::diameter::codec::Message* getMessage(int code) const throw() { //get the front message (begin()), returns NULL if deque is empty + anna::diameter::codec::Message *result = NULL; + reacting_answers_const_iterator it = a_deques.find(code); + if (it != a_deques.end()) { + if (!it->second->empty()) result = *(it->second->begin()); + } + return result; + } + + void nextMessage(int code) throw() { //pops the deque and release the message (when deque is not empty: deque::empty) + reacting_answers_const_iterator it = a_deques.find(code); + if (it != a_deques.end()) { + if (!it->second->empty()) { + anna::diameter::codec::Engine *engine = anna::functions::component (ANNA_FILE_LOCATION); + engine->releaseMessage(*(it->second->begin())); + it->second->pop_front(); + } + } + } + + std::string asString() const throw() { + std::string result = "No ocurrences found\n\n"; + if(a_deques.size() != 0) { + for(reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) { + result += "Answer code .............................................................. "; + result += anna::functions::asString(it->first); result += "\n"; + for(codec_messages_deque_const_iterator itm = it->second->begin(); itm != it->second->end(); itm++) { + result += (*itm)->asXMLString(); + result += "\n"; + } + result += "\n"; + } + } + return result; + } +}; + +ProgrammedAnswers G_reactingAnswers2C, G_reactingAnswers2E; @@ -330,8 +397,6 @@ public: void resetCounters() throw(); void signalUSR2() throw(anna::RuntimeException); std::string help() const throw(); - std::string programmedAnswers2e() const throw(); - std::string programmedAnswers2c() const throw(); // helpers bool getDataBlockFromHexFile(const std::string &pathfile, anna::DataBlock &db) const throw(); @@ -871,39 +936,6 @@ std::string Launcher::help() const throw() { } -std::string Launcher::programmedAnswers2c() const throw() { - std::string result = "\n"; - result += "\n ------------- CURRENT PROGRAMMED ANSWERS TO CLIENT -------------\n\n"; - - if(G_reactingAnswers2C.size() == 0) { - result += "No ocurrences found\n\n"; - } else { - for(reacting_answers_const_iterator it = G_reactingAnswers2C.begin(); it != G_reactingAnswers2C.end(); it++) { - result += (*it).second->asXMLString(); - result += "\n\n"; - } - } - - return result; -} - - -std::string Launcher::programmedAnswers2e() const throw() { - std::string result = "\n"; - result += "\n\n ------------- CURRENT PROGRAMMED ANSWERS TO ENTITY -------------\n\n"; - - if(G_reactingAnswers2E.size() == 0) { - result += "No ocurrences found\n\n"; - } else { - for(reacting_answers_const_iterator it = G_reactingAnswers2E.begin(); it != G_reactingAnswers2E.end(); it++) { - result += (*it).second->asXMLString(); - result += "\n\n"; - } - } - - return result; -} - void MyCommunicator::prepareAnswer(anna::diameter::codec::Message *answer, const anna::DataBlock &request) const throw() { // Sequence values (hop-by-hop and end-to-end), session-id and subscription-id avps, are mirrored to the peer which sent the request. // If user wants to test a specific answer without changing it, use send operations better than programming. @@ -1999,16 +2031,12 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons throw anna::RuntimeException("Cannot program diameter requests. Answer type must be provided", ANNA_FILE_LOCATION); int code = message->getId().first; - reacting_answers_const_iterator it = G_reactingAnswers2C.find(code); - - if(it != G_reactingAnswers2C.end()) { // found: replace - LOGDEBUG(anna::Logger::debug("Replacing formerly programed answer...", ANNA_FILE_LOCATION)); - engine->releaseMessage((*it).second); - } - - G_reactingAnswers2C[code] = message; + LOGDEBUG(anna::Logger::debug("Adding a new programed 'answer to client' to the deque...", ANNA_FILE_LOCATION)); + G_reactingAnswers2C.addMessage(code, message); } else { // answers query on stdout - std::cout << programmedAnswers2c() << std::endl; + std::cout << std::endl << std::endl; + std::cout << " ------------- CURRENT PROGRAMMED ANSWERS TO CLIENT -------------\n\n"; + std::cout << G_reactingAnswers2C.asString() << std::endl; response_content = "Programmed answers dumped on stdout\n"; return; } @@ -2030,16 +2058,12 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons throw anna::RuntimeException("Cannot program diameter requests. Answer type must be provided", ANNA_FILE_LOCATION); int code = message->getId().first; - reacting_answers_const_iterator it = G_reactingAnswers2E.find(code); - - if(it != G_reactingAnswers2E.end()) { // found: replace - LOGDEBUG(anna::Logger::debug("Replacing formerly programed answer...", ANNA_FILE_LOCATION)); - engine->releaseMessage((*it).second); - } - - G_reactingAnswers2E[code] = message; + LOGDEBUG(anna::Logger::debug("Adding a new programed 'answer to entity' to the deque...", ANNA_FILE_LOCATION)); + G_reactingAnswers2E.addMessage(code, message); } else { // answers query on stdout - std::cout << programmedAnswers2e() << std::endl; + std::cout << std::endl << std::endl; + std::cout << " ------------- CURRENT PROGRAMMED ANSWERS TO ENTITY -------------\n\n"; + std::cout << G_reactingAnswers2E.asString() << std::endl; response_content = "Programmed answers dumped on stdout\n"; return; } @@ -2184,10 +2208,8 @@ throw(anna::RuntimeException) { // Lookup reacting answers list: int code = cid.first; - reacting_answers_const_iterator it = G_reactingAnswers2E.find(code); - - if(it != G_reactingAnswers2E.end()) { - anna::diameter::codec::Message *answer_message = (*it).second; + anna::diameter::codec::Message *answer_message = G_reactingAnswers2E.getMessage(code); + if (answer_message) { // Prepare answer: my_app.getCommunicator()->prepareAnswer(answer_message, message); @@ -2201,26 +2223,38 @@ throw(anna::RuntimeException) { if(my_app.logEnabled()) my_app.writeLogFile(*answer_message, "send2eError", clientSession->asString()); } - } else { // not found: forward to client (if exists) - // Forward to client: - anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer(); - if(localServer && (cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CER */) { - try { - anna::diameter::comm::Message *msg = G_commMessages.create(); - msg->setBody(message); - msg->setRequestClientSessionKey(clientSession->getKey()); - bool success = localServer->send(msg); + // Pop front the reacting answer: + G_reactingAnswers2E.nextMessage(code); + return; + } - // Detailed log: - if(my_app.logEnabled()) { - anna::diameter::comm::ServerSession *usedServerSession = localServer->getLastUsedResource(); - std::string detail = usedServerSession ? usedServerSession->asString() : ""; // esto no deberia ocurrir - my_app.writeLogFile(message, (success ? "fwd2c" : "fwd2cError"), detail); - } - } catch(anna::RuntimeException &ex) { - ex.trace(); + LOGDEBUG + ( + std::string msg = "No answers programmed (maybe sold out) for request coming from entity: "; + msg += anna::diameter::functions::commandIdAsPairString(cid); + anna::Logger::debug(msg, ANNA_FILE_LOCATION); + ); + + // not found: forward to client (if exists) + // Forward to client: + anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer(); + + if(localServer && (cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CER */) { + try { + anna::diameter::comm::Message *msg = G_commMessages.create(); + msg->setBody(message); + msg->setRequestClientSessionKey(clientSession->getKey()); + bool success = localServer->send(msg); + + // Detailed log: + if(my_app.logEnabled()) { + anna::diameter::comm::ServerSession *usedServerSession = localServer->getLastUsedResource(); + std::string detail = usedServerSession ? usedServerSession->asString() : ""; // esto no deberia ocurrir + my_app.writeLogFile(message, (success ? "fwd2c" : "fwd2cError"), detail); } + } catch(anna::RuntimeException &ex) { + ex.trace(); } } } @@ -2364,10 +2398,10 @@ throw(anna::RuntimeException) { // If no answer is programmed and entity is configured, the failed request would be forwarded even being wrong (delegates at the end point) int code = cid.first; - reacting_answers_const_iterator it = G_reactingAnswers2C.find(code); - bool programmed = (it != G_reactingAnswers2C.end()); - anna::diameter::comm::Entity *entity = my_app.getEntity(); + anna::diameter::codec::Message *programmed_answer = G_reactingAnswers2C.getMessage(code); + bool programmed = (programmed_answer != NULL); + anna::diameter::comm::Entity *entity = my_app.getEntity(); if(!programmed && entity) { // forward condition (no programmed answer + entity available) anna::diameter::comm::Message *msg = G_commMessages.create(); msg->setBody(message); @@ -2403,7 +2437,7 @@ throw(anna::RuntimeException) { // Programmed answer only when all is ok if(analysisOK) { if(programmed) { - answer_message = (*it).second; + answer_message = programmed_answer; // Prepare answer: my_app.getCommunicator()->prepareAnswer(answer_message, message); } else return; // nothing done @@ -2428,6 +2462,9 @@ throw(anna::RuntimeException) { // Restore validation mode codecEngine->setValidationMode(backupVM); + + // Pop front the reacting answer: + if(analysisOK && programmed) G_reactingAnswers2C.nextMessage(code); } void MyLocalServer::eventResponse(const anna::diameter::comm::Response &response) diff --git a/include/anna/diameter/codec/EngineImpl.hpp b/include/anna/diameter/codec/EngineImpl.hpp index 4f27e06..b79aeb1 100644 --- a/include/anna/diameter/codec/EngineImpl.hpp +++ b/include/anna/diameter/codec/EngineImpl.hpp @@ -183,7 +183,7 @@ public: * Defines behaviour mode regarding when to validate a message: before encoding, after decoding (by default), always or never * Anyway validation procedure may be called at any moment (#valid) */ - struct ValidationMode { enum _v { BeforeCoding, AfterDecoding /* default */, Always, Never /* optimization */ }; }; + struct ValidationMode { enum _v { BeforeEncoding, AfterDecoding /* default */, Always, Never /* optimization */ }; }; /** * Defines behaviour mode regarding when to fix a message: before encoding (by default), after decoding, always or never. @@ -194,7 +194,7 @@ public: * hide any validation problem regarding Avps position at any level. * Anyway fix procedure may be called at any moment (#fix) */ - struct FixMode { enum _v { BeforeCoding /* default */, AfterDecoding, Always, Never /* optimization */ }; }; + struct FixMode { enum _v { BeforeEncoding /* default */, AfterDecoding, Always, Never /* optimization */ }; }; // Creators diff --git a/source/diameter/codec/EngineImpl.cpp b/source/diameter/codec/EngineImpl.cpp index 45a85ed..af13e09 100644 --- a/source/diameter/codec/EngineImpl.cpp +++ b/source/diameter/codec/EngineImpl.cpp @@ -146,7 +146,7 @@ EngineImpl::EngineImpl(const char* className) : a_validationMode(ValidationMode::AfterDecoding), a_ignoreFlags(false), a_selectStackWithApplicationId(false), - a_fixMode(FixMode::BeforeCoding) { + a_fixMode(FixMode::BeforeEncoding) { anna::diameter::sccs::activate(); anna::xml::functions::initialize(); a_dtd.initialize(MessageDTD); @@ -254,7 +254,7 @@ throw() { //------------------------------------------------------------------------------ const char* EngineImpl::asText(const ValidationMode::_v vm) throw() { - static const char* text [] = { "BeforeCoding", "AfterDecoding", "Always", "Never" }; + static const char* text [] = { "BeforeEncoding", "AfterDecoding", "Always", "Never" }; return text [vm]; } @@ -263,7 +263,7 @@ throw() { //------------------------------------------------------------------------------ const char* EngineImpl::asText(const FixMode::_v fm) throw() { - static const char* text [] = { "BeforeCoding", "AfterDecoding", "Always", "Never" }; + static const char* text [] = { "BeforeEncoding", "AfterDecoding", "Always", "Never" }; return text [fm]; } diff --git a/source/diameter/codec/Message.cpp b/source/diameter/codec/Message.cpp index b374b90..b286fbc 100644 --- a/source/diameter/codec/Message.cpp +++ b/source/diameter/codec/Message.cpp @@ -659,7 +659,7 @@ const anna::DataBlock & Message::code() throw(anna::RuntimeException) { // Pre-Validation Engine::ValidationMode::_v vmode = getEngine()->getValidationMode(); - if((vmode == Engine::ValidationMode::BeforeCoding) || (vmode == Engine::ValidationMode::Always)) { + if((vmode == Engine::ValidationMode::BeforeEncoding) || (vmode == Engine::ValidationMode::Always)) { if(!valid()) throw anna::RuntimeException("Try to encode an invalid message. See previous report on warning-level traces", ANNA_FILE_LOCATION); } @@ -667,7 +667,7 @@ const anna::DataBlock & Message::code() throw(anna::RuntimeException) { // Pre-Fixing Engine::FixMode::_v fmode = getEngine()->getFixMode(); - if((fmode == Engine::FixMode::BeforeCoding) || (fmode == Engine::FixMode::Always)) fix(); + if((fmode == Engine::FixMode::BeforeEncoding) || (fmode == Engine::FixMode::Always)) fix(); // Trace LOGDEBUG( -- 2.20.1