-
Notifications
You must be signed in to change notification settings - Fork 0
/
MP1Node.cpp
281 lines (242 loc) · 6.72 KB
/
MP1Node.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
/**********************************
* FILE NAME: MP1Node.cpp
*
* DESCRIPTION: Membership protocol run by this Node.
* Definition of MP1Node class functions.
**********************************/
#include "MP1Node.h"
/*
* Note: You can change/add any functions in MP1Node.{h,cpp}
*/
/**
* Overloaded Constructor of the MP1Node class
* You can add new members to the class if you think it
* is necessary for your logic to work
*/
MP1Node::MP1Node(Member *member, Params *params, EmulNet *emul, Log *log, Address *address) {
for( int i = 0; i < 6; i++ ) {
NULLADDR[i] = 0;
}
this->memberNode = member;
this->emulNet = emul;
this->log = log;
this->par = params;
this->memberNode->addr = *address;
}
/**
* Destructor of the MP1Node class
*/
MP1Node::~MP1Node() {}
/**
* FUNCTION NAME: recvLoop
*
* DESCRIPTION: This function receives message from the network and pushes into the queue
* This function is called by a node to receive messages currently waiting for it
*/
int MP1Node::recvLoop() {
if ( memberNode->bFailed ) {
return false;
}
else {
return emulNet->ENrecv(&(memberNode->addr), enqueueWrapper, NULL, 1, &(memberNode->mp1q));
}
}
/**
* FUNCTION NAME: enqueueWrapper
*
* DESCRIPTION: Enqueue the message from Emulnet into the queue
*/
int MP1Node::enqueueWrapper(void *env, char *buff, int size) {
Queue q;
return q.enqueue((queue<q_elt> *)env, (void *)buff, size);
}
/**
* FUNCTION NAME: nodeStart
*
* DESCRIPTION: This function bootstraps the node
* All initializations routines for a member.
* Called by the application layer.
*/
void MP1Node::nodeStart(char *servaddrstr, short servport) {
Address joinaddr;
joinaddr = getJoinAddress();
// Self booting routines
if( initThisNode(&joinaddr) == -1 ) {
#ifdef DEBUGLOG
log->LOG(&memberNode->addr, "init_thisnode failed. Exit.");
#endif
exit(1);
}
if( !introduceSelfToGroup(&joinaddr) ) {
finishUpThisNode();
#ifdef DEBUGLOG
log->LOG(&memberNode->addr, "Unable to join self to group. Exiting.");
#endif
exit(1);
}
return;
}
/**
* FUNCTION NAME: initThisNode
*
* DESCRIPTION: Find out who I am and start up
*/
int MP1Node::initThisNode(Address *joinaddr) {
/*
* This function is partially implemented and may require changes
*/
int id = *(int*)(&memberNode->addr.addr);
int port = *(short*)(&memberNode->addr.addr[4]);
memberNode->bFailed = false;
memberNode->inited = true;
memberNode->inGroup = false;
// node is up!
memberNode->nnb = 0;
memberNode->heartbeat = 0;
memberNode->pingCounter = TFAIL;
memberNode->timeOutCounter = -1;
initMemberListTable(memberNode);
return 0;
}
/**
* FUNCTION NAME: introduceSelfToGroup
*
* DESCRIPTION: Join the distributed system
*/
int MP1Node::introduceSelfToGroup(Address *joinaddr) {
MessageHdr *msg;
#ifdef DEBUGLOG
static char s[1024];
#endif
if ( 0 == memcmp((char *)&(memberNode->addr.addr), (char *)&(joinaddr->addr), sizeof(memberNode->addr.addr))) {
// I am the group booter (first process to join the group). Boot up the group
#ifdef DEBUGLOG
log->LOG(&memberNode->addr, "Starting up group...");
#endif
memberNode->inGroup = true;
}
else {
size_t msgsize = sizeof(MessageHdr) + sizeof(joinaddr->addr) + sizeof(long) + 1;
msg = (MessageHdr *) malloc(msgsize * sizeof(char));
// create JOINREQ message: format of data is {struct Address myaddr}
msg->msgType = JOINREQ;
memcpy((char *)(msg+1), &memberNode->addr.addr, sizeof(memberNode->addr.addr));
memcpy((char *)(msg+1) + 1 + sizeof(memberNode->addr.addr), &memberNode->heartbeat, sizeof(long));
#ifdef DEBUGLOG
sprintf(s, "Trying to join...");
log->LOG(&memberNode->addr, s);
#endif
// send JOINREQ message to introducer member
emulNet->ENsend(&memberNode->addr, joinaddr, (char *)msg, msgsize);
free(msg);
}
return 1;
}
/**
* FUNCTION NAME: finishUpThisNode
*
* DESCRIPTION: Wind up this node and clean up state
*/
int MP1Node::finishUpThisNode(){
/*
* Your code goes here
*/
}
/**
* FUNCTION NAME: nodeLoop
*
* DESCRIPTION: Executed periodically at each member
* Check your messages in queue and perform membership protocol duties
*/
void MP1Node::nodeLoop() {
if (memberNode->bFailed) {
return;
}
// Check my messages
checkMessages();
// Wait until you're in the group...
if( !memberNode->inGroup ) {
return;
}
// ...then jump in and share your responsibilites!
nodeLoopOps();
return;
}
/**
* FUNCTION NAME: checkMessages
*
* DESCRIPTION: Check messages in the queue and call the respective message handler
*/
void MP1Node::checkMessages() {
void *ptr;
int size;
// Pop waiting messages from memberNode's mp1q
while ( !memberNode->mp1q.empty() ) {
ptr = memberNode->mp1q.front().elt;
size = memberNode->mp1q.front().size;
memberNode->mp1q.pop();
recvCallBack((void *)memberNode, (char *)ptr, size);
}
return;
}
/**
* FUNCTION NAME: recvCallBack
*
* DESCRIPTION: Message handler for different message types
*/
bool MP1Node::recvCallBack(void *env, char *data, int size ) {
/*
* Your code goes here
*/
}
/**
* FUNCTION NAME: nodeLoopOps
*
* DESCRIPTION: Check if any node hasn't responded within a timeout period and then delete
* the nodes
* Propagate your membership list
*/
void MP1Node::nodeLoopOps() {
/*
* Your code goes here
*/
return;
}
/**
* FUNCTION NAME: isNullAddress
*
* DESCRIPTION: Function checks if the address is NULL
*/
int MP1Node::isNullAddress(Address *addr) {
return (memcmp(addr->addr, NULLADDR, 6) == 0 ? 1 : 0);
}
/**
* FUNCTION NAME: getJoinAddress
*
* DESCRIPTION: Returns the Address of the coordinator
*/
Address MP1Node::getJoinAddress() {
Address joinaddr;
memset(&joinaddr, 0, sizeof(Address));
*(int *)(&joinaddr.addr) = 1;
*(short *)(&joinaddr.addr[4]) = 0;
return joinaddr;
}
/**
* FUNCTION NAME: initMemberListTable
*
* DESCRIPTION: Initialize the membership list
*/
void MP1Node::initMemberListTable(Member *memberNode) {
memberNode->memberList.clear();
}
/**
* FUNCTION NAME: printAddress
*
* DESCRIPTION: Print the Address
*/
void MP1Node::printAddress(Address *addr)
{
printf("%d.%d.%d.%d:%d \n", addr->addr[0],addr->addr[1],addr->addr[2],
addr->addr[3], *(short*)&addr->addr[4]) ;
}