Multistack launcher
[anna.git] / example / diameter / launcher / Node.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
12 // Project
13 //#include <anna/diameter/codec/Engine.hpp>
14
15 // Process
16 #include "Node.hpp"
17
18
19 void Node::clear () throw() {
20   for (reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
21         anna::diameter::codec::Engine *engine = anna::functions::component <Engine> (ANNA_FILE_LOCATION);
22         engine->releaseMessage(*(it->second->begin()));
23         delete(it->second);
24   }
25   a_deques.clear();
26 }
27
28 void Node::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 Node::addMessage(int code, anna::diameter::codec::Message *message) throw() {
48   reacting_answers_const_iterator it = a_deques.find(code);
49   if (it != a_deques.end()) {
50         it->second->push_back(message);
51   }
52   else {
53         codec_messages_deque *deque = new codec_messages_deque;
54         a_deques[code] = deque;
55         deque->push_back(message);
56   }
57 }
58
59 anna::diameter::codec::Message* Node::getMessage(int code) const throw() { //get the front message (begin()), returns NULL if deque is empty
60   anna::diameter::codec::Message *result = NULL;
61   reacting_answers_const_iterator it = a_deques.find(code);
62   if (it != a_deques.end()) {
63         if (!it->second->empty()) result = *(it->second->begin());
64   }
65   return result;
66 }
67
68 void Node::nextMessage(int code) throw() { //pops the deque and release the message (when deque is not empty: deque::empty)
69   reacting_answers_const_iterator it = a_deques.find(code);
70   if (it != a_deques.end()) {
71         if (!it->second->empty()) {
72           anna::diameter::codec::Engine *engine = anna::functions::component <Engine> (ANNA_FILE_LOCATION);
73           if (a_rotate) {
74                 addMessage(code, *(it->second->begin()));
75           }
76           else {
77                 engine->releaseMessage(*(it->second->begin()));
78           }
79           it->second->pop_front();
80         }
81   }
82 }
83
84 std::string Node::asString(const char *queueName) const throw() {
85   std::string result = "";
86   std::string aux = "FIFO QUEUE '";
87   aux += queueName;
88   aux += "', Rotation ";
89   aux += a_rotate ? "enabled":"disabled";
90   result += anna::functions::highlightJustify(aux);
91   if(a_deques.size() != 0) {
92         for(reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
93           if (it->second->size() != 0) {
94                 aux = "Answer code ";
95                 aux += anna::functions::asString(it->first);
96                 result += anna::functions::highlightJustify(aux, anna::functions::TextHighlightMode::OverAndUnderline,
97                                                                                                                  anna::functions::TextJustifyMode::Left, '-');
98                 for(codec_messages_deque_const_iterator itm = it->second->begin(); itm != it->second->end(); itm++) {
99                   result += (*itm)->asXMLString();
100                   result += "\n";
101                 }
102                 result += "\n";
103           }
104         }
105   }
106   else {
107         result = "No ocurrences found\n\n";
108   }
109   return result;
110 }