Search in sources :

Example 1 with WebSocketClientAddress

use of joynr.system.RoutingTypes.WebSocketClientAddress in project joynr by bmwcarit.

the class WebSocketJettyServer method writeBytes.

@Override
public synchronized void writeBytes(Address toAddress, byte[] message, long timeout, TimeUnit unit, final SuccessAction successAction, final FailureAction failureAction) {
    if (!(toAddress instanceof WebSocketClientAddress)) {
        throw new JoynrIllegalStateException("Web Socket Server can only send to WebSocketClientAddresses");
    }
    WebSocketClientAddress toClientAddress = (WebSocketClientAddress) toAddress;
    Session session = sessionMap.get(toClientAddress.getId());
    if (session == null) {
        // TODO We need a delay with invalidation of the stub
        throw new JoynrDelayMessageException("no active session for WebSocketClientAddress: " + toClientAddress.getId());
    }
    try {
        session.getRemote().sendBytes(ByteBuffer.wrap(message), new WriteCallback() {

            @Override
            public void writeSuccess() {
                successAction.execute();
            }

            @Override
            public void writeFailed(Throwable error) {
                if (shutdown) {
                    return;
                }
                failureAction.execute(error);
            }
        });
    } catch (WebSocketException e) {
        // Jetty throws WebSocketException when expecting [OPEN or CONNECTED] but found a different state
        // The client must reconnect, but the message can be queued in the mean time.
        sessionMap.remove(toClientAddress.getId());
        // TODO We need a delay with invalidation of the stub
        throw new JoynrDelayMessageException(e.getMessage(), e);
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) WebSocketClientAddress(joynr.system.RoutingTypes.WebSocketClientAddress) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) Session(org.eclipse.jetty.websocket.api.Session)

Example 2 with WebSocketClientAddress

use of joynr.system.RoutingTypes.WebSocketClientAddress in project joynr by bmwcarit.

the class WebSocketTest method configure.

private void configure(int maxMessageSize, long reconnectDelay, long websocketIdleTimeout, Set<JoynrMessageProcessor> messageProcessor) {
    ObjectMapper objectMapper = new ObjectMapper();
    WebSocketEndpointFactory webSocketJettyServerFactory = new WebSocketJettyServerFactory(maxMessageSize, objectMapper);
    ccWebSocketMessagingSkeleton = new WebSocketMessagingSkeleton(serverAddress, webSocketJettyServerFactory, messageRouterMock, new WebSocketMessagingSkeleton.MainTransportFlagBearer(), messageProcessor);
    ownAddress = new WebSocketClientAddress(UUID.randomUUID().toString());
    webSocketJettyClientFactory = new WebSocketJettyClientFactory(ownAddress, maxMessageSize, reconnectDelay, websocketIdleTimeout, objectMapper);
    webSocketMessagingStub = new WebSocketMessagingStub(serverAddress, webSocketJettyClientFactory.create(serverAddress));
    libWebSocketMessagingSkeleton = new WebSocketMessagingSkeleton(serverAddress, webSocketJettyClientFactory, messageRouterMock, new WebSocketMessagingSkeleton.MainTransportFlagBearer(), messageProcessor);
    ccWebSocketMessagingSkeleton.init();
    libWebSocketMessagingSkeleton.init();
}
Also used : WebSocketEndpointFactory(io.joynr.messaging.websocket.WebSocketEndpointFactory) WebSocketJettyServerFactory(io.joynr.messaging.websocket.server.WebSocketJettyServerFactory) WebSocketJettyClientFactory(io.joynr.messaging.websocket.jetty.client.WebSocketJettyClientFactory) WebSocketMessagingStub(io.joynr.messaging.websocket.WebSocketMessagingStub) WebSocketClientAddress(joynr.system.RoutingTypes.WebSocketClientAddress) WebSocketMessagingSkeleton(io.joynr.messaging.websocket.WebSocketMessagingSkeleton) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

WebSocketClientAddress (joynr.system.RoutingTypes.WebSocketClientAddress)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)1 JoynrIllegalStateException (io.joynr.exceptions.JoynrIllegalStateException)1 WebSocketEndpointFactory (io.joynr.messaging.websocket.WebSocketEndpointFactory)1 WebSocketMessagingSkeleton (io.joynr.messaging.websocket.WebSocketMessagingSkeleton)1 WebSocketMessagingStub (io.joynr.messaging.websocket.WebSocketMessagingStub)1 WebSocketJettyClientFactory (io.joynr.messaging.websocket.jetty.client.WebSocketJettyClientFactory)1 WebSocketJettyServerFactory (io.joynr.messaging.websocket.server.WebSocketJettyServerFactory)1 Session (org.eclipse.jetty.websocket.api.Session)1 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)1 WriteCallback (org.eclipse.jetty.websocket.api.WriteCallback)1