-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHandler.java
More file actions
110 lines (99 loc) · 2.72 KB
/
Handler.java
File metadata and controls
110 lines (99 loc) · 2.72 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
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
/**
* Handler thread which processes request from listener and writes
* response to socket.
* Accepted requests: "KEEP" "YOURSUCC" "YOURPRE" "FINDSUCC" "IAMPRE" "FINDCLOS" "FINDRES"
*
*/
public class Handler implements Runnable{
Socket talkSoc;
private Node localNode;
public Handler(Socket handSoc, Node loc)
{
talkSoc = handSoc;
localNode = loc;
}
@Override
public void run()
{
InputStream input = null;
OutputStream output = null;
try {
input = talkSoc.getInputStream();
String request = Helper.inputStreamToString(input);
String response = processRequest(request);
if (response != null) {
output = talkSoc.getOutputStream();
output.write(response.getBytes());
}
input.close();
} catch (IOException e) {
throw new RuntimeException(
"Can't respond.\nServer port: "+ localNode.getAddress().getPort()+"; handler port: "+ talkSoc.getPort(), e);
}
}
private String processRequest(String request)
{
InetSocketAddress result = null;
String ret = null;
if (request == null) {
return null;
}
if (request.startsWith("YOURSUCC")) {
result = localNode.getSuccessor();
if (result != null) {
String ip = result.getAddress().toString();
int port = result.getPort();
ret = "MYSUCC_"+ip+":"+port;
}
else {
ret = "NOTHING";
}
}
else if (request.startsWith("YOURPRE")) {
result = localNode.getPredecessor();
if (result != null) {
String ip = result.getAddress().toString();
int port = result.getPort();
ret = "MYPRE_"+ip+":"+port;
}
else {
ret = "NOTHING";
}
}
else if (request.startsWith("FINDSUCC")) {
long id = Long.parseLong(request.split("_")[1]);
result = localNode.findSuccessor(id);
String ip = result.getAddress().toString();
int port = result.getPort();
ret = "FOUNDSUCC_"+ip+":"+port;
}
else if (request.startsWith("IAMPRE")) {
InetSocketAddress new_pre = Helper.createSocketAddress(request.split("_")[1]);
localNode.notified(new_pre);
ret = "NOTIFIED";
}
else if (request.startsWith("KEEP")) {
ret = "ALIVE";
}
else if (request.startsWith("FINDCLOS")){
long id = Long.parseLong(request.split("_")[1]);
result = localNode.findClosestNode(id);
String ip = result.getAddress().toString();
int port = result.getPort();
ret = "FOUNDCLOS_"+ip+":"+port;
}
else if (request.startsWith("FINDRES")){
long id = Long.parseLong(request.split("_")[1]);
result = localNode.findLeastResponseTime(id);
String ip = result.getAddress().toString();
int port = result.getPort();
ret = "FOUNDRES_"+ip+":"+port;
}
return ret;
}
}