Search in sources :

Example 6 with Message

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

the class ClientStreamingListener method invoke.

@Override
public StreamObserver<Message> invoke(StreamObserver<Message> responseObserver) {
    Resource onOpen = resourceMap.get(MessageConstants.ON_OPEN_RESOURCE);
    List<ParamDetail> paramDetails = onOpen.getParamDetails();
    BValue[] signatureParams = new BValue[paramDetails.size()];
    signatureParams[0] = getConnectionParameter(onOpen, responseObserver);
    CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
    Executor.submit(onOpen, callback, null, null, signatureParams);
    return new StreamObserver<Message>() {

        @Override
        public void onNext(Message value) {
            Resource onMessage = resourceMap.get(MessageConstants.ON_MESSAGE_RESOURCE);
            List<ParamDetail> paramDetails = onMessage.getParamDetails();
            BValue[] signatureParams = new BValue[paramDetails.size()];
            signatureParams[0] = getConnectionParameter(onMessage, responseObserver);
            BValue requestParam = getRequestParameter(onMessage, value);
            if (requestParam != null) {
                signatureParams[1] = requestParam;
            }
            CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
            Executor.submit(onMessage, callback, null, null, signatureParams);
        }

        @Override
        public void onError(Throwable t) {
            Resource onError = resourceMap.get(MessageConstants.ON_ERROR_RESOURCE);
            if (onError == null) {
                String message = "Error in listener service definition. onError resource does not exists";
                LOG.error(message);
                throw new RuntimeException(message);
            }
            List<ParamDetail> paramDetails = onError.getParamDetails();
            BValue[] signatureParams = new BValue[paramDetails.size()];
            signatureParams[0] = getConnectionParameter(onError, responseObserver);
            if (paramDetails.size() != 2) {
                String message = "Error in onError resource definition. It must have two input params, but have " + paramDetails.size();
                LOG.error(message);
                throw new RuntimeException(message);
            }
            BType errorType = paramDetails.get(1).getVarType();
            BStruct errorStruct = MessageUtils.getConnectorError((BStructType) errorType, t);
            signatureParams[1] = errorStruct;
            CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
            Executor.submit(onError, callback, null, null, signatureParams);
        }

        @Override
        public void onCompleted() {
            Resource onCompleted = resourceMap.get(MessageConstants.ON_COMPLETE_RESOURCE);
            if (onCompleted == null) {
                String message = "Error in listener service definition. onError resource does not exists";
                LOG.error(message);
                throw new RuntimeException(message);
            }
            List<ParamDetail> paramDetails = onCompleted.getParamDetails();
            BValue[] signatureParams = new BValue[paramDetails.size()];
            signatureParams[0] = getConnectionParameter(onCompleted, responseObserver);
            CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, isEmptyResponse());
            Executor.submit(onCompleted, callback, null, null, signatureParams);
        }
    };
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) GrpcCallableUnitCallBack(org.ballerinalang.net.grpc.GrpcCallableUnitCallBack) BStruct(org.ballerinalang.model.values.BStruct) Message(org.ballerinalang.net.grpc.Message) BValue(org.ballerinalang.model.values.BValue) Resource(org.ballerinalang.connector.api.Resource) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BType(org.ballerinalang.model.types.BType) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 7 with Message

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

Example 8 with Message

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

the class Send method execute.

