1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
10 #include <anna/diameter/codec/EngineImpl.hpp>
11 #include <anna/diameter/codec/Message.hpp>
12 #include <anna/diameter/codec/Avp.hpp>
13 #include <anna/diameter/stack/Engine.hpp>
14 #include <anna/diameter/stack/Dictionary.hpp>
15 #include <anna/diameter/stack/Command.hpp>
16 #include <anna/diameter/internal/sccs.hpp>
17 #include <anna/diameter/defines.hpp>
19 #include <anna/xml/xml.hpp>
20 #include <anna/core/tracing/Logger.hpp>
21 #include <anna/core/tracing/TraceMethod.hpp>
22 #include <anna/core/functions.hpp>
23 #include <anna/core/mt/Guard.hpp>
26 using namespace anna::diameter::codec;
29 //------------------------------------------------------------------------------
30 //----------------------------------------------------- EngineImpl::EngineImpl()
31 //------------------------------------------------------------------------------
32 EngineImpl::EngineImpl(const char* className, const stack::Dictionary * dictionary) :
33 anna::Component(className),
34 a_dictionary(dictionary),
35 a_validationDepth(ValidationDepth::FirstError),
36 a_validationMode(ValidationMode::AfterDecoding),
37 a_singleFailedAVP(true),
39 a_fixMode(FixMode::BeforeEncoding) {
40 anna::diameter::sccs::activate();
41 anna::xml::functions::initialize();
45 //------------------------------------------------------------------------------
46 //------------------------------------------------------ EngineImpl::createAvp()
47 //------------------------------------------------------------------------------
48 Avp* EngineImpl::createAvp(const AvpId *id) throw(anna::RuntimeException) {
50 anna::Guard guard(this, "diameter::codec::EngineImpl::createAvp");
52 if((result = allocateAvp()) == NULL)
53 throw anna::RuntimeException("diameter::codec::EngineImpl::allocateAvp returns NULL", ANNA_FILE_LOCATION);
56 result->setEngine((Engine*)this);
58 //result->clear(); better clear this at releaseAvp(), see class-help implementation example
59 if(id) result->setId(*id);
65 //------------------------------------------------------------------------------
66 //-------------------------------------------------- EngineImpl::createMessage()
67 //------------------------------------------------------------------------------
68 Message* EngineImpl::createMessage(const CommandId *id) throw(anna::RuntimeException) {
69 Message *result(NULL);
70 anna::Guard guard(this, "diameter::codec::EngineImpl::createMessage");
72 if((result = allocateMessage()) == NULL)
73 throw anna::RuntimeException("diameter::codec::EngineImpl::allocateMessage returns NULL", ANNA_FILE_LOCATION);
76 result->setEngine((Engine*)this);
78 //result->clear(); better clear this at releaseMessage(), see class-help implementation example
79 if(id) result->setId(*id);
85 //------------------------------------------------------------------------------
86 //-------------------------------------------------- EngineImpl::createMessage()
87 //------------------------------------------------------------------------------
88 Message *EngineImpl::createMessage(const std::string & xmlPathFile_or_string, bool pathfile_or_string) throw(anna::RuntimeException) {
89 Message *result = createMessage();
90 if (pathfile_or_string)
91 result->loadXMLFile(xmlPathFile_or_string);
93 result->loadXMLString(xmlPathFile_or_string);
100 std::string EngineImpl::asString(void) const throw() {
101 std::string result = anna::Component::asString();
102 result += "\nValidationDepth: ";
103 result += asText(a_validationDepth);
104 result += "\nValidationMode: ";
105 result += asText(a_validationMode);
106 result += "\nSingle Failed-AVP: ";
107 result += a_singleFailedAVP ? "yes" : "no";
108 result += "\nIgnore flags: ";
109 result += a_ignoreFlags ? "yes" : "no";
110 result += "\nFixMode: ";
111 result += asText(a_fixMode);
112 result += "\nActivated Dictionary: ";
113 result += a_dictionary ? (a_dictionary->getName()) : "<null>";
117 //------------------------------------------------------------------------------
118 //---------------------------------------------------------- EngineImpl::asXML()
119 //------------------------------------------------------------------------------
120 anna::xml::Node* EngineImpl::asXML(anna::xml::Node* parent) const
122 parent = anna::Component::asXML(parent);
123 anna::xml::Node* result = parent->createChild("diameter.codec.EngineImpl");
124 result->createAttribute("ValidationDepth", asText(a_validationDepth));
125 result->createAttribute("ValidationMode", asText(a_validationMode));
126 result->createAttribute("SingleFailedAVP", a_singleFailedAVP ? "yes" : "no");
127 result->createAttribute("IgnoreFlags", a_ignoreFlags ? "yes" : "no");
128 result->createAttribute("FixMode", asText(a_fixMode));
129 result->createChild("EngineImpl.ActivatedDictionary");
131 if(a_dictionary) a_dictionary->asXML(result);
136 //------------------------------------------------------------------------------
137 //--------------------------------------------------------- EngineImpl::asText()
138 //------------------------------------------------------------------------------
139 const char* EngineImpl::asText(const ValidationDepth::_v vd)
141 static const char* text [] = { "Complete", "FirstError" };
145 //------------------------------------------------------------------------------
146 //--------------------------------------------------------- EngineImpl::asText()
147 //------------------------------------------------------------------------------
148 const char* EngineImpl::asText(const ValidationMode::_v vm)
150 static const char* text [] = { "BeforeEncoding", "AfterDecoding", "Always", "Never" };
154 //------------------------------------------------------------------------------
155 //--------------------------------------------------------- EngineImpl::asText()
156 //------------------------------------------------------------------------------
157 const char* EngineImpl::asText(const FixMode::_v fm)
159 static const char* text [] = { "BeforeEncoding", "AfterDecoding", "Always", "Never" };
163 //------------------------------------------------------------------------------
164 //---------------------------------------------- EngineImpl::validationAnomaly()
165 //------------------------------------------------------------------------------
166 void EngineImpl::validationAnomaly(const std::string & description) const throw(anna::RuntimeException) {
167 std::string msg = "Validation error: ";
169 if(a_validationDepth == ValidationDepth::FirstError)
170 throw anna::RuntimeException(msg + description, ANNA_FILE_LOCATION);
172 LOGWARNING(anna::Logger::warning(msg + description, ANNA_FILE_LOCATION));
176 //------------------------------------------------------------------------------
177 //--------------------------------------------------- EngineImpl::avpIdForName()
178 //------------------------------------------------------------------------------
179 anna::diameter::AvpId EngineImpl::avpIdForName(const char * name) throw(anna::RuntimeException) {
181 throw anna::RuntimeException("Provided NULL Avp Logical Name", ANNA_FILE_LOCATION);
184 std::string msg = "Cannot find identifier '"; msg += name;
185 msg += "' for the avp: no dictionary available";
186 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
189 const stack::Avp * stackAvp = a_dictionary->getAvp(name);
192 std::string msg = "Cannot find identifier '"; msg += name;
193 msg += "' for the avp at the available dictionary";
194 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
197 return (stackAvp->getId());
201 //------------------------------------------------------------------------------
202 //----------------------------------------------- EngineImpl::commandIdForName()
203 //------------------------------------------------------------------------------
204 anna::diameter::CommandId EngineImpl::commandIdForName(const char * name) throw(anna::RuntimeException) {
206 throw anna::RuntimeException("Provided NULL Command Logical Name", ANNA_FILE_LOCATION);
209 std::string msg = "Cannot find identifier '"; msg += name;
210 msg += "' for the command: no dictionary available";
211 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
214 const stack::Command * stackCommand = a_dictionary->getCommand(name);
217 std::string msg = "Cannot find identifier '"; msg += name;
218 msg += "' for the command at the available dictionary";
219 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
222 return (stackCommand->getId());