Dynamic lib selection and deployment
[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     anna::diameter::codec::Engine *engine;
25
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());
29       if (engine) {
30         engine->releaseMessage(message);
31         delete (it->second);
32       }
33       else {
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));
35       }
36     }
37     a_deques.clear();
38   }
39   catch (anna::RuntimeException &ex) {
40     ex.trace();
41   }
42 }
43
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++) {
48     int sequence = 1;
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);
54       outfilename += ".";
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());
60       outfile.close();
61     }
62   }
63 }
64
65 void ProgrammedAnswers::addMessage(int code, anna::diameter::codec::Message *message) throw () {
66   if (!message)
67     return; // just in case
68
69   reacting_answers_const_iterator it = a_deques.find(code);
70   if (it != a_deques.end()) {
71     it->second->push_back(message);
72   } else {
73     codec_messages_deque *deque = new codec_messages_deque;
74     a_deques[code] = deque;
75     deque->push_back(message);
76   }
77 }
78
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());
85   }
86   return result;
87 }
88
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;
91
92   try {
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());
97         if (a_rotate) {
98           addMessage(code, message);
99         } else {
100           engine = anna::diameter::codec::EngineManager::instantiate().getCodecEngine(message->getApplicationId());
101           if (engine) {
102             engine->releaseMessage(message);
103           }
104           else {
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));
106             return;
107           }
108         }
109         it->second->pop_front();
110       }
111     }
112   }
113   catch (anna::RuntimeException &ex) {
114     ex.trace();
115   }
116 }
117
118 std::string ProgrammedAnswers::asString(const char *queueName) const throw () {
119   std::string result = "";
120   std::string aux = "FIFO QUEUE '";
121   aux += queueName;
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();
137           result += "\n";
138         }
139         result += "\n";
140       }
141     }
142   } else {
143     result = "No ocurrences found\n\n";
144   }
145   return result;
146 }