-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.c
112 lines (90 loc) · 2.46 KB
/
app.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
/**********************
*
* Progam Name: MP1. Membership Protocol
*
* Current file: app.c
* About this file: App (Application) Layer. Main simulator loop ( main() ).
*
***********************/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "app.h"
#include "MPtemplate.h"
#include <stdlib.h>
/*
*
* Main function.
*
*/
int main(int argc, char *argv[]){
int i,removed;
srand (time(NULL));
if (argc < 1)
{
printf("Config File Required\n");
return 1;
}
setparams(argv[1]);
srand(time(NULL));
MPinit=ENinit,MPp2psend=ENp2psend,MPrecv=ENrecv,MPcleanup=ENcleanup;
group = malloc(MAX_NNB*sizeof(member));
/*
Create an array of processes,
Call nodestart() on each of them,
Periodically call nodeloop() on each of them (once per time unit)
Finally call finishup_thisnode() on each one of them
*/
for(i=0;i<=EN_GPSZ-1;i++)
group[i].inited=0;
for(globaltime=0; globaltime<500; ++globaltime) {
/* call recvloop for all nodes currently in the system */
for(i=0;i<=EN_GPSZ-1;i++)
if(getcurrtime()>(int)(STEP_RATE*i) && group[i].bfailed==0)
recvloop(&group[i]);
for(i=EN_GPSZ-1;i>=0;i--) {
//int a = getcurrtime();
//printf("%d \n", a);
if(getcurrtime() == (int)(STEP_RATE*i)) {
/* introduce the ith node into the system at time STEPRATE*i */
nodestart(&group[i], JOINADDR, PORTNUM); /* last two params not used here */
printf("%d-th introduced node is assigned with the address: ", i);
}
else if(getcurrtime()>(int)(STEP_RATE*i) && group[i].bfailed==0) {
nodeloop(&group[i]);
#ifdef DEBUGLOG
if(i==0&&globaltime%500==0)
LOG(&group[0].addr, "@@time=%d", getcurrtime());
#endif
}
}
/* fail half the members at time t=400 */
if(DROP_MSG && getcurrtime()==50)
dropmsg = 1;
if(SINGLE_FAILURE && getcurrtime()==100)
{
removed = rand() % EN_GPSZ;
#ifdef DEBUGLOG
LOG(&group[removed].addr, "Node failed at time=%d", getcurrtime());
#endif
group[removed].bfailed=1;
}
else if(getcurrtime() == 100)
{
removed = rand() % EN_GPSZ/2;
for (i = removed; i < removed + EN_GPSZ/2; i++)
{
#ifdef DEBUGLOG
LOG(&group[i].addr, "Node failed at time=%d", getcurrtime());
#endif
group[i].bfailed = 1;
}
}
if(DROP_MSG && getcurrtime()==300)
dropmsg=0;
}
MPcleanup();
for(i=0;i<=EN_GPSZ-1;i++)
finishup_thisnode(&group[i]);
return 0;
}