-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodes.java
735 lines (572 loc) · 21.5 KB
/
Nodes.java
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/**
* The node in a distribution hash table
*
* Author: Ketan Joshi (ksj4205)
*/
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.net.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import static java.lang.Thread.sleep;
import static java.util.Objects.hash;
public class Nodes implements Runnable {
static int size = 16;
static FingerTable fingerTable = new FingerTable();
static int ID;
ObjectOutputStream out ;
ObjectInputStream in ;
ObjectOutputStream out2;
ObjectInputStream in2;
static ServerSocket server;
static ServerSocket server2;
static ConcurrentHashMap<Integer,HashSet<String>> Directory = new ConcurrentHashMap<>();
static HashSet<Integer> currentalivenodes = new HashSet<>();
boolean Notfirsttime = false;
int workID;
public Nodes(int workID)
{
this.workID = workID;
Notfirsttime = false;
}
public static void main(String[] args)
{
Nodes node = new Nodes(1);
node.StartProcess();
}
private void StartProcess() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter he GUID:");
ID = sc.nextInt();
System.out.println("Enter the IP address");
String ip = sc.next();
Directory.put(ID,new HashSet<>());// creating a new hshset
try {
Nodes.server = new ServerSocket(8000+ID);
Nodes.server2 = new ServerSocket(9000+ID);
new Thread(new Nodes(2)).start();
new Thread(new Nodes(3)).start();
Socket s = new Socket(ip,6000);
System.out.println("**Connection Established with Server!!\n");
out = new ObjectOutputStream(s.getOutputStream());
in = new ObjectInputStream(s.getInputStream());
out.writeUTF(""+ID);
out.flush();
Menu();
} catch (IOException e) {
e.printStackTrace();
}
}
public void Menu()
{
Scanner sc = new Scanner(System.in);
while (true){
System.out.println("1.Print the Table (p) \n" +
"2.Add a file (a)\n" +
"3.Delete the node (d)\n" +
"4.Retrieve a file (r)\n"+
"5.View the Directory (v)");
String input = sc.next();
switch (input)
{
case "1":
case "p":
case "P":
synchronized (fingerTable) {
System.out.println(fingerTable);
}
break;
case "2":
case "a":
case "A":
AddNewFile();
break;
case "3":
case "d":
case "D":
DeleteTheNode();
break;
case "4":
case "r":
case "R":
RetrieveFile();
break;
case "5":
case "v":
case "V":
System.out.println("The current files in the directory are\n"+Directory);
}
}
}
private void RetrieveFile() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the file");
String filename = sc.next();
int destination = Math.abs(filename.hashCode() % size);
System.out.println("Destination hash value: '"+destination+"'");
/// Code to send find the target node.
if (destination == ID)
{
System.out.println("The key is the Current node, File entered!");
// System.out.println(Directory.get(ID).add(filename));
}
if (Directory.containsKey(destination))
{
System.out.println("Current node contains files of the Node"+destination+"\n "+
filename+" \n");
// System.out.println(Directory.get(destination).add(filename));
}
// algorithm for sending the file
RetrieveFileHelper(destination,filename);
}
private void RetrieveFileHelper(int destination,String filename)
{
int ogdestination =destination;
try {
FingerTable dummy = fingerTable;
int successor = ID;
int inode= ID;
InetAddress ip = InetAddress.getByName("127.0.0.1");
int diff = 9999;
while (true) {
// System.out.println("inside while");
for (int i = 0; i < 4; i++) {
int tdiff =0;
// System.out.println("checkin for the node"+dummy.Table[i][0]);
if (dummy.Table[i][0] == destination) {
successor = dummy.Table[i][1];
System.out.println("Retrieving the file node '"+destination+"' at node '"+successor+
"'");
ip = dummy.ips[i];
RetrieveActualFile(filename,successor,ip,ogdestination);
return;
}
if(ID > destination)
{
if(dummy.Table[i][0]<16 && dummy.Table[i][0] > ID)
tdiff = destination - dummy.Table[i][0] + 16;
else
tdiff = destination - dummy.Table[i][0];
}
else
tdiff = destination - dummy.Table[i][0];
if(tdiff>0 && diff > tdiff)
{
// System.out.println("*"+dummy.Table[i][0]);
diff = tdiff;
successor = dummy.Table[i][1];
ip = dummy.ips[i];
inode = dummy.Table[i][0];
}
}
//System.out.println("asking node "+successor+" for fingertable");
if (Cycle(ID,inode,successor,destination))
destination = successor;
if (successor == destination)
{
// System.out.println("Retrieving the file node"+destination+"at node "+successor);
RetrieveActualFile(filename,successor,ip,ogdestination);
// s.close();
return;
}
//finding the file at some other node
Socket s2 = new Socket(ip, 9000 + successor);
ObjectOutputStream objout2 = new ObjectOutputStream(s2.getOutputStream());
ObjectInputStream objin2 = new ObjectInputStream(s2.getInputStream());
objout2.writeUTF("rt"); // rt for reading table
objout2.flush();
dummy = (FingerTable)objin2.readObject();
// System.out.println("recived the dummy table");
sleep(5000);
objout2.close();
objin2.close();
s2.close();
diff = 999;
}
}
catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void RetrieveActualFile(String filename, int successor, InetAddress ip, int destination) {
try {
Socket s = new Socket(ip, 9000 + successor);
ObjectOutputStream objout = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream objin = new ObjectInputStream(s.getInputStream());
objout.writeUTF("gf"); // getting the file
objout.flush();
objout.writeUTF(""+destination);
objout.flush();
objout.writeUTF(filename);
objout.flush();
String file = objin.readUTF();
if(file.equalsIgnoreCase("s"))
System.out.println(filename+"File receieved successfully!");
else
System.out.println(filename+" Not Found");
sleep(6000);
objout.close();
s.close();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void DeleteTheNode() {
try {
out.writeUTF("d");
out.flush();
// sending all the data to the node
int successor = 0;
InetAddress ip = InetAddress.getByName("127.0.0.1");
synchronized (fingerTable) {
successor = fingerTable.Table[0][1];
ip = fingerTable.ips[0];
}
Socket socket =new Socket(ip ,9000+successor);
ObjectOutputStream objout = new ObjectOutputStream(socket.getOutputStream());
objout.writeUTF("dl");
objout.flush();
objout.writeUTF(""+ID);
objout.flush();
int count = Directory.keySet().size();
objout.writeUTF(""+count);
objout.flush();
for(int nid : Directory.keySet())
{
objout.writeUTF(""+nid);
objout.flush();
objout.writeObject(Directory.get(nid));
objout.flush();
Directory.remove(nid);
}
System.out.println("Going Offline!!!!");
System.out.println("Sending all Data to Node '"+successor+"'");
Directory.remove(ID);
sleep(9000);
objout.close();
socket.close();
System.exit(9);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void AddNewFile() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the file");
String filename = sc.next();
int destination = Math.abs(filename.hashCode() % size);
System.out.println("The hash key of file is: "+destination);
if (destination == ID)
{
System.out.println("The Destiation ID is the GUID of the current node"+
"\n Saving the file in the Directory");
Directory.get(ID).add(filename);
}
if (Directory.containsKey(destination))
{
System.out.println("The current node contains files of Destiation ID"+
"\n Saving the file in the Directory");
Directory.get(destination).add(filename);
}
// algorithm for sending the file
else
SendtheFileToCorrectNode(destination,filename);
}
private void SendtheFileToCorrectNode(int destination, String filename) {
int ogdestination = destination;
try {
FingerTable dummy = fingerTable;
int successor = ID;
int inode= ID;
InetAddress ip = InetAddress.getByName("127.0.0.1");
int diff = 9999;
while (true) {
//System.out.println("inside while");
for (int i = 0; i < 4; i++) {
int tdiff =0;
// System.out.println("checkin for the node"+dummy.Table[i][0]);
if (dummy.Table[i][0] == destination) {
successor = dummy.Table[i][1];
System.out.println("sending the file to node '"+destination+"' at node '"+successor+"'");
ip = dummy.ips[i];
Socket s = new Socket(ip, 9000 + successor);
ObjectOutputStream objout = new ObjectOutputStream(s.getOutputStream());
objout.writeUTF("sf"); // s for sending file
objout.flush();
objout.writeUTF(""+ogdestination);
objout.flush();
objout.writeUTF(filename);
objout.flush();
sleep(5000);
s.close();
return;
}
if(ID > destination)
{
if(dummy.Table[i][0]<16 && dummy.Table[i][0] > ID)
tdiff = destination - dummy.Table[i][0] + 16;
else
tdiff = destination - dummy.Table[i][0];
}
else
tdiff = destination - dummy.Table[i][0];
if(tdiff>0 && diff > tdiff)
{
//System.out.println("*"+dummy.Table[i][0]);
diff = tdiff;
successor = dummy.Table[i][1];
ip = dummy.ips[i];
inode = dummy.Table[i][0];
}
}
//System.out.println("asking node "+successor+" for fingertable");
// if(inode<destination && successor > destination ||
// (inode > destination && successor >destination && successor < ID))
if (Cycle(ID,inode,successor,destination))
destination = successor;
if (successor == destination)
{
System.out.println("sending the file to node '"+destination+"' at node '"+successor+"'");
Socket s = new Socket(ip, 9000 + successor);
ObjectOutputStream objout = new ObjectOutputStream(s.getOutputStream());
objout.writeUTF("sf"); // s for sending file
objout.flush();
objout.writeUTF(""+ogdestination);
objout.flush();
objout.writeUTF(filename);
objout.flush();
sleep(5000);
objout.close();
// s.close();
return;
}
Socket s2 = new Socket(ip, 9000 + successor);
ObjectOutputStream objout2 = new ObjectOutputStream(s2.getOutputStream());
ObjectInputStream objin2 = new ObjectInputStream(s2.getInputStream());
objout2.writeUTF("rt"); // rt for reading table
objout2.flush();
dummy = (FingerTable)objin2.readObject();
// System.out.println("recived the dummy table");
sleep(5000);
objout2.close();
objin2.close();
s2.close();
diff = 999;
}
}
catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private boolean Cycle(int id, int inode, int successor, int destination) {
if (successor> inode && successor > destination)
if (destination > inode ) {
//System.out.println("*****C1");
return true;
}
if(inode > destination && inode> successor && inode > id)
{
if (inode < (destination + 16) && (destination +16) < (successor+16))
{
//System.out.println("*****C2");
return true;
}
}
if (destination > inode && destination > successor)
{
if ((successor+16)> destination && (successor+16)>inode && (inode+16)>(successor+16)) {
// System.out.println("*****C3");
return true;
}
}
return false;
}
private void FindtheProcess() {
try {
Socket regist_server = server.accept();
out2 = new ObjectOutputStream(regist_server.getOutputStream());
in2 = new ObjectInputStream(regist_server.getInputStream());
while (true) {
String input = in2.readUTF();
switch (input) {
case "u":
CalculateFingerTable();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void CalculateFingerTable()
{
try {
int[] alivenodes = (int[]) in2.readObject();
InetAddress[] ips = (InetAddress[])in2.readObject();
for(int i = 0;i<4;i++)
{
int entry= (ID + (int)(Math.pow(2,i)))%size;
fingerTable.Table[i][0]= entry;
int successor=99999;
int min = 999999;
InetAddress minIp=InetAddress.getByName("127.0.0.1");
InetAddress ip = InetAddress.getByName("127.0.0.1");;
for(int j =0;j<alivenodes.length;j++)
{
int e1 = alivenodes[j]; // entry in the table
if(e1>entry && e1<successor) {
successor = e1;
ip = ips[j];
}
if(entry == e1) {
successor = e1;
ip = ips[j];
break;
}
if(min>e1)
{
min = e1;
minIp = ips[j];
}
}
if(successor == 99999) {
successor = min;
ip = minIp;
}
fingerTable.Table[i][1]= successor;
fingerTable.ips[i] = ip;
}
if(Notfirsttime && Directory.size()>1)
LoadBalancer(alivenodes,ips);
Notfirsttime = true;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private void LoadBalancer(int[] alivenodes, InetAddress[] ips) {
ArrayList tp= new ArrayList<>(Arrays.asList(alivenodes));
HashSet<Integer> temp = new HashSet<>();
HashSet <Integer>t2 = new HashSet<>();
for(int j: alivenodes)
{
temp.add(j);
t2.add(j);
}
temp.removeAll(currentalivenodes);
// System.out.println("the subtracted set:"+temp);
ArrayList<Integer> sentNode = new ArrayList<>();
for(int values : Directory.keySet())
{
int dist = Math.min(Math.abs(values-ID),Math.abs(ID-values+16));
int min = ID;
for(int keys : temp)
{
if(keys!=ID && values !=ID) {
if (Cycle2(min, values, keys)) {
min = keys;
}
}
}
if(min!=ID)
{
sentNode.add(values);
for(int i =0;i<alivenodes.length;i++)
{
if(min == alivenodes[i])
{
try {
Socket s = new Socket(ips[i],9000+min);
ObjectOutputStream objout = new ObjectOutputStream(s.getOutputStream());
objout.writeUTF("lb"); // load balancing
objout.flush();
objout.writeUTF(""+values);
objout.flush();
objout.writeObject(Directory.get(values));
objout.flush();
sleep(2000);
objout.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
for(int val : sentNode)
{
Directory.remove(val);
}
//code to send the data to that id
}
}
currentalivenodes = t2;
// System.out.println(Directory);
}
private boolean Cycle2(int id, int values, int keys) {
//id is the id of the node which has the values
//keys is the new node emerged
//value is the value of the node ahead
//find the cycle in clockwise values- keys - id
if(keys == values)
return true;
if(values<keys && keys < id)
{
//123
//System.out.println("cond1");
return true;
}
if(values < keys && values > id)
{
//14 15 2
//System.out.println("cond2");
if(id< keys && keys < id + 16 )
return true;
}
if(values>keys && values >id) {
//14 2 3
// System.out.println("cond3");
if (values < keys + 16 && keys + 16 < id + 16)
return true;
}
return false;
}
@Override
public void run() {
if (this.workID == 2)
FindtheProcess();
if(this.workID == 3)
ListeningProcess();
}
private void ListeningProcess() {
// this process continuosly keep on listening
//System.out.print("inside listening process");
while (true)
{
try {
Socket temp = server2.accept();
//System.out.println("connection receieved for server 2");
new Thread(new Secondary_Process(temp)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}