Search in sources :

Example 1 with Request

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();
}
Also used : Response(triple.Response) ServerMethodDefinition(io.grpc.ServerMethodDefinition) ServerCallHandler(io.grpc.ServerCallHandler) ServerServiceDefinition(io.grpc.ServerServiceDefinition) Request(triple.Request) MethodDescriptor(io.grpc.MethodDescriptor)

Example 2 with Request

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;
    }
}
Also used : Request(triple.Request) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Method(java.lang.reflect.Method) ByteString(com.google.protobuf.ByteString) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) MethodDescriptor(io.grpc.MethodDescriptor) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Response(triple.Response) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Example 3 with Request

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();
}
Also used : ByteString(com.google.protobuf.ByteString) Request(triple.Request) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ByteString(com.google.protobuf.ByteString)

Example 4 with Request

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]);
}
Also used : Request(triple.Request) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 5 with Request

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;
}
Also used : Context(io.grpc.Context) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Request(triple.Request) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest)

Aggregations

Request (triple.Request)6 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)5 Method (java.lang.reflect.Method)3 ByteString (com.google.protobuf.ByteString)2 MethodDescriptor (io.grpc.MethodDescriptor)2 Test (org.junit.Test)2 Response (triple.Response)2 Serializer (com.alipay.sofa.rpc.codec.Serializer)1 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)1 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)1 Context (io.grpc.Context)1 ServerCallHandler (io.grpc.ServerCallHandler)1 ServerMethodDefinition (io.grpc.ServerMethodDefinition)1 ServerServiceDefinition (io.grpc.ServerServiceDefinition)1