8e310d72a4160d3b994b8d4c783144ad083b5229
[anna.git] / example / diameter / launcher / ProgrammedAnswers.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 // Standard
10 #include <string>
11 #include <fstream>
12
13 // Project
14 #include <anna/diameter/codec/Engine.hpp>
15
16 // Process
17 #include "ProgrammedAnswers.hpp"
18
19
20 void ProgrammedAnswers::clear () throw() {
21   for (reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
22         a_codecEngine->releaseMessage(*(it->second->begin()));
23         delete(it->second);
24   }
25   a_deques.clear();
26 }
27
28 void ProgrammedAnswers::dump () throw() {
29   std::string outfilename, xmlString;
30   for(reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
31         int sequence = 1;
32         for(codec_messages_deque_const_iterator itm = it->second->begin(); itm != it->second->end(); itm++) {
33           // programmed_answer.<code>.<sequence>
34           outfilename = "programmed_answer.";
35           outfilename += anna::functions::asString(it->first);
36           outfilename += ".";
37           outfilename += anna::functions::asString(sequence++);
38           outfilename += ".xml";
39           std::ofstream outfile(outfilename.c_str(), std::ifstream::out);
40           xmlString =  (*itm)->asXMLString();
41           outfile.write(xmlString.c_str(), xmlString.size());
42           outfile.close();
43         }
44   }
45 }
46
47 void ProgrammedAnswers::addMessage(int code, anna::diameter::codec::Message *message) throw() {
48   if (!message) return; // just in case
49   message->setEngine(a_codecEngine); // just in case
50
51   reacting_answers_const_iterator it = a_deques.find(code);
52   if (it != a_deques.end()) {
53         it->second->push_back(message);
54   }
55   else {
56         codec_messages_deque *deque = new codec_messages_deque;
57         a_deques[code] = deque;
58         deque->push_back(message);
59   }
60 }
61
62 anna::diameter::codec::Message* ProgrammedAnswers::getMessage(int code) const throw() { //get the front message (begin()), returns NULL if deque is empty
63   anna::diameter::codec::Message *result = NULL;
64   reacting_answers_const_iterator it = a_deques.find(code);
65   if (it != a_deques.end()) {
66         if (!it->second->empty()) result = *(it->second->begin());
67   }
68   return result;
69 }
70
71 void ProgrammedAnswers::nextMessage(int code) throw() { //pops the deque and release the message (when deque is not empty: deque::empty)
72   reacting_answers_const_iterator it = a_deques.find(code);
73   if (it != a_deques.end()) {
74         if (!it->second->empty()) {
75           if (a_rotate) {
76                 addMessage(code, *(it->second->begin()));
77           }
78           else {
79                 a_codecEngine->releaseMessage(*(it->second->begin()));
80           }
81           it->second->pop_front();
82         }
83   }
84 }
85
86 std::string ProgrammedAnswers::asString(const char *queueName) const throw() {
87   std::string result = "";
88   std::string aux = "FIFO QUEUE '";
89   aux += queueName;
90   aux += "', Rotation ";
91   aux += a_rotate ? "enabled":"disabled";
92   result += anna::functions::highlightJustify(aux);
93   result += "Codec engine: ";
94   result += a_codecEngine->getClassName();
95   if(a_deques.size() != 0) {
96         for(reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
97           if (it->second->size() != 0) {
98                 aux = "Answer code ";
99                 aux += anna::functions::asString(it->first);
100                 result += anna::functions::highlightJustify(aux, anna::functions::TextHighlightMode::OverAndUnderline,
101                                                                                                                  anna::functions::TextJustifyMode::Left, '-');
102                 for(codec_messages_deque_const_iterator itm = it->second->begin(); itm != it->second->end(); itm++) {
103                   result += (*itm)->asXMLString();
104                   result += "\n";
105                 }
106                 result += "\n";
107           }
108         }
109   }
110   else {
111         result = "No ocurrences found\n\n";
112   }
113   return result;
114 }