From: Eduardo Ramos Testillano Date: Sun, 20 Sep 2015 22:23:02 +0000 (+0200) Subject: Fix nswering procedure: have to use source resource. X-Git-Tag: REFACTORING_TESTING_LIBRARY~109 X-Git-Url: https://git.teslayout.com/public/public/public/?a=commitdiff_plain;h=888c763c650fbe314ba47bb0840c47fc508cc837;p=anna.git Fix nswering procedure: have to use source resource. --- diff --git a/example/comm/blocker/main.cpp b/example/comm/blocker/main.cpp index c8723b5..354d9b0 100644 --- a/example/comm/blocker/main.cpp +++ b/example/comm/blocker/main.cpp @@ -134,7 +134,7 @@ void MyCommunicator::eventReceiveMessage (ClientSocket& clientSocket, const Mess app::Application& app = app::functions::getApp (); cout << "Bloqueando las comunicaciones ejecute (kill -9 " << app.getPid () << ") ... " << flush; while (stop == false) { - if (gets (aux) != NULL) { + if (fgets(aux, sizeof(aux), stdin) != NULL) { if (aux [0] == 'q' || aux [0] == 'Q') stop = true; } diff --git a/example/diameter/launcher/Launcher.cpp b/example/diameter/launcher/Launcher.cpp index ffc814b..8f3cacd 100644 --- a/example/diameter/launcher/Launcher.cpp +++ b/example/diameter/launcher/Launcher.cpp @@ -1525,7 +1525,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons if(getWorkingNode()->logEnabled()) { anna::diameter::comm::Server *usedServer = entity->getLastUsedResource(); anna::diameter::comm::ClientSession *usedClientSession = usedServer ? usedServer->getLastUsedResource() : NULL; - std::string detail = usedClientSession ? usedClientSession->asString() : ""; // esto no deberia ocurrir + std::string detail = usedClientSession ? usedClientSession->asString() : ""; // shouldn't happen getWorkingNode()->writeLogFile(codecMsg, (success ? "sent2e" : "send2eError"), detail); } } else if((opType == "burst")) { @@ -1884,7 +1884,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons // Detailed log: if(getWorkingNode()->logEnabled()) { anna::diameter::comm::ServerSession *usedServerSession = localServer->getLastUsedResource(); - std::string detail = usedServerSession ? usedServerSession->asString() : ""; // esto no deberia ocurrir + std::string detail = usedServerSession ? usedServerSession->asString() : ""; // shouldn't happen getWorkingNode()->writeLogFile(codecMsg, (success ? "sent2c" : "send2cError"), detail); } } else if(opType == "loadxml") { diff --git a/example/diameter/launcher/testing/TestStep.cpp b/example/diameter/launcher/testing/TestStep.cpp index 491ec65..c8aef4e 100644 --- a/example/diameter/launcher/testing/TestStep.cpp +++ b/example/diameter/launcher/testing/TestStep.cpp @@ -20,7 +20,11 @@ #include #include #include +#include +#include +#include #include +#include #include #include @@ -88,6 +92,29 @@ namespace { // TODO: terminate thread when deprecated (RT signal ?) // TODO: mutex the step while setting data here !! } + + 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 (anna::app::functions::getApp()); + messageCodec = new anna::diameter::codec::Message(my_app.getCodecEngine()); + } + + messageCodec->decode(message); + } + catch (anna::RuntimeException &ex) { + ex.trace(); + result = false; + } + + return result; + } + } @@ -220,6 +247,11 @@ void TestStepTimeout::do_reset() throw() { //////////////////////////////////////////////////////////////////////////////////////////////////////// // TestStepSendxml //////////////////////////////////////////////////////////////////////////////////////////////////////// +TestStepSendxml::~TestStepSendxml() { + delete a_messageCodec; + a_messageCodec = NULL; +} + anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent) const throw() { anna::xml::Node* result = TestStep::asXML(parent); @@ -236,16 +268,10 @@ throw() { } } - if (!a_message.isEmpty()) { - try { - Launcher& my_app = static_cast (anna::app::functions::getApp()); - static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine()); - codecMsg.decode(a_message); - xmlmsg = "\n"; xmlmsg += codecMsg.asXMLString(); xmlmsg += "\n"; - } - catch (anna::RuntimeException &ex) { - ex.trace(); - } + if (decodeMessage(a_message, a_messageCodec)) { + xmlmsg = "\n"; + xmlmsg += a_messageCodec->asXMLString(); + xmlmsg += "\n"; } if (msg != "") result->createAttribute("Message", msg); @@ -258,15 +284,22 @@ throw() { } bool TestStepSendxml::do_execute() throw() { - anna::diameter::comm::Message *msg = a_realmNode->createCommMessage(); bool success = false; std::string failReason, s_warn; + MyDiameterEntity *entity = a_realmNode->getEntity(); // by default + MyLocalServer *localServer = a_realmNode->getDiameterServer(); // by default + const TestStepWait *tsw = NULL; + + // Create comm message: + anna::diameter::comm::Message *msg = a_realmNode->createCommMessage(); + //msg->clearBody(); + msg->setBody(a_message); try { // Update sequence for answers: if (a_waitForRequestStepNumber != -1) { // is an answer: try to copy sequence information; alert about Session-Id discrepance // Request which was received: - const TestStepWait *tsw = (const TestStepWait*)(a_testCase->getStep(a_waitForRequestStepNumber)); + tsw = (const TestStepWait*)(a_testCase->getStep(a_waitForRequestStepNumber)); const anna::DataBlock &request = tsw->getMsgDataBlock(); anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(request); anna::diameter::EndToEnd ete = anna::diameter::codec::functions::getEndToEnd(request); @@ -285,31 +318,81 @@ bool TestStepSendxml::do_execute() throw() { } if (getType() == Type::Sendxml2e) { - MyDiameterEntity *entity = a_realmNode->getEntity(); - if (entity) { - //msg->clearBody(); - msg->setBody(a_message); - /* response = NULL =*/entity->send(msg); - success = true; + anna::diameter::comm::ClientSession *usedClientSession = NULL; + + if (tsw) { // is an answer for a received request on wait condition + anna::diameter::comm::ClientSession *clientSession = tsw->getClientSession(); + if (clientSession) { + /* response NULL (is an answer) */clientSession->send(msg); + success = true; + usedClientSession = clientSession; + } + else { + failReason = "Reference wait step didn't store a valid client session. Unable to send the message"; + LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION)); + } } else { - failReason = "There is no diameter entity currently configured. Unable to send the message"; - LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION)); + if (entity) { + success = entity->send(msg); + anna::diameter::comm::Server *usedServer = entity->getLastUsedResource(); + usedClientSession = usedServer ? usedServer->getLastUsedResource() : NULL; + } + else { + failReason = "There is no diameter entity currently configured. Unable to send the message"; + LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION)); + } + } // else (normal sending) + + // Detailed log: + if(a_realmNode->logEnabled()) { + if (!a_messageCodec) + decodeMessage(a_message, a_messageCodec); + + if (a_messageCodec) { + std::string detail = usedClientSession ? usedClientSession->asString() : ""; // shouldn't happen + a_realmNode->writeLogFile(*a_messageCodec, (success ? "sent2e" : "send2eError"), detail); + } } } else if (getType() == Type::Sendxml2c) { - MyLocalServer *localServer = a_realmNode->getDiameterServer(); - if (localServer) { - //msg->clearBody(); - msg->setBody(a_message); - /* response = NULL =*/localServer->send(msg); - success = true; + anna::diameter::comm::ServerSession *usedServerSession = NULL; + + if (tsw) { // is an answer for a received request on wait condition + anna::diameter::comm::ServerSession *serverSession = tsw->getServerSession(); + if (serverSession) { + /* response NULL (is an answer) */serverSession->send(msg); + success = true; + usedServerSession = serverSession; + } + else { + failReason = "Reference wait step didn't store a valid server session. Unable to send the message"; + LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION)); + } } else { - failReason = "There is no diameter local server currently configured. Unable to send the message"; - LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION)); + if (localServer) { + success = localServer->send(msg); + usedServerSession = localServer->getLastUsedResource(); + } + else { + failReason = "There is no diameter local server currently configured. Unable to send the message"; + LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION)); + } + } // else (normal sending) + + // Detailed log: + if(a_realmNode->logEnabled()) { + if (!a_messageCodec) + decodeMessage(a_message, a_messageCodec); + + if (a_messageCodec) { + std::string detail = usedServerSession ? usedServerSession->asString() : ""; // shouldn't happen + a_realmNode->writeLogFile(*a_messageCodec, (success ? "sent2c" : "send2cError"), detail); + } } } + } catch(anna::RuntimeException &ex) { failReason = ex.asString(); } @@ -378,6 +461,11 @@ 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() { @@ -418,16 +506,10 @@ throw() { } } - if (!a_message.isEmpty()) { - try { - Launcher& my_app = static_cast (anna::app::functions::getApp()); - static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine()); - codecMsg.decode(a_message); - xmlmsg = "\n"; xmlmsg += codecMsg.asXMLString(); xmlmsg += "\n"; - } - catch (anna::RuntimeException &ex) { - ex.trace(); - } + if (decodeMessage(a_message, a_messageCodec)) { + xmlmsg = "\n"; + xmlmsg += a_messageCodec->asXMLString(); + xmlmsg += "\n"; } if (msg != "") result->createAttribute("MatchedMessage", msg); @@ -446,7 +528,8 @@ void TestStepWait::do_complete() throw() { bool TestStepWait::fulfilled(const anna::DataBlock &db/*, bool matchSessionId*/) throw() { if (a_condition.comply(db/*, matchSessionId*/)) { - a_message = db; // store matched + //a_message = db; // store matched + a_message.assign(db); complete(); return true; } diff --git a/example/diameter/launcher/testing/TestStep.hpp b/example/diameter/launcher/testing/TestStep.hpp index 1cb062d..e4574c1 100644 --- a/example/diameter/launcher/testing/TestStep.hpp +++ b/example/diameter/launcher/testing/TestStep.hpp @@ -31,6 +31,9 @@ namespace anna { class Node; } namespace diameter { + namespace codec { + class Message; + } namespace comm { class ClientSession; class ServerSession; @@ -118,12 +121,14 @@ class TestStepSendxml : public TestStep { // Message anna::DataBlock a_message; + anna::diameter::codec::Message *a_messageCodec; // used as helper and for traffic logs // Expired ? bool a_expired; // a_endTimestamp will be the expiration reception timestamp public: - TestStepSendxml(TestCase *testCase) : TestStep(testCase), a_message(true), a_expired(false), a_realmNode(NULL), a_waitForRequestStepNumber(-1) {;} + TestStepSendxml(TestCase *testCase) : TestStep(testCase), a_message(true), a_messageCodec(NULL), a_expired(false), a_realmNode(NULL), a_waitForRequestStepNumber(-1) {;} + ~TestStepSendxml(); // setter & getters void setRealmNode(RealmNode *realm) throw() { a_realmNode = realm; } @@ -174,12 +179,13 @@ class TestStepWait : public TestStep { TestCondition a_condition; anna::DataBlock a_message; // message which complies with condition + anna::diameter::codec::Message *a_messageCodec; // used as helper and for traffic logs anna::diameter::comm::ClientSession *a_clientSession; anna::diameter::comm::ServerSession *a_serverSession; public: - TestStepWait(TestCase *testCase) : TestStep(testCase), a_message(true) { a_type = Type::Wait; a_clientSession = NULL; a_serverSession = NULL; } - ~TestStepWait() {;} + TestStepWait(TestCase *testCase) : TestStep(testCase), a_message(true), a_messageCodec(NULL) { a_type = Type::Wait; a_clientSession = NULL; a_serverSession = NULL; } + ~TestStepWait(); // setter & getters void setCondition(bool fromEntity, @@ -189,6 +195,8 @@ class TestStepWait : public TestStep { void setClientSession(anna::diameter::comm::ClientSession *cs) throw() { a_clientSession = cs; } void setServerSession(anna::diameter::comm::ServerSession *ss) throw() { a_serverSession = ss; } + anna::diameter::comm::ClientSession *getClientSession() const throw() { return a_clientSession; } + anna::diameter::comm::ServerSession *getServerSession() const throw() { return a_serverSession; } const TestCondition &getCondition() const throw() { return a_condition; } //void setMsgDataBlock(const anna::DataBlock &db) throw() { a_message = db; }