-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspanningTree.cpp
More file actions
220 lines (182 loc) · 5.83 KB
/
spanningTree.cpp
File metadata and controls
220 lines (182 loc) · 5.83 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <iostream>
#include <string>
#include <vector>
#include "bridge.h"
#include <algorithm>
using namespace std;
bool checkspanning(string msg, string prevmsg){
if( msg != "" && msg == prevmsg){
return true;
}
else return false;
}
int find_bridge(string message, vector<bridge*> v){
for(int i = 0; i < v.size(); i++){
if(message.substr(3,2) == v[i]->name)return i;
}
return -1;
}
int get_dist_from_msg(string token){
string dist = token.substr(8);
std::stringstream ss;
// convert to string
ss << dist;
int num;
ss >> num;
return num;
}
bool if_present(vector< vector<string> >host, string temp){
for(int i = 0;i<host.size();i++){
if(host[i][0] == temp) return true;
}
return false;
}
// string LAN(vector< vector<string> >host, string s){
// for(int i = ; i < host.size();i++){
// for(int j = 1; j < host[i].size; j++){
// if(host[i][j] == s) return host[i][0];
// }
// }
// }
// // SOME functions I was doing for part 2 of the assignment.
// void transfers_message(vector< vector<string> > host, vector<bridge*> bridgearray, string sender, string reciever, string from, string host_send, string host_recieve){
// for(int i = 0; i < bridgearray; i++){
// bool x = false;
// for(int i = 0; i < bridgearray->LAN_connected.size();i++){
// if(sender == bridgearray->LAN_connected[0]) x = true;
// }
// if(x){
// // bridgearray[i]->make_entry(sender, host_send);
// }
// }
// }
int main(){
int trace,bridgenumber;
string temp;
cin >> trace >> bridgenumber;
vector<string> bridgestrings (bridgenumber);
vector<bridge*> bridgearray;
// temp for the newline // no use
std::getline(cin, temp);
for(int i = 0; i < bridgenumber; i++){
//input line of bridge
std::getline(cin, bridgestrings[i]);
bridge * tempbridge = new bridge(bridgestrings[i]);
bridgearray.push_back(tempbridge);
}
std::sort(bridgestrings.begin(), bridgestrings.end());
string root_port = bridgestrings[0].substr(0,2);
// count for simulation number
int simnum = 0;
string msg = "", prevmsg = "";
while(not(checkspanning(msg,prevmsg))) {
prevmsg = msg;
msg = "";
// For message generations.
for(int i = 0; i < bridgenumber; i++){
msg = msg + bridgearray[i]->generate_msg() + "|";
}
while(msg[msg.length() - 1] == '|')
msg = msg.substr(0,msg.length() - 1 );
// cout <<msg<<endl;
simnum++;
// For processing message at some host.
// step-1 tokenize the string.
size_t p = 0;
string message = msg;
string token;
vector<string> v;
while ((p = message.find("|")) != string::npos) {
token = message.substr(0, p);
v.push_back(token);
message = message.substr(p + 1);
}
message = message.substr(p + 1);
v.push_back(message);
// now v is a vector of all messages. Doing processing at all LANs.
for(int i = 0; i<v.size(); i++){
for(int j = i + 1; j < v.size();j++){
//if messages are for same host.
if(v[i].substr(6,1) == v[j].substr(6,1)){
if(v[j].substr(0,2) == root_port && v[j].substr(0,2) == v[i].substr(0,2) && get_dist_from_msg(v[i]) != get_dist_from_msg(v[j])){
if(get_dist_from_msg(v[i]) < get_dist_from_msg(v[j]) && v[i][6] != '*'){
int num = find_bridge(v[j], bridgearray);
if(num != -1){
bridgearray[num]->change_port_status(v[j].substr(6,1), "NP");
v[j][6] = '*';
}
}
else if(get_dist_from_msg(v[i]) > get_dist_from_msg(v[j]) && v[i][6] != '*'){
int num = find_bridge(v[i], bridgearray);
if(num != -1){
bridgearray[num]->change_port_status(v[i].substr(6,1), "NP");
v[i][6] = '*';
}
}
} else if(v[j].substr(0,2) == root_port && v[j].substr(0,2) == v[i].substr(0,2) && get_dist_from_msg(v[i]) == get_dist_from_msg(v[j]) && v[i].substr(3,2) != v[j].substr(3,2)){
if(int(v[i][4]) < int(v[j][4]) && v[i][6] != '*'){
int num = find_bridge(v[j], bridgearray);
if(num != -1){
bridgearray[num]->change_port_status(v[j].substr(6,1), "NP");
v[j][6] = '*';
}
}
else if(int(v[i][4]) > int(v[j][4]) && v[i][6] != '*'){
int num = find_bridge(v[i], bridgearray);
if(num != -1){
bridgearray[num]->change_port_status(v[i].substr(6,1), "NP");
v[i][6] = '*';
}
}
}
}
}
}
// For message processing at bridge.
for(int i = 0; i < bridgenumber; i++){
bridgearray[i]->process_message(msg);
}
// break;
// cout<<endl;
}
vector< vector<string> > host;
for (int i = 0 ; i < bridgearray.size() ;i++){
bridgearray[i]->printbridge();
// ========================================================================
// Part 1 of the assignment ends here
// ========================================================================
// vector< vector<string> > temp = bridgearray[i]->LAN_connected;
// for(int j = 0; j < temp.size(); j++){
// if(!if_present(host, temp[j][0])){
// vector<string> temp2 (1,temp[j][0]);
// host.push_back(temp2);
// }
// }
}
// std::sort(host.begin(), host.end());
// std::vector<string> hoststrings (host.size());
// for(int i = 0; i < host.size(); i++){
// //input line of bridge
// std::getline(cin, hoststrings[i]);
// // cout<<hoststrings[i]<<" ";
// }
// for(int i = 0; i< hoststrings.size();i++){
// for (int i = 4; i < hoststrings[i].length();i = i+2){
// host[i].push_back(hoststrings[i].substr(i,1));
// }
// }
// int no_of_transfers;
// cin >> no_of_transfers;
// string transfers[no_of_transfers][2];
// for(int i = 0; i< no_of_transfers; i++){
// string temp1, temp2;
// cin >> temp1 >> temp2;
// transfers[i][0] = temp1;
// transfers[i][1] = temp2;
// }
// for(int i = 0; i < no_of_transfers;i++){
// for (int i = 0 ; i < bridgearray.size() ;i++){
// transfers_message(host, bridgearray, LAN(host, transfers[i][0]), LAN(host, transfers[i][1]), "ALL", transfers[i][0], transfers[i][1]);
// }
// }
}