Search in sources :

Example 1 with Ping

use of org.jivesoftware.smackx.ping.packet.Ping in project xabber-android by redsolution.

the class PingManager method onPacket.

@Override
public void onPacket(ConnectionItem connection, final String bareAddress, Stanza packet) {
    if (!(connection instanceof AccountItem))
        return;
    final String account = ((AccountItem) connection).getAccount();
    if (!(packet instanceof Ping))
        return;
    final Ping ping = (Ping) packet;
    if (ping.getType() != IQ.Type.get)
        return;
    try {
        ConnectionManager.getInstance().sendStanza(account, IQ.createResultIQ(ping));
    } catch (NetworkException e) {
        LogManager.exception(this, e);
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) Ping(org.jivesoftware.smackx.ping.packet.Ping) NetworkException(com.xabber.android.data.NetworkException)

Example 2 with Ping

use of org.jivesoftware.smackx.ping.packet.Ping in project Smack by igniterealtime.

the class PingManager method pingAsync.

public SmackFuture<Boolean, Exception> pingAsync(final Jid jid, long pongTimeout) {
    final InternalProcessStanzaSmackFuture<Boolean, Exception> future = new InternalProcessStanzaSmackFuture<Boolean, Exception>() {

        @Override
        public void handleStanza(Stanza packet) {
            setResult(true);
        }

        @Override
        public boolean isNonFatalException(Exception exception) {
            if (exception instanceof XMPPErrorException) {
                XMPPErrorException xmppErrorException = (XMPPErrorException) exception;
                if (isValidErrorPong(jid, xmppErrorException)) {
                    setResult(true);
                    return true;
                }
            }
            return false;
        }
    };
    XMPPConnection connection = connection();
    Ping ping = new Ping(connection, jid);
    connection.sendIqRequestAsync(ping, pongTimeout).onSuccess(new SuccessCallback<IQ>() {

        @Override
        public void onSuccess(IQ result) {
            future.processStanza(result);
        }
    }).onError(new ExceptionCallback<Exception>() {

        @Override
        public void processException(Exception exception) {
            future.processException(exception);
        }
    });
    return future;
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) SuccessCallback(org.jivesoftware.smack.util.SuccessCallback) InternalProcessStanzaSmackFuture(org.jivesoftware.smack.SmackFuture.InternalProcessStanzaSmackFuture) Stanza(org.jivesoftware.smack.packet.Stanza) Ping(org.jivesoftware.smackx.ping.packet.Ping) IQ(org.jivesoftware.smack.packet.IQ) XMPPConnection(org.jivesoftware.smack.XMPPConnection) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException)

Example 3 with Ping

use of org.jivesoftware.smackx.ping.packet.Ping in project Smack by igniterealtime.

the class PingTest method checkProvider.

@Test
public void checkProvider() throws Exception {
    // @formatter:off
    String control = "<iq from='capulet.lit' to='juliet@capulet.lit/balcony' id='s2c1' type='get'>" + "<ping xmlns='urn:xmpp:ping'/>" + "</iq>";
    // @formatter:on
    DummyConnection con = new DummyConnection();
    con.connect();
    // Enable ping for this connection
    PingManager.getInstanceFor(con);
    IQ pingRequest = PacketParserUtils.parseStanza(control);
    assertTrue(pingRequest instanceof Ping);
    con.processStanza(pingRequest);
    Stanza pongPacket = con.getSentPacket();
    assertTrue(pongPacket instanceof IQ);
    IQ pong = (IQ) pongPacket;
    assertThat("capulet.lit", equalsCharSequence(pong.getTo()));
    assertEquals("s2c1", pong.getStanzaId());
    assertEquals(IQ.Type.result, pong.getType());
}
Also used : DummyConnection(org.jivesoftware.smack.DummyConnection) ThreadedDummyConnection(org.jivesoftware.smack.ThreadedDummyConnection) Ping(org.jivesoftware.smackx.ping.packet.Ping) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) Test(org.junit.jupiter.api.Test)

Example 4 with Ping

use of org.jivesoftware.smackx.ping.packet.Ping in project Smack by igniterealtime.

the class PingTest method checkSendingPing.

@Test
public void checkSendingPing() throws InterruptedException, SmackException, IOException, XMPPException {
    DummyConnection dummyCon = getAuthenticatedDummyConnection();
    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.jupiter.api.Test)

Example 5 with Ping

use of org.jivesoftware.smackx.ping.packet.Ping in project Smack by igniterealtime.

the class PingManager method ping.

/**
 * Pings the given jid. This method will return false if an error occurs.  The exception
 * to this, is a server ping, which will always return true if the server is reachable,
 * event if there is an error on the ping itself (i.e. ping not supported).
 * <p>
 * Use {@link #isPingSupported(Jid)} to determine if XMPP Ping is supported
 * by the entity.
 *
 * @param jid The id of the entity the ping is being sent to
 * @param pingTimeout The time to wait for a reply in milliseconds
 * @return true if a reply was received from the entity, false otherwise.
 * @throws NoResponseException if there was no response from the jid.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public boolean ping(Jid jid, long pingTimeout) throws NotConnectedException, NoResponseException, InterruptedException {
    final XMPPConnection connection = connection();
    // otherwise the client JID will be null causing an NPE
    if (!connection.isAuthenticated()) {
        throw new NotConnectedException();
    }
    Ping ping = new Ping(connection, jid);
    try {
        connection.createStanzaCollectorAndSend(ping).nextResultOrThrow(pingTimeout);
    } catch (XMPPErrorException e) {
        return isValidErrorPong(jid, e);
    }
    return true;
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) Ping(org.jivesoftware.smackx.ping.packet.Ping) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Aggregations

Ping (org.jivesoftware.smackx.ping.packet.Ping)5 Stanza (org.jivesoftware.smack.packet.Stanza)3 DummyConnection (org.jivesoftware.smack.DummyConnection)2 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)2 ThreadedDummyConnection (org.jivesoftware.smack.ThreadedDummyConnection)2 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)2 IQ (org.jivesoftware.smack.packet.IQ)2 Test (org.junit.jupiter.api.Test)2 NetworkException (com.xabber.android.data.NetworkException)1 AccountItem (com.xabber.android.data.account.AccountItem)1 SmackException (org.jivesoftware.smack.SmackException)1 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)1 InternalProcessStanzaSmackFuture (org.jivesoftware.smack.SmackFuture.InternalProcessStanzaSmackFuture)1 SuccessCallback (org.jivesoftware.smack.util.SuccessCallback)1