Search in sources :

Example 1 with WebSocketService

use of org.ballerinalang.net.http.WebSocketService in project ballerina by ballerina-lang.

the class NonListeningRegister method execute.

@Override
public void execute(Context context) {
    Service service = BLangConnectorSPIUtil.getServiceRegistered(context);
    Struct serviceEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
    HTTPServicesRegistry httpServicesRegistry = getHttpServicesRegistry(serviceEndpoint);
    WebSocketServicesRegistry webSocketServicesRegistry = getWebSocketServicesRegistry(serviceEndpoint);
    // TODO: In HTTP to WebSocket upgrade register WebSocket service in WebSocketServiceRegistry
    if (HttpConstants.HTTP_SERVICE_ENDPOINT_NAME.equals(service.getEndpointName())) {
        httpServicesRegistry.registerService(service);
    }
    if (WebSocketConstants.WEBSOCKET_ENDPOINT_NAME.equals(service.getEndpointName())) {
        WebSocketService webSocketService = new WebSocketService(service);
        webSocketServicesRegistry.registerService(webSocketService);
    }
    context.setReturnValues();
}
Also used : HTTPServicesRegistry(org.ballerinalang.net.http.HTTPServicesRegistry) WebSocketServicesRegistry(org.ballerinalang.net.http.WebSocketServicesRegistry) Service(org.ballerinalang.connector.api.Service) WebSocketService(org.ballerinalang.net.http.WebSocketService) WebSocketService(org.ballerinalang.net.http.WebSocketService) Struct(org.ballerinalang.connector.api.Struct)

Example 2 with WebSocketService

use of org.ballerinalang.net.http.WebSocketService in project ballerina by ballerina-lang.

the class UpgradeToWebSocket method execute.

@Override
public void execute(Context context) {
    BStruct serverConnector = (BStruct) context.getRefArgument(0);
    WebSocketService webSocketService = (WebSocketService) serverConnector.getNativeData(WebSocketConstants.WEBSOCKET_SERVICE);
    BMap<String, BString> headers = (BMap<String, BString>) context.getRefArgument(1);
    DefaultHttpHeaders httpHeaders = new DefaultHttpHeaders();
    Set<String> keys = headers.keySet();
    for (String key : keys) {
        httpHeaders.add(key, headers.get(key));
    }
    WebSocketUtil.handleHandshake(webSocketService, httpHeaders, serverConnector);
    context.setReturnValues();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BMap(org.ballerinalang.model.values.BMap) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) BString(org.ballerinalang.model.values.BString) WebSocketService(org.ballerinalang.net.http.WebSocketService) BString(org.ballerinalang.model.values.BString)

Example 3 with WebSocketService

use of org.ballerinalang.net.http.WebSocketService in project ballerina by ballerina-lang.

the class Start method execute.

@Override
public void execute(Context context) {
    HttpWsConnectorFactory connectorFactory = HttpUtil.createHttpWsConnectionFactory();
    Struct clientEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
    Struct clientEndpointConfig = clientEndpoint.getStructField(HttpConstants.CLIENT_ENDPOINT_CONFIG);
    Object configs = clientEndpointConfig.getNativeData(WebSocketConstants.CLIENT_CONNECTOR_CONFIGS);
    if (configs == null || !(configs instanceof WsClientConnectorConfig)) {
        throw new BallerinaConnectorException("Initialize the service before starting it");
    }
    WebSocketClientConnector clientConnector = connectorFactory.createWsClientConnector((WsClientConnectorConfig) configs);
    WebSocketClientConnectorListener clientConnectorListener = new WebSocketClientConnectorListener();
    Object serviceConfig = clientEndpointConfig.getNativeData(WebSocketConstants.CLIENT_SERVICE_CONFIG);
    if (serviceConfig == null || !(serviceConfig instanceof WebSocketService)) {
        throw new BallerinaConnectorException("Initialize the service before starting it");
    }
    WebSocketService wsService = (WebSocketService) serviceConfig;
    HandshakeFuture handshakeFuture = clientConnector.connect(clientConnectorListener);
    handshakeFuture.setHandshakeListener(new WsHandshakeListener(context, wsService, clientConnectorListener));
    context.setReturnValues();
}
Also used : BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) WsClientConnectorConfig(org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig) HandshakeFuture(org.wso2.transport.http.netty.contract.websocket.HandshakeFuture) WebSocketClientConnector(org.wso2.transport.http.netty.contract.websocket.WebSocketClientConnector) WebSocketClientConnectorListener(org.ballerinalang.net.http.WebSocketClientConnectorListener) WebSocketService(org.ballerinalang.net.http.WebSocketService) HttpWsConnectorFactory(org.wso2.transport.http.netty.contract.HttpWsConnectorFactory) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct)

Example 4 with WebSocketService

use of org.ballerinalang.net.http.WebSocketService in project ballerina by ballerina-lang.

the class InitEndpoint method execute.

