-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNERemoteObjectServer.java
184 lines (164 loc) · 4.98 KB
/
NERemoteObjectServer.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
/*
* Class : NERemoteObjectServer.java
* Authors : Eugene Choi, Norbert Chu
* Andrew IDs : dechoi, nrchu
* Description: This object server manages the objects stored in the server
* side of RMI.
*/
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.util.concurrent.*;
import java.util.*;
public class NERemoteObjectServer {
// The table of remote objects stored on the server, indexed by key
private NERemoteObjectTable remoteObjectTable;
// A table mapping each object (key) to its NERemoteObjectServerThread
private HashMap<Integer, NERemoteObjectServerThread> threads;
private int port;
private ServerSocket listener;
public NERemoteObjectServer (int port)
throws SocketException, IOException {
remoteObjectTable = new NERemoteObjectTable ();
threads = new HashMap<Integer, NERemoteObjectServerThread> ();
listener = new ServerSocket (port);
this.port = port;
new Thread (new Runnable () {
public void run () { listenLoop (); }
}).start ();
}
/*
* Add a remote object to the remote object table
*/
public synchronized int add (NERemote o) {
int key = remoteObjectTable.add (o);
NERemoteObjectServerThread thread = new NERemoteObjectServerThread (o);
thread.start ();
threads.put (key, thread);
return key;
}
/*
* Remove a remote object from the remote object table
*/
public synchronized void remove (int key) {
remoteObjectTable.remove (key);
threads.get (key).killThread ();
threads.remove (key);
}
/*
* Remove a remote object from the remote object table
*/
public synchronized void replace (int key, NERemote o) {
threads.get (key).killThread ();
threads.remove (key);
NERemoteObjectServerThread thread = new NERemoteObjectServerThread (o);
thread.start ();
threads.put (key, thread);
remoteObjectTable.replace (key, o, thread);
}
/*
* Find a remote object from the remote object table
*/
public synchronized NERemote getObject (int key) {
return remoteObjectTable.get (key);
}
/*
* The loop that listens for incoming method invocation requests
*/
public void listenLoop () {
while (true) {
Socket client = null;
ObjectInputStream in = null;
try {
// Listen for and accept client connection
client = listener.accept ();
System.out.println ("Client connect: " + client);
// Get input stream
in = new ObjectInputStream (client.getInputStream ());
System.out.println ("Got input stream");
}
catch (SocketException e) {
e.printStackTrace ();
closeConnection (client, in, null);
continue;
}
catch (IOException e) {
e.printStackTrace ();
closeConnection (client, in, null);
continue;
}
NERemoteObjectMethodRequest request;
NEMethodCall method = null;
NEMethodInvocation mi = null;
int key = -1;
try {
// Read the method invocation from the client socket
mi = (NEMethodInvocation) in.readObject ();
System.out.println (mi);
System.out.println ("Table: " + remoteObjectTable);
System.out.println (mi.getObjectKey ());
System.out.println (getObject (mi.getObjectKey ()));
key = mi.getObjectKey ();
}
catch (IOException e) {
e.printStackTrace ();
closeConnection (client, in, null);
continue;
}
catch (ClassNotFoundException e) {
// Should not reach here
continue;
}
try {
// Get the method from the method invocation
synchronized (remoteObjectTable) {
method = NEDemarshaller.demarshalMethodInvocation (mi, getObject (key));
System.out.println ("method: " + method);
request = new NERemoteObjectMethodRequest (method, client);
}
}
catch (NoSuchMethodException e) {
e.printStackTrace ();
request = new NERemoteObjectMethodRequest (method, client, new NEException (e));
}
catch (SecurityException e) {
e.printStackTrace ();
request = new NERemoteObjectMethodRequest (method, client, new NEException (e));
}
// Add the method request job the the target object's request queue
threads.get (key).addMethodRequest (request);
}
}
/*
* Close all connections to a client
*/
private void closeConnection (Socket client, InputStream in, OutputStream out) {
try {
if (in != null) in.close ();
}
catch (SocketException e) {
// ignore
}
catch (IOException e) {
// ignore
}
try {
if (out != null) out.close ();
}
catch (SocketException e) {
// ignore
}
catch (IOException e) {
// ignore
}
try {
if (client != null) client.close ();
}
catch (SocketException e) {
// ignore
}
catch (IOException e) {
// ignore
}
}
}