New feature to allow to register components with different names for same class:...
[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   if(!a_engine)
54     throw anna::RuntimeException("Invalid codec engine reference (NULL)", ANNA_FILE_LOCATION);
55
56   return a_engine;
57 }
58
59
60 //------------------------------------------------------------------------------
61 //---------------------------------------------------- Avp::initializeByFormat()
62 //------------------------------------------------------------------------------
63 void Avp::initializeByFormat() throw() {
64   a_ISDNNumber = NULL;
65   a_ISDNAddress = NULL;
66   a_Unsigned16 = NULL;
67 }
68
69
70 //------------------------------------------------------------------------------
71 //--------------------------------------------------------- Avp::clearByFormat()
72 //------------------------------------------------------------------------------
73 void Avp::clearByFormat() throw() {
74   delete a_ISDNNumber;
75   delete a_ISDNAddress;
76   delete a_Unsigned16;
77 }
78
79
80 //------------------------------------------------------------------------------
81 //---------------------------------------------------- Avp::allocationByFormat()
82 //------------------------------------------------------------------------------
83 void Avp::allocationByFormat(const stack::Format *stackFormat) throw() {
84   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber = new ISDNNumber();
85   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress = new ISDNAddress();
86   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16 = new Unsigned16();
87 }
88
89
90 //------------------------------------------------------------------------------
91 //----------------------------------------------------- Avp::getLengthByFormat()
92 //------------------------------------------------------------------------------
93 U24 Avp::getLengthByFormat(const stack::Format *stackFormat) const throw() {
94   U24 result = 0;
95
96   if(stackFormat->getName() == "ISDNNumber") result += a_ISDNNumber->getSize();
97   else if(stackFormat->getName() == "ISDNAddress") result += a_ISDNAddress->getSize();
98   else if(stackFormat->getName() == "Unsigned16") result += a_Unsigned16->getSize();
99
100   return result;
101 }
102
103
104 //------------------------------------------------------------------------------
105 //------------------------------------------------ Avp::decodeDataPartByFormat()
106 //------------------------------------------------------------------------------
107 void Avp::decodeDataPartByFormat(const char * buffer, int size, const stack::Format *stackFormat) throw(anna::RuntimeException) {
108   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber->decode(buffer, size);
109   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress->decode(buffer, size);
110   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16->decode(buffer, size);
111 }
112
113
114 //------------------------------------------------------------------------------
115 //---------------------------------------------------------- Avp::codeByFormat()
116 //------------------------------------------------------------------------------
117 void Avp::codeByFormat(char* dataPart, const stack::Format *stackFormat) const throw(anna::RuntimeException) {
118   int dataBytes;
119
120   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber->code(dataPart, dataBytes);
121   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress->code(dataPart, dataBytes);
122   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16->code(dataPart, dataBytes);
123 }
124
125
126 //------------------------------------------------------------------------------
127 //---------------------------------------------------- Avp::getXMLdataByFormat()
128 //------------------------------------------------------------------------------
129 std::string Avp::getXMLdataByFormat(bool & isHex, const stack::Format *stackFormat) const throw() {
130   std::string result;
131
132   if(stackFormat->getName() == "ISDNNumber") {
133     isHex = true;
134     return a_ISDNNumber->asHexString();
135   } else if(stackFormat->getName() == "ISDNAddress") {
136     isHex = true;
137     return a_ISDNAddress->asHexString();
138   } else if(stackFormat->getName() == "Unsigned16") return a_Unsigned16->asPrintableString();
139
140   return result;
141 }
142
143
144 //------------------------------------------------------------------------------
145 //------------------------------------------------------- Avp::fromXMLByFormat()
146 //------------------------------------------------------------------------------
147 void Avp::fromXMLByFormat(const anna::xml::Attribute* data, const anna::xml::Attribute* hexData, const stack::Format *stackFormat) throw(anna::RuntimeException) {
148   if(stackFormat->getName() == "ISDNNumber") {
149     if(data) a_ISDNNumber->fromPrintableString(data->getValue().c_str());
150     else if(hexData) a_ISDNNumber->fromHexString(hexData->getValue());
151   } else if(stackFormat->getName() == "ISDNAddress") {
152     if(data) a_ISDNAddress->fromPrintableString(data->getValue().c_str());
153     else if(hexData) a_ISDNAddress->fromHexString(hexData->getValue());
154   } else if(stackFormat->getName() == "Unsigned16") {
155     if(data) a_Unsigned16->fromPrintableString(data->getValue().c_str());
156     else if(hexData) a_Unsigned16->fromHexString(hexData->getValue());
157   }
158 }
159