RINASim  October 2016
Documentation of framework for OMNeT++
Routing.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 <DomainRouting/Routing.h>
24 
25 #include "DomainRouting/DV/DV.h"
26 #include "DomainRouting/LS/LS.h"
27 
28 namespace DMRnms {
29 
30 Register_Class(Routing);
31 
33  for(std::map<std::string, rModule*>::iterator it = rModules.begin(); it!=rModules.end(); it++){
34  delete it->second;
35  }
36  }
37 
39  RoutingUpdate * up = dynamic_cast<RoutingUpdate*>(update);
40  if(up == NULL) {
41  return false;
42  }
43 
44  EV << "Received update from "<< up->getSource() << " for \""<< up->domain << "\" at "<< simTime() <<endl;
45 
46 
47  if(rModules.find(up->domain) != rModules.end()){
48  if(rModules[up->domain]->processUpdate(up)){
49  changedModules.insert(up->domain);
50  return true;
51  }
52  }
53  return false;
54  }
55 
56  //Add/Remove a domain
57  void Routing::addDomain(const std::string &domain, const std::string &addr, const ModuleAlgs &alg) {
58  switch(alg) {
59  case DV :
60  rModules[domain] = new DMRnmsDV::DV(this, myAddress, domain, addr);
61  break;
62  case LS :
63  rModules[domain] = new DMRnmsLS::LS(this, myAddress, domain, addr);
64  break;
65  }
66  }
67  void Routing::removeDomain(const std::string &domain) {
68  if(rModules.find(domain) != rModules.end()){
69  delete rModules[domain];
70  rModules.erase(domain);
71  }
72  }
73 
74  //Add/Remove flow to neighbour
75  void Routing::addFlow(const Address &nAddr, const std::string &domain, const std::string &addr, const unsigned short &metric) {
76  if(rModules.find(domain) != rModules.end()){
77  rModules[domain]->addFlow(nAddr, addr, metric);
78  changedModules.insert(domain);
79  }
80  }
81 
82  void Routing::removeFlow(const Address &nAddr, const std::string &domain, const std::string &addr) {
83  if(rModules.find(domain) != rModules.end()){
84  rModules[domain]->removeFlow(nAddr, addr);
85  changedModules.insert(domain);
86  }
87  }
88 
89  // Add/Remove secondary addresses
90  void Routing::addAddr(const std::string &domain, const std::string &addr) {
91  if(rModules.find(domain) != rModules.end()){
92  rModules[domain]->addAddr(addr);
93  changedModules.insert(domain);
94  }
95  }
96  void Routing::removeAddr(const std::string &domain, const std::string &addr) {
97  if(rModules.find(domain) != rModules.end()){
98  rModules[domain]->removeAddr(addr);
99  changedModules.insert(domain);
100  }
101  }
102 
103  //Set Infinite
104  void Routing::setInfMetric(const std::string &domain, const unsigned short &inf) {
105  if(rModules.find(domain) != rModules.end()){
106  rModules[domain]->setInfMetric(inf);
107  }
108  }
109 
110  // Sends a Routing Update
112  EV << update->getDestination() << endl;
113  sendUpdate(update);
114  }
115 
116  //Get next hops
118  dmUpdateM dmu;
119 
120  for(std::set<std::string>::iterator it = changedModules.begin(); it!=changedModules.end(); it++){
121  dmu.push_back(rModules[*it]->getChanges());
122  }
123 
124  return dmu;
125  }
127  dmUpdateM dmu;
128 
129  for(std::map<std::string, rModule*>::iterator it = rModules.begin(); it!=rModules.end(); it++){
130  dmu.push_back(it->second->getAll());
131  }
132 
133  return dmu;
134  }
135 
137 
138 
139  void Routing::chScheduleAt(const std::string domain, const double & time, cMessage *msg){
140  Enter_Method_Silent();
141  RoutingSelf * m = new RoutingSelf();
142  m->domain = domain;
143  m->subM = msg;
144  scheduleAt(simTime()+time, m);
145  }
146  void Routing::handleMessage(cMessage *msg){
147  RoutingSelf * m = dynamic_cast<RoutingSelf * >(msg);
148  if(m){
149  if(rModules.find(m->domain) != rModules.end()){
150  rModules[m->domain]->handleMessage(m->subM);
151  } else {
152  delete m->subM;
153  }
154  }
155  delete msg;
156  }
157 
158 
160  if(par("printAtEnd").boolValue()){
161  EV<< "Routing at "<< myAddress <<endl;
162  dmUpdateM dmu = getAll();
163 
164  for(DMRnms::dmUpdateMIt it = dmu.begin(); it!= dmu.end(); it++){
165  EV << "Domain : \"" << it->domain<<"\"" << endl;
166  for(DMRnms::s2AIt eIt = it->entries.begin(); eIt != it->entries.end(); eIt++){
167  EV << " "<< eIt->first << " -> " << eIt->second << endl;
168  }
169  }
170  }
171  }
172 
173 }
void addDomain(const std::string &domain, const std::string &addr, const ModuleAlgs &alg)
Definition: Routing.cc:57
s2A::iterator s2AIt
Definition: rModule.h:54
dmUpdateM getAll()
Definition: Routing.cc:126
virtual void handleMessage(cMessage *msg)
Definition: Routing.cc:146
std::set< std::string > changedModules
Definition: Routing.h:91
ModuleAlgs
Definition: Routing.h:38
void addFlow(const Address &nAddr, const std::string &domain, const std::string &addr, const unsigned short &metric)
Definition: Routing.cc:75
void finish()
Definition: Routing.cc:159
void chSendUpdate(RoutingUpdate *update)
Definition: Routing.cc:111
void removeAddr(const std::string &domain, const std::string &addr)
Definition: Routing.cc:96
dmUpdateM getChanges()
Definition: Routing.cc:117
void onPolicyInit()
Definition: Routing.cc:136
void removeDomain(const std::string &domain)
Definition: Routing.cc:67
std::string domain
Definition: Routing.h:45
void setInfMetric(const std::string &domain, const unsigned short &inf)
Definition: Routing.cc:104
Register_Class(Routing)
Address myAddress
Definition: IntRouting.h:49
dmUpdateM::iterator dmUpdateMIt
Definition: rModule.h:56
Address getDestination()
std::map< std::string, rModule * > rModules
Definition: Routing.h:90
void removeFlow(const Address &nAddr, const std::string &domain, const std::string &addr)
Definition: Routing.cc:82
void sendUpdate(IntRoutingUpdate *update)
Definition: IntRouting.cc:62
void chScheduleAt(const std::string domain, const double &time, cMessage *msg)
Definition: Routing.cc:139
void addAddr(const std::string &domain, const std::string &addr)
Definition: Routing.cc:90
std::vector< dmNxt > dmUpdateM
Definition: rModule.h:52
cMessage * subM
Definition: Routing.h:46
Address class holds IPC Process identification.
Definition: Address.h:42
std::string domain
Definition: rModule.h:61
bool processUpdate(IntRoutingUpdate *update)
Definition: Routing.cc:38