Search in sources :

Example 16 with CallableUnitCallback

use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.

the class BLangFunctions method invokeNativeCallable.

private static WorkerExecutionContext invokeNativeCallable(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs, int flags) {
    WorkerData parentLocalData = parentCtx.workerLocal;
    BType[] retTypes = callableUnitInfo.getRetParamTypes();
    WorkerData caleeSF = BLangVMUtils.createWorkerDataForLocal(callableUnitInfo.getDefaultWorkerInfo(), parentCtx, argRegs, callableUnitInfo.getParamTypes());
    Context ctx = new NativeCallContext(parentCtx, callableUnitInfo, caleeSF);
    NativeCallableUnit nativeCallable = callableUnitInfo.getNativeCallableUnit();
    if (nativeCallable == null) {
        return parentCtx;
    }
    try {
        if (nativeCallable.isBlocking()) {
            nativeCallable.execute(ctx, null);
            BLangVMUtils.populateWorkerDataWithValues(parentLocalData, retRegs, ctx.getReturnValues(), retTypes);
            if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
                TraceUtil.finishTraceSpan(TraceUtil.getTracer(parentCtx));
            }
            /* we want the parent to continue, since we got the response of the native call already */
            return parentCtx;
        } else {
            CallableUnitCallback callback;
            if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
                callback = new TraceableCallbackWrapper(parentCtx, new BLangCallableUnitCallback(ctx, parentCtx, retRegs, retTypes));
            } else {
                callback = new BLangCallableUnitCallback(ctx, parentCtx, retRegs, retTypes);
            }
            nativeCallable.execute(ctx, callback);
            /* we want the parent to suspend (i.e. go to wait for response state) and stay until notified */
            return null;
        }
    } catch (BLangNullReferenceException e) {
        return BLangVMUtils.handleNativeInvocationError(parentCtx, BLangVMErrors.createNullRefException(callableUnitInfo));
    } catch (Throwable e) {
        return BLangVMUtils.handleNativeInvocationError(parentCtx, BLangVMErrors.createError(callableUnitInfo, e.getMessage()));
    }
}
Also used : InitWorkerResponseContext(org.ballerinalang.bre.bvm.InitWorkerResponseContext) SyncCallableWorkerResponseContext(org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext) NativeCallContext(org.ballerinalang.bre.NativeCallContext) ForkJoinWorkerResponseContext(org.ballerinalang.bre.bvm.ForkJoinWorkerResponseContext) WorkerResponseContext(org.ballerinalang.bre.bvm.WorkerResponseContext) Context(org.ballerinalang.bre.Context) CallableWorkerResponseContext(org.ballerinalang.bre.bvm.CallableWorkerResponseContext) WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) AsyncInvocableWorkerResponseContext(org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext) BLangCallableUnitCallback(org.ballerinalang.bre.BLangCallableUnitCallback) BType(org.ballerinalang.model.types.BType) TraceableCallbackWrapper(org.ballerinalang.util.tracer.TraceableCallbackWrapper) BLangNullReferenceException(org.ballerinalang.util.exceptions.BLangNullReferenceException) NativeCallContext(org.ballerinalang.bre.NativeCallContext) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) WorkerData(org.ballerinalang.bre.bvm.WorkerData) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback) BLangCallableUnitCallback(org.ballerinalang.bre.BLangCallableUnitCallback)

Example 17 with CallableUnitCallback

use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.

the class Close method closeResponse.

