Search in sources :

Example 6 with GrpcServerException

use of org.ballerinalang.net.grpc.exception.GrpcServerException in project ballerina by ballerina-lang.

the class ServiceProtoUtils method getStreamingServiceDefinition.

private static Service getStreamingServiceDefinition(ServiceNode serviceNode, ServiceConfiguration serviceConfig, File.Builder fileBuilder) throws GrpcServerException {
    // Protobuf service definition builder.
    Service.Builder serviceBuilder = Service.newBuilder(serviceNode.getName().getValue());
    Message requestMessage = null;
    Message responseMessage = null;
    for (ResourceNode resourceNode : serviceNode.getResources()) {
        if (ON_MESSAGE_RESOURCE.equals(resourceNode.getName().getValue())) {
            requestMessage = getRequestMessage(resourceNode);
            Message respMsg = getResponseMessage(resourceNode);
            if (respMsg != null && !(MessageKind.EMPTY.equals(respMsg.getMessageKind()))) {
                responseMessage = getResponseMessage(resourceNode);
                break;
            }
        }
        if (ON_COMPLETE_RESOURCE.equals(resourceNode.getName().getValue())) {
            Message respMsg = getResponseMessage(resourceNode);
            if (respMsg != null && !(MessageKind.EMPTY.equals(respMsg.getMessageKind()))) {
                responseMessage = respMsg;
            }
        }
    }
    if (requestMessage == null) {
        throw new GrpcServerException("Cannot find request message definition for streaming service: " + serviceNode.getName().getValue());
    }
    if (responseMessage == null) {
        responseMessage = generateMessageDefinition(new BNullType());
    /*throw new GrpcServerException("Cannot find response message definition for streaming service: " +
                    serviceNode.getName().getValue());*/
    }
    if (requestMessage.getMessageKind() == MessageKind.USER_DEFINED) {
        if (!fileBuilder.getRegisteredMessages().contains(requestMessage.getDescriptorProto())) {
            fileBuilder.setMessage(requestMessage);
        }
    }
    if (requestMessage.getDependency() != null) {
        if (!fileBuilder.getRegisteredDependencies().contains(requestMessage.getDependency())) {
            fileBuilder.setDependeny(requestMessage.getDependency());
        }
    }
    if (responseMessage.getDependency() != null) {
        if (!fileBuilder.getRegisteredDependencies().contains(responseMessage.getDependency())) {
            fileBuilder.setDependeny(responseMessage.getDependency());
        }
    }
    Method resourceMethod = Method.newBuilder(serviceConfig.getRpcEndpoint()).setClientStreaming(serviceConfig.isClientStreaming()).setServerStreaming(serviceConfig.isServerStreaming()).setInputType(requestMessage.getMessageType()).setOutputType(responseMessage.getMessageType()).build();
    serviceBuilder.addMethod(resourceMethod);
    return serviceBuilder.build();
}
Also used : Message(org.ballerinalang.net.grpc.proto.definition.Message) EmptyMessage(org.ballerinalang.net.grpc.proto.definition.EmptyMessage) WrapperMessage(org.ballerinalang.net.grpc.proto.definition.WrapperMessage) UserDefinedMessage(org.ballerinalang.net.grpc.proto.definition.UserDefinedMessage) Service(org.ballerinalang.net.grpc.proto.definition.Service) Method(org.ballerinalang.net.grpc.proto.definition.Method) BNullType(org.wso2.ballerinalang.compiler.semantics.model.types.BNullType) ResourceNode(org.ballerinalang.model.tree.ResourceNode) GrpcServerException(org.ballerinalang.net.grpc.exception.GrpcServerException)

Example 7 with GrpcServerException

use of org.ballerinalang.net.grpc.exception.GrpcServerException in project ballerina by ballerina-lang.

the class ServiceProtoUtils method getUnaryServiceDefinition.

