RINASim  October 2016
Documentation of framework for OMNeT++
DV.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 /*
24  * DV.cc
25  *
26  * Created on: Mar 23, 2015
27  * Author: gaixas1
28  */
29 
30 #include "DomainRouting/DV/DV.h"
31 #include "DomainRouting/Routing.h"
32 
33 namespace DMRnmsDV {
34 
35 using namespace DMRnms;
36 using namespace std;
37 
38 DVUpdate::DVUpdate(const Address &_addr, const string &_domain)
39  :RoutingUpdate(_addr, _domain){}
40 
42  entries.push_back(_e);
43 }
44 
46  return entries.begin();
47 }
48 
50  return entries.end();
51 }
52 
53 
54 DV::DV(Routing * parent, const Address &_nAddr, const string &_domain, const string &_addr)
55  :rModule(parent, _nAddr,_domain, _addr) {
56  infMetric = 32;
57  scheduledUpdate = false;
58  myAddrSet.insert(_addr);
59 }
60 
62  // Cast update to known type
63  DVUpdate * up = dynamic_cast<DVUpdate*>(update);
64  if(up == NULL) { return false; }
65 
66  // Check I'm not the src
67  Address src = up->getSource();
68  if(src == myNAddress){ return false; }
69 
70 
71  for(entriesIt newE = up->entriesBegin(); newE!= up->entriesEnd(); newE++){
72  // Not consider entries to myself or my aliasses
73  if(myAddrSet.find(newE->addr) != myAddrSet.end()){ continue; }
74 
75  tEntry * oldE = &table[newE->addr];
76  bool entryChanged = false;
77 
78  if(oldE->addr == src){
79  if(oldE->metric == newE->metric){
80  oldE->metric = newE->metric;
81  }
82  } else if(oldE->metric >= newE->metric){
83  oldE->addr = src;
84  oldE->metric = newE->metric;
85  entryChanged = true;
86  }
87 
88  if(oldE->metric >= infMetric){
89  changes[newE->addr] = Address();
90  table.erase(newE->addr);
91  } else if(entryChanged){
92  changes[newE->addr] = src;
93  changedEntries.insert(newE->addr);
94  }
95  }
96 
97  if(! changes.empty()) {
99  return true;
100  } else {
101  return false;
102  }
103 }
104 
105 void DV::addFlow(const Address &_nAddr, const std::string &_addr, const unsigned short &_metric){
106  nei[_nAddr] = _metric;
107 
108  tEntry * oldE = &table[_addr];
109  bool entryChangedDst = false, entryChanged = false;
110  if(oldE->addr == _nAddr){
111  if(oldE->metric != _metric){
112  oldE->metric = _metric;
113  entryChanged = true;
114  }
115  } else if (oldE->metric >= _metric) {
116  oldE->addr = _nAddr;
117  oldE->metric = _metric;
118  entryChanged = true;
119  entryChangedDst = true;
120  }
121 
122  if(oldE->metric >= infMetric){
123  changes[_addr] = Address();
124  table.erase(_addr);
125  } else {
126  if(entryChanged){
127  changedEntries.insert(_addr);
128  }
129  if(entryChangedDst){
130  changes[_addr] = _nAddr;
131  }
132  }
133 
134  scheduleUpdate();
135 }
136 
137 void DV::removeFlow(const Address &_nAddr, const std::string &_addr){
138  nei.erase(_nAddr);
139 
140  for(tTableIt it = table.begin(); it != table.end();){
141  tEntry * oldE = &it->second;
142  tTableIt actIt = it++;
143 
144  if(oldE->addr == _nAddr){
145  table.erase(actIt);
146  changedEntries.insert(_addr);
147  changes[_addr] = Address();
148  }
149  }
150 
151  scheduleUpdate();
152 }
153 
154 void DV::addAddr(const std::string &_addr){
155  myAddrSet.insert(_addr);
156 }
157 
158 void DV::removeAddr(const std::string &_addr){
159  myAddrSet.erase(_addr);
160 }
161 
162 void DV::setInfMetric(const unsigned short &inf){
163  infMetric = inf;
164 }
165 
167  dmNxt ret(domain, changes);
168  changes.clear();
169  return ret;
170 }
171 
173  dmNxt ret(domain);
174 
175  for(tTableIt it = table.begin(); it != table.end(); it++){
176  if(it->second.metric < infMetric){
177  ret.entries[it->first] = it->second.addr;
178  } else {
179  ret.entries[it->first] = Address();
180  }
181  }
182 
183  changes.clear();
184  return ret;
185 }
186 
187 void DV::handleMessage(cMessage *msg){
188  if(msg->isSelfMessage()){
189 
190  //iterate all Neighbour
191  for(neighMetricIt itN = nei.begin(); itN != nei.end(); itN++){
192  //New Update to Neighbour
193  DVUpdate * update = new DVUpdate(itN->first, domain);
194 
195  //Populate the update
196  for(sSetIt cIt = changedEntries.begin(); cIt != changedEntries.end(); cIt++){
197  tEntry * rt = &table[*cIt];
198  update->addEntry(
199  rtEntry(*cIt, rt->metric + itN->second)
200  );
201  }
202 
203  //Add our entries
204  update->addEntry(rtEntry(myAddr, itN->second));
205  for(sSetIt mIt = myAddrSet.begin(); mIt != myAddrSet.end(); mIt++){
206  update->addEntry(rtEntry(*mIt, itN->second));
207  }
208 
209  parent->chSendUpdate(update);
210  }
211 
212  changedEntries.clear();
213  scheduledUpdate = false;
214  }
215  delete msg;
216 }
217 
218 
220  if(!scheduledUpdate){
221  scheduledUpdate = true;
222  scheduleAt(1, new cMessage("Time2Update"));
223  }
224 }
225 
226 } /* namespace DomainRouting */
entriesL entries
Definition: DV.h:86
sSet myAddrSet
Definition: DV.h:120
void scheduleUpdate()
Definition: DV.cc:219
bool scheduledUpdate
Definition: DV.h:122
Definition: DV.h:57
tTable::iterator tTableIt
Definition: DV.h:66
void removeAddr(const std::string &_addr)
Definition: DV.cc:158
entriesIt entriesBegin()
Definition: DV.cc:45
bool processUpdate(RoutingUpdate *update)
Definition: DV.cc:61
dmNxt getAll()
Definition: DV.cc:172
DV(Routing *parent, const Address &_nAddr, const std::string &_domain, const std::string &_addr)
Definition: DV.cc:54
s2A entries
Definition: rModule.h:46
void chSendUpdate(RoutingUpdate *update)
Definition: Routing.cc:111
Routing * parent
Definition: rModule.h:87
Definition: DV.h:42
void addFlow(const Address &_nAddr, const std::string &_addr, const unsigned short &_metric)
Definition: DV.cc:105
std::string myAddr
Definition: rModule.h:90
std::vector< rtEntry >::iterator entriesIt
Definition: DV.h:51
void addAddr(const std::string &_addr)
Definition: DV.cc:154
sSet::iterator sSetIt
Definition: DV.h:69
void setInfMetric(const unsigned short &inf)
Definition: DV.cc:162
entriesIt entriesEnd()
Definition: DV.cc:49
s2A changes
Definition: DV.h:118
Address addr
Definition: DV.h:58
tTable table
Definition: DV.h:115
void scheduleAt(const double &time, cMessage *)
Definition: rModule.cc:42
neighMetric nei
Definition: DV.h:116
void handleMessage(cMessage *msg)
Definition: DV.cc:187
sSet changedEntries
Definition: DV.h:119
Address myNAddress
Definition: rModule.h:88
unsigned short metric
Definition: DV.h:59
dmNxt getChanges()
Definition: DV.cc:166
void addEntry(rtEntry _e)
Definition: DV.cc:41
std::string domain
Definition: rModule.h:89
DVUpdate(const Address &_addr, const std::string &_domain)
Definition: DV.cc:38
Address class holds IPC Process identification.
Definition: Address.h:42
void removeFlow(const Address &_nAddr, const std::string &_addr)
Definition: DV.cc:137
neighMetric::iterator neighMetricIt
Definition: DV.h:54
unsigned short infMetric
Definition: DV.h:117
Definition: DV.cc:33