1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-23 02:58:00 +01:00

Add PingManager.pingServerIfNecessary()

which is basically the body of the pingServerRunnable available as
public part of the API. The intention is to allow 3rd party
components (e.g. Android's AlarmManager) to trigger the code.
This commit is contained in:
Florian Schmaus 2014-09-08 10:07:01 +02:00
parent 184604bba2
commit 090f7cfc49

View file

@ -317,70 +317,76 @@ public class PingManager extends Manager {
} }
} }
private final Runnable pingServerRunnable = new Runnable() { /**
private static final int DELTA = 1000; // 1 seconds * Ping the server if deemed necessary because automatic server pings are
private static final int TRIES = 3; // 3 tries * enabled ({@link #setPingInterval(int)}) and the ping interval has expired.
*/
public void run() { public synchronized void pingServerIfNecessary() {
LOGGER.fine("ServerPingTask run()"); final int DELTA = 1000; // 1 seconds
XMPPConnection connection = connection(); final int TRIES = 3; // 3 tries
if (connection == null) { final XMPPConnection connection = connection();
// connection has been collected by GC if (connection == null) {
// which means we can stop the thread by breaking the loop // connection has been collected by GC
// 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; return;
} }
if (pingInterval <= 0) { }
// Ping has been disabled if (connection.isAuthenticated()) {
return; boolean res = false;
}
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++) { for (int i = 0; i < TRIES; i++) {
if (i != 0) { if (i != 0) {
try {
Thread.sleep(DELTA);
} catch (InterruptedException e) {
// We received an interrupt
// This only happens if we should stop pinging
return;
}
}
try { try {
res = pingMyServer(false); Thread.sleep(DELTA);
} } catch (InterruptedException e) {
catch (SmackException e) { // We received an interrupt
LOGGER.log(Level.WARNING, "SmackError while pinging server", e); // This only happens if we should stop pinging
res = false; return;
}
// stop when we receive a pong back
if (res) {
break;
} }
} }
LOGGER.fine("ServerPingTask res=" + res); try {
if (!res) { res = pingMyServer(false);
for (PingFailedListener l : pingFailedListeners) { }
l.pingFailed(); catch (SmackException e) {
} LOGGER.log(Level.WARNING, "SmackError while pinging server", e);
} else { res = false;
// Ping was successful, wind-up the periodic task again }
maybeSchedulePingServerTask(); // stop when we receive a pong back
if (res) {
break;
}
}
if (!res) {
for (PingFailedListener l : pingFailedListeners) {
l.pingFailed();
} }
} else { } else {
LOGGER.warning("ServerPingTask: XMPPConnection was not authenticated"); // Ping was successful, wind-up the periodic task again
maybeSchedulePingServerTask();
} }
} else {
LOGGER.warning("XMPPConnection was not authenticated");
}
}
private final Runnable pingServerRunnable = new Runnable() {
public void run() {
LOGGER.fine("ServerPingTask run()");
pingServerIfNecessary();
} }
}; };