-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.cc
546 lines (438 loc) · 13.3 KB
/
node.cc
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
// c++ libs
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
// c libs
#include <cstdio>
#include <cstdlib>
#include <arpa/inet.h>
#include <time.h>
#include "socket.h"
#include "sha1.h"
#include "messages.h"
#include "node_class.h"
#define STABILIZATION_INTERVAL .25
using namespace std;
// how many messages this node has sent
int TOTAL_MESSAGES = 0;
int STABILIZER_MESSAGES = 0;
// function definitions
int main(int argc, char ** argv);
Node closestFinger(int queryId);
void quit(int socket);
void addFile(int socket);
void findSuccessor(int socket);
void notify(int socket);
void delFile(int socket);
void findFile(int socket);
void getTable(int socket);
void startDetachedThread( void * (*functor)(void *), void * arg );
pthread_t startThread( void * (*functor)(void *), void * arg );
// threads
void * fixFingers(void * arg);
void * fixFiles(void * arg);
void * stabilizer(void * arg);
void * thread_conn_handler(void * arg);
// global variables definitions
int m;
int id;
int port;
bool shouldQuit = false;
// finger table
vector<Node> ft;
pthread_mutex_t ft_mutex = PTHREAD_MUTEX_INITIALIZER;
// files
vector<string> files;
// ip addresses
vector<string> ipaddrs;
pthread_t stabilizer_thread;
pthread_t fixFingers_thread;
// predecessor
Node next;
// successor
Node prev;
int main(int argc, char ** argv){
if( argc != 4 && argc != 5){
cerr << "Usage: " << argv[0] << " <m> <id> <port> [<introducer-port>]" << endl;
return EXIT_FAILURE;
}
// seed our RNG
srand( time(NULL) );
// initialzie our thread counter
// grab parameters
m = atoi(argv[1]);
id = atoi(argv[2]);
port = atoi(argv[3]);
int introducer_port = 0;
if( argc == 5 ){
introducer_port = atoi(argv[4]);
}
// initialize the next and prev nodes
next = Node(id, port);
prev = Node(id, port);
if( id != 0 ){
Node introducer(0, introducer_port);
next = introducer.findSuccessorTo(id);
}
// initialize finger table to self
for(int i=0;i<m;i++){
ft.push_back(next);
}
// if port == 0, we get the new port number back as an output param
int server = setup_server(port, &port);
stabilizer_thread = startThread(stabilizer, NULL);
fixFingers_thread = startThread(fixFingers, NULL);
socklen_t sin_size;
struct sockaddr_storage their_addr;
char s[INET6_ADDRSTRLEN];
// main loop, always accepting incoming connections
while( !shouldQuit ){
sin_size = sizeof their_addr;
// we'll let incoming connecctions timeout to check if we should quit...
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(server, &readfds);
// Timeout parameter
timeval tv = { 0 };
tv.tv_sec = 1;
int ret = select(1000, &readfds, NULL, NULL, &tv);
if (ret > 0) {
if (FD_ISSET(server, &readfds)) {
// Accept incoming connection and add new socket to list
int new_fd = accept(server, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s);
//cout << "Node " << id << " got connection from " << s << endl;
// prepare argument for thread
int * arg = (int *) malloc( sizeof(int) );
*arg = new_fd;
startDetachedThread(thread_conn_handler, arg);
}
}
}
return EXIT_SUCCESS;
}
// function which check if test lies on the ring
// between first and second
bool between(int first, int second, int test){
if( first > second ){
return test > first || test < second;
}
return test > first && test < second;
}
// thread which periodically runs to update the successor
// at a node
void * stabilizer(void * arg){
while( true ){
usleep( (int)(1000000 * STABILIZATION_INTERVAL) );
// lock
pthread_mutex_lock(&ft_mutex);
Node x = next.findPredecessor();
if( id == next.id || between(id, next.id, x.id) ){
next = x;
ft[0] = next;
}
next.notify(id, port);
// unlock
pthread_mutex_unlock(&ft_mutex);
}
return NULL;
}
// thread which moves files (if they need moving)
void * fixFiles(void * arg){
// wait for system to stablizie a little
usleep((int)(1000000 * STABILIZATION_INTERVAL));
vector<string> files_left;
vector<string> ips_left;
for(unsigned int i=0;i<files.size();i++){
int key = SHA1(files[i], m);
if(key == id || id == prev.id || between(prev.id, id, key)){
files_left.push_back( files[i] );
ips_left.push_back( ipaddrs[i] );
} else {
prev.addFile(files[i], ipaddrs[i]);
}
}
files = files_left;
ipaddrs = ips_left;
return NULL;
}
// stablilzer function to periodically update each node's finger table
// for nodes, we update ALL the finger table to ease grading
void * fixFingers(void * arg){
while( true ){
usleep((int)(1000000 * STABILIZATION_INTERVAL));
// lock
pthread_mutex_lock(&ft_mutex);
// we can reduce the number of messages sent by implementing random sending
// we left it as NOT random to ensure our finger tables will be correct for queries (for grading)
//int i = (int)( ft.size() * rand() / (RAND_MAX + 1.0) );
for(unsigned int i=0;i<ft.size();i++){
int fingerId = (id + (1 << i)) % (1 << m);
Node closest = closestFinger(fingerId);
if( id == closest.id || closest.id == fingerId ){
ft[i] = closest;
} else {
ft[i] = closest.findSuccessorTo( fingerId );
}
}
// unlock shared memory
pthread_mutex_unlock(&ft_mutex);
}
return NULL;
}
void startDetachedThread( void * (*functor)(void *), void * arg ){
pthread_attr_t DetachedAttr;
pthread_attr_init(&DetachedAttr);
pthread_attr_setdetachstate(&DetachedAttr, PTHREAD_CREATE_DETACHED);
pthread_t handler;
if( pthread_create(&handler, &DetachedAttr, functor, arg) ){
free(arg);
perror("pthread_create");
}
pthread_detach(handler);
// free resources for detached attribute
pthread_attr_destroy(&DetachedAttr);
}
pthread_t startThread( void * (*functor)(void *), void * arg ){
pthread_t handler;
if( pthread_create(&handler, NULL, functor, arg) ){
free(arg);
perror("pthread_create");
}
return handler;
}
// finds the closest id to a queryId in the finger table
Node closestFinger(int queryId){
for(int i=m-1;i>=0;i--){
if( ft[i].id == queryId ){
return ft[i];
}
if( between(ft[i].id, id, queryId) ){
return ft[i];
}
}
return ft[0];
}
// launch a new node process
pid_t launchNode(int m, int id){
pid_t pid = fork();
char mString[6] = {0};
sprintf(mString, "%d", m);
char idString[32] = {0};
sprintf(idString, "%d", id);
char portString[] = "0";
char introducerPortString[32] = {0};
sprintf(introducerPortString, "%d", port);
if( pid == 0 ){
// launch listener process
execlp("./node", "./node", mString, idString, portString, introducerPortString, (char *)0 );
}
return pid;
}
void * thread_conn_handler(void * arg){
int socket = *((int*)arg);
free(arg);
int command = readint(socket);
TOTAL_MESSAGES++;
if( command == ADD_NODE){
int size = readint(socket);
for(int i=0; i<size; i++){
int id = readint(socket);
launchNode(m, id);
}
}
else if( command == ADD_FILE){
addFile(socket);
}
else if( command == DEL_FILE){
delFile(socket);
}
else if( command == FIND_FILE){
findFile(socket);
}
else if( command == GET_TABLE){
getTable(socket);
}
else if( command == NOTIFY ){
STABILIZER_MESSAGES++;
notify(socket);
}
else if( command == FIND_SUCCESSOR ){
STABILIZER_MESSAGES++;
findSuccessor(socket);
}
else if( command == FIND_PREDECESSOR ){
STABILIZER_MESSAGES++;
sendint(socket, prev.id);
sendint(socket, prev.port);
}
else if( command == QUIT){
quit(socket);
}
// stop reading from socket, but keep tryingn to send data
close(socket);
return NULL;
}
void getTable(int socket){
int queryId = readint(socket);
Node closest = closestFinger(queryId);
pair< vector<int>, vector<int> > returnValue;
// if found
if( queryId == id )
{
for(unsigned int i=0;i<ft.size();i++) {
returnValue.first.push_back( ft[i].id );
}
for(unsigned int i=0;i<files.size();i++) {
returnValue.second.push_back( SHA1(files[i], m) );
}
} else if( id == next.id || between(id, next.id, queryId) ){
vector<int> temp;
returnValue = pair< vector<int>, vector<int> >(temp, temp);
}else {
returnValue = closest.getTable(queryId);
}
sendint(socket, returnValue.first.size());
for(unsigned int i=0;i<returnValue.first.size();i++){
sendint(socket, returnValue.first[i]);
}
sendint(socket, returnValue.second.size());
for(unsigned int i=0;i<returnValue.second.size();i++){
sendint(socket, returnValue.second[i] );
}
}
void findFile(int socket){
string filename = readstring(socket);
int key = SHA1(filename, m);
bool found = false;
string ip = "";
int fileNodeId = -1;
if( key == id || between(prev.id, id, key) ){
fileNodeId = id;
// look for this file
for(unsigned int i=0;i<files.size();i++){
if(files[i] == filename){
found = true;
ip = ipaddrs[i];
break;
}
}
} else {
Node closest = closestFinger(key);
pair<int, string> result = closest.findFile(filename);
if(result.first == -1){
found = false;
} else {
found = true;
fileNodeId = result.first;
ip = result.second;
}
}
if( found ){
sendint(socket, FILE_FOUND);
sendint(socket, fileNodeId);
sendstring(socket, ip);
} else {
sendint(socket, FILE_NOT_FOUND);
}
}
void delFile(int socket){
string filename = readstring(socket);
int key = SHA1(filename, m);
int fileNodeId = -1;
// if the key is the same as the node ID or in between node Id and its predecessor
if (key==id || between(prev.id,id,key))
{
// search for the file inside this node
for (unsigned int i=0; i < files.size(); ++i)
{
if (files[i]==filename)
{
files.erase(files.begin() + i);
ipaddrs.erase(ipaddrs.begin() + i);
fileNodeId = id;
break;
}
}
} else {
// Otherwise, ask the closest node
Node closestNode = closestFinger(key);
fileNodeId = closestNode.removeFile(filename);
}
if( fileNodeId != -1){
sendint(socket, FILE_FOUND);
sendint(socket, fileNodeId);
} else {
sendint(socket, FILE_NOT_FOUND);
}
}
void notify(int socket){
int theirId = readint(socket);
int theirPort = readint(socket);
Node them(theirId, theirPort);
if( prev.id == id || between(prev.id, id, them.id) ){
if( them.id != id ){
prev = them;
startDetachedThread(fixFiles, NULL);
}
prev = them;
}
// send an ack...
sendint(socket, 0);
}
void findSuccessor(int socket){
int queryId = readint(socket);
Node closest = closestFinger(queryId);
if( queryId == id ){
closest = Node(id, port);
}
else if( queryId == next.id || between(id, next.id, queryId) ){
closest = next;
}
else if( closest.id != id ){
closest = closest.findSuccessorTo(queryId);
}
sendint(socket, closest.id);
sendint(socket, closest.port);
}
void addFile(int socket){
string filename = readstring(socket);
string ipaddr = readstring(socket);
int key = SHA1(filename,m);
int fileNodeId = -1;
// if the key is the same as the node ID or in between node Id and its predecessor
if (prev.id == id || key == id || between(prev.id,id,key))
{
fileNodeId = id;
files.push_back(filename.c_str());
ipaddrs.push_back(ipaddr.c_str());
} else {
// Otherwise, ask the closest node (recursion)
Node closestNode = closestFinger(key);
fileNodeId = closestNode.addFile(filename, ipaddr);
}
sendint(socket, fileNodeId);
}
void quit(int socket){
int originalId = readint(socket);
int totalMessages = 0;
int stabilizerMessages = 0;
// don't send messages until our stabilization threads have stopped
pthread_cancel(stabilizer_thread);
pthread_cancel(fixFingers_thread);
if( next.id != originalId ){
pair<int, int> messages = next.quit(originalId);
totalMessages += messages.first;
stabilizerMessages += messages.second;
}
shouldQuit = true;
totalMessages += TOTAL_MESSAGES;
stabilizerMessages += STABILIZER_MESSAGES;
sendint(socket, totalMessages);
sendint(socket, stabilizerMessages);
}