Search in sources :

Example 6 with MethodDescriptor

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project ballerina by ballerina-lang.

the class StreamingExecute method execute.

@Override
public void execute(Context context) {
    BStruct serviceStub = (BStruct) context.getRefArgument(SERVICE_STUB_REF_INDEX);
    if (serviceStub == null) {
        notifyErrorReply(context, "Error while getting connector. gRPC Client connector " + "is not initialized properly");
        return;
    }
    Object connectionStub = serviceStub.getNativeData(SERVICE_STUB);
    if (connectionStub == null) {
        notifyErrorReply(context, "Error while getting connection stub. gRPC Client connector " + "is not initialized properly");
        return;
    }
    String methodName = context.getStringArgument(0);
    if (methodName == null) {
        notifyErrorReply(context, "Error while processing the request. RPC endpoint doesn't " + "set properly");
        return;
    }
    com.google.protobuf.Descriptors.MethodDescriptor methodDescriptor = MessageRegistry.getInstance().getMethodDescriptor(methodName);
    if (methodDescriptor == null) {
        notifyErrorReply(context, "No registered method descriptor for '" + methodName + "'");
        return;
    }
    if (connectionStub instanceof GrpcNonBlockingStub) {
        GrpcNonBlockingStub grpcNonBlockingStub = (GrpcNonBlockingStub) connectionStub;
        BTypeDescValue serviceType = (BTypeDescValue) context.getRefArgument(1);
        Service callbackService = BLangConnectorSPIUtil.getServiceFromType(context.getProgramFile(), getTypeField(serviceType));
        try {
            MethodDescriptor.MethodType methodType = getMethodType(methodDescriptor);
            DefaultStreamObserver responseObserver = new DefaultStreamObserver(callbackService);
            StreamObserver<Message> requestSender;
            if (methodType.equals(MethodDescriptor.MethodType.CLIENT_STREAMING)) {
                requestSender = grpcNonBlockingStub.executeClientStreaming(responseObserver, methodName);
            } else if (methodType.equals(MethodDescriptor.MethodType.BIDI_STREAMING)) {
                requestSender = grpcNonBlockingStub.executeBidiStreaming(responseObserver, methodName);
            } else {
                notifyErrorReply(context, "Error while executing the client call. Method type " + methodType.name() + " not supported");
                return;
            }
            BStruct connStruct = createStruct(context, CLIENT_CONNECTION);
            connStruct.addNativeData(REQUEST_SENDER, requestSender);
            connStruct.addNativeData(REQUEST_MESSAGE_DEFINITION, methodDescriptor.getInputType());
            BStruct clientEndpoint = (BStruct) serviceStub.getNativeData(CLIENT_END_POINT);
            clientEndpoint.addNativeData(CLIENT_CONNECTION, connStruct);
            context.setReturnValues(clientEndpoint);
        } catch (RuntimeException | GrpcClientException e) {
            notifyErrorReply(context, "gRPC Client Connector Error :" + e.getMessage());
        }
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Message(org.ballerinalang.net.grpc.Message) DefaultStreamObserver(org.ballerinalang.net.grpc.stubs.DefaultStreamObserver) Service(org.ballerinalang.connector.api.Service) MethodDescriptor(io.grpc.MethodDescriptor) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue) GrpcClientException(org.ballerinalang.net.grpc.exception.GrpcClientException) GrpcNonBlockingStub(org.ballerinalang.net.grpc.stubs.GrpcNonBlockingStub)

Example 7 with MethodDescriptor

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project motan by weibocom.

the class GrpcUtil method getMethodDescriptorByAnnotation.

@SuppressWarnings("rawtypes")
public static HashMap<String, MethodDescriptor> getMethodDescriptorByAnnotation(Class<?> clazz, String serialization) throws Exception {
    String clazzName = getGrpcClassName(clazz);
    HashMap<String, MethodDescriptor> result = serviceMap.get(clazzName);
    if (result == null) {
        synchronized (serviceMap) {
            if (!serviceMap.containsKey(clazzName)) {
                ServiceDescriptor serviceDesc = getServiceDesc(getGrpcClassName(clazz));
                HashMap<String, MethodDescriptor> methodMap = new HashMap<String, MethodDescriptor>();
                for (MethodDescriptor<?, ?> methodDesc : serviceDesc.getMethods()) {
                    Method interfaceMethod = getMethod(methodDesc.getFullMethodName(), clazz);
                    if (JSON_CODEC.equals(serialization)) {
                        methodDesc = convertJsonDescriptor(methodDesc, interfaceMethod.getParameterTypes()[0], interfaceMethod.getReturnType());
                    }
                    methodMap.put(interfaceMethod.getName(), methodDesc);
                }
                serviceMap.put(clazzName, methodMap);
            }
            result = serviceMap.get(clazzName);
        }
    }
    return result;
}
Also used : ServiceDescriptor(io.grpc.ServiceDescriptor) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) MethodDescriptor(io.grpc.MethodDescriptor)

Example 8 with MethodDescriptor

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project webpieces by deanhiller.

the class GrpcJsonRoutes method loadRoutes.

