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);
}
}
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;
}
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());
}
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);
}
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;
}
Aggregations