@Override
public void execute(Context context) {
    Struct clientEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
    Struct clientEndpointConfig = clientEndpoint.getStructField(HttpConstants.CLIENT_ENDPOINT_CONFIG);
    String remoteUrl = clientEndpointConfig.getStringField(WebSocketConstants.CLIENT_URL_CONFIG);
    Value clientServiceType = clientEndpointConfig.getTypeField(WebSocketConstants.CLIENT_SERVICE_CONFIG);
    Service service = BLangConnectorSPIUtil.getServiceFromType(context.getProgramFile(), clientServiceType);
    if (service == null) {
        throw new BallerinaConnectorException("Cannot find client service: " + clientServiceType);
    }
    if (WebSocketConstants.WEBSOCKET_CLIENT_ENDPOINT_NAME.equals(service.getEndpointName())) {
        WebSocketService wsService = new WebSocketService(service);
        // TODO: Fix this validation
        // WebSocketServiceValidator.validateServiceEndpoint(wsService);
        WsClientConnectorConfig clientConnectorConfig = new WsClientConnectorConfig(remoteUrl);
        Value[] subProtocolValues = clientEndpointConfig.getArrayField(WebSocketConstants.CLIENT_SUBPROTOCOLS_CONFIG);
        if (subProtocolValues != null) {
            clientConnectorConfig.setSubProtocols(Arrays.stream(subProtocolValues).map(Value::getStringValue).toArray(String[]::new));
        }
        Map<String, Value> headerValues = clientEndpointConfig.getMapField(WebSocketConstants.CLIENT_CUSTOMHEADERS_CONFIG);
        if (headerValues != null) {
            clientConnectorConfig.addHeaders(getCustomHeaders(headerValues));
        }
        long idleTimeoutInSeconds = clientEndpointConfig.getIntField(WebSocketConstants.CLIENT_IDLETIMOUT_CONFIG);
        if (idleTimeoutInSeconds > 0) {
            clientConnectorConfig.setIdleTimeoutInMillis((int) (idleTimeoutInSeconds * 1000));
        }
        clientEndpointConfig.addNativeData(WebSocketConstants.CLIENT_SERVICE_CONFIG, wsService);
        clientEndpointConfig.addNativeData(WebSocketConstants.CLIENT_CONNECTOR_CONFIGS, clientConnectorConfig);
    } else {
        throw new BallerinaConnectorException("Incorrect endpoint: " + service.getEndpointName());
    }
    context.setReturnValues();
}
Also used : BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) WsClientConnectorConfig(org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig) Value(org.ballerinalang.connector.api.Value) Service(org.ballerinalang.connector.api.Service) WebSocketService(org.ballerinalang.net.http.WebSocketService) WebSocketService(org.ballerinalang.net.http.WebSocketService) Struct(org.ballerinalang.connector.api.Struct)

Example 5 with WebSocketService

use of org.ballerinalang.net.http.WebSocketService in project ballerina by ballerina-lang.

the class Register method execute.

@Override
public void execute(Context context) {
    Service service = BLangConnectorSPIUtil.getServiceRegistered(context);
    Struct serviceEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
    HTTPServicesRegistry httpServicesRegistry = getHttpServicesRegistry(serviceEndpoint);
    WebSocketServicesRegistry webSocketServicesRegistry = getWebSocketServicesRegistry(serviceEndpoint);
    // TODO: In HTTP to WebSocket upgrade register WebSocket service in WebSocketServiceRegistry
    if (HttpConstants.HTTP_SERVICE_ENDPOINT_NAME.equals(service.getEndpointName())) {
        httpServicesRegistry.registerService(service);
    }
    if (WebSocketConstants.WEBSOCKET_ENDPOINT_NAME.equals(service.getEndpointName())) {
        WebSocketService webSocketService = new WebSocketService(service);
        webSocketServicesRegistry.registerService(webSocketService);
    }
    if (!isConnectorStarted(serviceEndpoint)) {
        startServerConnector(serviceEndpoint, httpServicesRegistry, webSocketServicesRegistry);
    }
    context.setReturnValues();
}
Also used : HTTPServicesRegistry(org.ballerinalang.net.http.HTTPServicesRegistry) WebSocketServicesRegistry(org.ballerinalang.net.http.WebSocketServicesRegistry) Service(org.ballerinalang.connector.api.Service) WebSocketService(org.ballerinalang.net.http.WebSocketService) WebSocketService(org.ballerinalang.net.http.WebSocketService) Struct(org.ballerinalang.connector.api.Struct)

Aggregations

WebSocketService (org.ballerinalang.net.http.WebSocketService)5 Struct (org.ballerinalang.connector.api.Struct)4 Service (org.ballerinalang.connector.api.Service)3 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)2 BStruct (org.ballerinalang.model.values.BStruct)2 HTTPServicesRegistry (org.ballerinalang.net.http.HTTPServicesRegistry)2 WebSocketServicesRegistry (org.ballerinalang.net.http.WebSocketServicesRegistry)2 WsClientConnectorConfig (org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 Value (org.ballerinalang.connector.api.Value)1 BMap (org.ballerinalang.model.values.BMap)1 BString (org.ballerinalang.model.values.BString)1 WebSocketClientConnectorListener (org.ballerinalang.net.http.WebSocketClientConnectorListener)1 HttpWsConnectorFactory (org.wso2.transport.http.netty.contract.HttpWsConnectorFactory)1 HandshakeFuture (org.wso2.transport.http.netty.contract.websocket.HandshakeFuture)1 WebSocketClientConnector (org.wso2.transport.http.netty.contract.websocket.WebSocketClientConnector)1