-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChordClient.java
78 lines (69 loc) · 2.7 KB
/
ChordClient.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
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import java.io.Console;
/**
* Created by ajay on 4/23/17.
*/
public class ChordClient {
private enum ClientActions { PRINT_TABLE, FIND, LOOKUP };
public static void main(String args[]) {
if (args.length!=1) {
System.err.println("Usage: java ChordClient node-0-url <ip:port>");
System.exit(1);
}
String parts[] = args[0].split(":");
final String host = parts[0].trim();
final String port = parts[0].trim();
treatConsole(host,port);
}
private static void treatConsole(String host, String port) {
Console console = System.console();
while(true) {
System.out.println("client> ");
String line = console.readLine();
String[] parts = line.split(":");
String cmd = parts[0];
switch (cmd) {
case "ftable":
query(host, port, null, ClientActions.PRINT_TABLE);
case "find":
String result = query(host, port, parts[1], ClientActions.FIND);
System.out.println(result);
case "quit":
break;
default:
System.out.println("Dictionary Usage: " +
"ftable -- print finger table\n " +
"find -- find a word, takes second argument word\n");
}
}
}
private static String query(String host, String port, String arg, ClientActions action) {
String result = null;
try {
TTransport serverPipe = new TSocket(host, Integer.parseInt(port));
serverPipe.open();
TProtocol protocol = new TBinaryProtocol(serverPipe);
ChordService.Client node0 = new ChordService.Client(protocol);
result = perform(node0, arg, action);
serverPipe.close();
} catch (TException te) { te.printStackTrace(); }
return (result==null ? "" : result);
}
private static String perform(ChordService.Client node0, String arg, ClientActions action) throws TException {
switch (action) {
case FIND:
NodeRef n = node0.findNode(arg);
return query(n.ip, String.valueOf(n.port), arg, ClientActions.LOOKUP);
case LOOKUP:
return node0.lookup(arg);
case PRINT_TABLE:
node0.printFingerTable();
return null;
}
return null;
}
}