Hard refactoring. CodecEngine is associated to a unique stack.
[anna.git] / example / diameter / launcher / testing / TestStep.cpp
index d08d482..8a5e2b0 100644 (file)
@@ -24,7 +24,6 @@
 #include <anna/diameter.comm/ServerSession.hpp>
 #include <anna/diameter.comm/Server.hpp>
 #include <anna/core/tracing/Logger.hpp>
-#include <anna/diameter/codec/Message.hpp>
 #include <anna/diameter/codec/functions.hpp>
 #include <anna/diameter/helpers/base/functions.hpp>
 
 #include <MyDiameterEntity.hpp>
 #include <MyLocalServer.hpp>
 #include <TestStep.hpp>
-#include <Launcher.hpp>
 #include <TestCase.hpp>
 #include <TestManager.hpp>
 #include <TestTimer.hpp>
+#include <Launcher.hpp>
 
 
 namespace {
@@ -93,19 +92,14 @@ namespace {
     // TODO: mutex the step while setting data here !!
   }
 
-  bool decodeMessage(const anna::DataBlock &message, anna::diameter::codec::Message *messageCodec) throw() {
+  bool decodeMessage(const anna::DataBlock &message, anna::diameter::codec::Message &messageCodec) throw() {
 
     if (message.isEmpty())
       return false;
 
     bool result = true;
     try {
-      if (!messageCodec) {
-        Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
-        messageCodec = new anna::diameter::codec::Message(my_app.getCodecEngine());
-      }
-
-      messageCodec->decode(message);
+      messageCodec.decode(message);
     }
     catch (anna::RuntimeException &ex) {
       ex.trace();
@@ -114,7 +108,6 @@ namespace {
 
     return result;
   }
-
 }
 
 
@@ -130,13 +123,23 @@ void TestStep::initialize(TestCase *testCase) {
   a_number = testCase->steps() + 1; // testCase is not NULL
 }
 
+bool TestStep::decodeMessage() throw() {
+  if (a_messageCodec) return true;
+  a_messageCodec = new anna::diameter::codec::Message;
+  if (::decodeMessage(a_message, *a_messageCodec)) return true;
+
+  delete a_messageCodec;
+  a_messageCodec = NULL;
+  return false;
+}
+
 const char* TestStep::asText(const Type::_v type)
 throw() {
   static const char* text [] = { "Unconfigured", "Timeout", "Sendxml2e", "Sendxml2c", "Delay", "Wait", "Cmd" };
   return text [type];
 }
 
-anna::xml::Node* TestStep::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStep::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = parent->createChild("TestStep");
 
@@ -159,7 +162,7 @@ throw() {
   return result;
 }
 
-std::string TestStep::asXMLString() const throw() {
+std::string TestStep::asXMLString() throw() {
   anna::xml::Node root("root");
   return anna::xml::Compiler().apply(asXML(&root));
 }
@@ -195,7 +198,7 @@ void TestStep::next() throw() {
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStepTimeout
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
-anna::xml::Node* TestStepTimeout::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStepTimeout::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent); // end timestamp will be 0 if test finished OK
   //parent->createChild("TestStepTimeout");
@@ -247,12 +250,7 @@ void TestStepTimeout::do_reset() throw() {
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStepSendxml
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
-TestStepSendxml::~TestStepSendxml() {
-  delete a_messageCodec;
-  a_messageCodec = NULL;
-}
-
-anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent);
   //parent->createChild("TestStepSendxml");
@@ -268,7 +266,7 @@ throw() {
     }
   }
 
-  if (decodeMessage(a_message, a_messageCodec)) {
+  if (decodeMessage()) {
     xmlmsg = "\n";
     xmlmsg += a_messageCodec->asXMLString();
     xmlmsg += "\n";
@@ -346,10 +344,7 @@ bool TestStepSendxml::do_execute() throw() {
 
       // Detailed log:
       if(a_realmNode->logEnabled()) {
-        if (!a_messageCodec)
-          decodeMessage(a_message, a_messageCodec);
-
-        if (a_messageCodec) {
+        if (decodeMessage()) {
           std::string detail = usedClientSession ? usedClientSession->asString() : "<null client session>"; // shouldn't happen
           a_realmNode->writeLogFile(*a_messageCodec, (success ? "sent2e" : "send2eError"), detail);
         }
@@ -383,10 +378,7 @@ bool TestStepSendxml::do_execute() throw() {
 
       // Detailed log:
       if(a_realmNode->logEnabled()) {
-        if (!a_messageCodec)
-          decodeMessage(a_message, a_messageCodec);
-
-        if (a_messageCodec) {
+        if (decodeMessage()) {
           std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // shouldn't happen
           a_realmNode->writeLogFile(*a_messageCodec, (success ? "sent2c" : "send2cError"), detail);
         }
@@ -414,12 +406,13 @@ bool TestStepSendxml::do_execute() throw() {
 void TestStepSendxml::do_reset() throw() {
   a_expired = false;
   //a_message.clear();
+  //a_messageAlreadyDecoded = false;
 }
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStepDelay
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
-anna::xml::Node* TestStepDelay::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStepDelay::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent);
   //parent->createChild("TestStepDelay");
@@ -461,14 +454,10 @@ void TestStepDelay::do_reset() throw() {
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStepWait
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
-TestStepWait::~TestStepWait() {
-  delete a_messageCodec;
-  a_messageCodec = NULL;
-}
-
 void TestStepWait::setCondition(bool fromEntity,
-                                  const std::string &code, const std::string &bitR, const std::string &resultCode, const std::string &sessionId,
-                                  const std::string &hopByHop, const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw() {
+    const std::string &code, const std::string &bitR, const std::string &hopByHop, const std::string &applicationId,
+    const std::string &sessionId, const std::string &resultCode,
+    const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw() {
 
   a_condition.setReceivedFromEntity(fromEntity);
   a_condition.setCode(code);
@@ -487,7 +476,7 @@ void TestStepWait::setCondition(bool fromEntity, const std::string &regexp) thro
   a_condition.setRegexp(regexp);
 }
 
-anna::xml::Node* TestStepWait::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStepWait::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent);
   //parent->createChild("TestStepWait");
@@ -506,7 +495,7 @@ throw() {
     }
   }
 
-  if (decodeMessage(a_message, a_messageCodec)) {
+  if (decodeMessage()) {
     xmlmsg = "\n";
     xmlmsg += a_messageCodec->asXMLString();
     xmlmsg += "\n";
@@ -546,7 +535,7 @@ void TestStepWait::do_reset() throw() {
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStepCmd
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
-anna::xml::Node* TestStepCmd::asXML(anna::xml::Node* parent) const
+anna::xml::Node* TestStepCmd::asXML(anna::xml::Node* parent)
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent);
   //parent->createChild("TestStepCmd");