Search in sources :

Example 1 with GrpcBlockingStub

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

the class BlockingExecute 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 service stub " + "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");
    }
    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 GrpcBlockingStub) {
        BValue payloadBValue = context.getRefArgument(1);
        Message requestMsg = MessageUtils.generateProtoMessage(payloadBValue, methodDescriptor.getInputType());
        GrpcBlockingStub grpcBlockingStub = (GrpcBlockingStub) connectionStub;
        try {
            MethodDescriptor.MethodType methodType = getMethodType(methodDescriptor);
            // TODO : check whether we can support blocking server streaming.
            if (methodType.equals(MethodDescriptor.MethodType.UNARY)) {
                Message responseMsg = grpcBlockingStub.executeUnary(requestMsg, methodName);
                Descriptors.Descriptor outputDescriptor = methodDescriptor.getOutputType();
                BValue responseBValue = MessageUtils.generateRequestStruct(responseMsg, outputDescriptor.getName(), getBalType(outputDescriptor.getName(), context), context);
                context.setReturnValues(responseBValue);
                return;
            } else {
                notifyErrorReply(context, "Error while executing the client call. Method type " + methodType.name() + " not supported");
            }
        } catch (RuntimeException | GrpcClientException e) {
            notifyErrorReply(context, "gRPC Client Connector Error :" + e.getMessage());
        }
    }
    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) BValue(org.ballerinalang.model.values.BValue) GrpcBlockingStub(org.ballerinalang.net.grpc.stubs.GrpcBlockingStub) MethodDescriptor(io.grpc.MethodDescriptor) GrpcClientException(org.ballerinalang.net.grpc.exception.GrpcClientException) Descriptors(com.google.protobuf.Descriptors)

Example 2 with GrpcBlockingStub

use of org.ballerinalang.net.grpc.stubs.GrpcBlockingStub 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)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)2 BValue (org.ballerinalang.model.values.BValue)2 GrpcClientException (org.ballerinalang.net.grpc.exception.GrpcClientException)2 GrpcBlockingStub (org.ballerinalang.net.grpc.stubs.GrpcBlockingStub)2 Descriptors (com.google.protobuf.Descriptors)1 ManagedChannel (io.grpc.ManagedChannel)1 MethodDescriptor (io.grpc.MethodDescriptor)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 Message (org.ballerinalang.net.grpc.Message)1 GrpcNonBlockingStub (org.ballerinalang.net.grpc.stubs.GrpcNonBlockingStub)1 ProtoFileDefinition (org.ballerinalang.net.grpc.stubs.ProtoFileDefinition)1