55 |
import java.util.Vector; |
import java.util.Vector; |
56 |
|
|
57 |
public class UnicastRemoteCall |
public class UnicastRemoteCall |
58 |
implements RemoteCall, ProtocolConstants { |
implements RemoteCall, ProtocolConstants |
59 |
|
{ |
60 |
|
|
61 |
private UnicastConnection conn; |
private UnicastConnection conn; |
62 |
private Object result; |
private Object result; |
66 |
private Vector vec; |
private Vector vec; |
67 |
private int ptr; |
private int ptr; |
68 |
|
|
69 |
private ObjectOutput oout; |
private ObjectOutput oout; |
70 |
private ObjectInput oin; |
private ObjectInput oin; |
71 |
|
|
72 |
/** |
/** |
73 |
* Incoming call. |
* Incoming call. |
74 |
*/ |
*/ |
75 |
UnicastRemoteCall(UnicastConnection conn) { |
UnicastRemoteCall(UnicastConnection conn) |
76 |
|
{ |
77 |
this.conn = conn; |
this.conn = conn; |
78 |
} |
} |
79 |
|
|
80 |
/** |
/** |
81 |
* Outgoing call. |
* Outgoing call. |
82 |
*/ |
*/ |
83 |
/* |
UnicastRemoteCall(UnicastConnection conn, ObjID objid, int opnum, long hash) |
84 |
UnicastRemoteCall(Object obj, int opnum, long hash) { |
throws RemoteException |
85 |
this.object = obj; |
{ |
86 |
|
this.conn = conn; |
87 |
this.opnum = opnum; |
this.opnum = opnum; |
88 |
this.hash = hash; |
this.hash = hash; |
89 |
|
|
90 |
|
// signal the call when constructing |
91 |
|
try |
92 |
|
{ |
93 |
|
DataOutputStream dout = conn.getDataOutputStream(); |
94 |
|
dout.write(MESSAGE_CALL); |
95 |
|
|
96 |
|
oout = conn.getObjectOutputStream(); |
97 |
|
objid.write(oout); |
98 |
|
oout.writeInt(opnum); |
99 |
|
oout.writeLong(hash); |
100 |
|
} |
101 |
|
catch(IOException ex) |
102 |
|
{ |
103 |
|
throw new MarshalException("Try to write header but failed.", ex); |
104 |
|
} |
105 |
} |
} |
106 |
*/ |
|
107 |
|
UnicastConnection getConnection() |
|
UnicastRemoteCall(UnicastConnection conn, ObjID objid, int opnum, long hash) throws RemoteException |
|
108 |
{ |
{ |
|
this.conn = conn; |
|
|
this.opnum = opnum; |
|
|
this.hash = hash; |
|
|
|
|
|
// signal the call when constructing |
|
|
try{ |
|
|
DataOutputStream dout = conn.getDataOutputStream(); |
|
|
dout.write(MESSAGE_CALL); |
|
|
|
|
|
oout = conn.getObjectOutputStream(); |
|
|
objid.write(oout); |
|
|
oout.writeInt(opnum); |
|
|
oout.writeLong(hash); |
|
|
}catch(IOException ex){ |
|
|
throw new MarshalException("Try to write header but failed.", ex); |
|
|
} |
|
|
} |
|
|
|
|
|
UnicastConnection getConnection(){ |
|
109 |
return conn; |
return conn; |
110 |
} |
} |
111 |
|
|
112 |
public ObjectOutput getOutputStream() throws IOException { |
public ObjectOutput getOutputStream() throws IOException |
113 |
if (conn != null) { |
{ |
114 |
if(oout == null) |
if (conn != null) |
115 |
return (oout = conn.getObjectOutputStream()); |
{ |
116 |
else |
if(oout == null) |
117 |
return oout; |
return (oout = conn.getObjectOutputStream()); |
118 |
}else{ |
else |
119 |
vec = new Vector(); |
return oout; |
120 |
return (new DummyObjectOutputStream()); |
} |
121 |
} |
else |
122 |
|
{ |
123 |
|
vec = new Vector(); |
124 |
|
return (new DummyObjectOutputStream()); |
125 |
|
} |
126 |
} |
} |
127 |
|
|
128 |
public void releaseOutputStream() throws IOException { |
public void releaseOutputStream() throws IOException |
129 |
if(oout != null) |
{ |
130 |
oout.flush(); |
if(oout != null) |
131 |
|
oout.flush(); |
132 |
} |
} |
133 |
|
|
134 |
public ObjectInput getInputStream() throws IOException { |
public ObjectInput getInputStream() throws IOException |
135 |
if (conn != null) { |
{ |
136 |
if(oin == null) |
if (conn != null) |
137 |
return (oin = conn.getObjectInputStream()); |
{ |
138 |
else |
if(oin == null) |
139 |
return oin; |
return (oin = conn.getObjectInputStream()); |
140 |
} |
else |
141 |
else { |
return oin; |
142 |
ptr = 0; |
} |
143 |
return (new DummyObjectInputStream()); |
else |
144 |
} |
{ |
145 |
|
ptr = 0; |
146 |
|
return (new DummyObjectInputStream()); |
147 |
|
} |
148 |
} |
} |
149 |
|
|
150 |
public void releaseInputStream() throws IOException { |
public void releaseInputStream() throws IOException |
151 |
|
{ |
152 |
// Does nothing. |
// Does nothing. |
153 |
} |
} |
154 |
|
|
155 |
public ObjectOutput getResultStream(boolean success) throws IOException, StreamCorruptedException { |
public ObjectOutput getResultStream(boolean success) |
156 |
|
throws IOException, StreamCorruptedException |
157 |
|
{ |
158 |
vec = new Vector(); |
vec = new Vector(); |
159 |
return (new DummyObjectOutputStream()); |
return new DummyObjectOutputStream(); |
160 |
} |
} |
161 |
|
|
162 |
|
public void executeCall() throws Exception |
163 |
|
{ |
164 |
|
byte returncode; |
165 |
|
ObjectInput oin; |
166 |
|
try |
167 |
|
{ |
168 |
|
releaseOutputStream(); |
169 |
|
DataInputStream din = conn.getDataInputStream(); |
170 |
|
if (din.readByte() != MESSAGE_CALL_ACK) |
171 |
|
throw new RemoteException("Call not acked"); |
172 |
|
|
|
public void executeCall() throws Exception { |
|
|
byte returncode; |
|
|
ObjectInput oin; |
|
|
try{ |
|
|
releaseOutputStream(); |
|
|
DataInputStream din = conn.getDataInputStream(); |
|
|
if (din.readByte() != MESSAGE_CALL_ACK) { |
|
|
throw new RemoteException("Call not acked"); |
|
|
} |
|
173 |
oin = getInputStream(); |
oin = getInputStream(); |
174 |
returncode = oin.readByte(); |
returncode = oin.readByte(); |
175 |
UID.read(oin); |
UID.read(oin); |
176 |
}catch(IOException ex){ |
} |
177 |
|
catch(IOException ex) |
178 |
|
{ |
179 |
throw new UnmarshalException("Try to read header but failed:", ex); |
throw new UnmarshalException("Try to read header but failed:", ex); |
180 |
} |
} |
181 |
|
|
182 |
//check return code |
//check return code |
183 |
switch(returncode){ |
switch(returncode) |
184 |
case RETURN_ACK: //it's ok |
{ |
185 |
return; |
case RETURN_ACK: //it's ok |
186 |
case RETURN_NACK:{ |
return; |
187 |
Object returnobj; |
case RETURN_NACK: |
188 |
try{ |
Object returnobj; |
189 |
returnobj = oin.readObject(); |
try |
190 |
} |
{ |
191 |
catch(Exception ex2){ |
returnobj = oin.readObject(); |
192 |
throw new UnmarshalException("Try to read exception object but failed", ex2); |
} |
193 |
} |
catch(Exception ex2) |
194 |
if(!(returnobj instanceof Exception)) |
{ |
195 |
throw new UnmarshalException("Should be Exception type here"); |
throw new UnmarshalException |
196 |
throw (Exception)returnobj; |
("Try to read exception object but failed", ex2); |
197 |
} |
} |
198 |
default: |
|
199 |
throw new UnmarshalException("Invalid return code"); |
if(!(returnobj instanceof Exception)) |
200 |
} |
throw new UnmarshalException("Should be Exception type here: " |
201 |
|
+ returnobj); |
202 |
|
throw (Exception)returnobj; |
203 |
|
|
204 |
|
default: |
205 |
|
throw new UnmarshalException("Invalid return code"); |
206 |
|
} |
207 |
} |
} |
208 |
|
|
209 |
public void done() throws IOException { |
public void done() throws IOException |
210 |
|
{ |
211 |
// conn.disconnect(); |
// conn.disconnect(); |
212 |
} |
} |
213 |
|
|
214 |
Object returnValue() { |
Object returnValue() |
215 |
return (vec.elementAt(0)); |
{ |
216 |
|
return vec.elementAt(0); |
217 |
} |
} |
218 |
|
|
219 |
Object[] getArguments() { |
Object[] getArguments() |
220 |
return (vec.toArray()); |
{ |
221 |
|
return vec.toArray(); |
222 |
} |
} |
223 |
|
|
224 |
Object getObject() { |
Object getObject() |
225 |
return (object); |
{ |
226 |
|
return object; |
227 |
} |
} |
228 |
|
|
229 |
int getOpnum() { |
int getOpnum() |
230 |
return (opnum); |
{ |
231 |
|
return opnum; |
232 |
} |
} |
233 |
|
|
234 |
long getHash() { |
long getHash() |
235 |
return (hash); |
{ |
236 |
} |
return hash; |
237 |
|
} |
238 |
|
|
239 |
void setReturnValue(Object obj) { |
void setReturnValue(Object obj) |
240 |
|
{ |
241 |
vec.removeAllElements(); |
vec.removeAllElements(); |
242 |
vec.addElement(obj); |
vec.addElement(obj); |
243 |
} |
} |
245 |
/** |
/** |
246 |
* Dummy object output class. |
* Dummy object output class. |
247 |
*/ |
*/ |
248 |
private class DummyObjectOutputStream implements ObjectOutput { |
private class DummyObjectOutputStream implements ObjectOutput |
249 |
|
{ |
250 |
|
/** |
251 |
|
* Non-private constructor to reduce bytecode emitted. |
252 |
|
*/ |
253 |
|
DummyObjectOutputStream() |
254 |
|
{ |
255 |
|
} |
256 |
|
|
257 |
public void writeBoolean(boolean v) throws IOException { |
public void writeBoolean(boolean v) throws IOException |
258 |
|
{ |
259 |
vec.addElement(new Boolean(v)); |
vec.addElement(new Boolean(v)); |
260 |
} |
} |
261 |
|
|
262 |
public void writeByte(int v) throws IOException { |
public void writeByte(int v) throws IOException |
263 |
|
{ |
264 |
vec.addElement(new Byte((byte) v)); |
vec.addElement(new Byte((byte) v)); |
265 |
} |
} |
266 |
|
|
267 |
public void writeChar(int v) throws IOException { |
public void writeChar(int v) throws IOException |
268 |
|
{ |
269 |
vec.addElement(new Character((char) v)); |
vec.addElement(new Character((char) v)); |
270 |
} |
} |
271 |
|
|
272 |
public void writeDouble(double v) throws IOException { |
public void writeDouble(double v) throws IOException |
273 |
|
{ |
274 |
vec.addElement(new Double(v)); |
vec.addElement(new Double(v)); |
275 |
} |
} |
276 |
|
|
277 |
public void writeFloat(float v) throws IOException { |
public void writeFloat(float v) throws IOException |
278 |
|
{ |
279 |
vec.addElement(new Float(v)); |
vec.addElement(new Float(v)); |
280 |
} |
} |
281 |
|
|
282 |
public void writeInt(int v) throws IOException { |
public void writeInt(int v) throws IOException |
283 |
|
{ |
284 |
vec.addElement(new Integer(v)); |
vec.addElement(new Integer(v)); |
285 |
} |
} |
286 |
|
|
287 |
public void writeLong(long v) throws IOException { |
public void writeLong(long v) throws IOException |
288 |
|
{ |
289 |
vec.addElement(new Long(v)); |
vec.addElement(new Long(v)); |
290 |
} |
} |
291 |
|
|
292 |
public void writeShort(int v) throws IOException { |
public void writeShort(int v) throws IOException |
293 |
|
{ |
294 |
vec.addElement(new Short((short) v)); |
vec.addElement(new Short((short) v)); |
295 |
} |
} |
296 |
|
|
297 |
public void writeObject(Object obj) throws IOException { |
public void writeObject(Object obj) throws IOException |
298 |
|
{ |
299 |
vec.addElement(obj); |
vec.addElement(obj); |
300 |
} |
} |
301 |
|
|
302 |
public void write(byte b[]) throws IOException { |
public void write(byte b[]) throws IOException |
303 |
|
{ |
304 |
throw new IOException("not required"); |
throw new IOException("not required"); |
305 |
} |
} |
306 |
|
|
307 |
public void write(byte b[], int off, int len) throws IOException { |
public void write(byte b[], int off, int len) throws IOException |
308 |
|
{ |
309 |
throw new IOException("not required"); |
throw new IOException("not required"); |
310 |
} |
} |
311 |
|
|
312 |
public void write(int b) throws IOException { |
public void write(int b) throws IOException |
313 |
|
{ |
314 |
throw new IOException("not required"); |
throw new IOException("not required"); |
315 |
} |
} |
316 |
|
|
317 |
public void writeBytes(String s) throws IOException { |
public void writeBytes(String s) throws IOException |
318 |
|
{ |
319 |
throw new IOException("not required"); |
throw new IOException("not required"); |
320 |
} |
} |
321 |
|
|
322 |
public void writeChars(String s) throws IOException { |
public void writeChars(String s) throws IOException |
323 |
|
{ |
324 |
throw new IOException("not required"); |
throw new IOException("not required"); |
325 |
} |
} |
326 |
|
|
327 |
public void writeUTF(String str) throws IOException { |
public void writeUTF(String str) throws IOException |
328 |
|
{ |
329 |
throw new IOException("not required"); |
throw new IOException("not required"); |
330 |
} |
} |
331 |
|
|
332 |
public void flush() throws IOException { |
public void flush() throws IOException |
333 |
} |
{ |
|
|
|
|
public void close() throws IOException { |
|
334 |
} |
} |
335 |
|
|
336 |
|
public void close() throws IOException |
337 |
|
{ |
338 |
} |
} |
339 |
|
} // class DummyObjectOutputStream |
340 |
|
|
341 |
/** |
/** |
342 |
* Dummy object input class. |
* Dummy object input class. |
343 |
*/ |
*/ |
344 |
private class DummyObjectInputStream implements ObjectInput { |
private class DummyObjectInputStream implements ObjectInput |
345 |
|
{ |
346 |
|
/** |
347 |
|
* Non-private constructor to reduce bytecode emitted. |
348 |
|
*/ |
349 |
|
DummyObjectInputStream() |
350 |
|
{ |
351 |
|
} |
352 |
|
|
353 |
public boolean readBoolean() throws IOException { |
public boolean readBoolean() throws IOException |
354 |
|
{ |
355 |
Object obj = vec.elementAt(ptr++); |
Object obj = vec.elementAt(ptr++); |
356 |
return (((Boolean)obj).booleanValue()); |
return ((Boolean) obj).booleanValue(); |
357 |
} |
} |
358 |
|
|
359 |
public byte readByte() throws IOException { |
public byte readByte() throws IOException |
360 |
|
{ |
361 |
Object obj = vec.elementAt(ptr++); |
Object obj = vec.elementAt(ptr++); |
362 |
return (((Byte)obj).byteValue()); |
return ((Byte) obj).byteValue(); |
363 |
} |
} |
364 |
|
|
365 |
public char readChar() throws IOException { |
public char readChar() throws IOException |
366 |
|
{ |
367 |
Object obj = vec.elementAt(ptr++); |
Object obj = vec.elementAt(ptr++); |
368 |
return (((Character)obj).charValue()); |
return ((Character) obj).charValue(); |
369 |
} |
} |
370 |
|
|
371 |
public double readDouble() throws IOException { |
public double readDouble() throws IOException |
372 |
|
{ |
373 |
Object obj = vec.elementAt(ptr++); |
Object obj = vec.elementAt(ptr++); |
374 |
return (((Double)obj).doubleValue()); |
return ((Double) obj).doubleValue(); |
375 |
} |
} |
376 |
|
|
377 |
public float readFloat() throws IOException { |
public float readFloat() throws IOException |
378 |
|
{ |
379 |
Object obj = vec.elementAt(ptr++); |
Object obj = vec.elementAt(ptr++); |
380 |
return (((Float)obj).floatValue()); |
return ((Float) obj).floatValue(); |
381 |
} |
} |
382 |
|
|
383 |
public int readInt() throws IOException { |
public int readInt() throws IOException |
384 |
|
{ |
385 |
Object obj = vec.elementAt(ptr++); |
Object obj = vec.elementAt(ptr++); |
386 |
return (((Integer)obj).intValue()); |
return ((Integer) obj).intValue(); |
387 |
} |
} |
388 |
|
|
389 |
public long readLong() throws IOException { |
public long readLong() throws IOException |
390 |
|
{ |
391 |
Object obj = vec.elementAt(ptr++); |
Object obj = vec.elementAt(ptr++); |
392 |
return (((Long)obj).longValue()); |
return ((Long) obj).longValue(); |
393 |
} |
} |
394 |
|
|
395 |
public short readShort() throws IOException { |
public short readShort() throws IOException |
396 |
|
{ |
397 |
Object obj = vec.elementAt(ptr++); |
Object obj = vec.elementAt(ptr++); |
398 |
return (((Short)obj).shortValue()); |
return ((Short) obj).shortValue(); |
399 |
} |
} |
400 |
|
|
401 |
public Object readObject() throws IOException { |
public Object readObject() throws IOException |
402 |
return (vec.elementAt(ptr++)); |
{ |
403 |
|
return vec.elementAt(ptr++); |
404 |
} |
} |
405 |
|
|
406 |
public int read(byte b[]) throws IOException { |
public int read(byte b[]) throws IOException |
407 |
|
{ |
408 |
throw new IOException("not required"); |
throw new IOException("not required"); |
409 |
} |
} |
410 |
|
|
411 |
public int read(byte b[], int off, int len) throws IOException { |
public int read(byte b[], int off, int len) throws IOException |
412 |
|
{ |
413 |
throw new IOException("not required"); |
throw new IOException("not required"); |
414 |
} |
} |
415 |
|
|
416 |
public int read() throws IOException { |
public int read() throws IOException |
417 |
|
{ |
418 |
throw new IOException("not required"); |
throw new IOException("not required"); |
419 |
} |
} |
420 |
|
|
421 |
public long skip(long n) throws IOException { |
public long skip(long n) throws IOException |
422 |
|
{ |
423 |
throw new IOException("not required"); |
throw new IOException("not required"); |
424 |
} |
} |
425 |
|
|
426 |
public int available() throws IOException { |
public int available() throws IOException |
427 |
|
{ |
428 |
throw new IOException("not required"); |
throw new IOException("not required"); |
429 |
} |
} |
430 |
|
|
431 |
public void readFully(byte b[]) throws IOException { |
public void readFully(byte b[]) throws IOException |
432 |
|
{ |
433 |
throw new IOException("not required"); |
throw new IOException("not required"); |
434 |
} |
} |
435 |
|
|
436 |
public void readFully(byte b[], int off, int len) throws IOException { |
public void readFully(byte b[], int off, int len) throws IOException |
437 |
|
{ |
438 |
throw new IOException("not required"); |
throw new IOException("not required"); |
439 |
} |
} |
440 |
|
|
441 |
public String readLine() throws IOException { |
public String readLine() throws IOException |
442 |
|
{ |
443 |
throw new IOException("not required"); |
throw new IOException("not required"); |
444 |
} |
} |
445 |
|
|
446 |
public String readUTF() throws IOException { |
public String readUTF() throws IOException |
447 |
|
{ |
448 |
throw new IOException("not required"); |
throw new IOException("not required"); |
449 |
} |
} |
450 |
|
|
451 |
public int readUnsignedByte() throws IOException { |
public int readUnsignedByte() throws IOException |
452 |
|
{ |
453 |
throw new IOException("not required"); |
throw new IOException("not required"); |
454 |
} |
} |
455 |
|
|
456 |
public int readUnsignedShort() throws IOException { |
public int readUnsignedShort() throws IOException |
457 |
|
{ |
458 |
throw new IOException("not required"); |
throw new IOException("not required"); |
459 |
} |
} |
460 |
|
|
461 |
public int skipBytes(int n) throws IOException { |
public int skipBytes(int n) throws IOException |
462 |
|
{ |
463 |
throw new IOException("not required"); |
throw new IOException("not required"); |
464 |
} |
} |
465 |
|
|
466 |
public void close() throws IOException { |
public void close() throws IOException |
467 |
} |
{ |
|
|
|
468 |
} |
} |
469 |
|
} // class DummyObjectInputStream |
470 |
|
|
471 |
} |
} |