Download this file

SimpleDV.cc    206 lines (159 with data), 5.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <SimpleDV/SimpleDV.h>
namespace SimpleDV {
Register_Class(SimpleDV);
using namespace std;
RoutingUpdate::RoutingUpdate(const Address &_addr, const std::string &_src, const unsigned short &_qos):IntRoutingUpdate(_addr){
src = _src;
qos = _qos;
}
std::string RoutingUpdate::getSrc(){
return src;
}
unsigned short RoutingUpdate::getQoS(){
return qos;
}
void RoutingUpdate::addEntry(rtEntry entry){
entries.push_back(entry);
}
entriesIt RoutingUpdate::entriesBegin(){
return entries.begin();
}
entriesIt RoutingUpdate::entriesEnd(){
return entries.end();
}
//Flow inserted/removed
void SimpleDV::insertFlow(const Address &addr, const std::string &dst, const unsigned short &qos, const unsigned short &metric){
neig[qos][addr] = metric;
rtEntry * oldEntry = &table[qos][dst];
bool entryChangedDst = false;
if(oldEntry->addr == dst){
if(oldEntry->metric != metric){
oldEntry->metric = metric;
}
} else if(oldEntry->metric >= metric){
oldEntry->addr = dst;
oldEntry->metric = metric;
entryChangedDst = true;
}
if(oldEntry->metric >= infMetric){
changes.insert(entries2NextItem(qosPaddr(qos, dst),""));
table[qos].erase(dst);
} else if(entryChangedDst){
changes.insert(entries2NextItem(qosPaddr(qos, dst),dst));
}
}
void SimpleDV::removeFlow(const Address &addr, const std::string &dst, const unsigned short &qos){
neig[qos].erase(addr);
if(neig[qos].size() <= 0){
neig.erase(qos);
}
tTable * qosTable = &table[qos];
for(tTableIt it = qosTable->begin(); it != qosTable->end();){
rtEntry * oldEntry = &it->second;
tTableIt actIt = it;
it++;
if(oldEntry->addr == dst){
changes.insert(entries2NextItem(qosPaddr(qos, oldEntry->addr),""));
qosTable->erase(actIt);
}
}
}
//Get Changes
entries2Next SimpleDV::getChanges(){
entries2Next ret = changes;
changes.clear();
return ret;
}
entries2Next SimpleDV::getAll(){
entries2Next ret;
for(rtTableIt it = table.begin(); it != table.end(); it++){
for(tTableIt it2 = it->second.begin(); it2 != it->second.end(); it2++){
if(it2->second.metric < infMetric) {
ret[qosPaddr(it->first, it2->first)] = it2->second.addr;
} else {
ret[qosPaddr(it->first, it2->first)] = "";
}
}
}
map<unsigned short, map<string, rtEntry> > table;
return ret;
}
//Process a Routing Update, return true => inform FWDG of the update
bool SimpleDV::processUpdate(IntRoutingUpdate * update){
RoutingUpdate * up = check_and_cast<RoutingUpdate*>(update);
std::string src = up->getSrc();
if(src == myAddr){
return false;
}
unsigned short qos = up->getQoS();
for(entriesIt it = up->entriesBegin(); it!= up->entriesEnd(); it++){
if(it->addr == myAddr) {
continue;
}
rtEntry * newEntry = &(*it);
rtEntry * oldEntry = &table[qos][newEntry->addr];
bool entryChangedDst = false;
if(oldEntry->addr == newEntry->addr){
if(oldEntry->metric == newEntry->metric){
oldEntry->metric = newEntry->metric;
}
} else if(oldEntry->metric >= newEntry->metric){
oldEntry->addr = src;
oldEntry->metric = newEntry->metric;
entryChangedDst = true;
}
if(oldEntry->metric >= infMetric){
changes.insert(entries2NextItem(qosPaddr(qos, newEntry->addr),""));
table[qos].erase(newEntry->addr);
} else if(entryChangedDst){
changes.insert(entries2NextItem(qosPaddr(qos, newEntry->addr),src));
}
}
return ! changes.empty();
}
// Called after initialize
void SimpleDV::onPolicyInit(){
myAddr = par("myAddr").stdstringValue();
if(myAddr == "") {
myAddr = myAddress.getIpcAddress().getName();
}
infMetric = 32;
scheduleAt(simTime()+30, new cMessage("Time2Update"));
}
void SimpleDV::handleMessage(cMessage *msg){
if(msg->isSelfMessage()){
//Iterate all Qos
for(qosNeighMetricIt it = neig.begin(); it!= neig.end(); it++){
//getTable per QoS
tTable * qosTable = &table[it->first];
//iterate all Qos->Neighbour
for(neighMetricIt it2 = it->second.begin(); it2 != it->second.end(); it2++){
//New Update to QoS + Neighbour
RoutingUpdate * update = new RoutingUpdate(it2->first, myAddr, it->first);
//Populate the update
for(tTableIt it3 = qosTable->begin(); it3 != qosTable->end();it3++){
update->addEntry(rtEntry(it3->first, it3->second.metric+ it2->second));
}
//Add our entry
update->addEntry(rtEntry(myAddr, it2->second));
//And send
sendUpdate(update);
}
}
scheduleAt(simTime()+30, msg);
}
}
void SimpleDV::finish(){
IntRouting::finish();
EV << "I'm " << myAddr<<endl;
for (rtTableIt it = table.begin(); it != table.end(); it++)
{
EV << " QoS " << it->first << endl;
for (tTableIt it2 = it->second.begin(); it2 != it->second.end(); it2++)
{
EV << " " << it2->first << " via " << it2->second.addr << " ("
<< it2->second.metric << " hops)" << endl;
}
}
}
}