Search in sources :

Example 1 with GrpcNonBlockingStub

use of org.ballerinalang.net.grpc.stubs.GrpcNonBlockingStub 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 2 with GrpcNonBlockingStub

use of org.ballerinalang.net.grpc.stubs.GrpcNonBlockingStub in project ballerina by ballerina-lang.

the class InitStub method execute.

@Override
public void execute(Context context) {
    BStruct serviceStub = (BStruct) context.getRefArgument(SERVICE_STUB_REF_INDEX);
    BStruct clientEndpoint = (BStruct) context.getRefArgument(CLIENT_ENDPOINT_REF_INDEX);
    ManagedChannel channel = (ManagedChannel) clientEndpoint.getNativeData(CHANNEL_KEY);
    String stubtype = context.getStringArgument(STUB_TYPE_STRING_INDEX);
    String descriptorKey = context.getStringArgument(DESCRIPTOR_KEY_STRING_INDEX);
    BMap<String, BValue> descriptorMap = (BMap<String, BValue>) context.getRefArgument(DESCRIPTOR_MAP_REF_INDEX);
    if (stubtype == null || descriptorKey == null || descriptorMap == null) {
        context.setError(MessageUtils.getConnectorError(context, new StatusRuntimeException(Status.fromCode(Status.INTERNAL.getCode()).withDescription("Error while initializing connector. " + "message descriptor keys not exist. Please check the generated sub file"))));
        return;
    }
    try {
        // If there are more than one descriptors exist, other descriptors are considered as dependent
        // descriptors.  client supported only one depth descriptor dependency.
        List<byte[]> depDescriptorData = new ArrayList<>();
        byte[] descriptorValue = null;
        for (String key : descriptorMap.keySet()) {
            if (descriptorMap.get(key) == null) {
                continue;
            }
            if (descriptorKey.equals(key)) {
                descriptorValue = hexStringToByteArray(descriptorMap.get(key).stringValue());
            } else {
                depDescriptorData.add(hexStringToByteArray(descriptorMap.get(key).stringValue()));
            }
        }
        if (descriptorValue == null) {
            context.setError(MessageUtils.getConnectorError(context, new StatusRuntimeException(Status.fromCode(Status.INTERNAL.getCode()).withDescription("Error while establishing the connection" + ". service descriptor is null."))));
            return;
        }
        ProtoFileDefinition protoFileDefinition = new ProtoFileDefinition(depDescriptorData);
        protoFileDefinition.setRootDescriptorData(descriptorValue);
        ClientConnectorFactory clientConnectorFactory = new ClientConnectorFactory(protoFileDefinition);
        if (BLOCKING_TYPE.equalsIgnoreCase(stubtype)) {
            GrpcBlockingStub grpcBlockingStub = clientConnectorFactory.newBlockingStub(channel);
            serviceStub.addNativeData(SERVICE_STUB, grpcBlockingStub);
        } else if (NON_BLOCKING_TYPE.equalsIgnoreCase(stubtype)) {
            GrpcNonBlockingStub nonBlockingStub = clientConnectorFactory.newNonBlockingStub(channel);
            serviceStub.addNativeData(SERVICE_STUB, nonBlockingStub);
        } else {
            context.setError(MessageUtils.getConnectorError(context, new StatusRuntimeException(Status.fromCode(Status.INTERNAL.getCode()).withDescription("Error while initializing connector. " + "invalid connector type"))));
            return;
        }
        serviceStub.addNativeData(CLIENT_END_POINT, clientEndpoint);
    } catch (RuntimeException | GrpcClientException e) {
        context.setError(MessageUtils.getConnectorError(context, e));
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) ArrayList(java.util.ArrayList) GrpcBlockingStub(org.ballerinalang.net.grpc.stubs.GrpcBlockingStub) StatusRuntimeException(io.grpc.StatusRuntimeException) ProtoFileDefinition(org.ballerinalang.net.grpc.stubs.ProtoFileDefinition) GrpcClientException(org.ballerinalang.net.grpc.exception.GrpcClientException) StatusRuntimeException(io.grpc.StatusRuntimeException) ManagedChannel(io.grpc.ManagedChannel) GrpcNonBlockingStub(org.ballerinalang.net.grpc.stubs.GrpcNonBlockingStub) ClientConnectorFactory(org.ballerinalang.net.grpc.ClientConnectorFactory)

Example 3 with GrpcNonBlockingStub

use of org.ballerinalang.net.grpc.stubs.GrpcNonBlockingStub in project ballerina by ballerina-lang.

the class NonBlockingExecute 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) {
        BValue payloadBValue = context.getRefArgument(1);
        Message requestMsg = MessageUtils.generateProtoMessage(payloadBValue, methodDescriptor.getInputType());
        GrpcNonBlockingStub grpcNonBlockingStub = (GrpcNonBlockingStub) connectionStub;
        BTypeDescValue serviceType = (BTypeDescValue) context.getRefArgument(2);
        Service callbackService = BLangConnectorSPIUtil.getServiceFromType(context.getProgramFile(), getTypeField(serviceType));
        try {
            MethodDescriptor.MethodType methodType = getMethodType(methodDescriptor);
            if (methodType.equals(MethodDescriptor.MethodType.UNARY)) {
                grpcNonBlockingStub.executeUnary(requestMsg, new DefaultStreamObserver(callbackService), methodName);
            } else if (methodType.equals(MethodDescriptor.MethodType.SERVER_STREAMING)) {
                grpcNonBlockingStub.executeServerStreaming(requestMsg, new DefaultStreamObserver(callbackService), methodName);
            } else {
                notifyErrorReply(context, "Error while executing the client call. Method type " + methodType.name() + " not supported");
                return;
            }
            context.setReturnValues();
            return;
        } catch (RuntimeException | GrpcClientException e) {
            notifyErrorReply(context, "gRPC Client Connector Error :" + e.getMessage());
            return;
        }
    }
    notifyErrorReply(context, "Error while processing the request message. Connection Sub " + "type not supported");
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Message(org.ballerinalang.net.grpc.Message) DefaultStreamObserver(org.ballerinalang.net.grpc.stubs.DefaultStreamObserver) BValue(org.ballerinalang.model.values.BValue) 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)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)3 GrpcClientException (org.ballerinalang.net.grpc.exception.GrpcClientException)3 GrpcNonBlockingStub (org.ballerinalang.net.grpc.stubs.GrpcNonBlockingStub)3 MethodDescriptor (io.grpc.MethodDescriptor)2 Service (org.ballerinalang.connector.api.Service)2 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)2 BValue (org.ballerinalang.model.values.BValue)2 Message (org.ballerinalang.net.grpc.Message)2 DefaultStreamObserver (org.ballerinalang.net.grpc.stubs.DefaultStreamObserver)2 ManagedChannel (io.grpc.ManagedChannel)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 ArrayList (java.util.ArrayList)1 BMap (org.ballerinalang.model.values.BMap)1 ClientConnectorFactory (org.ballerinalang.net.grpc.ClientConnectorFactory)1 GrpcBlockingStub (org.ballerinalang.net.grpc.stubs.GrpcBlockingStub)1 ProtoFileDefinition (org.ballerinalang.net.grpc.stubs.ProtoFileDefinition)1