1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-06-29 23:14:52 +02:00

Cleanup of pingMyServer() API

only throw NotConnectedException, otherwise return false. Before as
some sort of exception was always thrown in the error case.
This commit is contained in:
Florian Schmaus 2014-03-28 14:27:14 +01:00
parent 17a254edca
commit 665d65836c
2 changed files with 21 additions and 17 deletions

View file

@ -183,9 +183,10 @@ public class PingManager extends Manager {
* *
* @param jid The id of the entity the ping is being sent to * @param jid The id of the entity the ping is being sent to
* @return true if a reply was received from the entity, false otherwise. * @return true if a reply was received from the entity, false otherwise.
* @throws SmackException if there was no response from the server. * @throws NotConnectedException
* @throws NoResponseException
*/ */
public boolean ping(String jid) throws SmackException { public boolean ping(String jid) throws NoResponseException, NotConnectedException {
return ping(jid, connection().getPacketReplyTimeout()); return ping(jid, connection().getPacketReplyTimeout());
} }
@ -210,9 +211,9 @@ public class PingManager extends Manager {
* {@link #isPingSupported(String)} is false. * {@link #isPingSupported(String)} is false.
* *
* @return true if a reply was received from the server, false otherwise. * @return true if a reply was received from the server, false otherwise.
* @throws SmackException if there was no response from the server. * @throws NotConnectedException
*/ */
public boolean pingMyServer() throws SmackException { public boolean pingMyServer() throws NotConnectedException {
return pingMyServer(true); return pingMyServer(true);
} }
@ -225,13 +226,21 @@ public class PingManager extends Manager {
* *
* @param notifyListeners Notify the PingFailedListener in case of error if true * @param notifyListeners Notify the PingFailedListener in case of error if true
* @return true if the user's server could be pinged. * @return true if the user's server could be pinged.
* @throws SmackException if there was no response from the server. * @throws NotConnectedException
*/ */
public boolean pingMyServer(boolean notifyListeners) throws SmackException { public boolean pingMyServer(boolean notifyListeners) throws NotConnectedException {
boolean res = ping(connection().getServiceName()); boolean res;
try {
res = ping(connection().getServiceName());
}
catch (NoResponseException e) {
res = false;
}
if (res) { if (res) {
pongReceived(); pongReceived();
} else if (notifyListeners) { }
else if (notifyListeners) {
for (PingFailedListener l : pingFailedListeners) for (PingFailedListener l : pingFailedListeners)
l.pingFailed(); l.pingFailed();
} }

View file

@ -24,6 +24,7 @@ import static org.junit.Assert.fail;
import org.jivesoftware.smack.DummyConnection; import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.ThreadedDummyConnection; import org.jivesoftware.smack.ThreadedDummyConnection;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
@ -180,18 +181,12 @@ public class PingTest extends InitExtensions {
} }
@Test @Test
public void checkPingToServerTimeout() throws SmackException { public void checkPingToServerTimeout() throws NotConnectedException {
DummyConnection con = new DummyConnection(); DummyConnection con = new DummyConnection();
PingManager pinger = PingManager.getInstanceFor(con); PingManager pinger = PingManager.getInstanceFor(con);
try { boolean res = pinger.pingMyServer();
pinger.pingMyServer(); assertFalse(res);
}
catch (NoResponseException e) {
return;
}
fail();
} }
@Test @Test