This commit is contained in:
crusader 2018-04-23 21:26:10 +09:00
parent 58d6e8fe4a
commit 45375ef6fa
7 changed files with 24 additions and 23 deletions

View File

@ -4,5 +4,19 @@ package com.loafle.commons.rpc;
* RPCException
*/
public class RPCException extends Exception {
private Object data;
public RPCException(String message) {
super(message);
}
public RPCException(String message, Object data) {
super(message);
this.data = data;
}
public Object getData() {
return this.data;
}
}

View File

@ -7,5 +7,5 @@ import com.loafle.commons.rpc.RPCException;
*/
public interface RPCServerRequestCodec extends RPCRegistryCodec {
boolean hasResponse();
byte[] response(Object reply, Error error) throws RPCException;
byte[] response(Object reply, RPCException error) throws RPCException;
}

View File

@ -17,10 +17,11 @@ class JSONRPCServerRequestCodec implements RPCServerRequestCodec {
public JSONRPCServerRequestCodec(Gson gson, byte[] buff) throws RPCException {
this.gson = gson;
String json = new String(buff);
try {
this.request = this.gson.fromJson(new String(buff), JSONServerRequest.class);
this.request = this.gson.fromJson(json, JSONServerRequest.class);
} catch (JsonSyntaxException e) {
e.printStackTrace();
throw new RPCException("request is not valied", json);
}
}
@ -34,11 +35,11 @@ class JSONRPCServerRequestCodec implements RPCServerRequestCodec {
}
if ((null != paramTypes && null == this.request.params) || null == paramTypes && null != this.request.params) {
throw new RPCException();
throw new RPCException("params is not valied");
}
if (paramTypes.length != this.request.params.size()) {
throw new RPCException();
throw new RPCException(String.format("The size of params[%d] and types[%d] is not same", this.request.params.size(), paramTypes.length));
}
Object[] result = new Object[paramTypes.length];
@ -57,10 +58,10 @@ class JSONRPCServerRequestCodec implements RPCServerRequestCodec {
return null != this.request.id;
}
public byte[] response(Object reply, Error error) throws RPCException {
public byte[] response(Object reply, RPCException ex) throws RPCException {
RPCError rpcError = null;
if (null != error) {
rpcError = new RPCError(1, "", error);
if (null != ex) {
rpcError = new RPCError(1, ex.getMessage(), ex.getData());
}
JSONServerResponse response = new JSONServerResponse(JSONRPC.version, reply, rpcError);
response.id = this.request.id;

View File

@ -6,10 +6,7 @@ import com.google.gson.annotations.SerializedName;
* JSONServerNotification
*/
class JSONServerNotification {
@SerializedName("method")
public String method;
@SerializedName("params")
public Object[] params;
JSONServerNotification(String method, Object[] params) {

View File

@ -10,14 +10,8 @@ import com.google.gson.annotations.SerializedName;
class JSONServerRequest {
@SerializedName("jsonrpc")
public String version;
@SerializedName("method")
public String method;
@SerializedName("params")
public List<String> params;
@SerializedName("id")
public Object id;
}

View File

@ -10,13 +10,8 @@ class JSONServerResponse {
@SerializedName("jsonrpc")
public String version;
@SerializedName("result")
public Object result;
@SerializedName("error")
public RPCError error;
@SerializedName("id")
public Object id;
JSONServerResponse(String version, Object result, RPCError error) {