Search in sources :

Example 21 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.

the class CloseCharacterChannel method execute.

/**
 * <p>
 * Closes a character channel.
 * </p>
 * <p>
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(CHARACTER_CHANNEL_INDEX);
    CharacterChannel charChannel = (CharacterChannel) channel.getNativeData(IOConstants.CHARACTER_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.close(charChannel, eventContext, CloseCharacterChannel::closeResponse);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)

Example 22 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext 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 23 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.

the class HasNextTextRecord method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(TXT_RECORD_CHANNEL_INDEX);
    if (channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME) != null) {
        DelimitedRecordChannel textRecordChannel = (DelimitedRecordChannel) channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME);
        EventContext eventContext = new EventContext(context, callback);
        HasNextDelimitedRecordEvent hasNextEvent = new HasNextDelimitedRecordEvent(textRecordChannel, eventContext);
        CompletableFuture<EventResult> event = EventManager.getInstance().publish(hasNextEvent);
        event.thenApply(HasNextTextRecord::response);
    }
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) HasNextDelimitedRecordEvent(org.ballerinalang.nativeimpl.io.events.records.HasNextDelimitedRecordEvent) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Example 24 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext 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 25 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext 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

EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)27 BStruct (org.ballerinalang.model.values.BStruct)20 Context (org.ballerinalang.bre.Context)11 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)11 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)6 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)5 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 ByteChannel (java.nio.channels.ByteChannel)3 BInteger (org.ballerinalang.model.values.BInteger)3 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)3 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)3 Test (org.testng.annotations.Test)3 ExecutionException (java.util.concurrent.ExecutionException)2 BStringArray (org.ballerinalang.model.values.BStringArray)2 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 List (java.util.List)1 BBlob (org.ballerinalang.model.values.BBlob)1