Search in sources :

Example 1 with Response

use of triple.Response 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 Response

use of triple.Response 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 Response

use of triple.Response in project sofa-rpc by sofastack.

the class GenericServiceImplTest method getReturnValue.

private Object getReturnValue(Method method) {
    Object appResponse = null;
    Response response = responseObserver.getValue();
    byte[] responseDate = response.getData().toByteArray();
    Class returnType = method.getReturnType();
    if (returnType != void.class) {
        if (responseDate != null && responseDate.length > 0) {
            Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
            appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
        }
    }
    return appResponse;
}
Also used : Response(triple.Response) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaHessianSerializer(com.alipay.sofa.rpc.codec.sofahessian.SofaHessianSerializer) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Example 4 with Response

use of triple.Response in project sofa-rpc by sofastack.

the class GenericServiceImpl method generic.

@Override
public void generic(Request request, StreamObserver<Response> responseObserver) {
    SofaRequest sofaRequest = TracingContextKey.getKeySofaRequest().get(Context.current());
    String methodName = sofaRequest.getMethodName();
    String interfaceName = sofaRequest.getInterfaceName();
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Class proxyClass = ClassTypeUtils.getClass(interfaceName);
        ClassLoader interfaceClassLoader = proxyClass.getClassLoader();
        Thread.currentThread().setContextClassLoader(interfaceClassLoader);
        Class[] argTypes = getArgTypes(request);
        Serializer serializer = SerializerFactory.getSerializer(request.getSerializeType());
        Method declaredMethod = proxyClass.getDeclaredMethod(methodName, argTypes);
        Object[] invokeArgs = getInvokeArgs(request, argTypes, serializer);
        // fill sofaRequest
        sofaRequest.setMethod(declaredMethod);
        sofaRequest.setMethodArgs(invokeArgs);
        sofaRequest.setMethodArgSigs(ClassTypeUtils.getTypeStrs(argTypes, true));
        SofaResponse response = invoker.invoke(sofaRequest);
        Object ret = getAppResponse(declaredMethod, response);
        Response.Builder builder = Response.newBuilder();
        builder.setSerializeType(request.getSerializeType());
        builder.setType(declaredMethod.getReturnType().getName());
        builder.setData(ByteString.copyFrom(serializer.encode(ret, null).array()));
        Response build = builder.build();
        responseObserver.onNext(build);
        responseObserver.onCompleted();
    } catch (Exception e) {
        LOGGER.error("Invoke " + methodName + " error:", e);
        throw new SofaRpcRuntimeException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ByteString(com.google.protobuf.ByteString) Method(java.lang.reflect.Method) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Response(triple.Response) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Aggregations

Response (triple.Response)4 Serializer (com.alipay.sofa.rpc.codec.Serializer)3 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)2 ByteString (com.google.protobuf.ByteString)2 MethodDescriptor (io.grpc.MethodDescriptor)2 Method (java.lang.reflect.Method)2 Request (triple.Request)2 SofaHessianSerializer (com.alipay.sofa.rpc.codec.sofahessian.SofaHessianSerializer)1 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)1 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)1 ServerCallHandler (io.grpc.ServerCallHandler)1 ServerMethodDefinition (io.grpc.ServerMethodDefinition)1 ServerServiceDefinition (io.grpc.ServerServiceDefinition)1