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