Fixes and improvements
[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 //------------------------------------------------------------------ Avp::~Avp()
25 //------------------------------------------------------------------------------
26 Avp::~Avp() {
27   clear();
28 }
29
30
31 ////------------------------------------------------------------------------------
32 ////------------------------------------------------------------- Avp::getEngine()
33 ////------------------------------------------------------------------------------
34 //anna::diameter::codec::Engine * Avp::getEngine() const throw(anna::RuntimeException) {
35 //  if(!a_engine)
36 //    throw anna::RuntimeException("Invalid codec engine reference (NULL)", ANNA_FILE_LOCATION);
37 //
38 //  return a_engine;
39 //}
40
41
42 //------------------------------------------------------------------------------
43 //---------------------------------------------------- Avp::initializeByFormat()
44 //------------------------------------------------------------------------------
45 void Avp::initializeByFormat() throw() {
46   a_ISDNNumber = NULL;
47   a_ISDNAddress = NULL;
48   a_Unsigned16 = NULL;
49 }
50
51
52 //------------------------------------------------------------------------------
53 //--------------------------------------------------------- Avp::clearByFormat()
54 //------------------------------------------------------------------------------
55 void Avp::clearByFormat() throw() {
56   delete a_ISDNNumber;
57   delete a_ISDNAddress;
58   delete a_Unsigned16;
59 }
60
61
62 //------------------------------------------------------------------------------
63 //---------------------------------------------------- Avp::allocationByFormat()
64 //------------------------------------------------------------------------------
65 void Avp::allocationByFormat(const stack::Format *stackFormat) throw() {
66   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber = new ISDNNumber();
67   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress = new ISDNAddress();
68   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16 = new Unsigned16();
69 }
70
71
72 //------------------------------------------------------------------------------
73 //----------------------------------------------------- Avp::getLengthByFormat()
74 //------------------------------------------------------------------------------
75 U24 Avp::getLengthByFormat(const stack::Format *stackFormat) const throw() {
76   U24 result = 0;
77
78   if(stackFormat->getName() == "ISDNNumber") result += a_ISDNNumber->getSize();
79   else if(stackFormat->getName() == "ISDNAddress") result += a_ISDNAddress->getSize();
80   else if(stackFormat->getName() == "Unsigned16") result += a_Unsigned16->getSize();
81
82   return result;
83 }
84
85
86 //------------------------------------------------------------------------------
87 //------------------------------------------------ Avp::decodeDataPartByFormat()
88 //------------------------------------------------------------------------------
89 void Avp::decodeDataPartByFormat(const char * buffer, int size, const stack::Format *stackFormat) throw(anna::RuntimeException) {
90   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber->decode(buffer, size);
91   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress->decode(buffer, size);
92   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16->decode(buffer, size);
93 }
94
95
96 //------------------------------------------------------------------------------
97 //---------------------------------------------------------- Avp::codeByFormat()
98 //------------------------------------------------------------------------------
99 void Avp::codeByFormat(char* dataPart, const stack::Format *stackFormat) const throw(anna::RuntimeException) {
100   int dataBytes;
101
102   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber->code(dataPart, dataBytes);
103   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress->code(dataPart, dataBytes);
104   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16->code(dataPart, dataBytes);
105 }
106
107
108 //------------------------------------------------------------------------------
109 //---------------------------------------------------- Avp::getXMLdataByFormat()
110 //------------------------------------------------------------------------------
111 std::string Avp::getXMLdataByFormat(bool & isHex, const stack::Format *stackFormat) const throw() {
112   std::string result;
113
114   if(stackFormat->getName() == "ISDNNumber") {
115     isHex = true;
116     return a_ISDNNumber->asHexString();
117   } else if(stackFormat->getName() == "ISDNAddress") {
118     isHex = true;
119     return a_ISDNAddress->asHexString();
120   } else if(stackFormat->getName() == "Unsigned16") return a_Unsigned16->asPrintableString();
121
122   return result;
123 }
124
125
126 //------------------------------------------------------------------------------
127 //------------------------------------------------------- Avp::fromXMLByFormat()
128 //------------------------------------------------------------------------------
129 void Avp::fromXMLByFormat(const anna::xml::Attribute* data, const anna::xml::Attribute* hexData, const stack::Format *stackFormat) throw(anna::RuntimeException) {
130   if(stackFormat->getName() == "ISDNNumber") {
131     if(data) a_ISDNNumber->fromPrintableString(data->getValue().c_str());
132     else if(hexData) a_ISDNNumber->fromHexString(hexData->getValue());
133   } else if(stackFormat->getName() == "ISDNAddress") {
134     if(data) a_ISDNAddress->fromPrintableString(data->getValue().c_str());
135     else if(hexData) a_ISDNAddress->fromHexString(hexData->getValue());
136   } else if(stackFormat->getName() == "Unsigned16") {
137     if(data) a_Unsigned16->fromPrintableString(data->getValue().c_str());
138     else if(hexData) a_Unsigned16->fromHexString(hexData->getValue());
139   }
140 }
141