comparison c1x4hotspotsrc/HotSpotVM/src/com/sun/hotspot/c1x/InvocationSocket.java @ 2288:8c426c2891c8

client/server: new interface Remote marks classes that should not be serialized, but called remotely
author Lukas Stadler <lukas.stadler@jku.at>
date Mon, 11 Apr 2011 10:37:24 +0200
parents 569d3fe7d65c
children 6190d20bd6d6
comparison
equal deleted inserted replaced
2287:66ffa0e99cef 2288:8c426c2891c8
32 * While waiting for a result three types of objects can arrive through the socket: a method invocation, a method result or an exception. 32 * While waiting for a result three types of objects can arrive through the socket: a method invocation, a method result or an exception.
33 * Method invocation can thus be recursive. 33 * Method invocation can thus be recursive.
34 * 34 *
35 * @author Lukas Stadler 35 * @author Lukas Stadler
36 */ 36 */
37 public class InvocationSocket implements InvocationHandler { 37 public class InvocationSocket {
38 38
39 private final ObjectOutputStream output; 39 private final ObjectOutputStream output;
40 private final ObjectInputStream input; 40 private final ObjectInputStream input;
41 private Object delegate;
42 private DelegateCallback callback;
43 41
44 public InvocationSocket(ObjectOutputStream output, ObjectInputStream input) { 42 public InvocationSocket(ObjectOutputStream output, ObjectInputStream input) {
45 this.output = output; 43 this.output = output;
46 this.input = input; 44 this.input = input;
47 } 45 }
48 46
49 public void setDelegate(Object delegate) {
50 this.delegate = delegate;
51 }
52
53 public static interface DelegateCallback {
54 public Object getDelegate();
55 }
56
57 public void setDelegateCallback(DelegateCallback callback) {
58 this.callback = callback;
59 }
60
61 private static class Invocation implements Serializable { 47 private static class Invocation implements Serializable {
62 48
49 public Object receiver;
63 public String methodName; 50 public String methodName;
64 public Object[] args; 51 public Object[] args;
65 52
66 public Invocation(String methodName, Object[] args) { 53 public Invocation(Object receiver, String methodName, Object[] args) {
54 this.receiver = receiver;
67 this.methodName = methodName; 55 this.methodName = methodName;
68 this.args = args; 56 this.args = args;
69 } 57 }
70 } 58 }
71 59
76 public Result(Object result) { 64 public Result(Object result) {
77 this.result = result; 65 this.result = result;
78 } 66 }
79 } 67 }
80 68
81 @Override 69 public class Handler implements InvocationHandler {
82 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 70 private Object receiver;
83 try { 71
84 Logger.startScope("invoking remote " + method.getName()); 72 public Handler(Object receiver) {
85 output.writeObject(new Invocation(method.getName(), args)); 73 this.receiver = receiver;
86 output.flush(); 74 }
87 return waitForResult(); 75
88 } catch (Throwable t) { 76 @Override
89 t.printStackTrace(); 77 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
90 throw t; 78 if (!method.getDeclaringClass().isInterface()) {
91 } finally { 79 return method.invoke(receiver, args);
92 Logger.endScope(""); 80 }
81 try {
82 //Logger.startScope("invoking remote " + method.getName());
83 output.writeObject(new Invocation(receiver, method.getName(), args));
84 output.flush();
85 return waitForResult();
86 } catch (Throwable t) {
87 t.printStackTrace();
88 throw t;
89 } finally {
90 //Logger.endScope("");
91 }
93 } 92 }
94 } 93 }
95 94
96 public Object waitForResult() throws IOException, ClassNotFoundException { 95 public Object waitForResult() throws IOException, ClassNotFoundException {
97 while (true) { 96 while (true) {
102 throw (RuntimeException) in; 101 throw (RuntimeException) in;
103 } else if (in instanceof Throwable) { 102 } else if (in instanceof Throwable) {
104 throw new RuntimeException((Throwable) in); 103 throw new RuntimeException((Throwable) in);
105 } 104 }
106 105
107 if (delegate == null) {
108 delegate = callback.getDelegate();
109 callback = null;
110 }
111
112 Invocation invoke = (Invocation) in; 106 Invocation invoke = (Invocation) in;
113 Method method = null; 107 Method method = null;
114 for (Method m : delegate.getClass().getDeclaredMethods()) { 108 for (Class<?> clazz = invoke.receiver.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
115 if (invoke.methodName.equals(m.getName())) { 109 for (Method m : clazz.getDeclaredMethods()) {
116 method = m; 110 if (invoke.methodName.equals(m.getName())) {
117 break; 111 method = m;
112 break;
113 }
118 } 114 }
119 } 115 }
120 if (method == null) { 116 if (method == null) {
121 Exception e = new UnsupportedOperationException("unknown method " + invoke.methodName); 117 Exception e = new UnsupportedOperationException("unknown method " + invoke.methodName);
122 e.printStackTrace(); 118 e.printStackTrace();
123 output.writeObject(e); 119 output.writeObject(e);
124 output.flush(); 120 output.flush();
125 } else { 121 } else {
126 Object result; 122 Object result;
127 try { 123 try {
128 Logger.startScope("invoking local " + invoke.methodName);
129 if (invoke.args == null) { 124 if (invoke.args == null) {
130 result = method.invoke(delegate); 125 //Logger.startScope("invoking local " + invoke.methodName);
126 result = method.invoke(invoke.receiver);
131 } else { 127 } else {
132 result = method.invoke(delegate, invoke.args); 128 /*
129 StringBuilder str = new StringBuilder();
130 str.append("invoking local " + invoke.methodName + "(");
131 for (int i = 0; i < invoke.args.length; i++) {
132 str.append(i == 0 ? "" : ", ");
133 str.append(Logger.pretty(invoke.args[i]));
134 }
135 str.append(")");
136 Logger.startScope(str.toString());*/
137 result = method.invoke(invoke.receiver, invoke.args);
133 } 138 }
134 result = new Result(result); 139 result = new Result(result);
135 } catch (IllegalArgumentException e) { 140 } catch (IllegalArgumentException e) {
136 System.out.println("error while invoking " + invoke.methodName); 141 System.out.println("error while invoking " + invoke.methodName);
137 e.getCause().printStackTrace(); 142 e.getCause().printStackTrace();
143 } catch (IllegalAccessException e) { 148 } catch (IllegalAccessException e) {
144 System.out.println("error while invoking " + invoke.methodName); 149 System.out.println("error while invoking " + invoke.methodName);
145 e.getCause().printStackTrace(); 150 e.getCause().printStackTrace();
146 result = e.getCause(); 151 result = e.getCause();
147 } finally { 152 } finally {
148 Logger.endScope(""); 153 //Logger.endScope("");
149 } 154 }
150 output.writeObject(result); 155 output.writeObject(result);
151 output.flush(); 156 output.flush();
152 } 157 }
153 } 158 }
154 } 159 }
155 160
161 public void sendResult(Object obj) throws IOException {
162 output.writeObject(new Result(obj));
163 output.flush();
164 }
165
156 } 166 }