-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistanceVectorRouting.cpp
More file actions
165 lines (160 loc) · 5.14 KB
/
DistanceVectorRouting.cpp
File metadata and controls
165 lines (160 loc) · 5.14 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
#include <iostream>
#include <cstdlib>
#include <climits>
#include <iomanip>
#ifdef __linux__
#define CLRSCR "clear"
#else
#define CLRSCR "cls"
#endif
using namespace std;
class node
{
int totalNodes;
int numOfNeighbours;
int nodeId;
int *delayTable;
int **routingTable;
node **neighbouringNodes;
int *neighbouringNodeDistance;
public:
node(char nodeId, int totalNodes)
{
this->nodeId = nodeId - 'A';
this->totalNodes = totalNodes;
this->delayTable = NULL;
this->routingTable = NULL;
this->neighbouringNodes = new node *[totalNodes];
this->neighbouringNodeDistance = new int[totalNodes];
this->numOfNeighbours = 0;
}
~node()
{
if (delayTable != NULL)
delete[] delayTable;
if (routingTable != NULL)
{
for (int i = 0; i < totalNodes; i++)
delete[] routingTable[i];
delete[] routingTable;
}
if (neighbouringNodes != NULL)
delete[] neighbouringNodes;
}
void addNeighbour(node *nodeArg, int distanceToNode)
{
this->neighbouringNodes[numOfNeighbours] = nodeArg;
this->neighbouringNodeDistance[numOfNeighbours] = distanceToNode;
numOfNeighbours++;
}
void inputDelayTable()
{
delayTable = new int[totalNodes];
cout << "Enter delay table of node " << char(nodeId + 'A') << endl;
for (int i = 0; i < totalNodes; i++)
{
cout << char(i + 'A') << " : ";
cin >> delayTable[i];
}
}
void displayDelayTable()
{
cout << "Delay table of " << char(nodeId + 'A') << endl;
for (int i = 0; i < totalNodes; i++)
cout << char(i + 'A') << " : " << delayTable[i] << endl;
}
void calculateRoutingTable()
{
cout << "DEBUG: " << numOfNeighbours << endl;
routingTable = new int *[totalNodes];
for (int i = 0; i < totalNodes; i++)
routingTable[i] = new int[2];
for (int i = 0; i < totalNodes; i++)
{
int min = INT_MAX, delayNode;
for (int j = 0; j < numOfNeighbours; j++)
{
if (i == this->getNodeId())
{
min = 0;
delayNode = '-' - 'A';
}
else
{
if (min > neighbouringNodeDistance[j] + neighbouringNodes[j]->getDelayTo(i))
{
min = neighbouringNodeDistance[j] + neighbouringNodes[j]->getDelayTo(i);
delayNode = neighbouringNodes[j]->getNodeId();
}
}
}
routingTable[i][0] = min;
routingTable[i][1] = delayNode;
}
}
void displayRoutingTable()
{
cout << "Routing table of " << char(this->getNodeId() + 'A') << endl
<< endl;
for (int i = 0; i < 28; i++)
cout << "-";
cout << endl;
cout << setw(1) << "|" << setw(8) << setfill(' ') << right << "node" << setw(1) << "|" << setw(8) << setfill(' ') << right
<< "delay" << setw(1) << "|" << setw(8) << setfill(' ') << right << "via" << setw(1) << "|" << endl;
for (int i = 0; i < 28; i++)
cout << "-";
cout << endl;
for (int i = 0; i < totalNodes; i++)
{
cout << setw(1) << "|" << setw(8) << setfill(' ') << right << char(i + 'A') << setw(1) << "|" << setw(8) << setfill(' ')
<< right << routingTable[i][0] << setw(1) << "|" << setw(8) << setfill(' ') << right << char(routingTable[i][1] + 'A')
<< setw(1) << "|" << endl;
}
for (int i = 0; i < 28; i++)
cout << "-";
cout << endl;
}
int getDelayTo(int toNode)
{
return this->delayTable[toNode];
}
int getNodeId()
{
return this->nodeId;
}
};
int main(int argc, char const *argv[])
{
int totalNodes, numOfNeighbours;
int distance;
node **neighbours;
char nodeId;
cout << "Total nodes: ";
cin >> totalNodes;
cout << "Current node id: ";
cin >> nodeId;
node primaryNode(nodeId, totalNodes);
cout << "Num of Neighbours: ";
cin >> numOfNeighbours;
neighbours = new node *[numOfNeighbours];
for (int i = 0; i < numOfNeighbours; i++)
{
system(CLRSCR);
cout << "Id of neighbour " << i + 1 << " : ";
cin >> nodeId;
cout << "Distance from " << char('A' + primaryNode.getNodeId()) << " : ";
cin >> distance;
neighbours[i] = new node(nodeId, totalNodes);
neighbours[i]->inputDelayTable();
primaryNode.addNeighbour(neighbours[i], distance);
}
primaryNode.calculateRoutingTable();
system(CLRSCR);
primaryNode.displayRoutingTable();
cout << "Press enter to continue...";
cin.ignore();
cin.get();
for (int i = 0; i < numOfNeighbours; i++)
delete neighbours[i];
delete[] neighbours;
}