2 Copyright (c) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License.
28 package gnu.java.rmi.server;
30 import java.rmi.Remote;
31 import java.rmi.RemoteException;
32 import java.rmi.server.RemoteRef;
33 import java.rmi.server.RMISocketFactory;
34 import java.rmi.server.RMIClientSocketFactory;
35 import java.rmi.server.RMIServerSocketFactory;
36 import java.rmi.server.RemoteObject;
37 import java.rmi.server.RemoteCall;
38 import java.rmi.server.UnicastRemoteObject;
39 import java.rmi.server.Operation;
40 import java.rmi.server.ObjID;
41 import java.rmi.server.UID;
42 import java.lang.reflect.Method;
43 import java.io.ObjectOutput;
44 import java.io.ObjectInput;
45 import java.io.IOException;
46 import java.net.Socket;
47 import java.net.InetAddress;
48 import java.io.BufferedInputStream;
49 import java.io.BufferedOutputStream;
50 import java.io.ObjectInputStream;
51 import java.io.ObjectOutputStream;
52 import java.io.DataInputStream;
53 import java.io.DataOutputStream;
55 public class UnicastRef
56 implements RemoteRef, ProtocolConstants {
59 UnicastConnectionManager manager;
62 * Used by serialization.
64 private UnicastRef() {
67 public UnicastRef(ObjID objid, String host, int port, RMIClientSocketFactory csf) {
69 manager = UnicastConnectionManager.getInstance(host, port, csf);
72 public UnicastRef(ObjID objid) {
76 public Object invoke(Remote obj, Method method, Object[] params, long opnum) throws Exception {
77 return (invokeCommon(obj, method, params, -1, opnum));
80 private Object invokeCommon(Remote obj, Method method, Object[] params, int opnum, long hash) throws Exception {
81 UnicastConnection conn;
83 conn = manager.getConnection();
85 catch (IOException e1) {
86 throw new RemoteException("connection failed to host: " + manager.serverName, e1);
89 ObjectOutputStream out;
90 DataOutputStream dout;
92 dout = conn.getDataOutputStream();
93 dout.writeByte(MESSAGE_CALL);
95 out = conn.getObjectOutputStream();
100 if (params != null) {
101 for (int i = 0; i < params.length; i++) {
102 if (params[i] instanceof UnicastRemoteObject) {
103 out.writeObject(UnicastRemoteObject.exportObject((UnicastRemoteObject)params[i]));
106 out.writeObject(params[i]);
113 catch (IOException e2) {
114 throw new RemoteException("call failed: ", e2);
120 ObjectInputStream in;
123 din = conn.getDataInputStream();
124 if (din.readUnsignedByte() != MESSAGE_CALL_ACK) {
125 throw new RemoteException("Call not acked");
128 in = conn.getObjectInputStream();
130 returncode = in.readUnsignedByte();
132 returnval = in.readObject();
134 catch (IOException e3) {
135 throw new RemoteException("call return failed: ", e3);
138 manager.discardConnection(conn);
140 if (returncode != RETURN_ACK) {
141 throw (Exception)returnval;
150 public RemoteCall newCall(RemoteObject obj, Operation[] op, int opnum, long hash) throws RemoteException {
151 return (new UnicastRemoteCall(obj, opnum, hash));
157 public void invoke(RemoteCall call) throws Exception {
158 UnicastRemoteCall c = (UnicastRemoteCall)call;
159 Object ret = invokeCommon((Remote)c.getObject(), (Method)null, c.getArguments(), c.getOpnum(), c.getHash());
160 c.setReturnValue(ret);
166 public void done(RemoteCall call) throws RemoteException {
170 public void writeExternal(ObjectOutput out) throws IOException {
171 if (manager == null) {
172 throw new IOException("no connection");
176 out.writeByte(RETURN_ACK);
179 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
180 manager = UnicastConnectionManager.read(in);
181 objid = ObjID.read(in);
182 if (in.readByte() != RETURN_ACK) {
183 throw new IOException("no ack found");
187 public boolean remoteEquals(RemoteRef ref) {
188 throw new Error("Not implemented");
191 public int remoteHashCode() {
192 throw new Error("Not implemented");
195 public String getRefClass(ObjectOutput out) {
196 return ("UnicastRef");
199 public String remoteToString() {
200 throw new Error("Not implemented");
203 public void dump(UnicastConnection conn) {
205 DataInputStream din = conn.getDataInputStream();
207 int b = din.readUnsignedByte();
208 System.out.print(Integer.toHexString(b));
209 if (b >= 32 && b < 128) {
210 System.out.print(": " + (char)b);
212 System.out.println();
215 catch (IOException _) {