-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdvr.cpp
More file actions
187 lines (151 loc) · 6.25 KB
/
dvr.cpp
File metadata and controls
187 lines (151 loc) · 6.25 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
/**************************************************************************************
* Author: Eric Hansson
* Date: 04/22/2024
* File: dvr.cpp
* Purpose: To simulate updating routing tables of routers with the distance vector
* routing approach
**************************************************************************************/
#include <csignal>
#include <fstream>
#include <iostream>
#include <map>
using namespace std;
// Struct for routing table information
struct vectorInfo{
string destiantion;
int cost;
};
// A structure of a router
struct router{
string name;
map<string, vectorInfo> routingTable; // destiantion -> destiantion and cost
map<string, int> neigbours; // name of neigbours, and the cost to it
};
int main(int argc, char *argv[]){
// Check if user put in a file, if not show usage error messsage
if(argc!=2){
cerr << "<USAGE>: ./exe <data-file>"<< endl;
return 1;
}
// Read in the file
ifstream file(argv[1]);
// For reading in data
string router1;
string router2;
int cost;
// The network which contains routers
map<string, router> network;
// reads in the data from the file
if (file.is_open()) {
while (file.good()) {
file >> router1;
file >> router2;
file >> cost;
// if router does not exist, create it
if(network.find(router1) == network.end()){
// Create router and routing table for it
router newRouter;
vectorInfo table;
// set information about the router it self for the table
table.destiantion = router1;
table.cost = 0;
// Sets the name of the router and it's routing table
newRouter.name = router1;
newRouter.routingTable[router1] = table;
// inserts the new router to the network
network.insert({router1, newRouter});
}
// if router does not exist, create it
if(network.find(router2) == network.end()){
// Create router and routing table for it
router newRouter;
vectorInfo table;
// set information about the router it self for the table
table.destiantion = router2;
table.cost = 0;
// Sets the name of the router and it's routing table
newRouter.name = router2;
newRouter.routingTable[router2] = table;
// inserts the new router to the network
network.insert({router2, newRouter});
}
// Sets that the routers we read are neigbours because they have a link between them
network[router1].neigbours[router2] = cost;
network[router2].neigbours[router1] = cost;
}
file.close();
}
// Usage for commands
char command;
cout << "> ";
// Reads in command by user
cin >> command;
// As long as not quiting the program, keep giving command line
while(command != 'q'){
// If 'p', exit program
if(command == 'q'){
return 1;
}
// On 'p' and router name it will print out specific router and it's routing table
else if(command == 'p'){
string routerName;
cin >> routerName;
// Checks if exists
if(network.find(routerName) != network.end()){
// prints out routing table of the router
cout << "=======" << routerName << "=======" <<endl;
for(auto i : network[routerName].routingTable){
cout << i.first << ":<" << i.second.destiantion << "," << i.second.cost << ">" << endl;
}
cout << "================" << endl << endl;
}
}
// On 'l', print all routers and their routing tables
else if (command == 'l') {
for(auto router : network){
cout << "======" << router.first << "======" << endl;
for(auto i : router.second.routingTable){
cout << i.first << ":<" << i.second.destiantion << "," << i.second.cost << ">" << endl;
}
cout << "================" << endl << endl;
}
}
// On 'u', update router's routing table
else if (command == 'u') {
// reads in router name
string routerName;
cin >> routerName;
// checks if router exits
if (network.find(routerName) != network.end()) {
// goes through the routers neigbours
for(auto i : network[routerName].neigbours){
// goes through eachs neigbours routing table
for (auto j : network[i.first].routingTable){
// if the table has the
if(i.first == routerName){
continue;
}
// new cost to the destiantion from neighbour
int newCost = i.second + j.second.cost;
// If this hop does not exits in the source routers or the cost to the hop is lower than other
// then add it to the routing table of the source node
if(network[routerName].routingTable.find(j.first) == network[routerName].routingTable.end() || newCost < network[routerName].routingTable.find(j.first)->second.cost){
vectorInfo newT;
newT.destiantion = i.first;
newT.cost = newCost;
//network[routerName].routingTable.insert({j.first, newT});
network[routerName].routingTable[j.first] = {i.first, newCost};
}
}
}
}
else {
cout << "Router not found" << endl;
}
}
// Reads the next command
cout << "> ";
cin >> command;
}
return 0;
}