Search in sources :

Example 1 with SmackFuture

use of org.jivesoftware.smack.SmackFuture in project Smack by igniterealtime.

the class WebSocketConnectionAttemptState method establishWebSocketConnection.

private void establishWebSocketConnection(List<? extends WebSocketRemoteConnectionEndpoint> webSocketEndpoints, List<Exception> failures) throws InterruptedException {
    final int endpointCount = webSocketEndpoints.size();
    List<SmackFuture<AbstractWebSocket, Exception>> futures = new ArrayList<>(endpointCount);
    {
        List<AbstractWebSocket> webSockets = new ArrayList<>(endpointCount);
        // First only create the AbstractWebSocket instances, in case a constructor throws.
        for (WebSocketRemoteConnectionEndpoint endpoint : webSocketEndpoints) {
            AbstractWebSocket webSocket = webSocketFactory.create(endpoint, connectionInternal);
            webSockets.add(webSocket);
        }
        for (AbstractWebSocket webSocket : webSockets) {
            SmackFuture<AbstractWebSocket, Exception> future = webSocket.getFuture();
            futures.add(future);
        }
    }
    SmackFuture.await(futures, connectionInternal.connection.getReplyTimeout());
    for (SmackFuture<AbstractWebSocket, Exception> future : futures) {
        AbstractWebSocket connectedWebSocket = future.getIfAvailable();
        if (connectedWebSocket == null) {
            Exception exception = future.getExceptionIfAvailable();
            assert exception != null;
            failures.add(exception);
            continue;
        }
        if (webSocket == null) {
            webSocket = connectedWebSocket;
            // futures and close remaining successfully connected ones.
            continue;
        }
        connectedWebSocket.disconnect(1000, "Using other connection endpoint at " + webSocket.getEndpoint());
    }
}
Also used : SmackFuture(org.jivesoftware.smack.SmackFuture) WebSocketRemoteConnectionEndpoint(org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint) AbstractWebSocket(org.jivesoftware.smack.websocket.impl.AbstractWebSocket) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) WebSocketRemoteConnectionEndpoint(org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 SmackFuture (org.jivesoftware.smack.SmackFuture)1 AbstractWebSocket (org.jivesoftware.smack.websocket.impl.AbstractWebSocket)1 WebSocketRemoteConnectionEndpoint (org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint)1