Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / source / diameter / codec / tme / Avp.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 // Local
10 #include <anna/diameter/codec/tme/Avp.hpp>
11 #include <anna/diameter/codec/tme/Engine.hpp>
12
13 #include <anna/diameter/helpers/defines.hpp>
14 #include <anna/diameter/stack/Format.hpp>
15 #include <anna/core/functions.hpp>
16
17 #include <anna/xml/xml.hpp>
18
19 using namespace anna;
20 using namespace anna::diameter::codec::tme;
21
22
23
24 //------------------------------------------------------------------------------
25 //------------------------------------------------------------------- Avp::Avp()
26 //------------------------------------------------------------------------------
27 Avp::Avp() {
28   initialize();
29 }
30
31
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------- Avp::Avp()
34 //------------------------------------------------------------------------------
35 Avp::Avp(AvpId id) {
36   initialize();
37   setId(id);
38 }
39
40
41 //------------------------------------------------------------------------------
42 //------------------------------------------------------------------ Avp::~Avp()
43 //------------------------------------------------------------------------------
44 Avp::~Avp() {
45   clear();
46 }
47
48
49 //------------------------------------------------------------------------------
50 //------------------------------------------------------------- Avp::getEngine()
51 //------------------------------------------------------------------------------
52 anna::diameter::codec::Engine * Avp::getEngine() const throw(anna::RuntimeException) {
53   return a_engine ? a_engine : (a_engine = (anna::diameter::codec::Engine *)anna::functions::component <Engine> (ANNA_FILE_LOCATION));
54 }
55
56
57 //------------------------------------------------------------------------------
58 //---------------------------------------------------- Avp::initializeByFormat()
59 //------------------------------------------------------------------------------
60 void Avp::initializeByFormat() throw() {
61   a_ISDNNumber = NULL;
62   a_ISDNAddress = NULL;
63   a_Unsigned16 = NULL;
64 }
65
66
67 //------------------------------------------------------------------------------
68 //--------------------------------------------------------- Avp::clearByFormat()
69 //------------------------------------------------------------------------------
70 void Avp::clearByFormat() throw() {
71   delete a_ISDNNumber;
72   delete a_ISDNAddress;
73   delete a_Unsigned16;
74 }
75
76
77 //------------------------------------------------------------------------------
78 //---------------------------------------------------- Avp::allocationByFormat()
79 //------------------------------------------------------------------------------
80 void Avp::allocationByFormat(const stack::Format *stackFormat) throw() {
81   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber = new ISDNNumber();
82   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress = new ISDNAddress();
83   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16 = new Unsigned16();
84 }
85
86
87 //------------------------------------------------------------------------------
88 //----------------------------------------------------- Avp::getLengthByFormat()
89 //------------------------------------------------------------------------------
90 U24 Avp::getLengthByFormat(const stack::Format *stackFormat) const throw() {
91   U24 result = 0;
92
93   if(stackFormat->getName() == "ISDNNumber") result += a_ISDNNumber->getSize();
94   else if(stackFormat->getName() == "ISDNAddress") result += a_ISDNAddress->getSize();
95   else if(stackFormat->getName() == "Unsigned16") result += a_Unsigned16->getSize();
96
97   return result;
98 }
99
100
101 //------------------------------------------------------------------------------
102 //------------------------------------------------ Avp::decodeDataPartByFormat()
103 //------------------------------------------------------------------------------
104 void Avp::decodeDataPartByFormat(const char * buffer, int size, const stack::Format *stackFormat) throw(anna::RuntimeException) {
105   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber->decode(buffer, size);
106   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress->decode(buffer, size);
107   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16->decode(buffer, size);
108 }
109
110
111 //------------------------------------------------------------------------------
112 //---------------------------------------------------------- Avp::codeByFormat()
113 //------------------------------------------------------------------------------
114 void Avp::codeByFormat(char* dataPart, const stack::Format *stackFormat) const throw(anna::RuntimeException) {
115   int dataBytes;
116
117   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber->code(dataPart, dataBytes);
118   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress->code(dataPart, dataBytes);
119   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16->code(dataPart, dataBytes);
120 }
121
122
123 //------------------------------------------------------------------------------
124 //---------------------------------------------------- Avp::getXMLdataByFormat()
125 //------------------------------------------------------------------------------
126 std::string Avp::getXMLdataByFormat(bool & isHex, const stack::Format *stackFormat) const throw() {
127   std::string result;
128
129   if(stackFormat->getName() == "ISDNNumber") {
130     isHex = true;
131     return a_ISDNNumber->asHexString();
132   } else if(stackFormat->getName() == "ISDNAddress") {
133     isHex = true;
134     return a_ISDNAddress->asHexString();
135   } else if(stackFormat->getName() == "Unsigned16") return a_Unsigned16->asPrintableString();
136
137   return result;
138 }
139
140
141 //------------------------------------------------------------------------------
142 //------------------------------------------------------- Avp::fromXMLByFormat()
143 //------------------------------------------------------------------------------
144 void Avp::fromXMLByFormat(const anna::xml::Attribute* data, const anna::xml::Attribute* hexData, const stack::Format *stackFormat) throw(anna::RuntimeException) {
145   if(stackFormat->getName() == "ISDNNumber") {
146     if(data) a_ISDNNumber->fromPrintableString(data->getValue().c_str());
147     else if(hexData) a_ISDNNumber->fromHexString(hexData->getValue());
148   } else if(stackFormat->getName() == "ISDNAddress") {
149     if(data) a_ISDNAddress->fromPrintableString(data->getValue().c_str());
150     else if(hexData) a_ISDNAddress->fromHexString(hexData->getValue());
151   } else if(stackFormat->getName() == "Unsigned16") {
152     if(data) a_Unsigned16->fromPrintableString(data->getValue().c_str());
153     else if(hexData) a_Unsigned16->fromHexString(hexData->getValue());
154   }
155 }
156