-
Notifications
You must be signed in to change notification settings - Fork 2
/
cpp_rip_example.cpp
195 lines (125 loc) · 4.29 KB
/
cpp_rip_example.cpp
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
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
// CMPT 471 A3 part 2:
// RIP Example with 6 routers 7 networks.
class Router{
public:
Router();
int id;
string name;
//the array index is the Network Destination
//the data held inside that index is the number or hops required.
int *dest_network;
string *next_hop;
int *num_of_hops;
void printRoutingTable();
void addNetwork(int x);
~Router();
};
//constructor
Router::Router(){
this->name = "nameless";
this->dest_network = new int[8];
this->next_hop = new string[8];
this->num_of_hops = new int[8];
for(int i = 0; i<8; i++){
this->dest_network[i] = -1;
}
}
//destructor
Router::~Router(){
delete[] dest_network;
delete[] next_hop;
delete[] num_of_hops;
}
void Router::printRoutingTable(){
cout << this->name << " Routing Table:" <<endl;
cout << "Destination | Next Hop | Number of Hops"<<endl;
for(int i=1; i<8; i++){
cout <<setw(10)<<"N"<<i<<" "<<this->dest_network[i] << " | "<< setw(10)<<this->next_hop[i] << " | "<<setw(10)<<setw(10)<< this->num_of_hops[i]<<endl;
}
}
void Router::addNetwork(int x){
this->dest_network[x] = 0;
this->next_hop[x] = "* direct";
this->num_of_hops[x] = 0;
}
void changeNameTo(Router &r, string newName){
r.name = newName;
}
string intToStr(int x){
stringstream convert_to_string;
convert_to_string << x;
return convert_to_string.str();
}
void RIP(Router &from, Router &to){
cout<<endl;
cout<<"======== RIP from Router: " << from.name << " to Router: " << to.name <<"========="<< endl;
from.printRoutingTable();
to.printRoutingTable();
///do the matching and testing
for(int i=1; i<8; i++){
//if there is something from
if(from.dest_network[i] != -1){
//to is -1 or more hops.
if((to.dest_network[i] > from.dest_network[i]) || (to.dest_network[i] == -1)){
//set to hops to from +1 and from's name on next hop
to.dest_network[i] = from.dest_network[i]+1;
to.next_hop[i] = from.name;
to.num_of_hops[i] = from.dest_network[i]+1;
}
}
}
cout<<endl;
cout<< "------ Resulting routing table ------"<<endl;
to.printRoutingTable();
cout<< "================================================="<<endl;
}
int main(){
//Making an array of 7 routers, but only 1 - 6 will be used.
//Router0 will not be used.
Router *arrayOfRouters;
arrayOfRouters = new Router[7];
//Starting routers 1 - 6
for(int i = 1; i <= 6; i++){
arrayOfRouters[i].name = "Router#" + intToStr(i);;
arrayOfRouters[i].addNetwork(i);
arrayOfRouters[i].addNetwork(i+1);
arrayOfRouters[i].printRoutingTable();
}
// Performing RIP on routers and neighbough routers.
for(int y = 1; y < 6; y++){
cout<<endl;
cout<<"***********************************************************************"<<endl;
cout<<"**********************STARTING "<<intToStr(y) <<"ND ROUND*******************"<<endl;
cout<<"***********************************************************************"<<endl;
//RIP r1
RIP(arrayOfRouters[1], arrayOfRouters[2]);
//RIP r2
RIP(arrayOfRouters[2], arrayOfRouters[1]);
RIP(arrayOfRouters[2], arrayOfRouters[3]);
//RIP r3
RIP(arrayOfRouters[3], arrayOfRouters[2]);
RIP(arrayOfRouters[3], arrayOfRouters[4]);
//RIP r4
RIP(arrayOfRouters[4], arrayOfRouters[3]);
RIP(arrayOfRouters[4], arrayOfRouters[5]);
//RIP r5
RIP(arrayOfRouters[5], arrayOfRouters[4]);
RIP(arrayOfRouters[5], arrayOfRouters[6]);
//RIP r6
RIP(arrayOfRouters[6], arrayOfRouters[5]);
}
//output final table
cout<<endl;
cout<<"***********************************************************************"<<endl;
cout<<"**********************FINAL TABLES*******************"<<endl;
cout<<"***********************************************************************"<<endl;
for(int j=1; j<=6; j++){
arrayOfRouters[j].printRoutingTable();
}
return 0;
}