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 * RPCException
*/ */
public class RPCException extends Exception { 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 { public interface RPCServerRequestCodec extends RPCRegistryCodec {
boolean hasResponse(); 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 { public JSONRPCServerRequestCodec(Gson gson, byte[] buff) throws RPCException {
this.gson = gson; this.gson = gson;
String json = new String(buff);
try { try {
this.request = this.gson.fromJson(new String(buff), JSONServerRequest.class); this.request = this.gson.fromJson(json, JSONServerRequest.class);
} catch (JsonSyntaxException e) { } 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) { 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()) { 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]; Object[] result = new Object[paramTypes.length];
@ -57,10 +58,10 @@ class JSONRPCServerRequestCodec implements RPCServerRequestCodec {
return null != this.request.id; 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; RPCError rpcError = null;
if (null != error) { if (null != ex) {
rpcError = new RPCError(1, "", error); rpcError = new RPCError(1, ex.getMessage(), ex.getData());
} }
JSONServerResponse response = new JSONServerResponse(JSONRPC.version, reply, rpcError); JSONServerResponse response = new JSONServerResponse(JSONRPC.version, reply, rpcError);
response.id = this.request.id; response.id = this.request.id;

View File

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

View File

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

View File

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

View File

@ -12,4 +12,4 @@ public interface RPCRegistry extends RPCInvoker {
void registerService(Object receiver, String name) throws RPCException; void registerService(Object receiver, String name) throws RPCException;
void registerServices(Object... receivers) throws RPCException; void registerServices(Object... receivers) throws RPCException;
void registerServices(Map<String, Object> receivers) throws RPCException; void registerServices(Map<String, Object> receivers) throws RPCException;
} }