-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRPCClient.java
More file actions
195 lines (175 loc) · 8.89 KB
/
RPCClient.java
File metadata and controls
195 lines (175 loc) · 8.89 KB
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
185
186
187
188
189
190
191
192
193
194
195
package com.coolioasjulio.rpc.client;
import com.coolioasjulio.rpc.RPCException;
import com.coolioasjulio.rpc.RPCRequest;
import com.coolioasjulio.rpc.RPCResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
public class RPCClient implements AutoCloseable {
private Gson gson;
private BufferedReader in;
private PrintStream out;
private long id;
/**
* Create an RPC client.
*
* @param in InputStream coming from the RPC server.
* @param out OutputStream going to the RPC server.
*/
public RPCClient(InputStream in, OutputStream out) {
gson = new Gson();
this.in = new BufferedReader(new InputStreamReader(in));
this.out = new PrintStream(out);
}
private <T> T sendRPCRequest(boolean instantiate, String className, String objectName, String methodName, String[] argClassNames, Object[] args) {
if (argClassNames.length != args.length) {
throw new IllegalArgumentException("argClassNames and args must have same length!");
}
RPCRequest request = new RPCRequest(id++, instantiate, className, objectName, methodName, argClassNames, args);
String jsonRequest = gson.toJson(request);
out.println(jsonRequest);
out.flush();
String jsonResponse;
try {
jsonResponse = in.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
RPCResponse tempResponse = gson.fromJson(jsonResponse, RPCResponse.class);
if (request.getId() != tempResponse.getId()) {
throw new RPCException("Somehow the calls are out of sync! Are you using multithreading?");
}
if (tempResponse.isException()) {
RPCResponse<String> exception = gson.fromJson(jsonResponse, (new TypeToken<RPCResponse<String>>() {}).getType());
throw new RuntimeException(exception.getValue());
}
RPCResponse<T> response = gson.fromJson(jsonResponse, (new TypeToken<RPCResponse<T>>() {}).getType());
return response.getValue();
}
/**
* Execute a static method.
*
* @param className The canonical name of the class which defines the static method.
* @param methodName The name of the static method to execute.
* @param <T> The return type.
* @return The result of the static method.
*/
public <T> T executeStaticMethod(String className, String methodName) {
return executeStaticMethod(className, methodName, new String[0], new Object[0]);
}
/**
* Execute a static method.
*
* @param className The canonical name of the class which defines the static method.
* @param methodName The name of the static method to execute.
* @param argClassNames The canonical names of the classes of the arguments to the method call. Corresponds to <code>args.</code>
* Prepend with REMOTE: to pass a remote object as an argument. If remote object, the corresponding
* value in <code>args</code> should a string representing the name of the remote object.
* @param args The objects to pass as arguments to the method. To pass a remote object, prepend the class
* name with REMOTE: and the arg should be the remote object name.
* @param <T> The return type.
* @return The result of the static method.
*/
public <T> T executeStaticMethod(String className, String methodName, String[] argClassNames, Object[] args) {
return sendRPCRequest(false, className, "", methodName, argClassNames, args);
}
/**
* Execute a static method.
*
* @param objectName The name of the remote object which defines the method to execute.
* @param methodName The name of the method to execute.
* @param <T> The return type.
* @return The result of the method.
*/
public <T> T executeMethod(String objectName, String methodName) {
return executeMethod(objectName, methodName, new String[0], new Object[0]);
}
/**
* Execute a method on a remote object.
*
* @param objectName The name of the remote object which defines the method to execute.
* @param methodName The name of the method to execute.
* @param argClassNames The canonical names of the classes of the arguments to the method call. Corresponds to <code>args.</code>
* Prepend with REMOTE: to pass a remote object as an argument. If remote object, the corresponding
* value in <code>args</code> should a string representing the name of the remote object.
* @param args The objects to pass as arguments to the method. To pass a remote object, prepend the class
* name with REMOTE: and the arg should be the remote object name.
* @param <T> The return type.
* @return The result of the method.
*/
public <T> T executeMethod(String objectName, String methodName, String[] argClassNames, Object[] args) {
return sendRPCRequest(false, "", objectName, methodName, argClassNames, args);
}
/**
* Execute a method on a static object.
*
* @param className The canonical name of the class which defines the static object.
* @param objectName The name of the static object which defines the method to execute.
* @param methodName The name of the method to execute.
* @param <T> The return type.
* @return The result of the method.
*/
public <T> T executeMethodOnStaticObject(String className, String objectName, String methodName) {
return executeMethodOnStaticObject(className, objectName, methodName, new String[0], new Object[0]);
}
/**
* Execute a method on a static object.
*
* @param className The canonical name of the class which defines the static object.
* @param objectName The name of the static object which defines the method to execute.
* @param methodName The name of the method to execute.
* @param argClassNames The canonical names of the classes of the arguments to the method call. Corresponds to <code>args.</code>
* Prepend with REMOTE: to pass a remote object as an argument. If remote object, the corresponding
* value in <code>args</code> should a string representing the name of the remote object.
* @param args The objects to pass as arguments to the method. To pass a remote object, prepend the class
* name with REMOTE: and the arg should be the remote object name.
* @param <T> The return type.
* @return The result of the method.
*/
public <T> T executeMethodOnStaticObject(String className, String objectName, String methodName, String[] argClassNames, Object[] args) {
return sendRPCRequest(false, className, objectName, methodName, argClassNames, args);
}
/**
* Instantiate a remote object.
*
* @param className The canonical name of the class to instantiate.
* @param objectName The name of the remote object that's being instantiated.
* @param <T> The return type.
* @return The instantiated object.
*/
public <T> T instantiateObject(String className, String objectName) {
return instantiateObject(className, objectName, new String[0], new Object[0]);
}
/**
* Instantiate a remote object.
*
* @param className The canonical name of the class to instantiate.
* @param objectName The name of the remote object that's being instantiated.
* @param argClassNames The canonical names of the classes of the arguments to the method call. Corresponds to <code>args.</code>
* Prepend with REMOTE: to pass a remote object as an argument. If remote object, the corresponding
* value in <code>args</code> should a string representing the name of the remote object.
* @param args The objects to pass as arguments to the method. To pass a remote object, prepend the class
* name with REMOTE: and the arg should be the remote object name.
* @param <T> The return type.
* @return The instantiated object.
*/
public <T> T instantiateObject(String className, String objectName, String[] argClassNames, Object[] args) {
return sendRPCRequest(true, className, objectName, "", argClassNames, args);
}
/**
* Close the RPC session. A closed RPC session cannot be used any more.
*
* @throws Exception If an error occurs while closing.
*/
@Override
public void close() throws Exception {
in.close();
out.close();
}
}