Search in sources :

Example 36 with XMPPError

use of org.jivesoftware.smack.packet.XMPPError in project ecf by eclipse.

the class XMPPConnection method connectUsingConfiguration.

private void connectUsingConfiguration(ConnectionConfiguration config) throws XMPPException {
    XMPPException exception = null;
    Iterator<HostAddress> it = config.getHostAddresses().iterator();
    List<HostAddress> failedAddresses = new LinkedList<HostAddress>();
    boolean xmppIOError = false;
    while (it.hasNext()) {
        exception = null;
        HostAddress hostAddress = it.next();
        String host = hostAddress.getFQDN();
        int port = hostAddress.getPort();
        try {
            if (config.getSocketFactory() == null) {
                this.socket = new Socket(host, port);
            } else {
                this.socket = config.getSocketFactory().createSocket(host, port);
            }
        } catch (UnknownHostException uhe) {
            String errorMessage = "Could not connect to " + host + ":" + port + ".";
            exception = new XMPPException(errorMessage, new XMPPError(XMPPError.Condition.remote_server_timeout, errorMessage), uhe);
        } catch (IOException ioe) {
            String errorMessage = "XMPPError connecting to " + host + ":" + port + ".";
            exception = new XMPPException(errorMessage, new XMPPError(XMPPError.Condition.remote_server_error, errorMessage), ioe);
            xmppIOError = true;
        }
        if (exception == null) {
            // We found a host to connect to, break here
            config.setUsedHostAddress(hostAddress);
            break;
        }
        hostAddress.setException(exception);
        failedAddresses.add(hostAddress);
        if (!it.hasNext()) {
            // There are no more host addresses to try
            // throw an exception and report all tried
            // HostAddresses in the exception
            StringBuilder sb = new StringBuilder();
            for (HostAddress fha : failedAddresses) {
                sb.append(fha.getErrorMessage());
                sb.append("; ");
            }
            XMPPError xmppError;
            if (xmppIOError) {
                xmppError = new XMPPError(XMPPError.Condition.remote_server_error);
            } else {
                xmppError = new XMPPError(XMPPError.Condition.remote_server_timeout);
            }
            throw new XMPPException(sb.toString(), xmppError);
        }
    }
    socketClosed = false;
    initConnection();
}
Also used : UnknownHostException(java.net.UnknownHostException) XMPPError(org.jivesoftware.smack.packet.XMPPError) IOException(java.io.IOException) HostAddress(org.jivesoftware.smack.util.dns.HostAddress) LinkedList(java.util.LinkedList) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Aggregations

XMPPError (org.jivesoftware.smack.packet.XMPPError)36 XMPPException (org.jivesoftware.smack.XMPPException)11 IQ (org.jivesoftware.smack.packet.IQ)10 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)6 SmackException (org.jivesoftware.smack.SmackException)5 XmlPullParser (org.xmlpull.v1.XmlPullParser)5 AdHocCommandData (org.jivesoftware.smackx.packet.AdHocCommandData)3 NetworkException (com.xabber.android.data.NetworkException)2 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 Form (org.jivesoftware.smackx.Form)2 Action (org.jivesoftware.smackx.commands.AdHocCommand.Action)2 MultiUserChat (org.jivesoftware.smackx.muc.MultiUserChat)2 StreamInitiation (org.jivesoftware.smackx.packet.StreamInitiation)2 AccountItem (com.xabber.android.data.account.AccountItem)1 ConnectionThread (com.xabber.android.data.connection.ConnectionThread)1 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)1 UserJid (com.xabber.android.data.entity.UserJid)1 Realm (io.realm.Realm)1 BufferedImage (java.awt.image.BufferedImage)1