Search in sources :

Example 1 with ClientStreamingListener

use of org.ballerinalang.net.grpc.listener.ClientStreamingListener in project ballerina by ballerina-lang.

the class GrpcServicesBuilder method getServiceDefinition.

private static ServerServiceDefinition getServiceDefinition(Service service, Descriptors.ServiceDescriptor serviceDescriptor) throws GrpcServerException {
    // Generate full service name for the service definition. <package>.<service>
    final String serviceName;
    if (ServiceProtoConstants.CLASSPATH_SYMBOL.equals(service.getPackage())) {
        serviceName = service.getName();
    } else {
        serviceName = service.getPackage() + ServiceProtoConstants.CLASSPATH_SYMBOL + service.getName();
    }
    // Server Definition Builder for the service.
    Builder serviceDefBuilder = ServerServiceDefinition.builder(serviceName);
    for (Descriptors.MethodDescriptor methodDescriptor : serviceDescriptor.getMethods()) {
        final String methodName = serviceName + FILE_SEPARATOR + methodDescriptor.getName();
        Descriptors.Descriptor requestDescriptor = serviceDescriptor.findMethodByName(methodDescriptor.getName()).getInputType();
        Descriptors.Descriptor responseDescriptor = serviceDescriptor.findMethodByName(methodDescriptor.getName()).getOutputType();
        MessageRegistry.getInstance().addMessageDescriptor(requestDescriptor.getName(), requestDescriptor);
        MessageRegistry.getInstance().addMessageDescriptor(responseDescriptor.getName(), responseDescriptor);
        MethodDescriptor.Marshaller<Message> reqMarshaller = ProtoUtils.marshaller(Message.newBuilder(requestDescriptor.getName()).build());
        MethodDescriptor.Marshaller<Message> resMarshaller = ProtoUtils.marshaller(Message.newBuilder(responseDescriptor.getName()).build());
        MethodDescriptor.MethodType methodType;
        ServerCallHandler<Message, Message> serverCallHandler;
        Map<String, Resource> resourceMap = new HashMap<>();
        Resource mappedResource = null;
        for (Resource resource : service.getResources()) {
            if (methodDescriptor.getName().equals(resource.getName())) {
                mappedResource = resource;
            }
            resourceMap.put(resource.getName(), resource);
        }
        if (methodDescriptor.toProto().getServerStreaming() && methodDescriptor.toProto().getClientStreaming()) {
            methodType = MethodDescriptor.MethodType.BIDI_STREAMING;
            serverCallHandler = ServerCalls.asyncBidiStreamingCall(new BidirectionalStreamingListener(methodDescriptor, resourceMap));
        } else if (methodDescriptor.toProto().getClientStreaming()) {
            methodType = MethodDescriptor.MethodType.CLIENT_STREAMING;
            serverCallHandler = ServerCalls.asyncClientStreamingCall(new ClientStreamingListener(methodDescriptor, resourceMap));
        } else if (methodDescriptor.toProto().getServerStreaming()) {
            methodType = MethodDescriptor.MethodType.SERVER_STREAMING;
            serverCallHandler = ServerCalls.asyncServerStreamingCall(new ServerStreamingListener(methodDescriptor, mappedResource));
        } else {
            methodType = MethodDescriptor.MethodType.UNARY;
            serverCallHandler = ServerCalls.asyncUnaryCall(new UnaryMethodListener(methodDescriptor, mappedResource));
        }
        MethodDescriptor.Builder<Message, Message> methodBuilder = MethodDescriptor.newBuilder();
        MethodDescriptor<Message, Message> grpcMethodDescriptor = methodBuilder.setType(methodType).setFullMethodName(methodName).setRequestMarshaller(reqMarshaller).setResponseMarshaller(resMarshaller).setSchemaDescriptor(methodDescriptor).build();
        serviceDefBuilder.addMethod(grpcMethodDescriptor, serverCallHandler);
    }
    return serviceDefBuilder.build();
}
Also used : HashMap(java.util.HashMap) UnaryMethodListener(org.ballerinalang.net.grpc.listener.UnaryMethodListener) NettyServerBuilder(io.grpc.netty.NettyServerBuilder) Builder(io.grpc.ServerServiceDefinition.Builder) Resource(org.ballerinalang.connector.api.Resource) MethodDescriptor(io.grpc.MethodDescriptor) ServerStreamingListener(org.ballerinalang.net.grpc.listener.ServerStreamingListener) ClientStreamingListener(org.ballerinalang.net.grpc.listener.ClientStreamingListener) Descriptors(com.google.protobuf.Descriptors) BidirectionalStreamingListener(org.ballerinalang.net.grpc.listener.BidirectionalStreamingListener)

Aggregations

Descriptors (com.google.protobuf.Descriptors)1 MethodDescriptor (io.grpc.MethodDescriptor)1 Builder (io.grpc.ServerServiceDefinition.Builder)1 NettyServerBuilder (io.grpc.netty.NettyServerBuilder)1 HashMap (java.util.HashMap)1 Resource (org.ballerinalang.connector.api.Resource)1 BidirectionalStreamingListener (org.ballerinalang.net.grpc.listener.BidirectionalStreamingListener)1 ClientStreamingListener (org.ballerinalang.net.grpc.listener.ClientStreamingListener)1 ServerStreamingListener (org.ballerinalang.net.grpc.listener.ServerStreamingListener)1 UnaryMethodListener (org.ballerinalang.net.grpc.listener.UnaryMethodListener)1