leaving low ping timeout mode after coming online

This commit is contained in:
Daniel Gultsch 2016-11-02 09:36:14 +01:00
parent e538272417
commit e582b9fc10
1 changed files with 46 additions and 35 deletions

View File

@ -295,6 +295,11 @@ public class XmppConnectionService extends Service {
mOnAccountUpdate.onAccountUpdate(); mOnAccountUpdate.onAccountUpdate();
} }
if (account.getStatus() == Account.State.ONLINE) { if (account.getStatus() == Account.State.ONLINE) {
synchronized (mLowPingTimeoutMode) {
if (mLowPingTimeoutMode.remove(account.getJid().toBareJid())) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": leaving low ping timeout mode");
}
}
if (account.setShowErrorNotification(true)) { if (account.setShowErrorNotification(true)) {
databaseBackend.updateAccount(account); databaseBackend.updateAccount(account);
} }
@ -336,12 +341,14 @@ public class XmppConnectionService extends Service {
&& listeners; && listeners;
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": push mode="+Boolean.toString(pushMode)+" listeners="+Boolean.toString(listeners)); Log.d(Config.LOGTAG,account.getJid().toBareJid()+": push mode="+Boolean.toString(pushMode)+" listeners="+Boolean.toString(listeners));
if (!disabled && !pushMode) { if (!disabled && !pushMode) {
if (mLowPingTimeoutMode.contains(account.getJid().toBareJid())) { synchronized (mLowPingTimeoutMode) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": went into offline state during low ping mode. reconnecting now"); if (mLowPingTimeoutMode.contains(account.getJid().toBareJid())) {
reconnectAccount(account,true,false); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": went into offline state during low ping mode. reconnecting now");
} else { reconnectAccount(account, true, false);
int timeToReconnect = mRandom.nextInt(20) + 10; } else {
scheduleWakeUpCall(timeToReconnect, account.getUuid().hashCode()); int timeToReconnect = mRandom.nextInt(20) + 10;
scheduleWakeUpCall(timeToReconnect, account.getUuid().hashCode());
}
} }
} }
} else if (account.getStatus() == Account.State.REGISTRATION_SUCCESSFUL) { } else if (account.getStatus() == Account.State.REGISTRATION_SUCCESSFUL) {
@ -628,33 +635,35 @@ public class XmppConnectionService extends Service {
} }
} }
if (account.getStatus() == Account.State.ONLINE) { if (account.getStatus() == Account.State.ONLINE) {
long lastReceived = account.getXmppConnection().getLastPacketReceived(); synchronized (mLowPingTimeoutMode) {
long lastSent = account.getXmppConnection().getLastPingSent(); long lastReceived = account.getXmppConnection().getLastPacketReceived();
long pingInterval = (Config.PUSH_MODE || "ui".equals(action)) ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000; long lastSent = account.getXmppConnection().getLastPingSent();
long msToNextPing = (Math.max(lastReceived, lastSent) + pingInterval) - SystemClock.elapsedRealtime(); long pingInterval = (Config.PUSH_MODE || "ui".equals(action)) ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000;
int pingTimeout = mLowPingTimeoutMode.contains(account.getJid().toBareJid()) ? Config.LOW_PING_TIMEOUT * 1000 : Config.PING_TIMEOUT * 1000; long msToNextPing = (Math.max(lastReceived, lastSent) + pingInterval) - SystemClock.elapsedRealtime();
long pingTimeoutIn = (lastSent + pingTimeout) - SystemClock.elapsedRealtime(); int pingTimeout = mLowPingTimeoutMode.contains(account.getJid().toBareJid()) ? Config.LOW_PING_TIMEOUT * 1000 : Config.PING_TIMEOUT * 1000;
if (lastSent > lastReceived) { long pingTimeoutIn = (lastSent + pingTimeout) - SystemClock.elapsedRealtime();
if (pingTimeoutIn < 0) { if (lastSent > lastReceived) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": ping timeout"); if (pingTimeoutIn < 0) {
this.reconnectAccount(account, true, interactive); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": ping timeout");
} else { this.reconnectAccount(account, true, interactive);
int secs = (int) (pingTimeoutIn / 1000); } else {
this.scheduleWakeUpCall(secs, account.getUuid().hashCode()); int secs = (int) (pingTimeoutIn / 1000);
} this.scheduleWakeUpCall(secs, account.getUuid().hashCode());
} else {
pingCandidates.add(account);
if (CryptoHelper.getAccountFingerprint(account).equals(pushedAccountHash)) {
pingNow = true;
if (mLowPingTimeoutMode.add(account.getJid().toBareJid())) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": entering low ping timeout mode");
} }
} else if (msToNextPing <= 0) {
pingNow = true;
} else { } else {
this.scheduleWakeUpCall((int) (msToNextPing / 1000), account.getUuid().hashCode()); pingCandidates.add(account);
if (mLowPingTimeoutMode.remove(account.getJid().toBareJid())) { if (CryptoHelper.getAccountFingerprint(account).equals(pushedAccountHash)) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": leaving low ping timeout mode"); pingNow = true;
if (mLowPingTimeoutMode.add(account.getJid().toBareJid())) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": entering low ping timeout mode");
}
} else if (msToNextPing <= 0) {
pingNow = true;
} else {
this.scheduleWakeUpCall((int) (msToNextPing / 1000), account.getUuid().hashCode());
if (mLowPingTimeoutMode.remove(account.getJid().toBareJid())) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": leaving low ping timeout mode");
}
} }
} }
} }
@ -695,10 +704,12 @@ public class XmppConnectionService extends Service {
account.getXmppConnection().waitForPush(); account.getXmppConnection().waitForPush();
cancelWakeUpCall(account.getUuid().hashCode()); cancelWakeUpCall(account.getUuid().hashCode());
} else { } else {
final boolean lowTimeout = mLowPingTimeoutMode.contains(account.getJid().toBareJid()); synchronized (mLowPingTimeoutMode) {
account.getXmppConnection().sendPing(); final boolean lowTimeout = mLowPingTimeoutMode.contains(account.getJid().toBareJid());
Log.d(Config.LOGTAG, account.getJid().toBareJid() + " send ping (action=" + action + ",listeners="+Boolean.toString(listeners)+",lowTimeout="+Boolean.toString(lowTimeout)+")"); account.getXmppConnection().sendPing();
scheduleWakeUpCall(lowTimeout ? Config.LOW_PING_TIMEOUT: Config.PING_TIMEOUT, account.getUuid().hashCode()); Log.d(Config.LOGTAG, account.getJid().toBareJid() + " send ping (action=" + action + ",listeners=" + Boolean.toString(listeners) + ",lowTimeout=" + Boolean.toString(lowTimeout) + ")");
scheduleWakeUpCall(lowTimeout ? Config.LOW_PING_TIMEOUT : Config.PING_TIMEOUT, account.getUuid().hashCode());
}
} }
} }
} }