-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNERegistryLocator.java~
37 lines (31 loc) · 1.22 KB
/
NERegistryLocator.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
import java.net.*;
import java.io.*;
public class NERegistryLocator {
// this is the SOLE static method.
// you use it as: LocateSimpleRegistry.getRegistry(123.123.123.123, 2048)
// and it returns null if there is none, else it returns the registry.
// actually the registry is just a pair of host IP and port.
// inefficient? well you can change it as you like.
// for the rest, you can see SimpleRegistry.java.
public static NERegistry getRegistry (String address, int port)
throws NERegistryNotFoundException {
try {
Socket socket = new Socket (address, port);
ObjectInputStream in = new ObjectInputStream (socket.getInputStream ());
ObjectOutputStream out = new ObjectOutputStream (socket.getOutputStream ());
out.writeChar ('a');
if ((in.readChar ()).equals ('c')) {
return new NERegistry (address, port);
}
else throw new NERegistryNotFoundException (address, port);
}
catch (SocketException e) {
System.out.println (e);
throw new NERegistryNotFoundException (address, port);
}
catch (IOException e) {
System.out.println (e);
throw new NERegistryNotFoundException (address, port);
}
}
}