* Use {@link #isPingSupported(String)} to determine if XMPP Ping is supported
* by the entity.
@@ -65,7 +68,8 @@ public class PingManager {
SyncPacketSend.getReply(connection, ping);
}
catch (XMPPException exc) {
- return false;
+
+ return (jid.equals(connection.getServiceName()) && (exc.getSmackError() != SmackError.NO_RESPONSE_FROM_SERVER));
}
return true;
}
@@ -92,4 +96,17 @@ public class PingManager {
DiscoverInfo result = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(jid);
return result.containsFeature(Ping.NAMESPACE);
}
+
+ /**
+ * Pings the server. This method will return true if the server is reachable. It
+ * is the equivalent of calling ping
with the XMPP domain.
+ *
+ * Unlike the {@link #ping(String)} case, this method will return true even if
+ * {@link #isPingSupported(String)} is false.
+ *
+ * @return true if a reply was received from the server, false otherwise.
+ */
+ public boolean pingMyServer() {
+ return ping(connection.getServiceName());
+ }
}
diff --git a/test-unit/org/jivesoftware/smack/ping/ServerPingTest.java b/test-unit/org/jivesoftware/smack/ping/KeepaliveTest.java
similarity index 87%
rename from test-unit/org/jivesoftware/smack/ping/ServerPingTest.java
rename to test-unit/org/jivesoftware/smack/ping/KeepaliveTest.java
index fa16dce03..25927d4e8 100644
--- a/test-unit/org/jivesoftware/smack/ping/ServerPingTest.java
+++ b/test-unit/org/jivesoftware/smack/ping/KeepaliveTest.java
@@ -21,9 +21,12 @@ import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.ping.packet.Ping;
import org.jivesoftware.smack.util.PacketParserUtils;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
-public class ServerPingTest {
+public class KeepaliveTest {
+ private static final long PING_MINIMUM = 1000;
private static String TO = "juliet@capulet.lit/balcony";
private static String ID = "s2c1";
@@ -31,7 +34,21 @@ public class ServerPingTest {
{
outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
}
-
+
+ private int originalTimeout;
+
+ @Before
+ public void resetProperties()
+ {
+ originalTimeout = SmackConfiguration.getPacketReplyTimeout();
+ SmackConfiguration.setPacketReplyTimeout(1000);
+ }
+
+ @After
+ public void restoreProperties()
+ {
+ SmackConfiguration.setPacketReplyTimeout(originalTimeout);
+ }
/*
* Stanza copied from spec
*/
@@ -133,7 +150,7 @@ public class ServerPingTest {
private DummyConnection getConnection() {
DummyConnection con = new DummyConnection();
ServerPingManager mgr = ServerPingManager.getInstanceFor(con);
- mgr.setPingInterval(ServerPingManager.PING_MINIMUM);
+ mgr.setPingInterval(PING_MINIMUM);
return con;
}
@@ -141,7 +158,7 @@ public class ServerPingTest {
private ThreadedDummyConnection getThreadedConnection() {
ThreadedDummyConnection con = new ThreadedDummyConnection();
ServerPingManager mgr = ServerPingManager.getInstanceFor(con);
- mgr.setPingInterval(ServerPingManager.PING_MINIMUM);
+ mgr.setPingInterval(PING_MINIMUM);
return con;
}
@@ -157,6 +174,6 @@ public class ServerPingTest {
}
private long getWaitTime() {
- return ServerPingManager.PING_MINIMUM + SmackConfiguration.getPacketReplyTimeout() + 3000;
+ return PING_MINIMUM + SmackConfiguration.getPacketReplyTimeout() + 3000;
}
}
diff --git a/test-unit/org/jivesoftware/smackx/ping/PingPongTest.java b/test-unit/org/jivesoftware/smackx/ping/PingTest.java
similarity index 69%
rename from test-unit/org/jivesoftware/smackx/ping/PingPongTest.java
rename to test-unit/org/jivesoftware/smackx/ping/PingTest.java
index 0346bb034..b7b6358b8 100644
--- a/test-unit/org/jivesoftware/smackx/ping/PingPongTest.java
+++ b/test-unit/org/jivesoftware/smackx/ping/PingTest.java
@@ -23,19 +23,28 @@ import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.ping.packet.Ping;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.packet.DiscoverInfo;
+import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
-public class PingPongTest {
-
+public class PingTest {
+ private DummyConnection dummyCon;
+ private ThreadedDummyConnection threadedCon;
+
+ @Before
+ public void setup() {
+ dummyCon = new DummyConnection();
+ threadedCon = new ThreadedDummyConnection();
+ }
+
@Test
public void checkSendingPing() throws Exception {
- DummyConnection con = new DummyConnection();
- PingManager pinger = new PingManager(con);
+ dummyCon = new DummyConnection();
+ PingManager pinger = new PingManager(dummyCon);
pinger.ping("test@myserver.com");
- Packet sentPacket = con.getSentPacket();
+ Packet sentPacket = dummyCon.getSentPacket();
assertTrue(sentPacket instanceof Ping);
@@ -43,9 +52,9 @@ public class PingPongTest {
@Test
public void checkSuccessfulPing() throws Exception {
- ThreadedDummyConnection con = new ThreadedDummyConnection();
+ threadedCon = new ThreadedDummyConnection();
- PingManager pinger = new PingManager(con);
+ PingManager pinger = new PingManager(threadedCon);
boolean pingSuccess = pinger.ping("test@myserver.com");
@@ -59,8 +68,8 @@ public class PingPongTest {
*/
@Test
public void checkFailedPingOnTimeout() throws Exception {
- DummyConnection con = new DummyConnection();
- PingManager pinger = new PingManager(con);
+ dummyCon = new DummyConnection();
+ PingManager pinger = new PingManager(dummyCon);
boolean pingSuccess = pinger.ping("test@myserver.com");
@@ -69,12 +78,12 @@ public class PingPongTest {
}
/**
- * DummyConnection will not reply so it will timeout.
+ * Server returns an exception for entity.
* @throws Exception
*/
@Test
- public void checkFailedPingError() throws Exception {
- ThreadedDummyConnection con = new ThreadedDummyConnection();
+ public void checkFailedPingToEntityError() throws Exception {
+ threadedCon = new ThreadedDummyConnection();
//@formatter:off
String reply =
"