-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNERegistry.java
211 lines (187 loc) · 5.88 KB
/
NERegistry.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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
public class NERegistry {
public static final int REGISTRY_PORT = 1099; //well known port for registry
public static final int VALIDATE_TIMEOUT = 3000;
private HashMap<String, NERemoteObjectReference> reg;
private ServerSocket serverSocket;
private Socket socket;
private ObjectOutputStream outputStream;
private ObjectInputStream inputStream;
private String host;
private int port;
//localhost
public NERegistry() throws IOException {
reg = new HashMap<String, NERemoteObjectReference>();
serverSocket = new ServerSocket (REGISTRY_PORT);
host = "localhost";
NERegistryListenerThread listenThread = new NERegistryListenerThread(this, serverSocket);
listenThread.start();
}
public NERegistry (int port) throws IOException {
reg = new HashMap<String, NERemoteObjectReference>();
serverSocket = new ServerSocket (port);
host = "localhost";
this.port = port;
NERegistryListenerThread listenThread = new NERegistryListenerThread(this, serverSocket);
listenThread.start();
}
public NERegistry(String host) throws NERegistryNotFoundException, NERemoteException {
this.host = host;
this.port = REGISTRY_PORT;
validate();
}
public NERegistry(String host, int port) throws NERegistryNotFoundException, NERemoteException {
this.host = host;
this.port = port;
validate();
}
private void validate() throws NERemoteException, NERegistryNotFoundException {
boolean readResponse = false;
boolean res = false;
DataInputStream dis;
try {
this.socket = new Socket(host, REGISTRY_PORT);
this.outputStream = new ObjectOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
} catch (Exception ex) {
throw new NERemoteException(ex.toString());
}
try {
outputStream.writeInt(3);
outputStream.flush();
long startTime = System.currentTimeMillis();
while (!readResponse && ((System.currentTimeMillis()) - startTime < VALIDATE_TIMEOUT)) {
if (dis.available() > 0) {
res = dis.readBoolean();
readResponse = true;
}
}
if (!res) {
throw new NERegistryNotFoundException(host, port);
}
//if it passes this then we've validated that the server exists.
} catch (Exception ex) {
throw new NERemoteException(ex.toString());
}
}
public String[] list() throws NERemoteException, NEAccessException {
if (host.equals("localhost")) {
return localList();
}
return remoteList();
}
private String[] localList() {
Set<String> set = (HashSet<String>) reg.keySet();
return (set.toArray(new String[0]));
}
private String[] remoteList() throws NERemoteException, NEAccessException {
openRequest();
try {
outputStream.writeInt(0);
outputStream.flush();
int code = inputStream.readInt();
Object obj = inputStream.readObject();
if (code < 0) {
Exception ex = (Exception) obj;
System.out.println("error in remotelookup response: " + ex.toString());
throw ex;
} else {
return (String[]) obj;
}
} catch(Exception ex) {
System.out.println("datastream error in remotelookup: " + ex.toString());
throw new NERemoteException();
} finally {
closeRequest();
}
}
public NERemoteObjectStub lookup(String name) throws NERemoteException, NENotBoundException, NEAccessException {
if (host.equals("localhost")) {
return localLookup(name).localise ();
}
return remoteLookup(name).localise ();
}
private NERemoteObjectReference remoteLookup(String name) throws NERemoteException, NENotBoundException, NEAccessException {
openRequest();
try {
outputStream.writeInt(1);
outputStream.writeObject(name);
outputStream.flush();
int code = inputStream.readInt();
Object obj = inputStream.readObject();
if (code < 0) {
Exception ex = (Exception) obj;
System.out.println("error in remotelookup response: " + ex.toString());
throw ex;
} else {
return (NERemoteObjectReference) obj;
}
} catch(Exception ex) {
System.out.println("datastream error in remotelookup: " + ex.toString());
throw new NERemoteException();
} finally {
closeRequest();
}
}
private NERemoteObjectReference localLookup(String name) throws NENotBoundException {
NERemoteObjectReference ror;
if ((ror = reg.get(name)) == null) {
throw new NENotBoundException();
}
return ror;
}
public void bind(String name, NERemoteObjectReference obj) throws NERemoteException, NEAlreadyBoundException, NEAccessException {
if (!host.equals("localhost")) {
throw new NEAccessException();
}
if (reg.containsKey(name)) {
throw new NEAlreadyBoundException();
}
reg.put(name, obj);
}
public void rebind(String name, NERemoteObjectReference obj) throws NERemoteException, NEAccessException {
if (!host.equals("localhost")) {
throw new NEAccessException();
}
reg.put(name, obj);
}
public void unbind (String name) throws NERemoteException, NENotBoundException, NEAccessException {
if (!host.equals("localhost")) {
throw new NEAccessException();
}
if (reg.remove(name) == null) {
throw new NENotBoundException();
}
}
private void openRequest() throws NERemoteException {
try{
this.socket = new Socket(host, REGISTRY_PORT);
this.outputStream = new ObjectOutputStream(socket.getOutputStream());
this.inputStream = new ObjectInputStream(socket.getInputStream());
} catch (Exception ex) {
throw new NERemoteException(ex.toString());
}
}
private void closeRequest() {
try {
outputStream.close();
inputStream.close();
} catch (IOException ex) {
System.out.println("error closing streams: " + ex.toString());
}
try {
socket.close();
} catch (IOException ex) {
System.out.println("error closing socket: " + ex.toString());
}
}
}