-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChord.java
More file actions
116 lines (88 loc) · 2.91 KB
/
Chord.java
File metadata and controls
116 lines (88 loc) · 2.91 KB
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
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
import java.util.Enumeration;
/**
* Main program to create a chord cycle or join a node into a chord cycle
* (hash ip address+port number into 32 bits name)
* usage:
* new chord : Chord (port)
* join chord : Chord (port) (ip address we want to join) (port number we want to join)
*
*/
public class Chord {
private static InetSocketAddress conn;
private static Node node;
private static Helper helpers;
public static void main (String[] args) {
helpers = new Helper();
// get current ip address
String curIp = null;
// Used to try InetAddress.getLocalHost().getHostAddress() to get ip address,
// but one machine may contain multiple interface and different ip addresses which
// have localhost and ip address that cannot connect to WAN (127.0.0.1, 192.168.56.1 ...)
// So try to use connection to website to make sure the chose ip address can connect to WAN
Socket socket = new Socket();
try{
socket.connect(new InetSocketAddress("google.com", 80));
} catch (IOException e) {
throw new RuntimeException(e);
}
//System.out.println(socket.getLocalAddress());
curIp = socket.getLocalAddress().getHostAddress();
/*
try {
curIp = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
*/
// create node (ip + port num)
node = new Node (Helper.createSocketAddress(curIp+":"+args[0]));
// determine it's creating new chord or join chord
if (args.length == 1) {
conn = node.getAddress();
}
// join, contact is another node
else if (args.length == 3) {
conn = Helper.createSocketAddress(args[1]+":"+args[2]);
if (conn == null) {
System.out.println("\n\nCan't find the input ip address and port number, exiting.\n\n");
System.exit(0);
}
}
else {
System.out.println("\n\nInput format error, exiting.");
System.exit(0);
}
// try to join ring from contact node
boolean successJoin = node.join(conn);
// fail to join contact node
if (!successJoin) {
System.out.println("\n\nFail to connect the node we try to join, exiting.\n\n");
System.exit(0);
}
// print join info
System.out.println("Join the Chord successfully.");
System.out.println("Current IP address: "+curIp);
node.printNeighbors();
// begin to take user input, "info" or "quit"
Scanner userinput = new Scanner(System.in);
while(true) {
System.out.println("\nType \"table\" to get the node's finger table \nType \"quit\"to leave the chord ring: ");
String userInput = "";
userInput = userinput.next();
if (userInput.startsWith("quit")) {
node.stopAllThreads();
System.out.println("Successfully Leaves the chord ring...");
System.exit(0);
}
else if (userInput.startsWith("table")) {
node.printDataStructure();
}
else{
System.out.println("Can't find the command \""+userInput+"\"");
}
}
}
}