Search in sources :

Example 1 with Rfc6120TcpRemoteConnectionEndpoint

use of org.jivesoftware.smack.tcp.rce.Rfc6120TcpRemoteConnectionEndpoint in project Smack by igniterealtime.

the class XMPPTCPConnection method connectUsingConfiguration.

private void connectUsingConfiguration() throws ConnectionException, IOException, InterruptedException {
    RemoteXmppTcpConnectionEndpoints.Result<Rfc6120TcpRemoteConnectionEndpoint> result = RemoteXmppTcpConnectionEndpoints.lookup(config);
    List<RemoteConnectionException<Rfc6120TcpRemoteConnectionEndpoint>> connectionExceptions = new ArrayList<>();
    SocketFactory socketFactory = config.getSocketFactory();
    ProxyInfo proxyInfo = config.getProxyInfo();
    int timeout = config.getConnectTimeout();
    if (socketFactory == null) {
        socketFactory = SocketFactory.getDefault();
    }
    for (Rfc6120TcpRemoteConnectionEndpoint endpoint : result.discoveredRemoteConnectionEndpoints) {
        Iterator<? extends InetAddress> inetAddresses;
        String host = endpoint.getHost().toString();
        UInt16 portUint16 = endpoint.getPort();
        int port = portUint16.intValue();
        if (proxyInfo == null) {
            inetAddresses = endpoint.getInetAddresses().iterator();
            assert inetAddresses.hasNext();
            innerloop: while (inetAddresses.hasNext()) {
                // Create a *new* Socket before every connection attempt, i.e. connect() call, since Sockets are not
                // re-usable after a failed connection attempt. See also SMACK-724.
                SmackFuture.SocketFuture socketFuture = new SmackFuture.SocketFuture(socketFactory);
                final InetAddress inetAddress = inetAddresses.next();
                final InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, port);
                LOGGER.finer("Trying to establish TCP connection to " + inetSocketAddress);
                socketFuture.connectAsync(inetSocketAddress, timeout);
                try {
                    socket = socketFuture.getOrThrow();
                } catch (IOException e) {
                    RemoteConnectionException<Rfc6120TcpRemoteConnectionEndpoint> rce = new RemoteConnectionException<>(endpoint, inetAddress, e);
                    connectionExceptions.add(rce);
                    if (inetAddresses.hasNext()) {
                        continue innerloop;
                    } else {
                        break innerloop;
                    }
                }
                LOGGER.finer("Established TCP connection to " + inetSocketAddress);
                // We found a host to connect to, return here
                this.host = host;
                this.port = portUint16;
                return;
            }
        } else {
            // TODO: Move this into the inner-loop above. There appears no reason why we should not try a proxy
            // connection to every inet address of each connection endpoint.
            socket = socketFactory.createSocket();
            StringUtils.requireNotNullNorEmpty(host, "Host of endpoint " + endpoint + " must not be null when using a Proxy");
            final String hostAndPort = host + " at port " + port;
            LOGGER.finer("Trying to establish TCP connection via Proxy to " + hostAndPort);
            try {
                proxyInfo.getProxySocketConnection().connect(socket, host, port, timeout);
            } catch (IOException e) {
                CloseableUtil.maybeClose(socket, LOGGER);
                RemoteConnectionException<Rfc6120TcpRemoteConnectionEndpoint> rce = new RemoteConnectionException<>(endpoint, null, e);
                connectionExceptions.add(rce);
                continue;
            }
            LOGGER.finer("Established TCP connection to " + hostAndPort);
            // We found a host to connect to, return here
            this.host = host;
            this.port = portUint16;
            return;
        }
    }
    // HostAddresses in the exception
    throw EndpointConnectionException.from(result.lookupFailures, connectionExceptions);
}
Also used : RemoteConnectionException(org.jivesoftware.smack.util.rce.RemoteConnectionException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SocketFactory(javax.net.SocketFactory) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Rfc6120TcpRemoteConnectionEndpoint(org.jivesoftware.smack.tcp.rce.Rfc6120TcpRemoteConnectionEndpoint) SmackFuture(org.jivesoftware.smack.SmackFuture) ProxyInfo(org.jivesoftware.smack.proxy.ProxyInfo) RemoteXmppTcpConnectionEndpoints(org.jivesoftware.smack.tcp.rce.RemoteXmppTcpConnectionEndpoints) Rfc6120TcpRemoteConnectionEndpoint(org.jivesoftware.smack.tcp.rce.Rfc6120TcpRemoteConnectionEndpoint) UInt16(org.jivesoftware.smack.datatypes.UInt16) InetAddress(java.net.InetAddress)

Aggregations

IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 SocketFactory (javax.net.SocketFactory)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 SmackFuture (org.jivesoftware.smack.SmackFuture)1 UInt16 (org.jivesoftware.smack.datatypes.UInt16)1 ProxyInfo (org.jivesoftware.smack.proxy.ProxyInfo)1 RemoteXmppTcpConnectionEndpoints (org.jivesoftware.smack.tcp.rce.RemoteXmppTcpConnectionEndpoints)1 Rfc6120TcpRemoteConnectionEndpoint (org.jivesoftware.smack.tcp.rce.Rfc6120TcpRemoteConnectionEndpoint)1 RemoteConnectionException (org.jivesoftware.smack.util.rce.RemoteConnectionException)1