private static Service getUnaryServiceDefinition(ServiceNode serviceNode, File.Builder fileBuilder) throws GrpcServerException {
    // Protobuf service definition builder.
    Service.Builder serviceBuilder = Service.newBuilder(serviceNode.getName().getValue());
    for (ResourceNode resourceNode : serviceNode.getResources()) {
        Message requestMessage = getRequestMessage(resourceNode);
        if (requestMessage.getMessageKind() == MessageKind.USER_DEFINED) {
            if (!fileBuilder.getRegisteredMessages().contains(requestMessage.getDescriptorProto())) {
                fileBuilder.setMessage(requestMessage);
            }
        }
        if (requestMessage.getDependency() != null) {
            if (!fileBuilder.getRegisteredDependencies().contains(requestMessage.getDependency())) {
                fileBuilder.setDependeny(requestMessage.getDependency());
            }
        }
        Message responseMessage = getResponseMessage(resourceNode);
        if (responseMessage == null) {
            throw new GrpcServerException("Connection send expression not found in resource body");
        }
        if (responseMessage.getMessageKind() == MessageKind.USER_DEFINED) {
            if (!fileBuilder.getRegisteredMessages().contains(responseMessage.getDescriptorProto())) {
                fileBuilder.setMessage(responseMessage);
            }
        }
        if (responseMessage.getDependency() != null) {
            if (!fileBuilder.getRegisteredDependencies().contains(responseMessage.getDependency())) {
                fileBuilder.setDependeny(responseMessage.getDependency());
            }
        }
        boolean serverStreaming = isServerStreaming(resourceNode);
        Method resourceMethod = Method.newBuilder(resourceNode.getName().getValue()).setClientStreaming(false).setServerStreaming(serverStreaming).setInputType(requestMessage.getMessageType()).setOutputType(responseMessage.getMessageType()).build();
        serviceBuilder.addMethod(resourceMethod);
    }
    return serviceBuilder.build();
}
Also used : Message(org.ballerinalang.net.grpc.proto.definition.Message) EmptyMessage(org.ballerinalang.net.grpc.proto.definition.EmptyMessage) WrapperMessage(org.ballerinalang.net.grpc.proto.definition.WrapperMessage) UserDefinedMessage(org.ballerinalang.net.grpc.proto.definition.UserDefinedMessage) Service(org.ballerinalang.net.grpc.proto.definition.Service) Method(org.ballerinalang.net.grpc.proto.definition.Method) ResourceNode(org.ballerinalang.model.tree.ResourceNode) GrpcServerException(org.ballerinalang.net.grpc.exception.GrpcServerException)

Aggregations

GrpcServerException (org.ballerinalang.net.grpc.exception.GrpcServerException)7 BStruct (org.ballerinalang.model.values.BStruct)3 ResourceNode (org.ballerinalang.model.tree.ResourceNode)2 EmptyMessage (org.ballerinalang.net.grpc.proto.definition.EmptyMessage)2 Message (org.ballerinalang.net.grpc.proto.definition.Message)2 Method (org.ballerinalang.net.grpc.proto.definition.Method)2 Service (org.ballerinalang.net.grpc.proto.definition.Service)2 UserDefinedMessage (org.ballerinalang.net.grpc.proto.definition.UserDefinedMessage)2 WrapperMessage (org.ballerinalang.net.grpc.proto.definition.WrapperMessage)2 Server (io.grpc.Server)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Service (org.ballerinalang.connector.api.Service)1 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)1 BRefType (org.ballerinalang.model.values.BRefType)1 GrpcServicesBuilder.registerService (org.ballerinalang.net.grpc.GrpcServicesBuilder.registerService)1 BallerinaFileBuilder (org.ballerinalang.net.grpc.builder.BallerinaFileBuilder)1 ServiceConfiguration (org.ballerinalang.net.grpc.config.ServiceConfiguration)1 File (org.ballerinalang.net.grpc.proto.definition.File)1