Search in sources :

Example 26 with SmackException

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

the class PingManager method pingServerIfNecessary.

/**
     * Ping the server if deemed necessary because automatic server pings are
     * enabled ({@link #setPingInterval(int)}) and the ping interval has expired.
     */
public synchronized void pingServerIfNecessary() {
    // 1 seconds
    final int DELTA = 1000;
    // 3 tries
    final int TRIES = 3;
    final XMPPConnection connection = connection();
    if (connection == null) {
        // which means we can stop the thread by breaking the loop
        return;
    }
    if (pingInterval <= 0) {
        // Ping has been disabled
        return;
    }
    long lastStanzaReceived = connection.getLastStanzaReceived();
    if (lastStanzaReceived > 0) {
        long now = System.currentTimeMillis();
        // Delta since the last stanza was received
        int deltaInSeconds = (int) ((now - lastStanzaReceived) / 1000);
        // If the delta is small then the ping interval, then we can defer the ping
        if (deltaInSeconds < pingInterval) {
            maybeSchedulePingServerTask(deltaInSeconds);
            return;
        }
    }
    if (connection.isAuthenticated()) {
        boolean res = false;
        for (int i = 0; i < TRIES; i++) {
            if (i != 0) {
                try {
                    Thread.sleep(DELTA);
                } catch (InterruptedException e) {
                    // This only happens if we should stop pinging
                    return;
                }
            }
            try {
                res = pingMyServer(false);
            } catch (InterruptedException | SmackException e) {
                // Note that we log the connection here, so that it is not GC'ed between the call to isAuthenticated
                // a few lines above and the usage of the connection within pingMyServer(). In order to prevent:
                // https://community.igniterealtime.org/thread/59369
                LOGGER.log(Level.WARNING, "Exception while pinging server of " + connection, e);
                res = false;
            }
            // stop when we receive a pong back
            if (res) {
                break;
            }
        }
        if (!res) {
            for (PingFailedListener l : pingFailedListeners) {
                l.pingFailed();
            }
        } else {
            // Ping was successful, wind-up the periodic task again
            maybeSchedulePingServerTask();
        }
    } else {
        LOGGER.warning("XMPPConnection was not authenticated");
    }
}
Also used : SmackException(org.jivesoftware.smack.SmackException) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 27 with SmackException

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

the class AffiliationProvider method parse.

@Override
public Affiliation parse(XmlPullParser parser, int initialDepth) throws Exception {
    String node = parser.getAttributeValue(null, "node");
    BareJid jid = ParserUtils.getBareJidAttribute(parser);
    String affiliationString = parser.getAttributeValue(null, "affiliation");
    Affiliation.Type affiliationType = null;
    if (affiliationString != null) {
        affiliationType = Affiliation.Type.valueOf(affiliationString);
    }
    Affiliation affiliation;
    if (node != null && jid == null) {
        // affiliationType may be empty
        affiliation = new Affiliation(node, affiliationType);
    } else if (node == null && jid != null) {
        // TODO
        PubSubNamespace namespace = null;
        affiliation = new Affiliation(jid, affiliationType, namespace);
    } else {
        throw new SmackException("Invalid affililation. Either one of 'node' or 'jid' must be set" + ". Node: " + node + ". Jid: " + jid + '.');
    }
    return affiliation;
}
Also used : BareJid(org.jxmpp.jid.BareJid) SmackException(org.jivesoftware.smack.SmackException) Affiliation(org.jivesoftware.smackx.pubsub.Affiliation) PubSubNamespace(org.jivesoftware.smackx.pubsub.packet.PubSubNamespace)

Example 28 with SmackException

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

the class PingTest method checkSendingPing.

@Test
public void checkSendingPing() throws InterruptedException, SmackException, IOException, XMPPException {
    DummyConnection dummyCon = getAuthentiactedDummyConnection();
    PingManager pinger = PingManager.getInstanceFor(dummyCon);
    try {
        pinger.ping(DUMMY_AT_EXAMPLE_ORG);
    } catch (SmackException e) {
    // Ignore the fact the server won't answer for this unit test.
    }
    Stanza sentPacket = dummyCon.getSentPacket();
    assertTrue(sentPacket instanceof Ping);
}
Also used : DummyConnection(org.jivesoftware.smack.DummyConnection) ThreadedDummyConnection(org.jivesoftware.smack.ThreadedDummyConnection) SmackException(org.jivesoftware.smack.SmackException) Stanza(org.jivesoftware.smack.packet.Stanza) Ping(org.jivesoftware.smackx.ping.packet.Ping) Test(org.junit.Test)

Example 29 with SmackException

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

the class JingleContentDescriptionProvider method parse.

/**
     * Parse a iq/jingle/description element.
     *
     * @param parser the input to parse
     * @return a description element
     * @throws IOException 
     * @throws XmlPullParserException 
     * @throws SmackException 
     */
@Override
public JingleContentDescription parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException {
    boolean done = false;
    JingleContentDescription desc = getInstance();
    while (!done) {
        int eventType = parser.next();
        String name = parser.getName();
        if (eventType == XmlPullParser.START_TAG) {
            if (name.equals(JingleContentDescription.JinglePayloadType.NODENAME)) {
                desc.addJinglePayloadType(parsePayload(parser));
            } else {
                throw new SmackException("Unknow element \"" + name + "\" in content.");
            }
        } else if (eventType == XmlPullParser.END_TAG) {
            if (name.equals(JingleContentDescription.NODENAME)) {
                done = true;
            }
        }
    }
    return desc;
}
Also used : JingleContentDescription(org.jivesoftware.smackx.jingleold.packet.JingleContentDescription) SmackException(org.jivesoftware.smack.SmackException)

Example 30 with SmackException

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

the class JingleDescriptionProvider method parse.

/**
     * Parse a iq/jingle/description element.
     * 
     * @param parser
     *            the input to parse
     * @return a description element
     * @throws SmackException 
     * @throws IOException 
     * @throws XmlPullParserException 
     */
@Override
public JingleDescription parse(XmlPullParser parser, int initialDepth) throws SmackException, XmlPullParserException, IOException {
    boolean done = false;
    JingleDescription desc = getInstance();
    while (!done) {
        int eventType = parser.next();
        String name = parser.getName();
        if (eventType == XmlPullParser.START_TAG) {
            if (name.equals(PayloadType.NODENAME)) {
                desc.addPayloadType(parsePayload(parser));
            } else {
                throw new SmackException("Unknow element \"" + name + "\" in content.");
            }
        } else if (eventType == XmlPullParser.END_TAG) {
            if (name.equals(JingleDescription.NODENAME)) {
                done = true;
            }
        }
    }
    return desc;
}
Also used : JingleDescription(org.jivesoftware.smackx.jingleold.packet.JingleDescription) SmackException(org.jivesoftware.smack.SmackException)

Aggregations

SmackException (org.jivesoftware.smack.SmackException)48 IOException (java.io.IOException)20 XMPPException (org.jivesoftware.smack.XMPPException)13 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)10 Test (org.junit.Test)9 Socket (java.net.Socket)8 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)6 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)5 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)5 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)5 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)5 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 ConnectException (java.net.ConnectException)4 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)4 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)4 InputStream (java.io.InputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ServerSocket (java.net.ServerSocket)3 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)3