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