Add anna_ to libraries and examples
[anna.git] / example / diameter / tme / 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/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   std::string exec = argv[0];
61   std::string param = argv[1] ? argv[1] : "";
62
63   if(argc < 2) {
64     std::string msg = "Usage: "; msg += exec; msg += " <xml directory>";
65     msg += "\n     xml directory: contains the xml files needed, which are:";
66     msg += "\n";
67     msg += "\n                       avps_ietf.xml";
68     msg += "\n                       avps_etsi.xml";
69     msg += "\n                       avps_tgpp.xml";
70     msg += "\n                       avps_tme.xml";
71     msg += "\n                       commands_dccaOCS-CS_HuaweiNGIN_de-es.xml";
72     msg += "\n";
73     msg += "\n     For example:   "; msg += exec; msg += " ";
74     std::string projectRoot = exec_cmd("git rev-parse --show-toplevel");
75
76     if(projectRoot != "") {
77       //projectRoot.resize(projectRoot.size()-1);
78       projectRoot.erase(projectRoot.size() - 1);
79       msg += projectRoot;
80       msg += "/";
81     }
82
83     msg += "source/diameter/stack/setups";
84     _exit(msg);
85   }
86
87   try {
88     dictionary = stackEngine.createDictionary(0 /* general unique stack id */);
89     dictionary->allowUpdates();
90     dictionary->load(param + "/avps_ietf.xml");
91     dictionary->load(param + "/avps_etsi.xml");
92     dictionary->load(param + "/avps_tgpp.xml");
93     dictionary->load(param + "/avps_tme.xml");
94     dictionary->load(param + "/commands_dccaOCS-CS_HuaweiNGIN_de-es.xml");
95     // Trace:
96     LOGINFORMATION(Logger::information(stackEngine.asString(), ANNA_FILE_LOCATION));
97     LOGDEBUG(Logger::debug(dictionary->asString(), ANNA_FILE_LOCATION));
98   } catch(anna::RuntimeException &ex) {
99     ex.trace();
100     std::cout << ex.getText() << std::endl;
101   }
102
103   // Build CCA
104   anna::diameter::codec::Engine *codecEngine = new anna::diameter::codec::Engine("TME Engine", dictionary);
105   LOGDEBUG(Logger::debug(codecEngine->asString(), ANNA_FILE_LOCATION));
106   tmeMessage cca(codecEngine);
107   cca.setId("Credit-Control-Answer");
108   tmeAvp *sid = (tmeAvp*)cca.addAvp("Session-Id");
109   tmeAvp *ohst = (tmeAvp*)cca.addAvp("Origin-Host");
110   tmeAvp *orlm = (tmeAvp*)cca.addAvp("Origin-Realm");
111   sid->getUTF8String()->setValue("grump.example.com:33041;23432;893;0AF3B81");
112   ohst->getDiameterIdentity()->setValue("c0-10-70-238-74-w5p1vepg1.vepc.ericsson.se");
113   orlm->getDiameterIdentity()->setValue("vepc.ericsson.se");
114   int inRange = 65535;
115   int outRange = inRange + 1;
116   //((tmeAvp*)cca.addAvp("CS-Result-Code"))->getUnsigned16()->setValue(inRange);
117   ((tmeAvp*)cca.addAvp("CS-Result-Code"))->getUnsigned16()->setValue(outRange); // WILL BE 0 !!
118   /*
119      <command name="Credit-Control-Answer" code="272" type="Answer">
120         <avprule id="Session-Id" type="Fixed"/>
121         <avprule id="Result-Code" type="Mandatory"/>
122         <avprule id="CS-Result-Code" type="Optional"/>
123         <avprule id="CS-Dialogue-Handling" type="Optional"/>
124         <avprule id="Origin-Host" type="Mandatory"/>
125         <avprule id="Origin-Realm" type="Mandatory"/>
126         <avprule id="Auth-Application-Id" type="Mandatory"/>
127         <avprule id="CC-Request-Type" type="Mandatory"/>
128         <avprule id="CC-Request-Number" type="Mandatory"/>
129         <avprule id="Granted-Service-Unit" type="Optional"/>
130         <avprule id="Cost-Information" type="Optional"/>
131         <avprule id="Low-Balance-Indication" type="Optional"/>
132         <avprule id="Remaining-Balance" type="Optional"/>
133         <avprule id="Credit-Control-Failure-Handling" type="Optional"/>
134         <avprule id="CC-Correlation-Id" type="Optional"/>
135         <avprule id="Redirect-Host" type="Optional" qual="*"/>
136         <avprule id="Redirect-Host-Usage" type="Optional"/>
137         <avprule id="Redirect-Max-Cache-Time" type="Optional"/>
138         <avprule id="Proxy-Info" type="Optional" qual="*"/>
139         <avprule id="Route-Record" type="Optional" qual="*"/>
140         <avprule id="Failed-AVP" type="Optional" qual="*"/>
141         <avprule id="Service-Information" type="Optional"/>
142         <avprule id="AVP" type="Optional" qual="*"/>
143      </command>
144   */
145   LOGDEBUG(Logger::debug(cca.asXMLString(), ANNA_FILE_LOCATION));
146   _exit("Open 'file.trace' in order to see traces");
147 }
148