Search in sources :

Example 11 with Resource

use of org.ballerinalang.connector.api.Resource in project ballerina by ballerina-lang.

the class WebSocketDispatcher method dispatchBinaryMessage.

public static void dispatchBinaryMessage(WebSocketOpenConnectionInfo connectionInfo, WebSocketBinaryMessage binaryMessage) {
    WebSocketService wsService = connectionInfo.getService();
    Resource onBinaryMessageResource = wsService.getResourceByName(WebSocketConstants.RESOURCE_NAME_ON_BINARY_MESSAGE);
    if (onBinaryMessageResource == null) {
        return;
    }
    List<ParamDetail> paramDetails = onBinaryMessageResource.getParamDetails();
    BValue[] bValues = new BValue[paramDetails.size()];
    bValues[0] = connectionInfo.getWsConnection();
    BStruct wsBinaryFrame = wsService.createBinaryFrameStruct();
    byte[] data = binaryMessage.getByteArray();
    wsBinaryFrame.setBlobField(0, data);
    if (binaryMessage.isFinalFragment()) {
        wsBinaryFrame.setBooleanField(0, 1);
    } else {
        wsBinaryFrame.setBooleanField(0, 0);
    }
    bValues[1] = wsBinaryFrame;
    // TODO handle BallerinaConnectorException
    Executor.submit(onBinaryMessageResource, new WebSocketEmptyCallableUnitCallback(), null, null, bValues);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BValue(org.ballerinalang.model.values.BValue) Resource(org.ballerinalang.connector.api.Resource)

Example 12 with Resource

use of org.ballerinalang.connector.api.Resource in project ballerina by ballerina-lang.

the class WebSocketDispatcher method dispatchPongMessage.

private static void dispatchPongMessage(WebSocketOpenConnectionInfo connectionInfo, WebSocketControlMessage controlMessage) {
    WebSocketService wsService = connectionInfo.getService();
    Resource onPongMessageResource = wsService.getResourceByName(WebSocketConstants.RESOURCE_NAME_ON_PONG);
    if (onPongMessageResource == null) {
        return;
    }
    List<ParamDetail> paramDetails = onPongMessageResource.getParamDetails();
    BValue[] bValues = new BValue[paramDetails.size()];
    bValues[0] = connectionInfo.getWsConnection();
    BStruct wsPongFrame = wsService.createPongFrameStruct();
    byte[] data = controlMessage.getByteArray();
    wsPongFrame.setBlobField(0, data);
    bValues[1] = wsPongFrame;
    // TODO handle BallerinaConnectorException
    Executor.submit(onPongMessageResource, new WebSocketEmptyCallableUnitCallback(), null, null, bValues);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BValue(org.ballerinalang.model.values.BValue) Resource(org.ballerinalang.connector.api.Resource)

Example 13 with Resource

use of org.ballerinalang.connector.api.Resource in project ballerina by ballerina-lang.

the class DefaultStreamObserver method onCompleted.

@Override
public void onCompleted() {
    Resource onCompleted = resourceMap.get(MessageConstants.ON_COMPLETE_RESOURCE);
    if (onCompleted == null) {
        String message = "Error in listener service definition. onCompleted resource does not exists";
        LOG.error(message);
        throw new RuntimeException(message);
    }
    List<ParamDetail> paramDetails = onCompleted.getParamDetails();
    BValue[] signatureParams = new BValue[paramDetails.size()];
    CallableUnitCallback callback = new GrpcCallableUnitCallBack(null);
    Executor.submit(onCompleted, callback, null, null, signatureParams);
}
Also used : GrpcCallableUnitCallBack(org.ballerinalang.net.grpc.GrpcCallableUnitCallBack) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BValue(org.ballerinalang.model.values.BValue) Resource(org.ballerinalang.connector.api.Resource) BString(org.ballerinalang.model.values.BString) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 14 with Resource

use of org.ballerinalang.connector.api.Resource in project ballerina by ballerina-lang.

the class DefaultStreamObserver method onNext.

@Override
public void onNext(Message value) {
    Resource resource = resourceMap.get(MessageConstants.ON_MESSAGE_RESOURCE);
    if (resource == null) {
        String message = "Error in listener service definition. onNext resource does not exists";
        LOG.error(message);
        throw new RuntimeException(message);
    }
    List<ParamDetail> paramDetails = resource.getParamDetails();
    BValue[] signatureParams = new BValue[paramDetails.size()];
    BValue requestParam = getRequestParameter(resource, value);
    if (requestParam != null) {
        signatureParams[0] = requestParam;
    }
    CallableUnitCallback callback = new GrpcCallableUnitCallBack(null);
    Executor.submit(resource, callback, null, null, signatureParams);
}
Also used : GrpcCallableUnitCallBack(org.ballerinalang.net.grpc.GrpcCallableUnitCallBack) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BValue(org.ballerinalang.model.values.BValue) Resource(org.ballerinalang.connector.api.Resource) BString(org.ballerinalang.model.values.BString) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 15 with Resource

use of org.ballerinalang.connector.api.Resource in project ballerina by ballerina-lang.

the class DefaultStreamObserver method onError.

@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()];
    if (paramDetails.size() != 1) {
        String message = "Error in onError resource definition. It must have only error 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[0] = errorStruct;
    CallableUnitCallback callback = new GrpcCallableUnitCallBack(null);
    Executor.submit(onError, callback, null, null, signatureParams);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) GrpcCallableUnitCallBack(org.ballerinalang.net.grpc.GrpcCallableUnitCallBack) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BValue(org.ballerinalang.model.values.BValue) BType(org.ballerinalang.model.types.BType) Resource(org.ballerinalang.connector.api.Resource) BString(org.ballerinalang.model.values.BString) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Aggregations

Resource (org.ballerinalang.connector.api.Resource)16 ParamDetail (org.ballerinalang.connector.api.ParamDetail)12 BValue (org.ballerinalang.model.values.BValue)12 BStruct (org.ballerinalang.model.values.BStruct)10 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)6 GrpcCallableUnitCallBack (org.ballerinalang.net.grpc.GrpcCallableUnitCallBack)5 BString (org.ballerinalang.model.values.BString)4 BType (org.ballerinalang.model.types.BType)3 StreamObserver (io.grpc.stub.StreamObserver)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Annotation (org.ballerinalang.connector.api.Annotation)2 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)2 Struct (org.ballerinalang.connector.api.Struct)2 Message (org.ballerinalang.net.grpc.Message)2 Descriptors (com.google.protobuf.Descriptors)1 MethodDescriptor (io.grpc.MethodDescriptor)1 Builder (io.grpc.ServerServiceDefinition.Builder)1 NettyServerBuilder (io.grpc.netty.NettyServerBuilder)1