1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
13 #include <anna/diameter/codec/Message.hpp>
14 #include <anna/diameter/codec/Engine.hpp>
15 #include <anna/diameter/codec/EngineManager.hpp>
18 #include <ProgrammedAnswers.hpp>
21 void ProgrammedAnswers::clear() throw () {
23 anna::diameter::codec::EngineManager &em = anna::diameter::codec::EngineManager::instantiate();
24 anna::diameter::codec::Engine *engine;
26 for (reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
27 anna::diameter::codec::Message *message = *(it->second->begin());
28 engine = em.getCodecEngine(message->getApplicationId());
30 engine->releaseMessage(message);
34 LOGWARNING(anna::Logger::warning("Cannot release a message for which i don't know the codec engine (check the registered stack id regarding the message application id) !", ANNA_FILE_LOCATION));
39 catch (anna::RuntimeException &ex) {
44 void ProgrammedAnswers::dump() throw () {
45 std::string outfilename, xmlString;
46 for (reacting_answers_const_iterator it = a_deques.begin();
47 it != a_deques.end(); it++) {
49 for (codec_messages_deque_const_iterator itm = it->second->begin();
50 itm != it->second->end(); itm++) {
51 // programmed_answer.<code>.<sequence>
52 outfilename = "programmed_answer.";
53 outfilename += anna::functions::asString(it->first);
55 outfilename += anna::functions::asString(sequence++);
56 outfilename += ".xml";
57 std::ofstream outfile(outfilename.c_str(), std::ifstream::out);
58 xmlString = (*itm)->asXMLString();
59 outfile.write(xmlString.c_str(), xmlString.size());
65 void ProgrammedAnswers::addMessage(int code, anna::diameter::codec::Message *message) throw () {
67 return; // just in case
69 reacting_answers_const_iterator it = a_deques.find(code);
70 if (it != a_deques.end()) {
71 it->second->push_back(message);
73 codec_messages_deque *deque = new codec_messages_deque;
74 a_deques[code] = deque;
75 deque->push_back(message);
79 anna::diameter::codec::Message* ProgrammedAnswers::getMessage(int code) const throw () { //get the front message (begin()), returns NULL if deque is empty
80 anna::diameter::codec::Message *result = NULL;
81 reacting_answers_const_iterator it = a_deques.find(code);
82 if (it != a_deques.end()) {
83 if (!it->second->empty())
84 result = *(it->second->begin());
89 void ProgrammedAnswers::nextMessage(int code) throw () { //pops the deque and release the message (when deque is not empty: deque::empty)
90 anna::diameter::codec::Engine *engine;
93 reacting_answers_const_iterator it = a_deques.find(code);
94 if (it != a_deques.end()) {
95 if (!it->second->empty()) {
96 anna::diameter::codec::Message *message = *(it->second->begin());
98 addMessage(code, message);
100 engine = anna::diameter::codec::EngineManager::instantiate().getCodecEngine(message->getApplicationId());
102 engine->releaseMessage(message);
105 LOGWARNING(anna::Logger::warning("Cannot release a message for which i don't know the codec engine (check the registered stack id regarding the message application id) !", ANNA_FILE_LOCATION));
109 it->second->pop_front();
113 catch (anna::RuntimeException &ex) {
118 std::string ProgrammedAnswers::asString(const char *queueName) const throw () {
119 std::string result = "";
120 std::string aux = "FIFO QUEUE '";
122 aux += "', Rotation ";
123 aux += a_rotate ? "enabled" : "disabled";
124 result += anna::functions::highlightJustify(aux);
125 if (a_deques.size() != 0) {
126 for (reacting_answers_const_iterator it = a_deques.begin();
127 it != a_deques.end(); it++) {
128 if (it->second->size() != 0) {
129 aux = "Answer code ";
130 aux += anna::functions::asString(it->first);
131 result += anna::functions::highlightJustify(aux,
132 anna::functions::TextHighlightMode::OverAndUnderline,
133 anna::functions::TextJustifyMode::Left, '-');
134 for (codec_messages_deque_const_iterator itm = it->second->begin();
135 itm != it->second->end(); itm++) {
136 result += (*itm)->asXMLString();
143 result = "No ocurrences found\n\n";