-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbellmanford.cpp
More file actions
169 lines (127 loc) · 6.1 KB
/
bellmanford.cpp
File metadata and controls
169 lines (127 loc) · 6.1 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
#include "bellmanford.h"
// Maybe int BellmanFord?? return 1 if table was changed, 0 if nothing was
void BellmanFord::bellmanFord(char* buf, RoutingTable r, int clientPort, char nodeLetter) {
// First parse message in buf, using helper function
//---->INTO a new nodelist (routing table).
// Then we compare all nodelist entries with current node's routing table
Node* theirNodes = parseDV(buf);
// Get their letter
char theirLetter = clientPort - 10000 +65 ; // Get from clientPort
int nextHopCost; // Cost to them
Node* myNodes = r.getMyNodes(nodeLetter); // Get its own routing table
//first entry of myNodes is itself with a cost of 0. so go to next
Node *j = myNodes->getNext(); // For each entry in my table, excluding myself
while(j != NULL) {
if(theirLetter == j->getID()) { // Nexthopcost from my table
nextHopCost = j->getCost(); // My cost to them
}
j = j->getNext();
}
// Check if the slot is NULL
Node* i = theirNodes; // For each entry in their table
// First entry of myNodes is itself with a cost of 0. so go to next
j = myNodes->getNext(); // For each entry in my table, excluding myself
Node* prev = j;
while(i != NULL) {
while(j != NULL) {
if(i->getID() == j->getID()) { // If their letter is equal to something already in my table
if(i->getCost() + nextHopCost < j->getCost()) { // If their cost is less than my cost
j->setCost(i->getCost() + nextHopCost); // Update cost to their cost
j->setPort(clientPort); // Set port to route through them
}
break; // Get outta this while
} else if(i->getID() == nodeLetter) { // If it is their connection to myself
if(i->getCost() < nextHopCost) { // If their cost to me is less than my cost to them
// (if cost between us has changed)
nextHopCost = i->getCost(); // Update nextHopCost
while(j != NULL) { // Then update cost to them in my table
if(theirLetter == j->getID()) {
nextHopCost = j->getCost();
}
j = j->getNext();
}
i = theirNodes; // Go back to beginning of table comparison
j = myNodes->getNext();
break;
}
}
prev = j;
j = j->getNext();
} // If reached here, and j is NULL, never found equal. so we add this entry
if(j == NULL & i->getID() != nodeLetter) {
j = new Node; // Create new node
j->setDest(i->getID()); // Set its dest as i of theirNodes
j->setPort(clientPort); // Route through them
j->setCost(i->getCost() + nextHopCost);
prev->setNext(j); //ADD IT TO THE TABLE
//cout << "DEST: " << j->getID() << " PORT: " << j->getPort() << " COST: " << j->getCost() << endl;
}
i = i->getNext(); // Go to next of their nodes
j = myNodes->getNext(); // Back to beginning of my nodes
}
}
string BellmanFord::makeDV(RoutingTable r, char nodeLetter) {
string DV = "~d ";
string cost;
Node* myNodes = r.getMyNodes(nodeLetter); // Get its own routing table
Node* temp = myNodes->getNext();
// Format: each line is dest letter, cost (thassit)
while(temp != NULL) {
DV += temp->getID(); // Add dest character
DV += ",";
cost = to_string(temp->getCost());
DV += cost; // Add cost (see what the heck this does), may need int to char conversion
DV += "\n"; // Newline to separate
temp = temp->getNext(); // Go to next in list
}
//char* dv_c = (char*)DV.c_str();
//cout << "dv: " << dv_c << endl;
return DV;
}
Node* BellmanFord::parseDV(char* buf) {
NodeList table;
int cost;
int i = 0;
while(i < 3){ // Go past initial '~d ", buf[0], [1], [2]
i++;
}
// A,3\nB,4\n....etc
// All the ports are unimportant here. only use dest letter and cost
Node* head = new Node;
head->setDest(buf[i]);
Node* temp = NULL;
//cout << "letter first: " << buf[i] << endl;
while (buf[i] != ',' ) {
i++;
} // Now it's a comma
i++; // Now it's past the comma
cost = buf[i] - '0'; // Convert char to int
head->setCost(cost);
//cout << "cost first: " << buf[i] << endl;
while (buf[i] != '\n' ) {
i++;
} // Now it's a newline
i++; // Now it's past newline
Node* prev = head;
prev->setNext(temp);
// Now get the rest
while(buf[i] != '\0') { // While not end of string
temp = new Node;
temp->setDest(buf[i]); // Set dest letter
//cout << "DEST LETTER" << buf[i] << endl;
while (buf[i] != ',' ) {
i++;
} // Now it's a comma
i++; // Now it's past the comma
cost = buf[i] - '0'; // Convert char to int
temp->setCost(cost);
//cout << "COST" << buf[i] << endl;
prev->setNext(temp); // Link it to list
prev = temp; // Get ready to add next node
while (buf[i] != '\n' ) {
i++;
} // Now it's a newline
i++; // Now it's past newline
}
return head;
}