-
Notifications
You must be signed in to change notification settings - Fork 0
/
final.cpp
375 lines (331 loc) · 8.76 KB
/
final.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# include <iostream>
# include <vector>
# include <string>
# include <set>
# include <queue>
# include <utility>
# include <algorithm>
using namespace std;
//Class Definitions--------------------------------------------------------------------------
class lan{
public:
char id;
int DP;
vector<int> adj_bridges;
lan()
{
id = '\0';
DP = -1;
}
};
class bridge{
public:
int id;
int root;
int root_distance;
vector<char> adj_lans;
pair<char,int> RP;
bridge()
{
id = -1;
root = id;
RP = make_pair('\0',-1);
root_distance = -1;
}
};
class config_msg{
public:
int root;
int dist;
bridge source;
int destination;
char lan;
config_msg()
{
root = -1;
dist = -1;
destination = -1;
lan = '\0';
}
};
struct compare_func
{
bool operator()(config_msg const &j1, config_msg const &j2)
{
if(j1.destination < j2.destination) return true;
else return false;
//compare j1 and j2 and return true or false
}
};
class netw_info{
public:
int time;
int bridge;
char status;
config_msg m;
};
//--------------------------------------------------------------------------------------------
//Helper Functions-----------------------------------------------------------------------------
void print_bridge_network(vector<bridge> network)
{
cout<<"Bridge NO.\t"<<"Adjacent Lans"<<endl;
for(int i=0; i<network.size();i++)
{
cout<<network[i].id<<"\t\t";
for(int j=0; j<network[i].adj_lans.size();j++)
cout<<network[i].adj_lans[j]<<" ";
cout<<endl;
}
}
void print_lan_network(vector<lan> network)
{
cout<<"Lan No.\t\t"<<"Adj Bridges\t"<<"Designated Bridge"<<endl;
for(int i=0; i<network.size();i++)
{
cout<<network[i].id<<"\t\t";
for(int j=0; j<network[i].adj_bridges.size();j++)
cout<<network[i].adj_bridges[j]<<" ";
cout<<"\t\t"<<network[i].DP<<endl;
}
}
typedef set<config_msg,compare_func> messageSet;
messageSet SendMessage(config_msg m,vector<bridge> bridge_network, vector<lan> lan_network)
{
messageSet messages;
int root = m.root;
int d = m.dist;
bridge source = m.source;
for(int i=0; i<bridge_network.size();i++) //in bridge n/w
{
if(source.id == bridge_network[i].id) //if
{
for(int j=0; j<bridge_network[i].adj_lans.size(); j++)
{
for(int k=0; k<lan_network.size(); k++)
{
if(bridge_network[i].adj_lans[j] == lan_network[k].id)
{
for(int p=0; p<lan_network[k].adj_bridges.size();p++)
{
if(lan_network[k].adj_bridges[p] != source.id)
{
config_msg ms;
ms.root = root;
ms.dist = d;
ms.source = source;
ms.destination = lan_network[k].adj_bridges[p];
ms.lan = lan_network[k].id;
messages.insert(ms);
}
}
}
}
}
}
}
return messages;
}
config_msg UpdateConfig(config_msg m,vector<bridge>& bridge_network)
{
int root = m.root;
int d = m.dist;
bridge source = m.source;
int destination = m.destination;
char lan = m.lan;
config_msg return_message;
for(int i=0; i<bridge_network.size();i++)
{
if(destination == bridge_network[i].id)
{
bridge b = bridge_network[i];
if(root < b.root)
{
//cout<<"updating the table";
return_message.root = root;
return_message.dist = d+1;
return_message.source=bridge_network[i];
bridge_network[i].root = root;
bridge_network[i].RP = make_pair(lan,source.id);
bridge_network[i].root_distance = d+1;
}
else if(root == b.root && d+1 < bridge_network[i].root_distance)
{
//cout<<"updating the table ";
return_message.root = root;
return_message.dist = d+1;
return_message.source=bridge_network[i];
bridge_network[i].root = root;
bridge_network[i].RP = make_pair(lan,source.id);
bridge_network[i].root_distance = d+1;
}
else if (root == b.root && d+1 == bridge_network[i].root_distance && source.id<bridge_network[i].RP.second)
{
//cout<<"updating the table";
return_message.root = root;
return_message.dist = d+1;
return_message.source=bridge_network[i];
bridge_network[i].root = root;
bridge_network[i].RP = make_pair(lan,source.id);
bridge_network[i].root_distance = d+1;
}
else
{
return_message.root = -1;
return_message.dist = d+1;
return_message.source=bridge_network[i];
}
}
}
return return_message;
}
//--------------------------------------------------------------------------------------------------------
int main(){
int tr=1;
int n;
cin >> n;
vector<bridge> bridge_network;
set<char> lan_set;
// Initializing the bridges
for(int i=0; i<=n; i++)
{
bridge b;
string line;
getline(cin,line); // getting line by line i/p
if(i!=0)
{
for(int j=0; j<line.size(); j++)
{
if(j==1)
{
if(line[j+1]!=':') // check for colon
{
b.id = 10*((int) line[j] -48) + ((int) line[j+1] -48);
b.root = b.id;
j++;
}
else //set id
{
b.id = (int) line[j] - 48;
b.root = b.id;
}
}
if(j!=0 & j!=1 & j!=2)
{
if(line[j] != ' ' and line[j]!=':')
{
b.adj_lans.push_back(line[j]); // push for adjacent lans
lan_set.insert(line[j]); // push to make a lan set
}
}
}
sort(b.adj_lans.begin(),b.adj_lans.end());
bridge_network.push_back(b); // make a bridge network
}
}
cout<<"-----------------------------Bridge Representation-------------------------------"<<endl;
print_bridge_network(bridge_network); //printing bridge network
cout<<"-----------------------------LAN Representation-------------------------------"<<endl;
vector<lan> lan_network;
while(!lan_set.empty()) //creating a lan network
{
char c = *lan_set.begin();
lan l;
l.id = c;
for(int i=0; i<bridge_network.size();i++)
for(int j=0; j<bridge_network[i].adj_lans.size(); j++)
if (bridge_network[i].adj_lans[j] == c) l.adj_bridges.push_back(bridge_network[i].id);
lan_network.push_back(l); //pushing in lan network
lan_set.erase(lan_set.begin()); //removing from lan set
}
print_lan_network(lan_network); //print lan network
// Implementing spanning tree protocol
queue<config_msg> sender_queue,reciever_queue; // making a sender_queue and recieved
vector<class netw_info> netw_info_queue;
int timestamp = 0,initial=1;
for(int i=0; i<bridge_network.size();i++) //initialize config msg
{
config_msg m;
m.root = bridge_network[i].id;
m.dist=0;
m.source=bridge_network[i];
sender_queue.push(m);
}
while(!sender_queue.empty())
{
cout<<endl<<"time\t"<<"Bridge\t"<<"status\t"<<"sent/recieved msg"<<endl;
if(initial != 1)
{
while(!sender_queue.empty())
{
sender_queue.pop();
}
}
while(!reciever_queue.empty())
{
config_msg m = reciever_queue.front();
config_msg to_be_published = UpdateConfig(m, bridge_network);
if(to_be_published.root != -1)
{
sender_queue.push(to_be_published);
cout<<"Compare and updated"<<endl;
}
netw_info t;
t.time = timestamp;
t.bridge = m.destination;
t.status = 'r';
t.m = m;
if(tr==1) cout<<t.time<<"\tB"<<t.bridge<<"\t"<<t.status<<"\t(B"<<t.m.root<<","<<t.m.dist<<",B"<<t.m.source.id<<")\n";
netw_info_queue.push_back(t);
reciever_queue.pop();
}
queue<config_msg> temp;
while(!sender_queue.empty())
{
config_msg m = sender_queue.front();
messageSet reciever_queue_by_set = SendMessage(m, bridge_network, lan_network);
sender_queue.pop();
while(!reciever_queue_by_set.empty())
{
reciever_queue.push(*reciever_queue_by_set.begin());
reciever_queue_by_set.erase(reciever_queue_by_set.begin());
}
netw_info t;
t.time = timestamp;
t.bridge = m.source.id;
t.status = 's';
t.m = m;
netw_info_queue.push_back(t);
if(tr==1)
{
cout<<t.time<<"\tB"<<t.bridge<<"\t"<<t.status<<"\t(B"<<t.m.root<<","<<t.m.dist<<",B"<<t.m.source.id<<")\n";
}
temp.push(m);
}
while(!temp.empty())
{
sender_queue.push(temp.front());
temp.pop();
}
timestamp++;
initial = 0;
}
for(int i=0; i<lan_network.size(); i++) //deciding the designated ports
{
lan_network[i].DP = *min_element(begin(lan_network[i].adj_bridges),end(lan_network[i].adj_bridges));
}
cout<<endl<<"-----------------------------DP for LAN-------------------------------"<<endl;
print_lan_network(lan_network);
cout<<endl;
cout<<"-----------------------------Updating LANs-------------------------------"<<endl<<endl;
for(int i=0; i<lan_network.size();i++)
{
char c = lan_network[i].id;
vector<int> l;
for(int i=0; i<bridge_network.size();i++)
for(int j=0; j<bridge_network[i].adj_lans.size(); j++)
if (bridge_network[i].adj_lans[j] == c) l.push_back(bridge_network[i].id);
lan_network[i].adj_bridges = l;
}
cout<<"-----------------------------Final Bridge Representation-------------------------------"<<endl;
print_bridge_network(bridge_network);
}