ing
This commit is contained in:
parent
17dd51b7c1
commit
40ca74e2f4
8
pom.xml
8
pom.xml
|
@ -18,6 +18,7 @@
|
|||
|
||||
<properties>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
<protobuf-java.version>3.5.1</protobuf-java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
@ -26,6 +27,13 @@
|
|||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.protobuf</groupId>
|
||||
<artifactId>protobuf-java</artifactId>
|
||||
<version>${protobuf-java.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package com.loafle.commons.rpc.protocol.grpc;
|
||||
|
||||
/**
|
||||
* JSONRPC
|
||||
*/
|
||||
public class GRPC {
|
||||
public static final String version = "2.0";
|
||||
}
|
|
@ -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<ByteString> params) throws RPCException {
|
||||
return new GRPCServerRequestCodec(this.gson, service, method, params);
|
||||
}
|
||||
|
||||
public byte[] notification(String method, Object... params) throws RPCException {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -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<ByteString> params;
|
||||
|
||||
|
||||
public GRPCServerRequestCodec(Gson gson, String service, String method, List<ByteString> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user