Download this file

Routing.cc    152 lines (123 with data), 4.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <DomainRouting/Routing.h>
#include "DomainRouting/DV/DV.h"
#include "DomainRouting/LS/LS.h"
namespace DMRnms {
Register_Class(Routing);
Routing::~Routing() {
for(std::map<std::string, rModule*>::iterator it = rModules.begin(); it!=rModules.end(); it++){
delete it->second;
}
}
bool Routing::processUpdate(IntRoutingUpdate * update) {
RoutingUpdate * up = dynamic_cast<RoutingUpdate*>(update);
if(up == NULL) {
return false;
}
EV << "Received update from "<< up->getSource() << " for \""<< up->domain << "\" at "<< simTime() <<endl;
if(rModules.find(up->domain) != rModules.end()){
if(rModules[up->domain]->processUpdate(up)){
changedModules.insert(up->domain);
return true;
}
}
return false;
}
//Add/Remove a domain
void Routing::addDomain(const std::string &domain, const std::string &addr, const ModuleAlgs &alg) {
switch(alg) {
case DV :
rModules[domain] = new DMRnmsDV::DV(this, myAddress, domain, addr);
break;
case LS :
rModules[domain] = new DMRnmsLS::LS(this, myAddress, domain, addr);
break;
}
}
void Routing::removeDomain(const std::string &domain) {
if(rModules.find(domain) != rModules.end()){
delete rModules[domain];
rModules.erase(domain);
}
}
//Add/Remove flow to neighbour
void Routing::addFlow(const Address &nAddr, const std::string &domain, const std::string &addr, const unsigned short &metric) {
if(rModules.find(domain) != rModules.end()){
rModules[domain]->addFlow(nAddr, addr, metric);
changedModules.insert(domain);
}
}
void Routing::removeFlow(const Address &nAddr, const std::string &domain, const std::string &addr) {
if(rModules.find(domain) != rModules.end()){
rModules[domain]->removeFlow(nAddr, addr);
changedModules.insert(domain);
}
}
// Add/Remove secondary addresses
void Routing::addAddr(const std::string &domain, const std::string &addr) {
if(rModules.find(domain) != rModules.end()){
rModules[domain]->addAddr(addr);
changedModules.insert(domain);
}
}
void Routing::removeAddr(const std::string &domain, const std::string &addr) {
if(rModules.find(domain) != rModules.end()){
rModules[domain]->removeAddr(addr);
changedModules.insert(domain);
}
}
//Set Infinite
void Routing::setInfMetric(const std::string &domain, const unsigned short &inf) {
if(rModules.find(domain) != rModules.end()){
rModules[domain]->setInfMetric(inf);
}
}
// Sends a Routing Update
void Routing::chSendUpdate(RoutingUpdate * update){
EV << update->getDestination() << endl;
sendUpdate(update);
}
//Get next hops
dmUpdateM Routing::getChanges() {
dmUpdateM dmu;
for(std::set<std::string>::iterator it = changedModules.begin(); it!=changedModules.end(); it++){
dmu.push_back(rModules[*it]->getChanges());
}
return dmu;
}
dmUpdateM Routing::getAll() {
dmUpdateM dmu;
for(std::map<std::string, rModule*>::iterator it = rModules.begin(); it!=rModules.end(); it++){
dmu.push_back(it->second->getAll());
}
return dmu;
}
void Routing::onPolicyInit(){}
void Routing::chScheduleAt(const std::string domain, const double & time, cMessage *msg){
Enter_Method_Silent();
RoutingSelf * m = new RoutingSelf();
m->domain = domain;
m->subM = msg;
scheduleAt(simTime()+time, m);
}
void Routing::handleMessage(cMessage *msg){
RoutingSelf * m = dynamic_cast<RoutingSelf * >(msg);
if(m){
if(rModules.find(m->domain) != rModules.end()){
rModules[m->domain]->handleMessage(m->subM);
} else {
delete m->subM;
}
}
delete msg;
}
void Routing::finish(){
EV<< "Routing at "<< myAddress <<endl;
dmUpdateM dmu = getAll();
for(DMRnms::dmUpdateMIt it = dmu.begin(); it!= dmu.end(); it++){
EV << "Domain : \"" << it->domain<<"\"" << endl;
for(DMRnms::s2AIt eIt = it->entries.begin(); eIt != it->entries.end(); eIt++){
EV << " "<< eIt->first << " -> " << eIt->second << endl;
}
}
}
}