RINASim  October 2016
Documentation of framework for OMNeT++
NamingInformation.cc
Go to the documentation of this file.
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2014-2016 Brno University of Technology, PRISTINE project
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 
23 #include "NamingInformation.h"
24 
25 //Constants
26 const char* ELEM_NAMINGTABLE = "NamingInfo";
27 const char* ELEM_SYNONYM = "Synonym";
28 
30 
32 {
33  //Parse XML config
34  parseConfig(par(PAR_CONFIGDATA).xmlValue());
35 
37  //Init watchers
38  WATCH_LIST(NamingInfoTable);
39 }
40 
43 }
44 
46  for (NamingItem it = NamingInfoTable.begin(); it != NamingInfoTable.end(); ++it) {
47  if (it->getApn() == apn)
48  return &(*it);
49  }
50  return NULL;
51 }
52 
53 void NamingInformation::addNewSynonym(const APN& apn, const APN& synonym) {
54  findNamingEntryByApn(apn)->addSynonym(synonym);
55 }
56 
58  NamingInfoTable.remove(*(findNamingEntryByApn(apn)));
59 }
60 
62  for (NamingItem it = NamingInfoTable.begin(); it != NamingInfoTable.end(); ++it) {
63  if (it->hasSynonym(synonym))
64  return &(*it);
65  }
66  return NULL;
67 
68 }
69 
72  return entry ? &(entry->getApn()) : NULL;
73 }
74 
76 {
77 
78 }
79 
83 
84  APNList result = APNList();
85  //APN does not exist within NamingInfo
86  if (entry1 == NULL && entry2 == NULL) {
87  EV << "No NamingInfo thus considering " << apn << " as unique!" << endl;
88  result.push_back(apn);
89  }
90  //APN without any synonyms
91  else if (entry1 != NULL && entry2 == NULL) {
92  result.push_back(apn);
93  }
94  //Found based on synonym
95  else if (entry1 == NULL && entry2 != NULL) {
96  result = entry2->getSynonyms();
97  result.push_back(apn);
98  }
99  //Inconsistancy where two different NI are returned
100  else {
101  EV << "NamingInfo contains inconsistency" << endl
102  << *entry1 << endl
103  << *entry2 << endl;
104  }
105  return result;
106 }
107 
108 void NamingInformation::parseConfig(cXMLElement* config) {
109  cXMLElement* mainTag = NULL;
110  if (config != NULL && config->hasChildren() && config->getFirstChildWithTag(ELEM_NAMINGTABLE))
111  mainTag = config->getFirstChildWithTag(ELEM_NAMINGTABLE);
112  else {
113  EV << "configData parameter not initialized!" << endl;
114  return;
115  }
116 
117 
118  cXMLElementList apnlist = mainTag->getChildrenByTagName(ELEM_APN);
119  for (cXMLElementList::const_iterator it = apnlist.begin(); it != apnlist.end(); ++it) {
120  cXMLElement* m = *it;
121 
122  if (!(m->getAttribute(ATTR_APN) && m->getFirstChildWithTag(ELEM_SYNONYM))) {
123  EV << "\nError when parsing NaimingInfo record" << endl;
124  continue;
125  }
126 
127  APN newapn = APN(m->getAttribute(ATTR_APN));
128 
129  addNamingEntry(newapn);
130 
131  cXMLElementList synonymlist = m->getChildrenByTagName(ELEM_SYNONYM);
132  for (cXMLElementList::const_iterator jt = synonymlist.begin(); jt != synonymlist.end(); ++jt) {
133  cXMLElement* n = *jt;
134 
135  if (!(n->getAttribute(ATTR_APN))) {
136  EV << "\nError when parsing Synonym record" << endl;
137  continue;
138  }
139 
140  addNewSynonym(newapn, APN(n->getAttribute(ATTR_APN)));
141  }
142  }
143 }
144 
146  //Update display string
147  std::ostringstream description;
148  description << NamingInfoTable.size() << " entries";
149  this->getDisplayString().setTagArg("t", 0, description.str().c_str());
150  this->getDisplayString().setTagArg("t", 1, "t");
151 }
const APN & getApn() const
Define_Module(NamingInformation)
void removeNamingEntry(const APN &apn)
Application Process Name class.
Definition: APN.h:36
void addSynonym(const APN &synonym)
const std::list< APN > & getSynonyms() const
const char * ELEM_APN
const char * ELEM_SYNONYM
std::list< APN > APNList
APNList represents the list of APNs.
Definition: APN.h:96
void addNamingEntry(const APN &apn)
const char * PAR_CONFIGDATA
virtual void handleMessage(cMessage *msg)
const char * ATTR_APN
NamingInformations::iterator NamingItem
const APN * findApnBySynonym(const APN &synonym)
const APNList findAllApnNames(const APN &apn)
NamingInformations NamingInfoTable
void parseConfig(cXMLElement *config)
const char * ELEM_NAMINGTABLE
virtual void initialize()
void addNewSynonym(const APN &apn, const APN &synonym)
NamingInformationEntry * findNamingEntryByApn(const APN &apn)
NamingInformationEntry * findNamingEntryBySynonym(const APN &synonym)