@Override
public void execute(Context context) {
    BStruct connectionStruct = (BStruct) context.getRefArgument(0);
    BValue responseValue = context.getRefArgument(1);
    StreamObserver requestSender = (StreamObserver) connectionStruct.getNativeData(REQUEST_SENDER);
    if (requestSender == null) {
        context.setError(MessageUtils.getConnectorError(context, new StatusRuntimeException(Status.fromCode(Status.INTERNAL.getCode()).withDescription("Error while initializing connector. " + "response sender doesnot exist"))));
    } else {
        Descriptors.Descriptor inputType = (Descriptors.Descriptor) connectionStruct.getNativeData(MessageConstants.REQUEST_MESSAGE_DEFINITION);
        try {
            Message requestMessage = MessageUtils.generateProtoMessage(responseValue, inputType);
            requestSender.onNext(requestMessage);
        } catch (Throwable e) {
            LOG.error("Error while sending client response.", e);
            context.setError(MessageUtils.getConnectorError(context, e));
        }
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) BStruct(org.ballerinalang.model.values.BStruct) Message(org.ballerinalang.net.grpc.Message) BValue(org.ballerinalang.model.values.BValue) StatusRuntimeException(io.grpc.StatusRuntimeException) Descriptors(com.google.protobuf.Descriptors)

Example 9 with Message

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

the class BidirectionalStreamingListener method invoke.

@Override
public StreamObserver<Message> invoke(StreamObserver<Message> responseObserver) {
    Resource onOpen = resourceMap.get(MessageConstants.ON_OPEN_RESOURCE);
    List<ParamDetail> paramDetails = onOpen.getParamDetails();
    BValue[] signatureParams = new BValue[paramDetails.size()];
    signatureParams[0] = getConnectionParameter(onOpen, responseObserver);
    CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
    Executor.submit(onOpen, callback, null, null, signatureParams);
    return new StreamObserver<Message>() {

        @Override
        public void onNext(Message value) {
            Resource onMessage = resourceMap.get(MessageConstants.ON_MESSAGE_RESOURCE);
            List<ParamDetail> paramDetails = onMessage.getParamDetails();
            BValue[] signatureParams = new BValue[paramDetails.size()];
            signatureParams[0] = getConnectionParameter(onMessage, responseObserver);
            BValue requestParam = getRequestParameter(onMessage, value);
            if (requestParam != null) {
                signatureParams[1] = requestParam;
            }
            CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, isEmptyResponse());
            Executor.submit(onMessage, callback, null, null, signatureParams);
        }

        @Override
        public void onError(Throwable t) {
            Resource onError = resourceMap.get(MessageConstants.ON_ERROR_RESOURCE);
            if (onError == null) {
                String message = "Error in listener service definition. onError resource does not exists";
                LOG.error(message);
                throw new RuntimeException(message);
            }
            List<ParamDetail> paramDetails = onError.getParamDetails();
            BValue[] signatureParams = new BValue[paramDetails.size()];
            signatureParams[0] = getConnectionParameter(onError, responseObserver);
            if (paramDetails.size() != 2) {
                String message = "Error in onError resource definition. It must have two input params, but have " + paramDetails.size();
                LOG.error(message);
                throw new RuntimeException(message);
            }
            BType errorType = paramDetails.get(1).getVarType();
            BStruct errorStruct = MessageUtils.getConnectorError((BStructType) errorType, t);
            signatureParams[1] = errorStruct;
            CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
            Executor.submit(onError, callback, null, null, signatureParams);
        }

        @Override
        public void onCompleted() {
            Resource onCompleted = resourceMap.get(MessageConstants.ON_COMPLETE_RESOURCE);
            if (onCompleted == null) {
                String message = "Error in listener service definition. onError resource does not exists";
                LOG.error(message);
                throw new RuntimeException(message);
            }
            List<ParamDetail> paramDetails = onCompleted.getParamDetails();
            BValue[] signatureParams = new BValue[paramDetails.size()];
            signatureParams[0] = getConnectionParameter(onCompleted, responseObserver);
            CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
            Executor.submit(onCompleted, callback, null, null, signatureParams);
        }
    };
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) GrpcCallableUnitCallBack(org.ballerinalang.net.grpc.GrpcCallableUnitCallBack) BStruct(org.ballerinalang.model.values.BStruct) Message(org.ballerinalang.net.grpc.Message) BValue(org.ballerinalang.model.values.BValue) Resource(org.ballerinalang.connector.api.Resource) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BType(org.ballerinalang.model.types.BType) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)9 Message (org.ballerinalang.net.grpc.Message)9 BValue (org.ballerinalang.model.values.BValue)8 BType (org.ballerinalang.model.types.BType)4 Descriptors (com.google.protobuf.Descriptors)3 MethodDescriptor (io.grpc.MethodDescriptor)3 StreamObserver (io.grpc.stub.StreamObserver)3 GrpcClientException (org.ballerinalang.net.grpc.exception.GrpcClientException)3 StatusRuntimeException (io.grpc.StatusRuntimeException)2 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)2 ParamDetail (org.ballerinalang.connector.api.ParamDetail)2 Resource (org.ballerinalang.connector.api.Resource)2 Service (org.ballerinalang.connector.api.Service)2 BStructType (org.ballerinalang.model.types.BStructType)2 BBoolean (org.ballerinalang.model.values.BBoolean)2 BFloat (org.ballerinalang.model.values.BFloat)2 BInteger (org.ballerinalang.model.values.BInteger)2 BRefType (org.ballerinalang.model.values.BRefType)2 BString (org.ballerinalang.model.values.BString)2 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)2