Search in sources :

Example 6 with EventContext

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

the class NextTextRecord method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(TXT_RECORD_CHANNEL_INDEX);
    DelimitedRecordChannel delimitedRecordChannel = (DelimitedRecordChannel) channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.read(delimitedRecordChannel, eventContext, NextTextRecord::response);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Example 7 with EventContext

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

the class ReadCharacters method execute.

/**
 * <p>
 * Reads characters from the channel.
 * </p>
 * <p>
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(CHAR_CHANNEL_INDEX);
    long numberOfCharacters = context.getIntArgument(NUMBER_OF_CHARS_INDEX);
    CharacterChannel characterChannel = (CharacterChannel) channel.getNativeData(IOConstants.CHARACTER_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.read(characterChannel, (int) numberOfCharacters, eventContext, ReadCharacters::readCharactersResponse);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)

Example 8 with EventContext

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

the class Write method writeResponse.

/*
     * 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 writeResponse(EventResult<Integer, EventContext> result) {
    BStruct errorStruct = null;
    EventContext eventContext = result.getContext();
    Context context = eventContext.getContext();
    Throwable error = eventContext.getError();
    Integer numberOfBytesWritten = result.getResponse();
    CallableUnitCallback callback = eventContext.getCallback();
    if (null != error) {
        errorStruct = IOUtils.createError(context, error.getMessage());
        context.setReturnValues(errorStruct);
    } else {
        context.setReturnValues(new BInteger(numberOfBytesWritten));
    }
    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) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 9 with EventContext

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

the class Write method execute.

/**
 * Writes bytes to a given channel.
 * <p>
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(BYTE_CHANNEL_INDEX);
    byte[] content = context.getBlobArgument(CONTENT_INDEX);
    int offset = (int) context.getIntArgument(START_OFFSET_INDEX);
    Channel byteChannel = (Channel) channel.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.write(byteChannel, content, offset, eventContext, Write::writeResponse);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel)

Example 10 with EventContext

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

the class WriteCharacters method writeResponse.

/**
 * Processors the response after reading characters.
 *
 * @param result the response returned after reading characters.
 * @return the response returned from the event.
 */
private static EventResult writeResponse(EventResult<Integer, EventContext> result) {
    BStruct errorStruct = null;
    EventContext eventContext = result.getContext();
    Integer numberOfCharactersWritten = result.getResponse();
    Context context = eventContext.getContext();
    CallableUnitCallback callback = eventContext.getCallback();
    Throwable error = eventContext.getError();
    if (null != error) {
        errorStruct = IOUtils.createError(context, error.getMessage());
        context.setReturnValues(errorStruct);
    } else {
        context.setReturnValues(new BInteger(numberOfCharactersWritten));
    }
    callback.notifySuccess();
    return result;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BInteger(org.ballerinalang.model.values.BInteger) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) BInteger(org.ballerinalang.model.values.BInteger) 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