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);
}
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);
}
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;
}
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);
}
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;
}
Aggregations