3bd2df513a6d44e25731b0d966a6db925fb8c220
[anna.git] / 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/core.hpp>
17 #include <anna/xml/xml.hpp>
18 #include <anna/diameter/stack/Engine.hpp>
19 #include <anna/time/functions.hpp>
20 #include <anna/time/Date.hpp>
21
22 #include <anna/diameter/codec/tme/Engine.hpp>
23 #include <anna/diameter/codec/tme/Message.hpp>
24
25
26 typedef anna::diameter::codec::tme::Message tmeMessage;
27 typedef anna::diameter::codec::tme::Avp tmeAvp;
28
29
30 void _exit(const std::string & msg) {
31   std::cout << std::endl << msg << std::endl;
32   exit(-1);
33 }
34
35
36 std::string exec_cmd(const char* cmd) {
37   FILE* pipe = popen(cmd, "r");
38
39   if(!pipe) return "";
40
41   char buffer[128];
42   std::string result = "";
43
44   while(!feof(pipe)) {
45     if(fgets(buffer, 128, pipe) != NULL)
46       result += buffer;
47   }
48
49   pclose(pipe);
50   return result;
51 }
52
53 int main(int argc, char** argv) {
54   Logger::setLevel(Logger::Debug);
55   Logger::initialize("tme", new TraceWriter("file.trace", 2048000));
56   anna::time::functions::initialize(); // before application instantiation (it have a anna::time object)
57   anna::time::functions::setControlPoint(); // start control point (application lifetime)
58   anna::diameter::stack::Engine & stackEngine = anna::diameter::stack::Engine::instantiate();
59   anna::diameter::stack::Dictionary *dictionary;
60   anna::diameter::codec::tme::Engine *codecEngine = new anna::diameter::codec::tme::Engine();
61   std::string exec = argv[0];
62   std::string param = argv[1] ? argv[1] : "";
63
64   if(argc < 2) {
65     std::string msg = "Usage: "; msg += exec; msg += " <xml directory>";
66     msg += "\n     xml directory: contains the xml files needed, which are:";
67     msg += "\n";
68     msg += "\n                       avps_ietf.xml";
69     msg += "\n                       avps_etsi.xml";
70     msg += "\n                       avps_tgpp.xml";
71     msg += "\n                       avps_tme.xml";
72     msg += "\n                       commands_dccaOCS-CS_HuaweiNGIN_de-es.xml";
73     msg += "\n";
74     msg += "\n     For example:   "; msg += exec; msg += " ";
75     std::string projectRoot = exec_cmd("git rev-parse --show-toplevel");
76
77     if(projectRoot != "") {
78       //projectRoot.resize(projectRoot.size()-1);
79       projectRoot.erase(projectRoot.size() - 1);
80       msg += projectRoot;
81       msg += "/";
82     }
83
84     msg += "source/diameter/stack/setups";
85     _exit(msg);
86   }
87
88   try {
89     dictionary = stackEngine.createDictionary(0 /* general unique stack id */);
90     dictionary->allowUpdates();
91     dictionary->load(param + "/avps_ietf.xml");
92     dictionary->load(param + "/avps_etsi.xml");
93     dictionary->load(param + "/avps_tgpp.xml");
94     dictionary->load(param + "/avps_tme.xml");
95     dictionary->load(param + "/commands_dccaOCS-CS_HuaweiNGIN_de-es.xml");
96     codecEngine->setDictionary(dictionary);
97     // Trace:
98     LOGINFORMATION(Logger::information(stackEngine.asString(), ANNA_FILE_LOCATION));
99     LOGDEBUG(Logger::debug(dictionary->asString(), ANNA_FILE_LOCATION));
100     LOGDEBUG(Logger::debug(codecEngine->asString(), ANNA_FILE_LOCATION));
101   } catch(anna::RuntimeException &ex) {
102     ex.trace();
103     std::cout << ex.getText() << std::endl;
104   }
105
106   // Build CCA
107   tmeMessage cca;
108   cca.setId("Credit-Control-Answer");
109   tmeAvp *sid = (tmeAvp*)cca.addAvp("Session-Id");
110   tmeAvp *ohst = (tmeAvp*)cca.addAvp("Origin-Host");
111   tmeAvp *orlm = (tmeAvp*)cca.addAvp("Origin-Realm");
112   sid->getUTF8String()->setValue("grump.example.com:33041;23432;893;0AF3B81");
113   ohst->getDiameterIdentity()->setValue("c0-10-70-238-74-w5p1vepg1.vepc.ericsson.se");
114   orlm->getDiameterIdentity()->setValue("vepc.ericsson.se");
115   int inRange = 65535;
116   int outRange = inRange + 1;
117   //((tmeAvp*)cca.addAvp("CS-Result-Code"))->getUnsigned16()->setValue(inRange);
118   ((tmeAvp*)cca.addAvp("CS-Result-Code"))->getUnsigned16()->setValue(outRange); // WILL BE 0 !!
119   /*
120      <command name="Credit-Control-Answer" code="272" type="Answer">
121         <avprule id="Session-Id" type="Fixed"/>
122         <avprule id="Result-Code" type="Mandatory"/>
123         <avprule id="CS-Result-Code" type="Optional"/>
124         <avprule id="CS-Dialogue-Handling" type="Optional"/>
125         <avprule id="Origin-Host" type="Mandatory"/>
126         <avprule id="Origin-Realm" type="Mandatory"/>
127         <avprule id="Auth-Application-Id" type="Mandatory"/>
128         <avprule id="CC-Request-Type" type="Mandatory"/>
129         <avprule id="CC-Request-Number" type="Mandatory"/>
130         <avprule id="Granted-Service-Unit" type="Optional"/>
131         <avprule id="Cost-Information" type="Optional"/>
132         <avprule id="Low-Balance-Indication" type="Optional"/>
133         <avprule id="Remaining-Balance" type="Optional"/>
134         <avprule id="Credit-Control-Failure-Handling" type="Optional"/>
135         <avprule id="CC-Correlation-Id" type="Optional"/>
136         <avprule id="Redirect-Host" type="Optional" qual="*"/>
137         <avprule id="Redirect-Host-Usage" type="Optional"/>
138         <avprule id="Redirect-Max-Cache-Time" type="Optional"/>
139         <avprule id="Proxy-Info" type="Optional" qual="*"/>
140         <avprule id="Route-Record" type="Optional" qual="*"/>
141         <avprule id="Failed-AVP" type="Optional" qual="*"/>
142         <avprule id="Service-Information" type="Optional"/>
143         <avprule id="AVP" type="Optional" qual="*"/>
144      </command>
145   */
146   LOGDEBUG(Logger::debug(cca.asXMLString(), ANNA_FILE_LOCATION));
147   _exit("Open 'file.trace' in order to see traces");
148 }
149