use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.
the class WebSocketServerConnectorListener method onMessage.
@Override
public void onMessage(WebSocketInitMessage webSocketInitMessage) {
HTTPCarbonMessage msg = new HTTPCarbonMessage(((DefaultWebSocketInitMessage) webSocketInitMessage).getHttpRequest());
Map<String, String> pathParams = new HashMap<>();
WebSocketService wsService = WebSocketDispatcher.findService(servicesRegistry, pathParams, webSocketInitMessage, msg);
BStruct serviceEndpoint = BLangConnectorSPIUtil.createBStruct(wsService.getResources()[0].getResourceInfo().getServiceInfo().getPackageInfo().getProgramFile(), PROTOCOL_PACKAGE_HTTP, WEBSOCKET_ENDPOINT);
BStruct serverConnector = WebSocketUtil.createAndGetBStruct(wsService.getResources()[0]);
serverConnector.addNativeData(WebSocketConstants.WEBSOCKET_MESSAGE, webSocketInitMessage);
serverConnector.addNativeData(WebSocketConstants.WEBSOCKET_SERVICE, wsService);
serviceEndpoint.setRefField(SERVICE_ENDPOINT_CONNECTION_INDEX, serverConnector);
serviceEndpoint.setRefField(3, new BMap());
serverConnector.addNativeData(WEBSOCKET_ENDPOINT, serviceEndpoint);
Map<String, String> upgradeHeaders = webSocketInitMessage.getHeaders();
BMap<String, BString> bUpgradeHeaders = new BMap<>();
upgradeHeaders.forEach((key, value) -> bUpgradeHeaders.put(key, new BString(value)));
serviceEndpoint.setRefField(4, bUpgradeHeaders);
Resource onUpgradeResource = wsService.getResourceByName(WebSocketConstants.RESOURCE_NAME_ON_UPGRADE);
if (onUpgradeResource != null) {
Semaphore semaphore = new Semaphore(0);
AtomicBoolean isResourceExeSuccessful = new AtomicBoolean(false);
BStruct inRequest = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), PROTOCOL_PACKAGE_HTTP, HttpConstants.REQUEST);
BStruct inRequestEntity = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), org.ballerinalang.mime.util.Constants.PROTOCOL_PACKAGE_MIME, Constants.ENTITY);
BStruct mediaType = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), org.ballerinalang.mime.util.Constants.PROTOCOL_PACKAGE_MIME, Constants.MEDIA_TYPE);
BStruct cacheControlStruct = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), PROTOCOL_PACKAGE_HTTP, REQUEST_CACHE_CONTROL);
RequestCacheControlStruct requestCacheControl = new RequestCacheControlStruct(cacheControlStruct);
HttpUtil.populateInboundRequest(inRequest, inRequestEntity, mediaType, msg, requestCacheControl);
List<ParamDetail> paramDetails = onUpgradeResource.getParamDetails();
BValue[] bValues = new BValue[paramDetails.size()];
bValues[0] = serviceEndpoint;
bValues[1] = inRequest;
WebSocketDispatcher.setPathParams(bValues, paramDetails, pathParams, 2);
Tracer tracer = TraceManagerWrapper.newTracer(null, false);
upgradeHeaders.entrySet().stream().filter(c -> c.getKey().startsWith(TraceConstants.TRACE_PREFIX)).forEach(e -> tracer.addProperty(e.getKey(), e.getValue()));
Executor.submit(onUpgradeResource, new CallableUnitCallback() {
@Override
public void notifySuccess() {
isResourceExeSuccessful.set(true);
semaphore.release();
}
@Override
public void notifyFailure(BStruct error) {
ErrorHandlerUtils.printError("error: " + BLangVMErrors.getPrintableStackTrace(error));
semaphore.release();
}
}, null, tracer, bValues);
try {
semaphore.acquire();
if (isResourceExeSuccessful.get() && !webSocketInitMessage.isCancelled() && !webSocketInitMessage.isHandshakeStarted()) {
WebSocketUtil.handleHandshake(wsService, null, serverConnector);
}
} catch (InterruptedException e) {
throw new BallerinaConnectorException("Connection interrupted during handshake");
}
} else {
WebSocketUtil.handleHandshake(wsService, null, serverConnector);
}
}
use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.
the class ClientStreamingListener method invoke.
@Override
public StreamObserver<Message> invoke(StreamObserver<Message> responseObserver) {
Resource onOpen = resourceMap.get(MessageConstants.ON_OPEN_RESOURCE);
List<ParamDetail> paramDetails = onOpen.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onOpen, responseObserver);
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
Executor.submit(onOpen, callback, null, null, signatureParams);
return new StreamObserver<Message>() {
@Override
public void onNext(Message value) {
Resource onMessage = resourceMap.get(MessageConstants.ON_MESSAGE_RESOURCE);
List<ParamDetail> paramDetails = onMessage.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onMessage, responseObserver);
BValue requestParam = getRequestParameter(onMessage, value);
if (requestParam != null) {
signatureParams[1] = requestParam;
}
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
Executor.submit(onMessage, callback, null, null, signatureParams);
}
@Override
public void onError(Throwable t) {
Resource onError = resourceMap.get(MessageConstants.ON_ERROR_RESOURCE);
if (onError == null) {
String message = "Error in listener service definition. onError resource does not exists";
LOG.error(message);
throw new RuntimeException(message);
}
List<ParamDetail> paramDetails = onError.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onError, responseObserver);
if (paramDetails.size() != 2) {
String message = "Error in onError resource definition. It must have two input params, but have " + paramDetails.size();
LOG.error(message);
throw new RuntimeException(message);
}
BType errorType = paramDetails.get(1).getVarType();
BStruct errorStruct = MessageUtils.getConnectorError((BStructType) errorType, t);
signatureParams[1] = errorStruct;
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
Executor.submit(onError, callback, null, null, signatureParams);
}
@Override
public void onCompleted() {
Resource onCompleted = resourceMap.get(MessageConstants.ON_COMPLETE_RESOURCE);
if (onCompleted == null) {
String message = "Error in listener service definition. onError resource does not exists";
LOG.error(message);
throw new RuntimeException(message);
}
List<ParamDetail> paramDetails = onCompleted.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onCompleted, responseObserver);
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, isEmptyResponse());
Executor.submit(onCompleted, callback, null, null, signatureParams);
}
};
}
use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.
the class HasNextTextRecord method response.
/**
* Responds whether a next record exists.
*
* @param result the result processed.
* @return result context.
*/
private static EventResult response(EventResult<Boolean, EventContext> result) {
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
CallableUnitCallback callback = eventContext.getCallback();
Boolean response = result.getResponse();
context.setReturnValues(new BBoolean(response));
callback.notifySuccess();
return result;
}
use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.
the class LoadToTable method response.
private static EventResult response(EventResult<List, EventContext> result) {
BStruct errorStruct;
BTable table;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
context.setReturnValues(errorStruct);
} else {
try {
List records = result.getResponse();
table = getbTable(context, records);
context.setReturnValues(table);
} catch (Throwable e) {
errorStruct = IOUtils.createError(context, e.getMessage());
context.setReturnValues(errorStruct);
}
}
CallableUnitCallback callback = eventContext.getCallback();
callback.notifySuccess();
return result;
}
use of org.ballerinalang.bre.bvm.CallableUnitCallback 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;
}
Aggregations