-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNERegistryThread.java~
120 lines (107 loc) · 3.15 KB
/
NERegistryThread.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
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.NullPointerException;
import java.net.ServerSocket;
import java.net.Socket;
import java.lang.Thread;
/*
* 0 = list
* 1 = lookup
* 2 = isRegistry
*/
/* The thread first writes an int signalling success or error (and what type of error)
* If there is a success, it will write the return as a second object
* Else it will write the exception as the second object
* 1 is success
* -1 is NERemoteException
* -2 is NEAccessException
* -3 is NENotBoundException
* //addendum: I suppose specific error codes is not necessary but
* //it might be useful later, leaving it in for now though its not being used
*/
public class NERegistryThread extends Thread {
private NERegistry registry;
private ObjectInputStream inputStream;
private ObjectOutputStream outputStream;
private Socket socket;
public NERegistryThread (NERegistry registry, Socket socket) {
this.registry = registry;
this.socket = socket;
}
private void closeThread() {
try {
inputStream.close();
outputStream.close();
socket.close();
} catch (Exception ex) {
System.out.println("error ending registry listener thread: " + ex.toString());
}
}
public void run() {
try {
this.inputStream = new ObjectInputStream(socket.getInputStream());
this.outputStream = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException ex) {
System.out.println("error creating input/output streams for registry listener thread: " + ex.toString());
return;
}
int methodKey = -1;
String name = "";
try {
methodKey = inputStream.readInt();
if (methodKey == 1) {
name = (String) inputStream.readObject();
}
} catch (Exception ex) {
System.out.println("error reading method request: " + ex.toString());
try {
outputStream.writeInt(-1);
outputStream.writeObject(ex);
outputStream.flush();
} catch (Exception ex2) {
System.out.println("error outputting response: " + ex2.toString());
}
closeThread();
return;
}
try {
if (methodKey != 1 || (methodKey != 0)) {
System.out.println("invalid method request, code: " + methodKey);
outputStream.writeInt(-2);
outputStream.writeObject(new NEAccessException());
closeThread();
return;
} else {
if (methodKey == 1) {
try {
NERemoteObjectSub ror = registry.lookup(name);
ror.getRemoteObjectReference ();
outputStream.writeInt(1);
outputStream.writeObject(ror);
} catch (NENotBoundException ex) {
outputStream.writeInt(-3);
outputStream.writeObject(ex);
} catch (Exception ex) {
outputStream.writeInt(-1);
outputStream.writeObject(ex);
}
} else if (methodKey == 2) {
try {
outputStream.writeObject(registry.list());
outputStream.writeInt(1);
} catch (Exception ex) {
outputStream.writeInt(-1);
outputStream.writeObject(ex);
}
} else if (methodKey == 3) {
outputStream.writeBoolean(true);
}
outputStream.flush();
}
} catch (Exception ex) {
System.out.println("error sending response: " + ex.toString());
}
closeThread();
}
}