From 8a83c3072f9c073ad0b29e79c19ca88e1ac636d9 Mon Sep 17 00:00:00 2001 From: Eduardo Ramos Testillano Date: Wed, 30 Sep 2015 02:41:16 +0200 Subject: [PATCH] Internal clear respecting codec engine --- include/anna/diameter/codec/Message.hpp | 23 ++++++++++++++++++----- source/diameter/codec/Message.cpp | 8 +++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/include/anna/diameter/codec/Message.hpp b/include/anna/diameter/codec/Message.hpp index 4c3e6e6..27ee6da 100644 --- a/include/anna/diameter/codec/Message.hpp +++ b/include/anna/diameter/codec/Message.hpp @@ -180,16 +180,24 @@ public: * * Once assigned (here or at constructor), this method SHALL NOT be used anymore. * Also, the associated dictionary SHOULD NOT BE CHANGED through the engine, - * unless you know what are you doing. If you want to reconfigure the engine, - * first #clear the message and then you could reuse the same object with - * different configurations (execution contexts). - * + * unless you know what are you doing. * Setting a new different engine with different stack, even same engine where the * stack has been dynamically changed, could cause a bad behaviour depending on the * changes: in general, if the dictionary grows, nothing bad will happen, but if * you remove or modified some elements which were processed with a certain format, * will be interpreted as 'unknown' with the new dictionary, and then some problems * may occur. If you add elements (vendors, avps, messages) is not a problem. + * + * IMPORTANT NOTES: + * 1) if you want to reuse the message, as a recommendation, you should set engine to + * NULL or use #clear. In that way, next operation will adjust automatically the needed + * engine because it would not be configured in such stage. + * 2) if you want to pre-configure the engine you will need to set the engine to NULL + * previously and then you could change to the new one without warning/ignoring. + * 3) if you have dedicated message objects for each interface (application id), then + * you could set the corresponding engine on constructor (or setEngine), and forget + * about #clear. The needed cleanup will be done automatically from decoding and xml + * loading procedures, and initialized engine will be kept along message operations. */ void setEngine(Engine *engine) throw(); @@ -493,8 +501,13 @@ public: * Application must clear auxiliary message objects before adding Avps in a new context if the same object is reused. * Application don't need to clear a message object before decode operation (decode invokes #clear before any other task). * Any reimplementation must first invoke base class method. + * + * @param resetEngine Sets to NULL the codec engine (true, default) or respect its current value (false). If you are going + * to reuse the message instance it is better to clear all the information (default) to manage different stacks, because if + * you don't initialize the engine to NULL, the second use of the message will keep the same engine deduced from the first + * decoding/loading operation, which could be wrong if the second message belongs to a different application identifier. */ - virtual void clear() throw(anna::RuntimeException); + virtual void clear(bool resetEngine = true) throw(anna::RuntimeException); /** Decodes buffer provided over class content. If an error ocurred, decoding will stop launching exception (fatal error) or a warning trace (perhaps the achieved diff --git a/source/diameter/codec/Message.cpp b/source/diameter/codec/Message.cpp index 049e85a..6593dd4 100644 --- a/source/diameter/codec/Message.cpp +++ b/source/diameter/codec/Message.cpp @@ -124,7 +124,7 @@ void Message::initialize() throw() { //------------------------------------------------------------------------------ //------------------------------------------------------------- Message::clear() //------------------------------------------------------------------------------ -void Message::clear() throw(anna::RuntimeException) { +void Message::clear(bool resetEngine) throw(anna::RuntimeException) { for(avp_iterator it = avp_begin(); it != avp_end(); it++) { /*avp(it)->clear(); */getEngine()->releaseAvp(Avp::avp(it)); } a_avps.clear(); @@ -133,6 +133,7 @@ void Message::clear() throw(anna::RuntimeException) { a_finds.clear(); // Initialize: initialize(); + if (resetEngine) a_engine = NULL; } @@ -227,6 +228,7 @@ void Message::setApplicationId(U32 aid) throw(anna::RuntimeException) { // Codec engine manager (a multithreaded application, normally does not achieve this point, because // messages are prepared for each interface with the corresponding codec engine) anna::diameter::codec::EngineManager &em = anna::diameter::codec::EngineManager::instantiate(); + if (em.size() == 0) return; if (em.selectFromApplicationId()) { Engine *monostackEngine = em.getMonoStackCodecEngine(); if (monostackEngine) { a_engine = monostackEngine; return; } @@ -303,7 +305,7 @@ void Message::decode(const anna::DataBlock &db, Message *ptrAnswer) throw(anna:: trace += db.asString(); anna::Logger::debug(trace, ANNA_FILE_LOCATION); ); - clear(); + clear(false /* respect engine */); // EXCEPTION MANAGEMENT IN THIS METHOD // =================================== // DECODE PHASE @@ -773,7 +775,7 @@ void Message::fromXML(const anna::xml::Node* messageNode) throw(anna::RuntimeExc unsigned int u_aux; // Clear the message - clear(); + clear(false /* respect engine */); if(version) { i_aux = version->getIntegerValue(); -- 2.20.1