Search in sources :

Example 6 with Stanza

use of com.googlecode.asmack.Stanza in project AsmackService by rtreffer.

the class AccountConnection method connectionSuccess.

/**
     * Mark the connection as connected, unless the connection is connected.
     * @param loginThread The initial login thread.
     * @param connection The new connection.
     */
public synchronized void connectionSuccess(LoginThread loginThread, Connection connection) {
    Connection oldConnection = null;
    if (currentState == State.Connected) {
        Log.w(TAG, "TWO CONNECTIONS RUNNING");
        oldConnection = this.connection;
    }
    // Try to send an initial stanza
    Stanza stanza = new Stanza("presence", "", null, "<presence />", null);
    try {
        connection.send(stanza);
    } catch (XmppException e) {
        // Initial stanza failed
        try {
            connection.close();
        } catch (XmppException e1) {
        /* IGNORE */
        }
        if (oldConnection == null || oldConnection.isClosed()) {
            // Only fail if the old connection is invalid
            transition(State.Failed);
        }
        return;
    }
    if (loginThread != null && loginThread != this.loginThread) {
        // concurrent login attempt
        loginThread.interrupt();
    }
    this.loginThread = null;
    if (oldConnection != null) {
        try {
            oldConnection.close();
        } catch (XmppException e) {
        /* IGNORE */
        }
    }
    this.connection = connection;
    transition(State.Connected);
}
Also used : XmppException(com.googlecode.asmack.XmppException) Stanza(com.googlecode.asmack.Stanza)

Example 7 with Stanza

use of com.googlecode.asmack.Stanza in project AsmackService by rtreffer.

the class PresenceRunnable method run.

/**
     * Execute the presence update.
     */
@Override
public void run() {
    String payload = "<presence />";
    if (verification != null) {
        payload = "<presence><c xmlns='http://jabber.org/protocol/caps' " + "hash='sha-1' " + "node='http://github.com/rtreffer/AsmackService' " + "ver='" + verification + "'" + "/></presence>";
    }
    Stanza stanza = new Stanza("presence", "", "", payload, null);
    try {
        connection.send(stanza);
    } catch (XmppException e) {
    /* PING is non critical */
    }
}
Also used : XmppException(com.googlecode.asmack.XmppException) Stanza(com.googlecode.asmack.Stanza)

Example 8 with Stanza

use of com.googlecode.asmack.Stanza in project AsmackService by rtreffer.

the class ConncetionPullToSinkPushThread method run.

/**
     * <p>Run the main pull/push loop.</p>
     * <p>The {@link XmppInputStream#nextStanza()} to
     * {@link StanzaSink#receive(Stanza)} will run until a
     * {@link XmppException} is received.</p>
     */
@Override
public void run() {
    String resourceJid = connection.getResourceJid();
    try {
        while (true) {
            Stanza stanza = xmppInput.nextStanza();
            stanza.setVia(resourceJid);
            sink.receive(stanza);
        }
    } catch (XmppException e) {
        try {
            connection.close();
        } catch (Exception ex) {
        // we just try to clean up, ignore problems
        }
        Log.e(TAG, "Connection aborted", e);
        sink.connectionFailed(connection, e);
    }
}
Also used : XmppException(com.googlecode.asmack.XmppException) Stanza(com.googlecode.asmack.Stanza) XmppException(com.googlecode.asmack.XmppException)

Example 9 with Stanza

use of com.googlecode.asmack.Stanza in project AsmackService by rtreffer.

the class FeatureNegotiationEngine method bind.

/**
     * Bind a given resource, probably resuming an old session.
     * @param resource String The preferred resource string.
     * @return String The actual resource string.
     * @throws XmppException On Error.
     */
public String bind(String resource) throws XmppException {
    try {
        if (!TextUtils.isEmpty(resource)) {
            xmppOutput.sendUnchecked("<iq type=\"set\" id=\"bind_1\">" + "<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">" + "<resource>" + resource + "</resource>" + "</bind>" + "</iq>");
        } else {
            xmppOutput.sendUnchecked("<iq type=\"set\" id=\"bind_1\">" + "<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">" + "</bind>" + "</iq>");
        }
        Stanza stanza = xmppInput.nextStanza();
        Node node = XMLUtils.getDocumentNode(stanza.getXml());
        Node bind = XMLUtils.getFirstChild(node, "urn:ietf:params:xml:ns:xmpp-bind", "bind");
        Node jid = XMLUtils.getFirstChild(bind, null, "jid");
        if (sessionsSupported) {
            startSession();
        }
        return jid.getTextContent();
    } catch (IllegalArgumentException e) {
        throw new XmppMalformedException("bind malformed", e);
    } catch (IllegalStateException e) {
        throw new XmppMalformedException("bind malformed", e);
    } catch (SAXException e) {
        throw new XmppMalformedException("bind malformed", e);
    }
}
Also used : XmppMalformedException(com.googlecode.asmack.XmppMalformedException) Stanza(com.googlecode.asmack.Stanza) Node(org.w3c.dom.Node) SAXException(org.xml.sax.SAXException)

Example 10 with Stanza

use of com.googlecode.asmack.Stanza in project AsmackService by rtreffer.

the class XmppInputStream method nextStanza.

/**
     * Pull the next stanza from the stream, throwing a XmppException on error.
     * @return Stanza The next stream stanza.
     * @throws XmppException In case of a xmpp error.
     */
public Stanza nextStanza() throws XmppException {
    Stanza stanza = null;
    try {
        stanza = XMLUtils.readStanza(parser);
    } catch (IllegalArgumentException e) {
        throw new XmppMalformedException("can't parse stanza", e);
    } catch (IllegalStateException e) {
        throw new XmppMalformedException("can't parse stanza", e);
    } catch (XmlPullParserException e) {
        throw new XmppMalformedException("can't parse stanza", e);
    } catch (IOException e) {
        throw new XmppTransportException("error during stanza read", e);
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new XmppTransportException("XML reader b0rked", e);
    }
    if (debugEnabled) {
        Log.d(TAG, "Stanza: " + stanza.getXml());
    }
    lastReceiveTime = System.currentTimeMillis();
    return stanza;
}
Also used : XmppMalformedException(com.googlecode.asmack.XmppMalformedException) Stanza(com.googlecode.asmack.Stanza) XmppTransportException(com.googlecode.asmack.connection.XmppTransportException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Aggregations

Stanza (com.googlecode.asmack.Stanza)11 XmppMalformedException (com.googlecode.asmack.XmppMalformedException)5 Node (org.w3c.dom.Node)5 XmppException (com.googlecode.asmack.XmppException)3 Attribute (com.googlecode.asmack.Attribute)2 BroadcastReceiver (android.content.BroadcastReceiver)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 RemoteException (android.os.RemoteException)1 XmppIdentity (com.googlecode.asmack.XmppIdentity)1 XmppTransportException (com.googlecode.asmack.connection.XmppTransportException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 SAXException (org.xml.sax.SAXException)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1