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 for (reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
25 anna::diameter::codec::Message *message = *(it->second->begin());
26 em.getCodecEngine(message->getApplicationId())->releaseMessage(message);
31 catch (anna::RuntimeException &ex) {
36 void ProgrammedAnswers::dump() throw () {
37 std::string outfilename, xmlString;
38 for (reacting_answers_const_iterator it = a_deques.begin();
39 it != a_deques.end(); it++) {
41 for (codec_messages_deque_const_iterator itm = it->second->begin();
42 itm != it->second->end(); itm++) {
43 // programmed_answer.<code>.<sequence>
44 outfilename = "programmed_answer.";
45 outfilename += anna::functions::asString(it->first);
47 outfilename += anna::functions::asString(sequence++);
48 outfilename += ".xml";
49 std::ofstream outfile(outfilename.c_str(), std::ifstream::out);
50 xmlString = (*itm)->asXMLString();
51 outfile.write(xmlString.c_str(), xmlString.size());
57 void ProgrammedAnswers::addMessage(int code, anna::diameter::codec::Message *message) throw () {
59 return; // just in case
61 reacting_answers_const_iterator it = a_deques.find(code);
62 if (it != a_deques.end()) {
63 it->second->push_back(message);
65 codec_messages_deque *deque = new codec_messages_deque;
66 a_deques[code] = deque;
67 deque->push_back(message);
71 anna::diameter::codec::Message* ProgrammedAnswers::getMessage(int code) const throw () { //get the front message (begin()), returns NULL if deque is empty
72 anna::diameter::codec::Message *result = NULL;
73 reacting_answers_const_iterator it = a_deques.find(code);
74 if (it != a_deques.end()) {
75 if (!it->second->empty())
76 result = *(it->second->begin());
81 void ProgrammedAnswers::nextMessage(int code) throw () { //pops the deque and release the message (when deque is not empty: deque::empty)
83 reacting_answers_const_iterator it = a_deques.find(code);
84 if (it != a_deques.end()) {
85 if (!it->second->empty()) {
87 addMessage(code, *(it->second->begin()));
89 anna::diameter::codec::EngineManager &em = anna::diameter::codec::EngineManager::instantiate();
90 anna::diameter::codec::Message *message = *(it->second->begin());
91 em.getCodecEngine(message->getApplicationId())->releaseMessage(message);
93 it->second->pop_front();
97 catch (anna::RuntimeException &ex) {
102 std::string ProgrammedAnswers::asString(const char *queueName) const throw () {
103 std::string result = "";
104 std::string aux = "FIFO QUEUE '";
106 aux += "', Rotation ";
107 aux += a_rotate ? "enabled" : "disabled";
108 result += anna::functions::highlightJustify(aux);
109 if (a_deques.size() != 0) {
110 for (reacting_answers_const_iterator it = a_deques.begin();
111 it != a_deques.end(); it++) {
112 if (it->second->size() != 0) {
113 aux = "Answer code ";
114 aux += anna::functions::asString(it->first);
115 result += anna::functions::highlightJustify(aux,
116 anna::functions::TextHighlightMode::OverAndUnderline,
117 anna::functions::TextJustifyMode::Left, '-');
118 for (codec_messages_deque_const_iterator itm = it->second->begin();
119 itm != it->second->end(); itm++) {
120 result += (*itm)->asXMLString();
127 result = "No ocurrences found\n\n";