-
Notifications
You must be signed in to change notification settings - Fork 19
/
Helper.java
348 lines (301 loc) · 8.64 KB
/
Helper.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
/**
* A helper method that does the following things:
* (1) Hashing - for string, for socket address, and integer number
* (2) Computation - relative id (one node is how far behind another node),
* a address' hex string and its percentage position in the ring (so we can
* easily draw the picture of ring!), address' 8-digit hex string, the ith
* start of a node's finger table, power of two (to avoid computation of power
* of 2 everytime we need it)
* (3) Network and address services - send request to a node to get desired
* socket address/response, create socket address object using string, read
* string from an input stream.
* @author Chuan Xia
*
*/
public class Helper {
private static HashMap<Integer, Long> powerOfTwo = null;
/**
* Constructor
*/
public Helper() {
//initialize power of two table
powerOfTwo = new HashMap<Integer, Long>();
long base = 1;
for (int i = 0; i <= 32; i++) {
powerOfTwo.put(i, base);
base *= 2;
}
}
/**
* Compute a socket address' 32 bit identifier
* @param addr: socket address
* @return 32-bit identifier in long type
*/
public static long hashSocketAddress (InetSocketAddress addr) {
int i = addr.hashCode();
return hashHashCode(i);
}
/**
* Compute a string's 32 bit identifier
* @param s: string
* @return 32-bit identifier in long type
*/
public static long hashString (String s) {
int i = s.hashCode();
return hashHashCode(i);
}
/**
* Compute a 32 bit integer's identifier
* @param i: integer
* @return 32-bit identifier in long type
*/
private static long hashHashCode (int i) {
//32 bit regular hash code -> byte[4]
byte[] hashbytes = new byte[4];
hashbytes[0] = (byte) (i >> 24);
hashbytes[1] = (byte) (i >> 16);
hashbytes[2] = (byte) (i >> 8);
hashbytes[3] = (byte) (i /*>> 0*/);
// try to create SHA1 digest
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// successfully created SHA1 digest
// try to convert byte[4]
// -> SHA1 result byte[]
// -> compressed result byte[4]
// -> compressed result in long type
if (md != null) {
md.reset();
md.update(hashbytes);
byte[] result = md.digest();
byte[] compressed = new byte[4];
for (int j = 0; j < 4; j++) {
byte temp = result[j];
for (int k = 1; k < 5; k++) {
temp = (byte) (temp ^ result[j+k]);
}
compressed[j] = temp;
}
long ret = (compressed[0] & 0xFF) << 24 | (compressed[1] & 0xFF) << 16 | (compressed[2] & 0xFF) << 8 | (compressed[3] & 0xFF);
ret = ret&(long)0xFFFFFFFFl;
return ret;
}
return 0;
}
/**
* Normalization, computer universal id's value relative to local id
* (regard local node as 0)
* @param original: original/universal identifier
* @param n: node's identifier
* @return relative identifier
*/
public static long computeRelativeId (long universal, long local) {
long ret = universal - local;
if (ret < 0) {
ret += powerOfTwo.get(32);
}
return ret;
}
/**
* Compute a socket address' SHA-1 hash in hex
* and its approximate position in string
* @param addr
* @return
*/
public static String hexIdAndPosition (InetSocketAddress addr) {
long hash = hashSocketAddress(addr);
return (longTo8DigitHex(hash)+" ("+hash*100/Helper.getPowerOfTwo(32)+"%)");
}
/**
*
* @param Generate a long type number's 8-digit hex string
* @return
*/
public static String longTo8DigitHex (long l) {
String hex = Long.toHexString(l);
int lack = 8-hex.length();
StringBuilder sb = new StringBuilder();
for (int i = lack; i > 0; i--) {
sb.append("0");
}
sb.append(hex);
return sb.toString();
}
/**
* Return a node's finger[i].start, universal
* @param node: node's identifier
* @param i: finger table index
* @return finger[i].start's identifier
*/
public static long ithStart (long nodeid, int i) {
return (nodeid + powerOfTwo.get(i-1)) % powerOfTwo.get(32);
}
/**
* Get power of 2
* @param k
* @return 2^k
*/
public static long getPowerOfTwo (int k) {
return powerOfTwo.get(k);
}
/**
* Generate requested address by sending request to server
* @param server
* @param req: request
* @return generated socket address,
* might be null if
* (1) invalid input
* (2) response is null (typically cannot send request)
* (3) fail to create address from reponse
*/
public static InetSocketAddress requestAddress (InetSocketAddress server, String req) {
// invalid input, return null
if (server == null || req == null) {
return null;
}
// send request to server
String response = sendRequest(server, req);
// if response is null, return null
if (response == null) {
return null;
}
// or server cannot find anything, return server itself
else if (response.startsWith("NOTHING"))
return server;
// server find something,
// using response to create, might fail then and return null
else {
InetSocketAddress ret = Helper.createSocketAddress(response.split("_")[1]);
return ret;
}
}
/**
* Send request to server and read response
* @param server
* @param request
* @return response, might be null if
* (1) invalid input
* (2) cannot open socket or write request to it
* (3) response read by inputStreamToString() is null
*/
public static String sendRequest(InetSocketAddress server, String req) {
// invalid input
if (server == null || req == null)
return null;
Socket talkSocket = null;
// try to open talkSocket, output request to this socket
// return null if fail to do so
try {
talkSocket = new Socket(server.getAddress(),server.getPort());
PrintStream output = new PrintStream(talkSocket.getOutputStream());
output.println(req);
} catch (IOException e) {
//System.out.println("\nCannot send request to "+server.toString()+"\nRequest is: "+req+"\n");
return null;
}
// sleep for a short time, waiting for response
try {
Thread.sleep(60);
} catch (InterruptedException e) {
e.printStackTrace();
}
// get input stream, try to read something from it
InputStream input = null;
try {
input = talkSocket.getInputStream();
} catch (IOException e) {
System.out.println("Cannot get input stream from "+server.toString()+"\nRequest is: "+req+"\n");
}
String response = Helper.inputStreamToString(input);
// try to close socket
try {
talkSocket.close();
} catch (IOException e) {
throw new RuntimeException(
"Cannot close socket", e);
}
return response;
}
/**
* Create InetSocketAddress using ip address and port number
* @param addr: socket address string, e.g. 127.0.0.1:8080
* @return created InetSocketAddress object;
* return null if:
* (1) not valid input
* (2) cannot find split input into ip and port strings
* (3) fail to parse ip address.
*/
public static InetSocketAddress createSocketAddress (String addr) {
// input null, return null
if (addr == null) {
return null;
}
// split input into ip string and port string
String[] splitted = addr.split(":");
// can split string
if (splitted.length >= 2) {
//get and pre-process ip address string
String ip = splitted[0];
if (ip.startsWith("/")) {
ip = ip.substring(1);
}
//parse ip address, if fail, return null
InetAddress m_ip = null;
try {
m_ip = InetAddress.getByName(ip);
} catch (UnknownHostException e) {
System.out.println("Cannot create ip address: "+ip);
return null;
}
// parse port number
String port = splitted[1];
int m_port = Integer.parseInt(port);
// combine ip addr and port in socket address
return new InetSocketAddress(m_ip, m_port);
}
// cannot split string
else {
return null;
}
}
/**
* Read one line from input stream
* @param in: input steam
* @return line, might be null if:
* (1) invalid input
* (2) cannot read from input stream
*/
public static String inputStreamToString (InputStream in) {
// invalid input
if (in == null) {
return null;
}
// try to read line from input stream
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
System.out.println("Cannot read line from input stream.");
return null;
}
return line;
}
}