-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithm.c
More file actions
594 lines (506 loc) · 20.1 KB
/
algorithm.c
File metadata and controls
594 lines (506 loc) · 20.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#include "algorithm.h"
void ghs (struct node *node) {
uint8_t inmsg[50];
struct node_data node_data;
/*print edge information, for clarity's sake*/
print_edges(node->neighs, node->log);
/*we 'wake up' every node by default*/
wakeup(node, &node_data);
/*main infinite loop, read from message queue and react appropriately*/
uint8_t run = 1;
while(run) {
/*nothing to do when there are no messages to process*/
if (is_empty(node->queue)) {
continue;
}
/*process first message in the queue*/
memset(inmsg, 0, 50);
dequeue(node->queue, inmsg);
/*Retrieve information about which link the message came from beforehand.
This is very hacky and should have been externalized to a function, but
since we can't return both the socket and index of the edge at the same
time, and we need to find out the edge's status using its index...*/
struct edge *link = node->neighs->head;
uint16_t i, inweight = (inmsg[1] << 8) | inmsg[2];
uint8_t sock;
for (i = 0; i < node_data.num_neighs; i++) {
if (link->weight == inweight) {
sock = link->sock;
break;
}
link = link->next;
}
/*react based on incoming message type*/
uint8_t msg_type = inmsg[0];
switch(msg_type) {
case MSG_CONNECT: {
process_connect(node, &node_data, i, sock, inmsg);
break;
}
case MSG_INITIATE: {
process_initiate(node, &node_data, i, sock, inmsg);
break;
}
case MSG_TEST: {
process_test(node, &node_data, i, sock, inmsg);
break;
}
case MSG_ACCEPT: {
process_accept(node, &node_data, i, sock, inmsg);
break;
}
case MSG_REJECT: {
process_reject(node, &node_data, i, inmsg);
break;
}
case MSG_REPORT: {
/*node should terminate execution based on report's return value*/
if (process_report(node, &node_data, i, sock, inmsg)) {
run = 0;
}
break;
}
case MSG_CHGROOT: {
changeroot(node, &node_data);
break;
}
default: {
fprintf(stderr, "Invalid message type at arrival!\n");
break;
}
}
}
/*after node has finished running, print its output (the status of its edges)
to the global log*/
output(node, &node_data);
}
void process_connect(struct node *node, struct node_data *ndata,
uint16_t edge_index, uint32_t edge_sock, uint8_t *msg) {
uint8_t inlevel, outmsg[50];
char logmsg[60];
uint16_t inweight;
/*retrieve message data*/
inlevel = msg[3];
inweight = (msg[1] << 8) | msg[2];
snprintf(logmsg, 60, "Received CONNECT msg, lvl: %d, weight: %d",
inlevel, inweight);
log_msg(logmsg, node->log);
/*received connect from lower level, sender node's fragment can be absorbed*/
if (inlevel < ndata->level) {
/*mark edge as part of MST*/
ndata->edge_status[edge_index] = EDGE_BRANCH;
/*send INITIATE message and log it*/
uint8_t len;
len = create_msg(MSG_INITIATE, inweight, ndata->level, ndata->frag_id,
ndata->state, outmsg);
send(edge_sock, outmsg, len, 0);
snprintf(logmsg, 60, "Sending INITIATE message to absorbable fragment!");
log_msg(logmsg, node->log);
/*if we were in a discovering state, this node must report to us*/
if (ndata->state == NODE_FIND) {
ndata->fcount += 1;
}
}
/*if level is same or above and edge isn't classified, we delay the response*/
else if (ndata->edge_status[edge_index] == EDGE_UNKNOWN) {
snprintf(logmsg, 60, "Cannot respond yet, delaying response!");
log_msg(logmsg, node->log);
/*place message at end of queue, CONNECT messages always have len 4*/
enqueue(node->queue, msg, 4);
sleep(1);
}
/*only case left is a merge, so we send the INITIATE message with next level*/
else {
uint8_t len;
len = create_msg(MSG_INITIATE, inweight, (ndata->level)+1, inweight,
NODE_FIND, outmsg);
send(edge_sock, outmsg, len, 0);
snprintf(logmsg, 60, "Sending INITIATE message to ADVANCE LEVEL!");
log_msg(logmsg, node->log);
}
}
void process_initiate(struct node *node, struct node_data *ndata,
uint16_t edge_index, uint32_t edge_sock, uint8_t *msg) {
uint8_t inlevel, instate, outmsg[50];
uint16_t inweight, infrag;
char logmsg[60];
/*retrieve message data*/
inlevel = msg[3];
instate = msg[4];
inweight = (msg[1] << 8) | msg[2];
infrag = (msg[5] << 8) | msg[6];
/*log message arrival and its parameters*/
snprintf(logmsg, 60, "Received INITIATE message. Lvl: %d, F: %d, St: %d",
inlevel, infrag, instate);
log_msg(logmsg, node->log);
/*node is advancing level, update node data to match new level and fragment*/
ndata->level = inlevel;
ndata->frag_id = infrag;
ndata->state = instate;
ndata->edge_status[edge_index] = EDGE_BRANCH;
ndata->in_branch = edge_index;
ndata->branch_wt = inweight;
ndata->branch_sock = edge_sock;
ndata->best_edge = -1;
ndata->best_weight = ~0;
/*log level advancement in the global log*/
snprintf(logmsg, 60, "Node %d is ADVANCING to level %d in fragment %d!",
node->id, ndata->level, ndata->frag_id);
log_msg(logmsg, node->globallog);
/*now for each MST neighbour (edge is BRANCH), who isn't the node who just
sent us the INITIATE message, we propagate the new fragment's INITIATE*/
uint16_t i;
struct edge *link = node->neighs->head;
for (i = 0; i < ndata->num_neighs; i++, link = link->next) {
if (i == edge_index || ndata->edge_status[i] != EDGE_BRANCH) {
continue;
}
uint8_t len;
len=create_msg(MSG_INITIATE, link->weight,inlevel, infrag, instate, outmsg);
/*propagate INITIATE forward, and log*/
send(link->sock, outmsg, len, 0);
snprintf(logmsg, 60, "Propagating INITIATE message on edge with weight %d",
link->weight);
log_msg(logmsg, node->log);
/*if we were in the discovery state, this node must report to us later*/
if (ndata->state == NODE_FIND) {
ndata->fcount += 1;
}
}
/*if in discovery state, begin testing edges*/
if (ndata->state == NODE_FIND) {
test(node, ndata);
}
}
void process_test(struct node *node,struct node_data *ndata,uint16_t edge_index,
uint32_t edge_sock, uint8_t *msg) {
char logmsg[60];
uint16_t inweight, infrag;
uint8_t inlevel, outmsg[50];
/*retrieve message data*/
inweight = (msg[1] << 8) | msg[2];
inlevel = msg[3];
infrag = (msg[4] << 8) | msg[5];
/*log message arrival*/
snprintf(logmsg, 60, "Received TEST msg. W: %d, L: %d, F: %d", inweight,
inlevel, infrag);
log_msg(logmsg, node->log);
/*If sender is at higher level, we don't know if we are in the same fragment
or not yet, so delay response*/
if (inlevel > ndata->level) {
snprintf(logmsg, 60, "Sender has higher level, delaying response!");
log_msg(logmsg, node->log);
/*place message at the end of the queue, TEST messages have length 6*/
enqueue(node->queue, msg, 6);
sleep(1);
}
/*Sender is outside our fragment and lower/equal level, send ACCEPT*/
else if (infrag != ndata->frag_id) {
snprintf(logmsg, 60, "Sending ACCEPT msg on edge with weight %d",
inweight);
log_msg(logmsg, node->log);
uint8_t len = create_msg(MSG_ACCEPT, inweight, 0, 0, 0, outmsg);
send(edge_sock, outmsg, len, 0);
}
/*Only other possibility is an invalid edge (leads to same fragment), so we
reject it. If it's our current test edge we've already sent a test message
ourselves and will eventually get a REJECT, so we move on to the next edge.
If not, we reply with our own REJECT message.*/
else {
/*REJECT edge*/
if (ndata->edge_status[edge_index] == EDGE_UNKNOWN) {
ndata->edge_status[edge_index] = EDGE_REJECT;
}
if (ndata->test_edge != edge_index) {
snprintf(logmsg, 60, "Sending REJECT msg on tested edge!");
log_msg(logmsg, node->log);
uint8_t len = create_msg(MSG_REJECT, inweight, 0, 0, 0, outmsg);
send(edge_sock, outmsg, len, 0);
}
else {
snprintf(logmsg, 60, "Rejecting TEST edge, no need to report!");
log_msg(logmsg, node->log);
test(node, ndata);
}
}
}
void process_accept(struct node *node, struct node_data *ndata,
uint16_t edge_index, uint32_t edge_sock, uint8_t *msg) {
char logmsg[60];
uint16_t inweight;
/*retrieve message data*/
inweight = (msg[1] << 8) | msg[2];
/*log message arrival*/
snprintf(logmsg, 60, "Received ACCEPT on edge with weight %d", inweight);
log_msg(logmsg, node->log);
/*edge was accepted, we don't need test_edge anymore for this level*/
ndata->test_edge = -1;
/*if new best edge, update it*/
if (inweight < ndata->best_weight) {
ndata->best_edge = edge_index;
ndata->best_weight = inweight;
ndata->best_sock = edge_sock;
}
/*report LWOE to 'parent'*/
report(node, ndata);
}
void process_reject(struct node *node, struct node_data *ndata,
uint16_t edge_index, uint8_t *msg) {
char logmsg[60];
uint16_t inweight;
/*retrieve edge weight, for logging*/
inweight = (msg[1] << 8) | msg[2];
/*log message arrival*/
snprintf(logmsg, 60, "Received REJECT on edge with weight %d!", inweight);
log_msg(logmsg, node->log);
/*update edge status to REJECT if necessary, and begin testing other edges*/
if (ndata->edge_status[edge_index] == EDGE_UNKNOWN) {
ndata->edge_status[edge_index] = EDGE_REJECT;
}
test(node, ndata);
}
uint8_t process_report(struct node *node, struct node_data *ndata,
uint16_t edge_index, uint32_t edge_sock, uint8_t *msg) {
char logmsg[60];
uint16_t reported_weight, max_wt;
/*retrieve message data*/
reported_weight = (msg[3] << 8) | msg[4];
max_wt = (uint16_t) ~0;
/*log message arrival*/
snprintf(logmsg,60,"Received REPORT msg with LWOE cost %d",reported_weight);
log_msg(logmsg, node->log);
/*it's a regular neighbour reporting to us*/
if (edge_index != ndata->in_branch) {
ndata->fcount -= 1;
/*if new best edge, update it*/
if (reported_weight < ndata->best_weight) {
snprintf(logmsg,60,"Found new LWOE w/ weight %d!", reported_weight);
log_msg(logmsg, node->log);
ndata->best_weight = reported_weight;
ndata->best_edge = edge_index;
ndata->best_sock = edge_sock;
}
/*report back to 'parent'*/
report(node, ndata);
}
/*we're still in a different discovery phase, delay response*/
else if (ndata->state == NODE_FIND) {
snprintf(logmsg, 60, "Delaying response to REPORT message!");
log_msg(logmsg, node->log);
/*place message back into the end of the queue*/
enqueue(node->queue, msg, 5);
sleep(1);
}
/*received a weight that is higher than current candidate, means we found
LWOE already, and can start changeroot immediately*/
else if (reported_weight > ndata->best_weight) {
snprintf(logmsg, 60, "Found LWOE, calling CHANGEROOT procedure!");
log_msg(logmsg, node->log);
changeroot(node, ndata);
}
/*if incoming edge has 'infinite' weight, the algorithm is done!*/
else if (reported_weight == max_wt && ndata->best_weight == max_wt) {
/*this is a hacky way to force termination for other nodes: we send them
report messages with maximum weight, so they are notified the algorithm
has finished as well*/
uint16_t i;
struct edge *link = node->neighs->head;
for (i = 0; i < ndata->num_neighs; i++) {
if (ndata->edge_status[i] == EDGE_BRANCH && i != ndata->in_branch) {
uint8_t outmsg[50];
uint8_t len=create_msg(MSG_REPORT,link->weight,0,ndata->best_weight,0,outmsg);
send(link->sock, outmsg, len, 0);
}
link = link->next;
}
return 1;
}
return 0;
}
void changeroot(struct node *node, struct node_data *ndata) {
char logmsg[60];
uint8_t outmsg[50];
snprintf(logmsg, 60, "Beginning CHANGEROOT procedure!");
log_msg(logmsg, node->log);
/*our best edge is already in the MST, so we're not the new root, pass the
changeroot message forward*/
if (ndata->edge_status[ndata->best_edge] == EDGE_BRANCH) {
snprintf(logmsg, 60, "Passing CHGEROOT message forward!");
log_msg(logmsg, node->log);
uint8_t len = create_msg(MSG_CHGROOT, 0, 0, 0, 0, outmsg);
send(ndata->best_sock, outmsg, len, 0);
}
/*we are the new ROOT! Send CONNECT to the other fragment*/
else {
snprintf(logmsg, 60, "Sending CONNECT message with level %d!",
ndata->level);
log_msg(logmsg, node->log);
uint8_t len;
len=create_msg(MSG_CONNECT,ndata->best_weight,ndata->level,0,0,outmsg);
send(ndata->best_sock, outmsg, len, 0);
ndata->edge_status[ndata->best_edge] = EDGE_BRANCH;
}
}
void wakeup(struct node *node, struct node_data *data) {
uint8_t outmsg[50];
char logmsg[60];
/*Initialize node's data. All nodes start at state FOUND, with level, fcount
and fragment id 0. All edges begin as UNKNOWN. We also keep track of the
node's number of neighbours, which is a bit redundant but whatever*/
data->state = NODE_FOUND;
data->level = 0;
data->fcount = 0;
data->frag_id = 0;
data->num_neighs = node->neighs->num;
data->edge_status = (uint8_t*) malloc(data->num_neighs*sizeof(uint8_t));
memset(data->edge_status, EDGE_UNKNOWN, data->num_neighs);
/*at wakeup we haven't touched any edges yet, so lowest is first in the list*/
struct edge *lowest = node->neighs->head;
/*log lowest cost edge, and update its status*/
data->edge_status[0] = EDGE_BRANCH;
snprintf(logmsg, 60, "My lowest edge has weight %d", lowest->weight);
log_msg(logmsg, node->log);
/*send lowest edge neighbour a CONNECT message*/
uint8_t msg_len;
msg_len = create_msg(MSG_CONNECT, lowest->weight, data->level, 0, 0, outmsg);
send(lowest->sock, outmsg, msg_len, 0);
/*and log the send event*/
snprintf(logmsg, 60, "Sending CONNECT message with level %d to lowest edge!",
data->level);
log_msg(logmsg, node->log);
}
void test(struct node *node, struct node_data *ndata) {
char logmsg[60];
uint8_t outmsg[50];
snprintf(logmsg, 60, "Running TEST procedure to find LWOE!");
log_msg(logmsg, node->log);
/*Iterate over the node's edges, storing the lowest weight edge that hasn't
been classified as REJECT or BRANCH*/
ndata->test_edge = -1;
uint16_t i, edge_weight;
uint32_t sock;
struct edge *link = node->neighs->head;
for (i = 0; i < ndata->num_neighs; i++, link = link->next) {
if (ndata->edge_status[i] == EDGE_UNKNOWN) {
ndata->test_edge = i;
edge_weight = link->weight;
sock = link->sock;
break;
}
}
/*Found a candidate edge, send test message across it.*/
if (ndata->test_edge != -1) {
uint8_t len;
len = create_msg(MSG_TEST, edge_weight, ndata->level, ndata->frag_id,
0, outmsg);
send(sock, outmsg, len, 0);
snprintf(logmsg, 60, "Sending TEST message on edge with weight %d",
edge_weight);
log_msg(logmsg, node->log);
}
/*No candidate edge was found, report back to 'parent'*/
else {
snprintf(logmsg, 60, "No candidate edge, reporting!");
log_msg(logmsg, node->log);
report(node, ndata);
}
}
void report(struct node *node, struct node_data *ndata) {
char logmsg[60];
uint8_t outmsg[50];
/*We only report if all our neighbours are really done reporting to us.*/
if (ndata->fcount == 0 && ndata->test_edge == -1) {
/*we finished the discovery phase, move on to state FOUND*/
ndata->state = NODE_FOUND;
/*log beginning of report procedure*/
snprintf(logmsg, 60, "Node %d has begun reporting LWOE!", node->id);
log_msg(logmsg, node->log);
/*send report message to 'parent' in the MST*/
uint8_t len=create_msg(MSG_REPORT, ndata->branch_wt, 0,
ndata->best_weight, 0, outmsg);
send(ndata->branch_sock, outmsg, len, 0);
}
}
void output (struct node *node, struct node_data *ndata) {
char logmsg[60];
/*log beginning of final report*/
snprintf(logmsg, 60, "Node %d is reporting its BRANCH edges!", node->id);
log_msg(logmsg, node->globallog);
/*initialize report string*/
char *ptr = logmsg;
ptr += snprintf(logmsg, 60, "Node %d: ", node->id);
/*go through edges, appending their status*/
uint16_t i;
struct edge *link = node->neighs->head;
for (i = 0; i < ndata->num_neighs; i++) {
if (ndata->edge_status[i] == EDGE_BRANCH) {
ptr += snprintf(ptr,10,"%d ",link->weight);
}
link = link->next;
}
/*print final GHS algorithm log message for the node*/
log_msg(logmsg, node->globallog);
/*free its edge status array, which is the only dynamically allocated struct-
ture we malloc for each node in the algorithm implementation*/
free(ndata->edge_status);
}
uint8_t create_msg(uint8_t type, uint16_t weight, uint8_t level, uint16_t frag,
uint8_t state, uint8_t *buffer) {
/*avoid messing with invalid pointers*/
if (buffer == NULL) {
return 0;
}
uint8_t msg_len;
/*all messages share the first three bytes, for msg type and weight. All
messages piggyback edge weight so the receiving node can identify the edge*/
buffer[0] = type;
buffer[1] = (weight >> 8) & 0xFF;
buffer[2] = weight & 0xFF;
msg_len = 3;
/*fill out message content based on type*/
switch(type) {
/*CONNECT messages piggyback the fragment level*/
case MSG_CONNECT: {
buffer[3] = level;
msg_len++;
break;
}
/*INITIATE messages piggyback fragment edge, level and state*/
case MSG_INITIATE: {
buffer[3] = level;
buffer[4] = state;
buffer[5] = (frag >> 8) & 0xFF;
buffer[6] = frag & 0xFF;
msg_len += 4;
break;
}
/*TEST messages piggyback fragment level and edge*/
case MSG_TEST: {
buffer[3] = level;
buffer[4] = (frag >> 8) & 0xFF;
buffer[5] = frag & 0xFF;
msg_len += 3;
break;
}
/*ACCEPT and REJECT messages don't have any additional info. Neither does
CHANGEROOT*/
case MSG_ACCEPT:
case MSG_REJECT:
case MSG_CHGROOT: {
break;
}
/*REPORT messages add on the reported weight in the fragment field*/
case MSG_REPORT: {
buffer[3] = (frag >> 8) & 0xFF;
buffer[4] = frag & 0xFF;
msg_len += 2;
break;
}
/*Unknown message ID, something went very wrong...*/
default: {
fprintf(stderr, "Invalid message type at create_msg: %d!\n", type);
break;
}
}
return msg_len;
}