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/stack/Command.hpp>
11 #include <anna/diameter/stack/Avp.hpp>
12 #include <anna/diameter/functions.hpp>
13 #include <anna/diameter/stack/Dictionary.hpp>
15 #include <anna/core/functions.hpp>
16 #include <anna/xml/xml.hpp>
18 //using namespace diameter::stack;
21 //------------------------------------------------------------------------------
22 //-------------------------------------------------------- Command::addAvpRule()
23 //------------------------------------------------------------------------------
24 void anna::diameter::stack::Command::addAvpRule(const AvpRule & avpRule) throw(anna::RuntimeException) {
25 if(avpRule.isFixed()) {
26 if(!a_allowFixedRule) {
27 std::string s_ex = anna::functions::asString("Incorrect position for fixed avp rule '<%s>' within command '%s'", avpRule.getAvpName().c_str(), getName().c_str());
28 s_ex += ". Fixed avp rules must be located at the beginning";
29 throw anna::RuntimeException(s_ex, ANNA_FILE_LOCATION);
31 } else a_allowFixedRule = false;
33 //a_avprules[(avpRule.getAvp())->getId()] = avpRule;
35 // Restriction for redefinition (at this same level) of two rules for the same avp:
36 if(isChild(avpRule.getId())) {
37 std::string s_ex = anna::functions::asString("Cannot add two rules for avp '%s', at the same level within command '%s'", avpRule.getAvpName().c_str(), getName().c_str());
38 throw anna::RuntimeException(s_ex, ANNA_FILE_LOCATION);
41 a_avprules[a_avprulePosition++] = avpRule;
45 //------------------------------------------------------------------------------
46 //----------------------------------------------------------- Command::isChild()
47 //------------------------------------------------------------------------------
48 bool anna::diameter::stack::Command::isChild(const AvpId & avpId) const throw() {
49 for(const_avprule_iterator it = avprule_begin(); it != avprule_end(); it++)
50 if(avpId == ((*it).second.getId()))
57 //------------------------------------------------------------------------------
58 //---------------------------------------------------------- Command::asString()
59 //------------------------------------------------------------------------------
60 std::string anna::diameter::stack::Command::asString(void) const throw() {
62 //trace = "Command '";
66 trace += anna::diameter::functions::commandIdAsPairString(a_id);
70 trace += DICTIONARY_AVPRULE_TAB;
71 trace += "No Avp rules defined\n";
73 for(const_avprule_iterator it = avprule_begin(); it != avprule_end(); it++) {
75 std::string qual = (*it).second.getQual();
76 int NumberOfSpaces = strlen(DICTIONARY_AVPRULE_TAB) - qual.size();
78 for(int k = 0; k < NumberOfSpaces; k++) trace += " ";
80 trace += (*it).second.asString();
89 //------------------------------------------------------------------------------
90 //------------------------------------------------------------- Command::asXML()
91 //------------------------------------------------------------------------------
92 anna::xml::Node* anna::diameter::stack::Command::asXML(anna::xml::Node* parent) const throw() {
93 // <!ELEMENT command (avprule*)>
94 // <!ATTLIST command name CDATA #REQUIRED code CDATA #REQUIRED type (Request | Answer) #REQUIRED>
95 anna::xml::Node* result = parent->createChild("command");
96 result->createAttribute("name", a_name);
97 result->createAttribute("code", anna::functions::asString(a_id.first));
98 result->createAttribute("type", anna::functions::asString(a_id.second ? "Request" : "Answer"));
100 for(const_avprule_iterator it = avprule_begin(); it != avprule_end(); it++)
101 (*it).second.asXML(result);