Programming answers in double ended queue. Fix word "Coding" by "Encoding"
[anna.git] / source / diameter / codec / Message.cpp
index 3a989cf..b286fbc 100644 (file)
@@ -227,6 +227,20 @@ void Message::setId(const char *name) throw(anna::RuntimeException) {
 }
 
 
+//------------------------------------------------------------------------------
+//-------------------------------------------------- Message::setApplicationId()
+//------------------------------------------------------------------------------
+void Message::setApplicationId(U32 aid) throw() {
+  a_applicationId = aid;
+
+  // Default behaviour:
+  if (!getEngine()->hasSelectStackWithApplicationId()) return;
+
+  // Adapts for Application-ID stack identifier:
+  getEngine()->setDictionary(aid);
+}
+
+
 //------------------------------------------------------------------------------
 //------------------------------------------------------------ Message::addAvp()
 //------------------------------------------------------------------------------
@@ -294,7 +308,7 @@ void Message::decode(const anna::DataBlock &db, Message *ptrAnswer) throw(anna::
   //
   // VALIDATION PHASE
   // Launch exception on first validation error (validateAll == false), or log warning reporting all validation errors when
-  //  complete validation is desired (validateAll == true, engine default) launching a final exception like "decoded an invalid message".
+  //  complete validation is desired (validateAll == true, engine default) launching a final exception like "the decoded message is invalid".
   // OAM
   OamModule &oamModule = OamModule::instantiate();
 
@@ -331,9 +345,9 @@ void Message::decode(const anna::DataBlock &db, Message *ptrAnswer) throw(anna::
 
   U24 code = DECODE3BYTES_INDX_VALUETYPE(buffer, 5, U24);
 
-  setId(CommandId(code, requestBit() /* based on a_flags */));
+  a_id = CommandId(code, requestBit() /* based on a_flags */);
 
-  a_applicationId = DECODE4BYTES_INDX_VALUETYPE(buffer, 8, U32);
+  setApplicationId(DECODE4BYTES_INDX_VALUETYPE(buffer, 8, U32)); // centralize set, because it could be used for stack selection.
 
   a_hopByHop = DECODE4BYTES_INDX_VALUETYPE(buffer, 12, U32);
 
@@ -409,7 +423,7 @@ void Message::decode(const anna::DataBlock &db, Message *ptrAnswer) throw(anna::
 
   if((vmode == Engine::ValidationMode::AfterDecoding) || (vmode == Engine::ValidationMode::Always))
     if(!valid(answer))
-      throw anna::RuntimeException("Decoded an invalid message. See previous report on warning-level traces", ANNA_FILE_LOCATION);
+      throw anna::RuntimeException("The decoded message is invalid. See previous report on warning-level traces", ANNA_FILE_LOCATION);
 }
 
 
@@ -460,7 +474,7 @@ int Message::getResultCode() const throw() {
 //------------------------------------------------------------------------------
 //------------------------------------------------------ Message::setFailedAvp()
 //------------------------------------------------------------------------------
-void Message::setFailedAvp(const parent_t &parent, AvpId wrong) throw(anna::RuntimeException) {
+void Message::setFailedAvp(const parent_t &parent, AvpId wrong, const char *wrongName) throw(anna::RuntimeException) {
 
   if(isRequest()) return;
 
@@ -495,7 +509,7 @@ void Message::setFailedAvp(const parent_t &parent, AvpId wrong) throw(anna::Runt
 
   LOGDEBUG(
     std::string msg = "Adding to Failed-AVP, the wrong avp ";
-    msg += anna::diameter::functions::avpIdAsPairString(wrong);
+    msg += wrongName ? wrongName : (anna::diameter::functions::avpIdAsPairString(wrong));
     msg += " found inside ";
     msg += parent.asString();
 
@@ -645,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);
   }
@@ -653,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(
@@ -756,6 +770,9 @@ void Message::fromXML(const anna::xml::Node* messageNode) throw(anna::RuntimeExc
   int i_aux;
   unsigned int u_aux;
 
+  // Clear the message
+  clear();
+
   if(version) {
     i_aux = version->getIntegerValue();
 
@@ -768,6 +785,10 @@ void Message::fromXML(const anna::xml::Node* messageNode) throw(anna::RuntimeExc
     a_version = i_aux;
   }
 
+  // Application-id
+  // This is called before any operation which needs to know about the stack elements (this could set the dictionary)
+  setApplicationId(appid->getIntegerValue());
+
   // Dictionary
   const stack::Dictionary * dictionary = getEngine()->getDictionary();
   const stack::Command *stackCommand = NULL;
@@ -797,7 +818,7 @@ void Message::fromXML(const anna::xml::Node* messageNode) throw(anna::RuntimeExc
       throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
     }
 
-    setId(stackCommand->getId());
+    setId(stackCommand->getId(), false /* don't clear */);
     // 'P', 'E' and 'T' flags:
     bool activateP = pbit ? (pbit->getValue() == "yes") : false;
     bool activateE = ebit ? (ebit->getValue() == "yes") : false;
@@ -848,24 +869,11 @@ void Message::fromXML(const anna::xml::Node* messageNode) throw(anna::RuntimeExc
     a_flags = i_aux;
     int flagsBCK = a_flags;
     // Final assignments
-    setId(CommandId(u_code, requestBit() /* based on a_flags */));
+    a_id = CommandId(u_code, requestBit() /* based on a_flags */);
     // Flags could have been updated regarding dictionary, but during parsing we must respect xml file:
     a_flags = flagsBCK;
   }
 
-  // Application-id
-  u_aux = appid->getIntegerValue();
-
-  /*
-  if(u_aux < 0) {
-    std::string msg = "Error processing command <application-id '"; msg += appid->getValue();
-    msg += "': negative values are not allowed";
-    throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
-  }
-  */
-
-  setApplicationId(u_aux);
-
   // Hob-by-hop-id
   if(hbh) {
     u_aux = hbh->getIntegerValue();