diff --git a/pom.xml b/pom.xml index 072fcd5..788b75e 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,7 @@ 2.8.2 + 3.5.1 @@ -26,6 +27,13 @@ gson ${gson.version} + + + com.google.protobuf + protobuf-java + ${protobuf-java.version} + + diff --git a/src/main/java/com/loafle/commons/rpc/protocol/grpc/GRPC.java b/src/main/java/com/loafle/commons/rpc/protocol/grpc/GRPC.java new file mode 100644 index 0000000..022ab50 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/grpc/GRPC.java @@ -0,0 +1,8 @@ +package com.loafle.commons.rpc.protocol.grpc; + +/** + * JSONRPC + */ +public class GRPC { + public static final String version = "2.0"; +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/grpc/GRPCServerCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/grpc/GRPCServerCodec.java new file mode 100644 index 0000000..6b541e4 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/grpc/GRPCServerCodec.java @@ -0,0 +1,38 @@ +package com.loafle.commons.rpc.protocol.grpc; + +import java.util.List; + +import com.google.gson.Gson; +import com.google.protobuf.ByteString; +import com.loafle.commons.rpc.RPCException; +import com.loafle.commons.rpc.protocol.RPCRegistryCodec; + +/** + * GRPCServerCodec + */ +public class GRPCServerCodec { + private Gson gson; + + public GRPCServerCodec() { + this.gson = new Gson(); + } + public GRPCServerCodec(Gson gson) { + this.gson = gson; + } + + public void setGson(Gson gson) { + this.gson = gson; + } + + public Gson getGson() { + return this.gson; + } + + public RPCRegistryCodec request(String service, String method, List params) throws RPCException { + return new GRPCServerRequestCodec(this.gson, service, method, params); + } + + public byte[] notification(String method, Object... params) throws RPCException { + return null; + } +} diff --git a/src/main/java/com/loafle/commons/rpc/protocol/grpc/GRPCServerRequestCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/grpc/GRPCServerRequestCodec.java new file mode 100644 index 0000000..601d205 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/grpc/GRPCServerRequestCodec.java @@ -0,0 +1,56 @@ +package com.loafle.commons.rpc.protocol.grpc; + +import java.lang.reflect.Type; +import java.util.List; + +import com.google.gson.Gson; +import com.google.protobuf.ByteString; +import com.loafle.commons.rpc.RPCException; +import com.loafle.commons.rpc.protocol.RPCRegistryCodec; + +/** + * JSONRPCServerRequestCodec + */ +class GRPCServerRequestCodec implements RPCRegistryCodec { + private Gson gson; + private String serviceName; + private String methodName; + private List params; + + + public GRPCServerRequestCodec(Gson gson, String service, String method, List params) throws RPCException { + this.gson = gson; + this.serviceName = service; + this.methodName = method; + this.params = params; + } + + public String method() { + return String.format("%s.%s", this.serviceName, this.methodName); + } + + public Object[] params(Type[] paramTypes) throws RPCException { + if (null == paramTypes && null == this.params) { + return null; + } + + if ((null != paramTypes && null == this.params) || null == paramTypes && null != this.params) { + throw new RPCException("params is not valied"); + } + + if (paramTypes.length != this.params.size()) { + throw new RPCException(String.format("The size of params[%d] and types[%d] is not same", this.params.size(), paramTypes.length)); + } + + Object[] result = new Object[paramTypes.length]; + + for (int i = 0; i < paramTypes.length; i++) { + Type paramType = paramTypes[i]; + String param = this.params.get(i).toStringUtf8(); + + result[i] = this.gson.fromJson(param, paramType); + } + + return result; + } +} \ No newline at end of file