Repository navigation
Expand file tree
/
Copy pathDynamicRouting.cc
More file actions
115 lines (100 loc) · 4.61 KB
/
Copy pathDynamicRouting.cc
File metadata and controls
115 lines (100 loc) · 4.61 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
#include "inet/common/INETDefs.h"
#include "inet/networklayer/contract/IRoutingTable.h"
#include "inet/networklayer/ipv4/Ipv4RoutingTable.h"
#include "inet/networklayer/common/L3AddressResolver.h"
#include "inet/networklayer/contract/IInterfaceTable.h"
using namespace inet;
class DynamicRouting : public cSimpleModule
{
private:
std::map<std::string, double> linkMetrics; // Stores dynamic metrics for each link
IRoutingTable *routingTable = nullptr;
IInterfaceTable *interfaceTable = nullptr; // To manage network interfaces
cMessage *updateLinkMetricsMessage = nullptr;
protected:
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
virtual void finish() override;
void updateLinkMetrics();
void calculateRoutes();
};
Define_Module(DynamicRouting);
void DynamicRouting::initialize() {
// Resolve the routing table
const char *routingTablePath = par("routingTableModule");
cModule *routingTableModule = findModuleByPath(routingTablePath);
if (!routingTableModule) {
throw cRuntimeError("Routing table module not found at path '%s'. Ensure the parameter 'routingTableModule' is correctly set.", routingTablePath);
}
routingTable = check_and_cast<IRoutingTable *>(routingTableModule);
// Resolve the interface table
const char *interfaceTablePath = par("interfaceTableModule");
cModule *interfaceTableModule = findModuleByPath(interfaceTablePath);
if (!interfaceTableModule) {
throw cRuntimeError("Interface table module not found at path '%s'. Ensure the parameter 'interfaceTableModule' is correctly set.", interfaceTablePath);
}
interfaceTable = check_and_cast<IInterfaceTable *>(interfaceTableModule);
// Initialize link metrics with default values
linkMetrics["router1-router2"] = 1.0;
linkMetrics["router2-router3"] = 1.0;
linkMetrics["router3-router4"] = 1.0;
linkMetrics["router4-router1"] = 1.0;
// Create a reusable message for periodic updates
updateLinkMetricsMessage = new cMessage("UpdateLinkMetrics");
// Start periodic updates of link metrics
scheduleAt(simTime() + 1, updateLinkMetricsMessage);
}
void DynamicRouting::handleMessage(cMessage *msg) {
if (msg == updateLinkMetricsMessage) {
updateLinkMetrics();
calculateRoutes();
scheduleAt(simTime() + 1, updateLinkMetricsMessage); // Reschedule the update
} else {
// Handle incoming packets and route them based on the current routing table
}
}
void DynamicRouting::finish() {
// Clean up the reusable message to prevent memory leaks
cancelAndDelete(updateLinkMetricsMessage);
}
void DynamicRouting::updateLinkMetrics() {
// Simulate changing link metrics randomly
linkMetrics["router1-router2"] = uniform(0.5, 2.0);
linkMetrics["router2-router3"] = uniform(0.5, 2.0);
linkMetrics["router3-router4"] = uniform(0.5, 2.0);
linkMetrics["router4-router1"] = uniform(0.5, 2.0);
EV << "Updated link metrics: "
<< "r1-r2: " << linkMetrics["router1-router2"]
<< ", r2-r3: " << linkMetrics["router2-router3"]
<< ", r3-r4: " << linkMetrics["router3-router4"]
<< ", r4-r1: " << linkMetrics["router4-router1"] << endl;
}
void DynamicRouting::calculateRoutes() {
// Clear existing routes
while (routingTable->getNumRoutes() > 0) {
IRoute *route = routingTable->getRoute(0);
routingTable->deleteRoute(route);
}
// Add a new route based on current link metrics
Ipv4Route *newRoute = new Ipv4Route();
newRoute->setDestination(L3AddressResolver().resolve("router3").toIpv4());
newRoute->setNetmask(Ipv4Address::ALLONES_ADDRESS);
if (linkMetrics["router1-router2"] < linkMetrics["router4-router1"]) {
newRoute->setGateway(L3AddressResolver().resolve("router2").toIpv4());
auto interfaceEntry = interfaceTable->findInterfaceByName("eth0");
if (!interfaceEntry) {
throw cRuntimeError("Interface 'eth0' not found.");
}
newRoute->setInterface(interfaceEntry);
} else {
newRoute->setGateway(L3AddressResolver().resolve("router4").toIpv4());
auto interfaceEntry = interfaceTable->findInterfaceByName("eth1");
if (!interfaceEntry) {
throw cRuntimeError("Interface 'eth1' not found.");
}
newRoute->setInterface(interfaceEntry);
}
newRoute->setSourceType(Ipv4Route::MANUAL);
routingTable->addRoute(newRoute);
EV << "Calculated and updated routes based on current link metrics." << endl;
}