ing
This commit is contained in:
parent
58d6e8fe4a
commit
45375ef6fa
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user