3f64a3504c504931857c11a0792beebdb271e8b0
[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 // Standard
9 #include <string>
10 #include <fstream>
11
12 // Project
13 #include <anna/diameter/codec/Message.hpp>
14 #include <anna/diameter/codec/Engine.hpp>
15 #include <anna/diameter/codec/EngineManager.hpp>
16
17 // Process
18 #include <ProgrammedAnswers.hpp>
19
20
21 void ProgrammedAnswers::clear() throw () {
22   try {
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);
27       delete (it->second);
28     }
29     a_deques.clear();
30   }
31   catch (anna::RuntimeException &ex) {
32     ex.trace();
33   }
34 }
35
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++) {
40     int sequence = 1;
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);
46       outfilename += ".";
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());
52       outfile.close();
53     }
54   }
55 }
56
57 void ProgrammedAnswers::addMessage(int code, anna::diameter::codec::Message *message) throw () {
58   if (!message)
59     return; // just in case
60
61   reacting_answers_const_iterator it = a_deques.find(code);
62   if (it != a_deques.end()) {
63     it->second->push_back(message);
64   } else {
65     codec_messages_deque *deque = new codec_messages_deque;
66     a_deques[code] = deque;
67     deque->push_back(message);
68   }
69 }
70
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());
77   }
78   return result;
79 }
80
81 void ProgrammedAnswers::nextMessage(int code) throw () { //pops the deque and release the message (when deque is not empty: deque::empty)
82   try {
83     reacting_answers_const_iterator it = a_deques.find(code);
84     if (it != a_deques.end()) {
85       if (!it->second->empty()) {
86         if (a_rotate) {
87           addMessage(code, *(it->second->begin()));
88         } else {
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);
92         }
93         it->second->pop_front();
94       }
95     }
96   }
97   catch (anna::RuntimeException &ex) {
98     ex.trace();
99   }
100 }
101
102 std::string ProgrammedAnswers::asString(const char *queueName) const throw () {
103   std::string result = "";
104   std::string aux = "FIFO QUEUE '";
105   aux += queueName;
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();
121           result += "\n";
122         }
123         result += "\n";
124       }
125     }
126   } else {
127     result = "No ocurrences found\n\n";
128   }
129   return result;
130 }