NEW Restruct launcher source code. Separate classes in different files to improve...
[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         anna::diameter::codec::Engine *engine = anna::functions::component <Engine> (ANNA_FILE_LOCATION);
23         engine->releaseMessage(*(it->second->begin()));
24         delete(it->second);
25   }
26   a_deques.clear();
27 }
28
29 void ProgrammedAnswers::dump () throw() {
30   std::string outfilename, xmlString;
31   for(reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
32         int sequence = 1;
33         for(codec_messages_deque_const_iterator itm = it->second->begin(); itm != it->second->end(); itm++) {
34           // programmed_answer.<code>.<sequence>
35           outfilename = "programmed_answer.";
36           outfilename += anna::functions::asString(it->first);
37           outfilename += ".";
38           outfilename += anna::functions::asString(sequence++);
39           outfilename += ".xml";
40           std::ofstream outfile(outfilename.c_str(), std::ifstream::out);
41           xmlString =  (*itm)->asXMLString();
42           outfile.write(xmlString.c_str(), xmlString.size());
43           outfile.close();
44         }
45   }
46 }
47
48 void ProgrammedAnswers::addMessage(int code, anna::diameter::codec::Message *message) throw() {
49   reacting_answers_const_iterator it = a_deques.find(code);
50   if (it != a_deques.end()) {
51         it->second->push_back(message);
52   }
53   else {
54         codec_messages_deque *deque = new codec_messages_deque;
55         a_deques[code] = deque;
56         deque->push_back(message);
57   }
58 }
59
60 anna::diameter::codec::Message* ProgrammedAnswers::getMessage(int code) const throw() { //get the front message (begin()), returns NULL if deque is empty
61   anna::diameter::codec::Message *result = NULL;
62   reacting_answers_const_iterator it = a_deques.find(code);
63   if (it != a_deques.end()) {
64         if (!it->second->empty()) result = *(it->second->begin());
65   }
66   return result;
67 }
68
69 void ProgrammedAnswers::nextMessage(int code) throw() { //pops the deque and release the message (when deque is not empty: deque::empty)
70   reacting_answers_const_iterator it = a_deques.find(code);
71   if (it != a_deques.end()) {
72         if (!it->second->empty()) {
73           anna::diameter::codec::Engine *engine = anna::functions::component <Engine> (ANNA_FILE_LOCATION);
74           if (a_rotate) {
75                 addMessage(code, *(it->second->begin()));
76           }
77           else {
78                 engine->releaseMessage(*(it->second->begin()));
79           }
80           it->second->pop_front();
81         }
82   }
83 }
84
85 std::string ProgrammedAnswers::asString(const char *queueName) const throw() {
86   std::string result = "";
87   std::string aux = "FIFO QUEUE '";
88   aux += queueName;
89   aux += "', Rotation ";
90   aux += a_rotate ? "enabled":"disabled";
91   result += anna::functions::highlightJustify(aux);
92   if(a_deques.size() != 0) {
93         for(reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
94           if (it->second->size() != 0) {
95                 aux = "Answer code ";
96                 aux += anna::functions::asString(it->first);
97                 result += anna::functions::highlightJustify(aux, anna::functions::TextHighlightMode::OverAndUnderline,
98                                                                                                                  anna::functions::TextJustifyMode::Left, '-');
99                 for(codec_messages_deque_const_iterator itm = it->second->begin(); itm != it->second->end(); itm++) {
100                   result += (*itm)->asXMLString();
101                   result += "\n";
102                 }
103                 result += "\n";
104           }
105         }
106   }
107   else {
108         result = "No ocurrences found\n\n";
109   }
110   return result;
111 }