-
Notifications
You must be signed in to change notification settings - Fork 1
/
mp1_node.c
607 lines (483 loc) · 15.2 KB
/
mp1_node.c
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
595
596
597
598
599
600
601
602
603
604
605
606
607
/**********************
*
* Progam Name: MP1. Membership Protocol
*
* Code authors: Udit Mehrotra, NetID : umehrot2
*
* Current file: mp1_node.c
* About this file: Member Node Implementation
*
***********************/
#include "mp1_node.h"
#include "emulnet.h"
#include "MPtemplate.h"
#include "log.h"
#define TFAIL 10
#define TCLEANUP 10
/*
*
* Routines for introducer and current time.
*
*/
char NULLADDR[] = {0,0,0,0,0,0};
int isnulladdr( address *addr){
return (memcmp(addr, NULLADDR, 6)==0?1:0);
}
/*
Return the address of the introducer member.
*/
address getjoinaddr(void){
address joinaddr;
memset(&joinaddr, 0, sizeof(address));
*(int *)(&joinaddr.addr)=1;
*(short *)(&joinaddr.addr[4])=0;
return joinaddr;
}
/*
*
* Message Processing routines.
*
*/
/*
Adds a Node to membership table of the Introducer
*/
void addNode(void *env,char *data)
{
member *node = (member *) env;
//create a new membership Entry object
struct memEntry t;
t.addr.addr[0] = data[0];
t.addr.addr[1] = data[1];
t.addr.addr[2] = data[2];
t.addr.addr[3] = data[3];
t.addr.addr[4] = data[4];
t.addr.addr[5] = data[5];
t.heartbeat = 0;
t.flag = 0;
t.time = globaltime;
//Check to find the first deleted entry in the table where new entry can be added
int i=0;
while(i < node->memCount)
{
if(node->ml[i].flag == 1)
break;
i++;
}
//If a deleted entry is found, add the new entry there, else append it to the table at the end
if(i < node->memCount)
{
node->ml[i] = t;
}
else
{
node->ml[node->memCount] = t;
node->memCount++;
}
//Log the node added
#ifdef DEBUGLOG
logNodeAdd(&node->addr,&t.addr);
#endif
}
/*
Received a JOINREQ (joinrequest) message.
*/
void Process_joinreq(void *env, char *data, int size)
{
member *node = (member *) env;
#ifdef DEBUGLOG
static char s[1024];
#endif
#ifdef DEBUGLOG
LOG(&((member *)env)->addr, "Processing Join Request !!");
#endif
/* create JOINREP message: format of data is {struct address myaddr} */
#ifdef DEBUGLOG
sprintf(s, "Trying to join...");
LOG(&node->addr, s);
#endif
//Add the node to introducers membership table
addNode(env,data);
//Create a new JOIN REPLY message
size_t msgsize = sizeof(messagehdr) + sizeof(memEntry *);
messagehdr *msg = malloc(msgsize);
msg->msgtype = JOINREP;
//Fetch the Membership list that is to be sent in JOIN REPLY
struct memEntry *mlist = malloc(MAX_NNB * sizeof(memEntry));
int loop = 0;
struct memEntry *a = node->ml;
while(a->addr.addr[0] != '\0')
{
mlist->addr = a->addr;
mlist->heartbeat = a->heartbeat;
mlist->flag = a->flag;
mlist->time = a->time;
a++;
mlist++;
loop++;
}
mlist = mlist - loop;
//Add the membership list in message + 1
memcpy((char *) (msg +1),&mlist, sizeof(memEntry *));
/* send JOINREP message to introducer member. */
MPp2psend(&node->addr, data, (char *)msg, msgsize);
free(msg);
return;
}
/*
This function merges the membership list passed, with the membership list of the node
*/
void load_membershipTable(void *env, void *mlist)
{
int count;
member *node = (member *) env;
memEntry *list = (memEntry *)mlist;
if(list->addr.addr != NULL)
{
//Loop through all the entries in the membership list to be merged
while(list->addr.addr[0] != '\0')
{
if(list->flag == 0)
{
count = 0;
int j=0;
//Loop through all the entries in the node's membership list
while(node->ml[j].addr.addr[0] != '\0')
{
if(memcmp(&node->ml[j].addr,&list->addr,4*sizeof(char)) == 0)
{
//Check if entry already there is nodes membership table and set flag count
count = 1;
break;
}
j++;
}
if(count == 0)
{
//This means that the entry is not there in node's membership table
//Find the first deleted entry where the new entry can be added, else append it
int x=0;
while(x < node->memCount)
{
if(node->ml[x].flag == 1)
break;
x++;
}
node->ml[x].addr = list->addr;
node->ml[x].heartbeat = list->heartbeat;
node->ml[x].flag = list->flag;
node->ml[x].time = list->time;
//Log the newly added node to the membership table
#ifdef DEBUGLOG
logNodeAdd(&node->addr, &list->addr);
#endif
if(x == node->memCount)
node->memCount++;
}
else
{
//check for better heartbeats, for non deleted nodes in the membership table
if(node->ml[j].flag != 1)
{
if(node->ml[j].heartbeat < list->heartbeat)
{
node->ml[j].heartbeat = list->heartbeat;
node->ml[j].time = globaltime;
node->ml[j].flag = 0;
}
}
}
}
list = list + 1;
}
}
}
/*
Received a JOINREP (joinreply) message.
*/
void Process_joinrep(void *env, char *data, int size)
{
member *node = (member *) env;
memEntry **temp = (memEntry **) data;
memEntry *node1 = *temp;
//Merge the current node's membership list with introducers list
load_membershipTable(node, node1);
//Set current node's ingroup to 1
node->ingroup = 1;
#ifdef DEBUGLOG
LOG(&((member *)env)->addr, "Processing Rep Request !!");
#endif
return;
}
/*
Process a received a GOSSIP (gossip) message.
*/
void Process_gossip(void *env, char *data, int size)
{
member *node = (member *) env;
memEntry **temp = (memEntry **) data;
memEntry *node1 = *temp;
//Merge membership table from the gossip message with current nodes membership table
load_membershipTable(node, node1);
}
/*
Array of Message handlers.
*/
void ( ( * MsgHandler [20] ) STDCLLBKARGS )={
/* Message processing operations at the P2P layer. */
Process_joinreq,
Process_joinrep,
Process_gossip
};
/*
Called from nodeloop() on each received packet dequeue()-ed from node->inmsgq.
Parse the packet, extract information and process.
env is member *node, data is 'messagehdr'.
*/
int recv_callback(void *env, char *data, int size){
member *node = (member *) env;
messagehdr *msghdr = (messagehdr *)data;
char *pktdata = (char *)(msghdr+1);
if(size < sizeof(messagehdr)){
#ifdef DEBUGLOG
LOG(&((member *)env)->addr, "Faulty packet received - ignoring");
#endif
return -1;
}
#ifdef DEBUGLOG
LOG(&((member *)env)->addr, "Received msg type %d with %d B payload", msghdr->msgtype, size - sizeof(messagehdr));
#endif
if((node->ingroup && msghdr->msgtype >= 0 && msghdr->msgtype <= DUMMYLASTMSGTYPE)
|| (!node->ingroup && msghdr->msgtype==JOINREP))
/* if not yet in group, accept only JOINREPs */
MsgHandler[msghdr->msgtype](env, pktdata, size-sizeof(messagehdr));
/* else ignore (garbled message) */
free(data);
return 0;
}
/*
*
* Initialization and cleanup routines.
*
*/
/*
Find out who I am, and start up.
*/
int init_thisnode(member *thisnode, address *joinaddr){
if(MPinit(&thisnode->addr, PORTNUM, (char *)joinaddr)== NULL){ /* Calls ENInit */
#ifdef DEBUGLOG
LOG(&thisnode->addr, "MPInit failed");
#endif
exit(1);
}
#ifdef DEBUGLOG
else LOG(&thisnode->addr, "MPInit succeeded. Hello.");
#endif
//Initialize the values for the newly inited node, and allocate its membership table
thisnode->bfailed=0;
thisnode->inited=1;
thisnode->ingroup=0;
thisnode->ml = malloc(MAX_NNB*sizeof(memEntry));
thisnode->memCount = 1;
thisnode->ml[0].addr = thisnode->addr;
thisnode->ml[0].heartbeat = 0;
thisnode->ml[0].flag = 0;
thisnode->ml[0].time = globaltime;
/* node is up! */
return 0;
}
/*
Clean up this node.
*/
int finishup_thisnode(member *node){
free(node->ml); //de-allocating the membership table
memcpy(&node->addr,NULLADDR,6 * sizeof(char)); //Converting node address to null
LOG(&node->addr,"Cleaned up the node");
return 0;
}
/*
*
* Main code for a node
*
*/
/*
Introduce self to group.
*/
int introduceselftogroup(member *node, address *joinaddr){
messagehdr *msg;
#ifdef DEBUGLOG
static char s[1024];
#endif
if(memcmp(&node->addr, joinaddr, 4*sizeof(char)) == 0){
/* I am the group booter (first process to join the group). Boot up the group. */
#ifdef DEBUGLOG
LOG(&node->addr, "Starting up group...");
#endif
node->ingroup = 1;
}
else{
size_t msgsize = sizeof(messagehdr) + sizeof(address);
msg=malloc(msgsize);
/* create JOINREQ message: format of data is {struct address myaddr} */
msg->msgtype=JOINREQ;
memcpy((char *)(msg+1), &node->addr, sizeof(address));
#ifdef DEBUGLOG
sprintf(s, "Trying to join...");
LOG(&node->addr, s);
#endif
/* send JOINREQ message to introducer member. */
MPp2psend(&node->addr, joinaddr, (char *)msg, msgsize);
free(msg);
}
return 1;
}
/*
Called from nodeloop().
*/
void checkmsgs(member *node){
void *data;
int size;
/* Dequeue waiting messages from node->inmsgq and process them. */
while((data = dequeue(&node->inmsgq, &size)) != NULL) {
recv_callback((void *)node, data, size);
}
return;
}
/*
Called periodically from nodelooops() to check for any Membership entry for which Tfail or Tcleanup time
has passed, and set them to deleted or suspended state
*/
void checkDelete(void *env)
{
member *node = (member *) env;
int i = 1;
//Loop through all the entries in the membership list of the node
while(node->ml[i].addr.addr[0] != '\0')
{
if(node->ml[i].flag != 1)
{
if(globaltime > (node->ml[i].time+ TFAIL))
{
//Set Suspended flag if TFAIL time has passed
node->ml[i].flag = 2;
}
if(globaltime > (node->ml[i].time + TFAIL + TCLEANUP))
{
//Set Deleted flag if TFAIL & TCLEANUP time has passed
node->ml[i].flag = 1;
//Log the deleted node
#ifdef DEBUGLOG
logNodeRemove(&node->addr, &node->ml[i].addr);
#endif
}
}
i++;
}
}
/*
Returns a random number in the range 1 to n-1
*/
int random_num(int n)
{
int random = 0;
while(random == 0)
random = rand() % n;
return random;
}
/*
Executed periodically for each member.
Performs necessary periodic operations.
Called by nodeloop(). It picks a random other node to gossip its membership list.
*/
void nodeloopops(member *node){
int random = 0;
if(node->ingroup == 1)
{
int count = node->memCount;
int i = 1;
int flag = 0;
//Check if there is a non deleted/suspended entry in the membership table to GOSSIP to
while(i < node->memCount)
{
if(node->ml[i].flag == 0)
{
//Found atleast one alive entry, so can go ahead with gossip
flag = 1;
break;
}
i++;
}
if(flag == 1)
{
/* Get a random number for indexing the membership list, such that the number is not 0, since first entry in each members table is its own entry, and the entry at the number is not deleted/suspended */
while(node->ml[random].flag != 0 || random == 0)
{
random = random_num(count);
int i = 0;
while(i < node->memCount)
{
i++;
}
}
/* Create a message of type GOSSIP and send across the membership table */
size_t msgsize = sizeof(messagehdr) + sizeof(memEntry *);
messagehdr *msg=malloc(msgsize);
msg->msgtype=GOSSIP;
node->ml[0].heartbeat++;
node->ml[0].time = globaltime;
struct address a = node->ml[random].addr;
memcpy((char *) (msg +1),&node->ml, sizeof(memEntry *));
MPp2psend(&node->addr, &a, (char *)msg, msgsize);
free(msg);
}
checkDelete(node);
}
return;
}
/*
Executed periodically at each member. Called from app.c.
*/
void nodeloop(member *node){
if (node->bfailed) return;
checkmsgs(node);
/* Wait until you're in the group... */
if(!node->ingroup) return ;
/* ...then jump in and share your responsibilites! */
nodeloopops(node);
return;
}
/*
All initialization routines for a member. Called by app.c.
*/
void nodestart(member *node, char *servaddrstr, short servport){
address joinaddr=getjoinaddr();
/* Self booting routines */
if(init_thisnode(node, &joinaddr) == -1){
#ifdef DEBUGLOG
LOG(&node->addr, "init_thisnode failed. Exit.");
#endif
exit(1);
}
#ifdef DEBUGLOG
logNodeAdd(&node->addr,&node->addr);
#endif
if(!introduceselftogroup(node, &joinaddr)){
finishup_thisnode(node);
#ifdef DEBUGLOG
LOG(&node->addr, "Unable to join self to group. Exiting.");
#endif
exit(1);
}
return;
}
/*
Enqueue a message (buff) onto the queue env.
*/
int enqueue_wrppr(void *env, char *buff, int size){ return enqueue((queue *)env, buff, size);}
/*
Called by a member to receive messages currently waiting for it.
*/
int recvloop(member *node){
if (node->bfailed) return -1;
else return MPrecv(&(node->addr), enqueue_wrppr, NULL, 1, &node->inmsgq);
/* Fourth parameter specifies number of times to 'loop'. */
}