2461e192854ed8e25572a068a74e0428aef4882c
[anna.git] / source / diameter / codec / MessagesDeque.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 <anna/diameter/codec/MessagesDeque.hpp>
19
20
21 using namespace anna::diameter::codec;
22
23
24 void MessagesDeque::clear() throw () {
25   try {
26     anna::diameter::codec::EngineManager &em = anna::diameter::codec::EngineManager::instantiate();
27     anna::diameter::codec::Engine *engine;
28
29     for (messages_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
30       anna::diameter::codec::Message *message = *(it->second->begin());
31       engine = em.getCodecEngine(message->getApplicationId());
32       if (engine) {
33         engine->releaseMessage(message);
34         delete (it->second);
35       }
36       else {
37         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));
38       }
39     }
40     a_deques.clear();
41   }
42   catch (anna::RuntimeException &ex) {
43     ex.trace();
44   }
45 }
46
47 void MessagesDeque::dump(const char *filenamePrefix) throw () {
48   std::string outfilename, xmlString;
49   for (messages_const_iterator it = a_deques.begin();
50       it != a_deques.end(); it++) {
51     int sequence = 1;
52     for (codec_messages_deque_const_iterator itm = it->second->begin();
53         itm != it->second->end(); itm++) {
54       // message.<code>.<sequence>
55       outfilename = filenamePrefix;
56       outfilename += ".";
57       outfilename += anna::functions::asString(it->first);
58       outfilename += ".";
59       outfilename += anna::functions::asString(sequence++);
60       outfilename += ".xml";
61       std::ofstream outfile(outfilename.c_str(), std::ifstream::out);
62       xmlString = (*itm)->asXMLString();
63       outfile.write(xmlString.c_str(), xmlString.size());
64       outfile.close();
65     }
66   }
67 }
68
69 void MessagesDeque::addMessage(int code, anna::diameter::codec::Message *message) throw () {
70   if (!message)
71     return; // just in case
72
73   messages_const_iterator it = a_deques.find(code);
74   if (it != a_deques.end()) {
75     it->second->push_back(message);
76   } else {
77     codec_messages_deque *deque = new codec_messages_deque;
78     a_deques[code] = deque;
79     deque->push_back(message);
80   }
81 }
82
83 anna::diameter::codec::Message* MessagesDeque::getMessage(int code) const throw () { //get the front message (begin()), returns NULL if deque is empty
84   anna::diameter::codec::Message *result = NULL;
85   messages_const_iterator it = a_deques.find(code);
86   if (it != a_deques.end()) {
87     if (!it->second->empty())
88       result = *(it->second->begin());
89   }
90   return result;
91 }
92
93 void MessagesDeque::nextMessage(int code) throw () { //pops the deque and release the message (when deque is not empty: deque::empty)
94   anna::diameter::codec::Engine *engine;
95
96   try {
97     messages_const_iterator it = a_deques.find(code);
98     if (it != a_deques.end()) {
99       if (!it->second->empty()) {
100         anna::diameter::codec::Message *message = *(it->second->begin());
101         if (a_rotate) {
102           addMessage(code, message);
103         } else {
104           engine = anna::diameter::codec::EngineManager::instantiate().getCodecEngine(message->getApplicationId());
105           if (engine) {
106             engine->releaseMessage(message);
107           }
108           else {
109             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));
110             return;
111           }
112         }
113         it->second->pop_front();
114       }
115     }
116   }
117   catch (anna::RuntimeException &ex) {
118     ex.trace();
119   }
120 }
121
122 std::string MessagesDeque::asString(const char *queueName) const throw () {
123   std::string result = "";
124   std::string aux = "FIFO QUEUE '";
125   aux += queueName;
126   aux += "', Rotation ";
127   aux += a_rotate ? "enabled" : "disabled";
128   result += anna::functions::highlightJustify(aux);
129   if (a_deques.size() != 0) {
130     for (messages_const_iterator it = a_deques.begin();
131         it != a_deques.end(); it++) {
132       if (it->second->size() != 0) {
133         aux = "Message code ";
134         aux += anna::functions::asString(it->first);
135         result += anna::functions::highlightJustify(aux,
136             anna::functions::TextHighlightMode::OverAndUnderline,
137             anna::functions::TextJustifyMode::Left, '-');
138         for (codec_messages_deque_const_iterator itm = it->second->begin();
139             itm != it->second->end(); itm++) {
140           result += (*itm)->asXMLString();
141           result += "\n";
142         }
143         result += "\n";
144       }
145     }
146   } else {
147     result = "No ocurrences found\n\n";
148   }
149   return result;
150 }