@SuppressWarnings("rawtypes")
private void loadRoutes(RouteBuilder bldr, String baseUrl, ServiceMetaInfo meta) {
    try {
        // MUST use Thread context class loader in case we are in DevelopmentServer.
        // This class is loaded in AppClassLoader.  We need to be using CompilingClassLoader in dev server
        ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
        String controller = meta.getController().getName();
        // NOTE: If we do not do this, it will not recompile in the DevelopmentServer as it will use the original class
        Class grpcClazz = threadClassLoader.loadClass(meta.getGrpcService().getName());
        @SuppressWarnings("unchecked") Method method = grpcClazz.getMethod("getServiceDescriptor");
        ServiceDescriptor descriptor = (ServiceDescriptor) method.invoke(null);
        Collection<MethodDescriptor<?, ?>> methods = descriptor.getMethods();
        for (MethodDescriptor<?, ?> methodDescriptor : methods) {
            String fullMethodName = methodDescriptor.getFullMethodName();
            int index = fullMethodName.indexOf("/");
            String grpcMethod = fullMethodName.substring(index + 1, fullMethodName.length());
            String javaMethodName = grpcMethod.substring(0, 1).toLowerCase() + grpcMethod.substring(1);
            // the world is moving ALL to HTTPS so it's not bad to just do it all on https
            // POST is fine for rpc calls.  YES, some calls may be stricly get BUT this is rpc at this point so POST
            // this plugin is EASY to copy and fork and maintain your self so you can easily do that
            bldr.addContentRoute(HTTPS, POST, baseUrl + "/" + fullMethodName, controller + "." + javaMethodName);
        }
    } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw SneakyThrow.sneak(e);
    }
}
Also used : Method(java.lang.reflect.Method) MethodDescriptor(io.grpc.MethodDescriptor) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServiceDescriptor(io.grpc.ServiceDescriptor)

Example 9 with MethodDescriptor

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project alluxio by Alluxio.

the class GrpcSerializationUtils method overrideMethods.

/**
 * Creates a service definition that uses custom marshallers.
 *
 * @param service the service to intercept
 * @param marshallers a map that specifies which marshaller to use for each method
 * @return the new service definition
 */
public static ServerServiceDefinition overrideMethods(final ServerServiceDefinition service, final Map<MethodDescriptor, MethodDescriptor> marshallers) {
    List<ServerMethodDefinition<?, ?>> newMethods = new ArrayList<ServerMethodDefinition<?, ?>>();
    List<MethodDescriptor<?, ?>> newDescriptors = new ArrayList<MethodDescriptor<?, ?>>();
    // intercepts the descriptors
    for (final ServerMethodDefinition<?, ?> definition : service.getMethods()) {
        ServerMethodDefinition<?, ?> newMethod = interceptMethod(definition, marshallers);
        newDescriptors.add(newMethod.getMethodDescriptor());
        newMethods.add(newMethod);
    }
    // builds the new service descriptor
    final ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition.builder(new ServiceDescriptor(service.getServiceDescriptor().getName(), newDescriptors));
    // creates the new service definition
    for (ServerMethodDefinition<?, ?> definition : newMethods) {
        serviceBuilder.addMethod(definition);
    }
    return serviceBuilder.build();
}
Also used : ServerMethodDefinition(io.grpc.ServerMethodDefinition) ServiceDescriptor(io.grpc.ServiceDescriptor) ServerServiceDefinition(io.grpc.ServerServiceDefinition) ArrayList(java.util.ArrayList) MethodDescriptor(io.grpc.MethodDescriptor)

Example 10 with MethodDescriptor

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project alluxio by Alluxio.

the class GrpcSerializationUtils method interceptMethod.

private static <ReqT, RespT> ServerMethodDefinition<ReqT, RespT> interceptMethod(final ServerMethodDefinition<ReqT, RespT> definition, final Map<MethodDescriptor, MethodDescriptor> newMethods) {
    MethodDescriptor<ReqT, RespT> descriptor = definition.getMethodDescriptor();
    MethodDescriptor newMethod = newMethods.get(descriptor);
    if (newMethod != null) {
        return ServerMethodDefinition.create(newMethod, definition.getServerCallHandler());
    }
    return definition;
}
Also used : MethodDescriptor(io.grpc.MethodDescriptor)

Aggregations

MethodDescriptor (io.grpc.MethodDescriptor)35 CallOptions (io.grpc.CallOptions)20 Metadata (io.grpc.Metadata)19 Channel (io.grpc.Channel)18 Test (org.junit.Test)17 ClientInterceptor (io.grpc.ClientInterceptor)13 ClientCall (io.grpc.ClientCall)9 Status (io.grpc.Status)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 ByteString (com.google.protobuf.ByteString)6 ManagedChannel (io.grpc.ManagedChannel)6 Duration (com.google.protobuf.Duration)5 NoopClientCall (io.grpc.internal.NoopClientCall)5 SocketAddress (java.net.SocketAddress)5 SimpleForwardingClientCall (io.grpc.ForwardingClientCall.SimpleForwardingClientCall)4 SimpleForwardingClientCallListener (io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener)4 InetSocketAddress (java.net.InetSocketAddress)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 ClientStreamTracer (io.grpc.ClientStreamTracer)3 ForwardingClientCall (io.grpc.ForwardingClientCall)3