Add fourth work package for REST API implementation
[anna.git] / example / diameter / stackManagement / main.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 <iostream>
11 #include <fstream>
12
13 // STL
14 #include <string>
15
16 #include <anna/core/functions.hpp>
17 #include <anna/core/tracing/Logger.hpp>
18 #include <anna/core/tracing/TraceWriter.hpp>
19 #include <anna/core/RuntimeException.hpp>
20 #include <anna/xml/xml.hpp>
21 #include <anna/diameter/stack/Engine.hpp>
22
23
24 using namespace anna;
25 using namespace anna::diameter;
26
27
28
29 void _exit(const std::string & msg) {
30   std::cout << std::endl << msg << std::endl;
31   exit(-1);
32 }
33
34
35 int main(int argc, char** argv) {
36   std::string exec = argv[0];
37   std::string execBN = exec.substr(exec.find_last_of("/") + 1);
38   std::string filetrace = execBN + ".trace";
39   Logger::setLevel(Logger::Debug);
40   Logger::initialize(execBN.c_str(), new TraceWriter(filetrace.c_str(), 2048000));
41   stack::Engine & engine = stack::Engine::instantiate();
42
43   if(argc < 2) {
44     std::string msg = anna::functions::asString("Usage: %s <list of '.xml' dictionaries overloaded>\n       e.g. '%s avps.xml commands.xml'", exec.c_str(), exec.c_str());
45     _exit(msg);
46   }
47
48   stack::Dictionary *dictionary;
49   int index = 1;
50
51   try {
52     dictionary = engine.createDictionary(0 /* general unique stack id */);
53     dictionary->allowUpdates();
54
55     while(index < argc) {
56       dictionary->load(argv[index]);
57       index++;
58     }
59
60     engine.removeStack(0);
61     // Trace:
62     LOGINFORMATION(Logger::information(engine.asString(), ANNA_FILE_LOCATION));
63     LOGDEBUG(Logger::debug(dictionary->asString(), ANNA_FILE_LOCATION));
64     // Output:
65     std::ofstream out("./result.xml", std::ifstream::out);
66     std::string buffer = dictionary->asXMLString();
67     out.write(buffer.c_str(), buffer.size());
68     out.close();
69     std::cout << "Written 'result.xml'" << std::endl;
70   } catch(anna::RuntimeException &ex) {
71     ex.trace();
72     std::cout << ex.getText() << std::endl;
73   }
74
75   _exit("Open 'file.trace' in order to see the stacks loaded");
76 }
77