ing
This commit is contained in:
8
src/main/java/com/loafle/commons/rpc/RPCException.java
Normal file
8
src/main/java/com/loafle/commons/rpc/RPCException.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.loafle.commons.rpc;
|
||||
|
||||
/**
|
||||
* RPCException
|
||||
*/
|
||||
public class RPCException extends Exception {
|
||||
|
||||
}
|
||||
44
src/main/java/com/loafle/commons/rpc/protocol/RPCError.java
Normal file
44
src/main/java/com/loafle/commons/rpc/protocol/RPCError.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.loafle.commons.rpc.protocol;
|
||||
|
||||
/**
|
||||
* Error
|
||||
*/
|
||||
public class RPCError {
|
||||
public final int code;
|
||||
public final String message;
|
||||
public final Object data;
|
||||
|
||||
/**
|
||||
* Creates the error.
|
||||
*
|
||||
* @param code the code
|
||||
* @param message the message
|
||||
* @param data the data
|
||||
*/
|
||||
public RPCError(int code, String message, Object data) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the code
|
||||
*/
|
||||
int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the message
|
||||
*/
|
||||
String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the data
|
||||
*/
|
||||
Object getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.loafle.commons.rpc.protocol;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.loafle.commons.rpc.RPCException;
|
||||
|
||||
/**
|
||||
* RegistryCodec
|
||||
*/
|
||||
public interface RPCRegistryCodec {
|
||||
String method();
|
||||
Object[] params(Type[] paramTypes) throws RPCException;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.loafle.commons.rpc.protocol;
|
||||
|
||||
import com.loafle.commons.rpc.RPCException;
|
||||
|
||||
/**
|
||||
* ServerCodec
|
||||
*/
|
||||
public interface RPCServerCodec {
|
||||
RPCServerRequestCodec request(byte[] buf) throws RPCException;
|
||||
byte[] notification(String method, Object... params) throws RPCException;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.loafle.commons.rpc.protocol;
|
||||
|
||||
import com.loafle.commons.rpc.RPCException;
|
||||
|
||||
/**
|
||||
* ServerRequestCodec
|
||||
*/
|
||||
public interface RPCServerRequestCodec extends RPCRegistryCodec {
|
||||
boolean hasResponse();
|
||||
byte[] response(Object reply, Error error) throws RPCException;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.loafle.commons.rpc.protocol.json;
|
||||
|
||||
/**
|
||||
* JSONRPC
|
||||
*/
|
||||
public class JSONRPC {
|
||||
public static final String version = "2.0";
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.loafle.commons.rpc.protocol.json;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.loafle.commons.rpc.RPCException;
|
||||
import com.loafle.commons.rpc.protocol.RPCServerCodec;
|
||||
import com.loafle.commons.rpc.protocol.RPCServerRequestCodec;
|
||||
|
||||
/**
|
||||
* JSONRPCServerCodec
|
||||
*/
|
||||
public class JSONRPCServerCodec implements RPCServerCodec {
|
||||
private Gson gson;
|
||||
|
||||
public JSONRPCServerCodec() {
|
||||
this.gson = new Gson();
|
||||
}
|
||||
public JSONRPCServerCodec(Gson gson) {
|
||||
this.gson = gson;
|
||||
}
|
||||
|
||||
public void setGson(Gson gson) {
|
||||
this.gson = gson;
|
||||
}
|
||||
|
||||
public Gson getGson() {
|
||||
return this.gson;
|
||||
}
|
||||
|
||||
public RPCServerRequestCodec request(byte[] buff) throws RPCException {
|
||||
return new JSONRPCServerRequestCodec(this.gson, buff);
|
||||
}
|
||||
|
||||
public byte[] notification(String method, Object... params) throws RPCException {
|
||||
JSONServerNotification notification = new JSONServerNotification(method, params);
|
||||
JSONServerResponse response = new JSONServerResponse(JSONRPC.version, notification, null);
|
||||
|
||||
String json = this.gson.toJson(response);
|
||||
return json.getBytes();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.loafle.commons.rpc.protocol.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.loafle.commons.rpc.RPCException;
|
||||
import com.loafle.commons.rpc.protocol.RPCError;
|
||||
import com.loafle.commons.rpc.protocol.RPCServerRequestCodec;
|
||||
|
||||
/**
|
||||
* JSONRPCServerRequestCodec
|
||||
*/
|
||||
class JSONRPCServerRequestCodec implements RPCServerRequestCodec {
|
||||
private Gson gson;
|
||||
private JSONServerRequest request;
|
||||
|
||||
public JSONRPCServerRequestCodec(Gson gson, byte[] buff) throws RPCException {
|
||||
this.gson = gson;
|
||||
try {
|
||||
this.request = this.gson.fromJson(new String(buff), JSONServerRequest.class);
|
||||
} catch (JsonSyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String method() {
|
||||
return this.request.method;
|
||||
}
|
||||
|
||||
public Object[] params(Type[] paramTypes) throws RPCException {
|
||||
if (null == paramTypes && null == this.request.params) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ((null != paramTypes && null == this.request.params) || null == paramTypes && null != this.request.params) {
|
||||
throw new RPCException();
|
||||
}
|
||||
|
||||
if (paramTypes.length != this.request.params.size()) {
|
||||
throw new RPCException();
|
||||
}
|
||||
|
||||
Object[] result = new Object[paramTypes.length];
|
||||
|
||||
for (int i = 0; i < paramTypes.length; i++) {
|
||||
Type paramType = paramTypes[i];
|
||||
String param = this.request.params.get(i);
|
||||
|
||||
result[i] = this.gson.fromJson(param, paramType);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean hasResponse() {
|
||||
return null != this.request.id;
|
||||
}
|
||||
|
||||
public byte[] response(Object reply, Error error) throws RPCException {
|
||||
RPCError rpcError = null;
|
||||
if (null != error) {
|
||||
rpcError = new RPCError(1, "", error);
|
||||
}
|
||||
JSONServerResponse response = new JSONServerResponse(JSONRPC.version, reply, rpcError);
|
||||
response.id = this.request.id;
|
||||
|
||||
String json = this.gson.toJson(response);
|
||||
return json.getBytes();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.loafle.commons.rpc.protocol.json;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
/**
|
||||
* JSONServerNotification
|
||||
*/
|
||||
class JSONServerNotification {
|
||||
@SerializedName("method")
|
||||
public String method;
|
||||
|
||||
@SerializedName("params")
|
||||
public Object[] params;
|
||||
|
||||
JSONServerNotification(String method, Object[] params) {
|
||||
this.method = method;
|
||||
this.params = params;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.loafle.commons.rpc.protocol.json;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
/**
|
||||
* JSONServerRequest
|
||||
*/
|
||||
class JSONServerRequest {
|
||||
@SerializedName("jsonrpc")
|
||||
public String version;
|
||||
|
||||
@SerializedName("method")
|
||||
public String method;
|
||||
|
||||
@SerializedName("params")
|
||||
public List<String> params;
|
||||
|
||||
@SerializedName("id")
|
||||
public Object id;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.loafle.commons.rpc.protocol.json;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import com.loafle.commons.rpc.protocol.RPCError;
|
||||
|
||||
/**
|
||||
* JSONServerResponse
|
||||
*/
|
||||
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) {
|
||||
this(version, result, error, null);
|
||||
}
|
||||
|
||||
JSONServerResponse(String version, Object result, RPCError error, Object id) {
|
||||
this.version = version;
|
||||
this.result = result;
|
||||
this.error = error;
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.loafle.commons.rpc.registry;
|
||||
|
||||
import com.loafle.commons.rpc.RPCException;
|
||||
import com.loafle.commons.rpc.protocol.RPCRegistryCodec;
|
||||
|
||||
/**
|
||||
* RPCInvoker
|
||||
*/
|
||||
public interface RPCInvoker {
|
||||
boolean hasMethod(String method);
|
||||
Object invoke(RPCRegistryCodec codec, Object... leadingParams) throws RPCException;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.loafle.commons.rpc.registry;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.loafle.commons.rpc.RPCException;
|
||||
|
||||
/**
|
||||
* RPCRegistry
|
||||
*/
|
||||
public interface RPCRegistry extends RPCInvoker {
|
||||
Object getService(String name);
|
||||
void registerService(Object receiver, String name) throws RPCException;
|
||||
void registerServices(Object... receivers) throws RPCException;
|
||||
void registerServices(Map<String, Object> receivers) throws RPCException;
|
||||
}
|
||||
0
src/main/resources/_
Normal file
0
src/main/resources/_
Normal file
17
src/test/resources/logback.xml
Normal file
17
src/test/resources/logback.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="3 seconds">
|
||||
<contextName>commons_java</contextName>
|
||||
<!-- TRACE > DEBUG > INFO > WARN > ERROR -->
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>
|
||||
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
<logger name="com.loafle.overflow" level="ALL" />
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user