use of triple.Request in project sofa-rpc by sofastack.
the class TripleServer method buildSofaServiceDef.
private ServerServiceDefinition buildSofaServiceDef(GenericServiceImpl genericService, ProviderConfig providerConfig) {
ServerServiceDefinition templateDefinition = genericService.bindService();
ServerCallHandler<Request, Response> templateHandler = (ServerCallHandler<Request, Response>) templateDefinition.getMethods().iterator().next().getServerCallHandler();
List<MethodDescriptor<Request, Response>> methodDescriptor = getMethodDescriptor(providerConfig);
List<ServerMethodDefinition<Request, Response>> methodDefs = getMethodDefinitions(templateHandler, methodDescriptor);
ServerServiceDefinition.Builder builder = ServerServiceDefinition.builder(getServiceDescriptor(templateDefinition, providerConfig, methodDescriptor));
for (ServerMethodDefinition<Request, Response> methodDef : methodDefs) {
builder.addMethod(methodDef);
}
return builder.build();
}
use of triple.Request in project sofa-rpc by sofastack.
the class TripleClientInvoker method invoke.
@Override
public SofaResponse invoke(SofaRequest sofaRequest, int timeout) throws Exception {
if (!useGeneric) {
SofaResponse sofaResponse = new SofaResponse();
Object stub = sofaStub.invoke(null, channel, buildCustomCallOptions(sofaRequest, timeout), timeout);
final Method method = sofaRequest.getMethod();
Object appResponse = method.invoke(stub, sofaRequest.getMethodArgs()[0]);
sofaResponse.setAppResponse(appResponse);
return sofaResponse;
} else {
String serviceName = sofaRequest.getInterfaceName();
String methodName = sofaRequest.getMethodName();
MethodDescriptor.Marshaller<?> requestMarshaller = null;
MethodDescriptor.Marshaller<?> responseMarshaller = null;
requestMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Request.getDefaultInstance());
responseMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Response.getDefaultInstance());
String fullMethodName = generateFullMethodName(serviceName, methodName);
MethodDescriptor methodDescriptor = io.grpc.MethodDescriptor.newBuilder().setType(io.grpc.MethodDescriptor.MethodType.UNARY).setFullMethodName(fullMethodName).setSampledToLocalTracing(true).setRequestMarshaller((MethodDescriptor.Marshaller<Object>) requestMarshaller).setResponseMarshaller((MethodDescriptor.Marshaller<Object>) responseMarshaller).build();
Request request = getRequest(sofaRequest, serialization, serializer);
Response response = (Response) ClientCalls.blockingUnaryCall(channel, methodDescriptor, buildCustomCallOptions(sofaRequest, timeout), request);
SofaResponse sofaResponse = new SofaResponse();
byte[] responseDate = response.getData().toByteArray();
Class returnType = sofaRequest.getMethod().getReturnType();
if (returnType != void.class) {
if (responseDate != null && responseDate.length > 0) {
Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
Object appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
sofaResponse.setAppResponse(appResponse);
}
}
return sofaResponse;
}
}
use of triple.Request in project sofa-rpc by sofastack.
the class TripleClientInvoker method getRequest.
public static Request getRequest(SofaRequest sofaRequest, String serialization, Serializer serializer) {
Request.Builder builder = Request.newBuilder();
builder.setSerializeType(serialization);
String[] methodArgSigs = sofaRequest.getMethodArgSigs();
Object[] methodArgs = sofaRequest.getMethodArgs();
for (int i = 0; i < methodArgSigs.length; i++) {
Object arg = methodArgs[i];
ByteString argByteString = ByteString.copyFrom(serializer.encode(arg, null).array());
builder.addArgs(argByteString);
builder.addArgTypes(methodArgSigs[i]);
}
return builder.build();
}
use of triple.Request in project sofa-rpc by sofastack.
the class GenericServiceImplTest method testArray.
@Test
public void testArray() throws Exception {
// test return array
String methodName = "testArray";
Method method = HelloService.class.getDeclaredMethod(methodName, long[].class);
Object[] args = new Object[1];
long[] param = new long[1];
long l = 100L;
param[0] = l;
args[0] = param;
Request request = buildRequest(method, args);
doInvoke(request);
Object appResponse = getReturnValue(method);
long[] appResponse1 = (long[]) appResponse;
Assert.assertEquals(l, appResponse1[0]);
}
use of triple.Request in project sofa-rpc by sofastack.
the class GenericServiceImplTest method buildRequest.
private Request buildRequest(Method method, Object[] args) {
Class<?>[] parameterTypes = method.getParameterTypes();
SofaRequest sofaRequest = MessageBuilder.buildSofaRequest(HelloService.class, method, parameterTypes, args);
Request request = TripleClientInvoker.getRequest(sofaRequest, serialization, serializer);
Context context = Context.current().withValue(TracingContextKey.getKeySofaRequest(), sofaRequest);
context.attach();
return request;
}
Aggregations