Search in sources :

Example 1 with GrpcServerException

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

the class ServiceProtoBuilder method process.

@Override
public void process(ServiceNode serviceNode, List<AnnotationAttachmentNode> annotations) {
    for (AnnotationAttachmentNode annotationNode : annotations) {
        if (ANN_MESSAGE_LISTENER.equals(annotationNode.getAnnotationName().getValue())) {
            return;
        }
    }
    try {
        File fileDefinition = ServiceProtoUtils.generateProtoDefinition(serviceNode);
        ServiceConfiguration serviceConfig = ServiceProtoUtils.getServiceConfiguration(serviceNode);
        ServiceProtoUtils.writeServiceFiles(fileDefinition, serviceNode.getName().getValue(), serviceConfig.isGenerateClientConnector());
    } catch (GrpcServerException e) {
        dlog.logDiagnostic(Diagnostic.Kind.WARNING, serviceNode.getPosition(), e.getMessage());
    }
}
Also used : ServiceConfiguration(org.ballerinalang.net.grpc.config.ServiceConfiguration) File(org.ballerinalang.net.grpc.proto.definition.File) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) GrpcServerException(org.ballerinalang.net.grpc.exception.GrpcServerException)

Example 2 with GrpcServerException

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

the class ServiceProtoUtils method writeServiceFiles.

/**
 * Write protobuf file definition content to the filename.
 *
 * @param protoFileDefinition protobuf file definition.
 * @param filename            filename
 * @throws GrpcServerException when error occur while writing content to file.
 */
static void writeServiceFiles(File protoFileDefinition, String filename, boolean generateClientConnector) throws GrpcServerException {
    try {
        // write the proto string to the file in protobuf contract directory
        Path protoFilePath = Paths.get(filename + ServiceProtoConstants.PROTO_FILE_EXTENSION);
        Files.write(protoFilePath, protoFileDefinition.getFileDefinition().getBytes(ServiceProtoConstants.UTF_8_CHARSET));
        // write the proto descriptor byte array to the file in protobuf contract directory
        byte[] fileDescriptor = protoFileDefinition.getFileDescriptorProto().toByteArray();
        Path descFilePath = Paths.get(filename + ServiceProtoConstants.DESC_FILE_EXTENSION);
        Files.write(descFilePath, fileDescriptor);
        if (generateClientConnector) {
            List<byte[]> dependentDescriptorsList = new ArrayList<>();
            dependentDescriptorsList.add(com.google.protobuf.WrappersProto.getDescriptor().toProto().toByteArray());
            // Path path = Paths.get("");
            BallerinaFileBuilder ballerinaFileBuilder = new BallerinaFileBuilder(dependentDescriptorsList);
            ballerinaFileBuilder.setRootDescriptor(fileDescriptor);
            ballerinaFileBuilder.build();
        }
    } catch (IOException e) {
        throw new GrpcServerException("Error while writing file descriptor to file.", e);
    }
}
Also used : Path(java.nio.file.Path) BallerinaFileBuilder(org.ballerinalang.net.grpc.builder.BallerinaFileBuilder) ArrayList(java.util.ArrayList) IOException(java.io.IOException) GrpcServerException(org.ballerinalang.net.grpc.exception.GrpcServerException)

Example 3 with GrpcServerException

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

the class GetClient method execute.

@Override
public void execute(Context context) {
    BStruct serviceEndpoint = (BStruct) context.getRefArgument(SERVICE_ENDPOINT_INDEX);
    // Service client responder is populated in method listener. There we create new service endpoint instance
    // and bind client responder instance.
    BRefType clientType = serviceEndpoint.getRefField(CLIENT_RESPONDER_REF_INDEX);
    if (clientType instanceof BStruct) {
        BStruct endpointClient = (BStruct) clientType;
        context.setReturnValues(endpointClient);
    } else {
        context.setError(MessageUtils.getConnectorError(context, new GrpcServerException("Error while " + "retrieving endpoint client.")));
    }
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BStruct(org.ballerinalang.model.values.BStruct) GrpcServerException(org.ballerinalang.net.grpc.exception.GrpcServerException)

Example 4 with GrpcServerException

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

the class Register method execute.

@Override
public void execute(Context context) {
    BStruct serviceEndpoint = (BStruct) context.getRefArgument(SERVICE_ENDPOINT_INDEX);
    Service service = BLangConnectorSPIUtil.getServiceRegistered(context);
    io.grpc.ServerBuilder serverBuilder = getServiceBuilder(serviceEndpoint);
    try {
        registerService(serverBuilder, service);
        context.setReturnValues();
    } catch (GrpcServerException e) {
        context.setError(MessageUtils.getConnectorError(context, e));
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Service(org.ballerinalang.connector.api.Service) GrpcServicesBuilder.registerService(org.ballerinalang.net.grpc.GrpcServicesBuilder.registerService) GrpcServerException(org.ballerinalang.net.grpc.exception.GrpcServerException)

Example 5 with GrpcServerException

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

the class Start method execute.

@Override
public void execute(Context context) {
    BStruct serviceEndpoint = (BStruct) context.getRefArgument(SERVICE_ENDPOINT_INDEX);
    io.grpc.ServerBuilder serverBuilder = getServiceBuilder(serviceEndpoint);
    try {
        Server server = GrpcServicesBuilder.start(serverBuilder);
        serviceEndpoint.addNativeData(GRPC_SERVER, server);
        Runtime.getRuntime().addShutdownHook(new Thread(() -> stop(server)));
        console.println("ballerina: started gRPC server connector on port " + server.getPort());
        GrpcServicesBuilder.blockUntilShutdown(server);
    } catch (GrpcServerException e) {
        context.setError(MessageUtils.getConnectorError(context, new GrpcServerException("Error in starting gRPC " + "service.", e)));
    } catch (InterruptedException e) {
        context.setError(MessageUtils.getConnectorError(context, new GrpcServerException("gRPC server " + "interrupted", e)));
    }
    context.setReturnValues();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Server(io.grpc.Server) 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