private static EventResult closeResponse(EventResult<Boolean, EventContext> result) {
    BStruct errorStruct = null;
    EventContext eventContext = result.getContext();
    Context context = eventContext.getContext();
    CallableUnitCallback callback = eventContext.getCallback();
    Throwable error = eventContext.getError();
    if (null != error) {
        errorStruct = IOUtils.createError(context, error.getMessage());
    }
    context.setReturnValues(errorStruct);
    callback.notifySuccess();
    return result;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 18 with CallableUnitCallback

use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.

the class CloseDelimitedRecordChannel method closeResponse.

private static EventResult closeResponse(EventResult<Boolean, EventContext> result) {
    BStruct errorStruct = null;
    EventContext eventContext = result.getContext();
    Context context = eventContext.getContext();
    CallableUnitCallback callback = eventContext.getCallback();
    Throwable error = eventContext.getError();
    if (null != error) {
        errorStruct = IOUtils.createError(context, error.getMessage());
    }
    context.setReturnValues(errorStruct);
    callback.notifySuccess();
    return result;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 19 with CallableUnitCallback

use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.

the class NextTextRecord method response.

/*
     * Response obtained after reading record.
     *
     * @param result the result obtained after processing the record.
     * @return the response obtained after reading record.
     */
private static EventResult response(EventResult<String[], EventContext> result) {
    BStruct errorStruct = null;
    EventContext eventContext = result.getContext();
    Context context = eventContext.getContext();
    String[] fields = result.getResponse();
    CallableUnitCallback callback = eventContext.getCallback();
    Throwable error = eventContext.getError();
    if (null != error) {
        errorStruct = IOUtils.createError(context, error.getMessage());
        context.setReturnValues(errorStruct);
    } else {
        context.setReturnValues(new BStringArray(fields));
    }
    callback.notifySuccess();
    return result;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) BStringArray(org.ballerinalang.model.values.BStringArray) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 20 with CallableUnitCallback

use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.

the class Read method readResponse.

/*
     * Function which will be notified on the response obtained after the async operation.
     *
     * @param result context of the callback.
     * @return Once the callback is processed we further return back the result.
     */
private static EventResult readResponse(EventResult<Integer, EventContext> result) {
    BStruct errorStruct;
    BRefValueArray contentTuple = new BRefValueArray(readTupleType);
    EventContext eventContext = result.getContext();
    Context context = eventContext.getContext();
    Throwable error = eventContext.getError();
    Integer numberOfBytes = result.getResponse();
    CallableUnitCallback callback = eventContext.getCallback();
    byte[] content = (byte[]) eventContext.getProperties().get(ReadBytesEvent.CONTENT_PROPERTY);
    if (null != error) {
        errorStruct = IOUtils.createError(context, error.getMessage());
        context.setReturnValues(errorStruct);
    } else {
        contentTuple.add(0, new BBlob(content));
        contentTuple.add(1, new BInteger(numberOfBytes));
        context.setReturnValues(contentTuple);
    }
    callback.notifySuccess();
    return result;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BInteger(org.ballerinalang.model.values.BInteger) BStruct(org.ballerinalang.model.values.BStruct) BInteger(org.ballerinalang.model.values.BInteger) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BBlob(org.ballerinalang.model.values.BBlob) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Aggregations

CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)21 BStruct (org.ballerinalang.model.values.BStruct)15 Context (org.ballerinalang.bre.Context)12 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)11 BValue (org.ballerinalang.model.values.BValue)9 ParamDetail (org.ballerinalang.connector.api.ParamDetail)8 GrpcCallableUnitCallBack (org.ballerinalang.net.grpc.GrpcCallableUnitCallBack)7 Resource (org.ballerinalang.connector.api.Resource)6 BString (org.ballerinalang.model.values.BString)5 BType (org.ballerinalang.model.types.BType)4 BInteger (org.ballerinalang.model.values.BInteger)3 StreamObserver (io.grpc.stub.StreamObserver)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 BLangConnectorSPIUtil (org.ballerinalang.connector.api.BLangConnectorSPIUtil)2 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)2 Executor (org.ballerinalang.connector.api.Executor)2 Message (org.ballerinalang.net.grpc.Message)2 PROTOCOL_PACKAGE_HTTP (org.ballerinalang.net.http.HttpConstants.PROTOCOL_PACKAGE_HTTP)2