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