fixed #53 aka server not found bug

Daniel Gultsch created

Change summary

src/eu/siacs/conversations/entities/Account.java               |  2 
src/eu/siacs/conversations/services/XmppConnectionService.java | 48 +--
src/eu/siacs/conversations/xmpp/XmppConnection.java            | 16 +
3 files changed, 39 insertions(+), 27 deletions(-)

Detailed changes

src/eu/siacs/conversations/entities/Account.java 🔗

@@ -135,7 +135,7 @@ public class Account  extends AbstractEntity{
 	}
 	
 	public boolean hasErrorStatus() {
-		return getStatus() > STATUS_NO_INTERNET;
+		return getStatus() > STATUS_NO_INTERNET && (getXmppConnection().getAttempt() >= 2);
 	}
 	
 	public void setResource(String resource) {

src/eu/siacs/conversations/services/XmppConnectionService.java 🔗

@@ -242,10 +242,17 @@ public class XmppConnectionService extends Service {
 			} else if (account.getStatus() == Account.STATUS_REGISTRATION_SUCCESSFULL) {
 				databaseBackend.updateAccount(account);
 				reconnectAccount(account, true);
-			} else {
-				UIHelper.showErrorNotification(getApplicationContext(),
-						getAccounts());
+			} else if (account.getStatus() != Account.STATUS_CONNECTING) {
+				int next = account.getXmppConnection().getTimeToNextAttempt();
+				Log.d(LOGTAG, account.getJid()
+						+ ": error connecting account. try again in " + next
+						+ "s for the "
+						+ (account.getXmppConnection().getAttempt() + 1)
+						+ " time");
+				scheduleWakeupCall(next, false);
 			}
+			UIHelper.showErrorNotification(getApplicationContext(),
+					getAccounts());
 		}
 	};
 
@@ -576,8 +583,6 @@ public class XmppConnectionService extends Service {
 							statusListener.onStatusChanged(account);
 						}
 					}
-
-					// TODO 3 remaining cases
 					if (account.getStatus() == Account.STATUS_ONLINE) {
 						long lastReceived = account.getXmppConnection().lastPaketReceived;
 						long lastSent = account.getXmppConnection().lastPingSent;
@@ -605,15 +610,9 @@ public class XmppConnectionService extends Service {
 								+ ": time out during connect reconnecting");
 						reconnectAccount(account, true);
 					} else {
-						Log.d(LOGTAG,
-								"seconds since last connect:"
-										+ ((SystemClock.elapsedRealtime() - account
-												.getXmppConnection().lastConnect) / 1000));
-						Log.d(LOGTAG,
-								account.getJid() + ": status="
-										+ account.getStatus());
-						// TODO notify user of ssl cert problem or auth problem
-						// or what ever
+						if (account.getXmppConnection().getTimeToNextAttempt() <= 0) {
+							reconnectAccount(account, true);
+						}
 					}
 					// in any case. reschedule wakup call
 					this.scheduleWakeupCall(PING_MAX_INTERVAL, true);
@@ -676,7 +675,6 @@ public class XmppConnectionService extends Service {
 						this.pingIntent, 0);
 				alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
 						timeToWake, pendingPingIntent);
-				// Log.d(LOGTAG,"schedule ping in "+seconds+" seconds");
 			} else {
 				long scheduledTime = this.pingIntent.getLongExtra("time", 0);
 				if (scheduledTime < SystemClock.elapsedRealtime()
@@ -687,7 +685,6 @@ public class XmppConnectionService extends Service {
 							context, 0, this.pingIntent, 0);
 					alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
 							timeToWake, pendingPingIntent);
-					// Log.d(LOGTAG,"reschedule old ping to ping in "+seconds+" seconds");
 				}
 			}
 		} else {
@@ -829,16 +826,16 @@ public class XmppConnectionService extends Service {
 			}
 		}
 	}
-	
+
 	private void resendMessage(Message message) {
 		Account account = message.getConversation().getAccount();
 		MessagePacket packet = null;
 		if (message.getEncryption() == Message.ENCRYPTION_NONE) {
-			packet = prepareMessagePacket(account, message,null);
+			packet = prepareMessagePacket(account, message, null);
 		} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
 			packet = prepareMessagePacket(account, message, null);
 			packet.setBody("This is an XEP-0027 encryted message");
-			if (message.getEncryptedBody()==null) {
+			if (message.getEncryptedBody() == null) {
 				markMessage(message, Message.STATUS_SEND_FAILED);
 				return;
 			}
@@ -850,7 +847,7 @@ public class XmppConnectionService extends Service {
 			packet.addChild("x", "jabber:x:encrypted").setContent(
 					message.getBody());
 		}
-		if (packet!=null) {
+		if (packet != null) {
 			account.getXmppConnection().sendMessagePacket(packet);
 			markMessage(message, Message.STATUS_SEND);
 		}
@@ -1159,8 +1156,7 @@ public class XmppConnectionService extends Service {
 		if (accountChangedListener != null) {
 			accountChangedListener.onAccountListChangedListener();
 		}
-		UIHelper.showErrorNotification(getApplicationContext(),
-				getAccounts());
+		UIHelper.showErrorNotification(getApplicationContext(), getAccounts());
 	}
 
 	public void deleteAccount(Account account) {
@@ -1172,8 +1168,7 @@ public class XmppConnectionService extends Service {
 		if (accountChangedListener != null) {
 			accountChangedListener.onAccountListChangedListener();
 		}
-		UIHelper.showErrorNotification(getApplicationContext(),
-				getAccounts());
+		UIHelper.showErrorNotification(getApplicationContext(), getAccounts());
 	}
 
 	public void setOnConversationListChangedListener(
@@ -1492,8 +1487,9 @@ public class XmppConnectionService extends Service {
 		}
 		return false;
 	}
-	
-	public boolean markMessage(Conversation conversation, String uuid, int status) {
+
+	public boolean markMessage(Conversation conversation, String uuid,
+			int status) {
 		for (Message message : conversation.getMessages()) {
 			if (message.getUuid().equals(uuid)) {
 				markMessage(message, status);

src/eu/siacs/conversations/xmpp/XmppConnection.java 🔗

@@ -88,6 +88,8 @@ public class XmppConnection implements Runnable {
 	public long lastPingSent = 0;
 	public long lastConnect = 0;
 	public long lastSessionStarted = 0;
+	
+	private int attempt = 0;
 
 	private static final int PACKET_IQ = 0;
 	private static final int PACKET_MESSAGE = 1;
@@ -113,6 +115,9 @@ public class XmppConnection implements Runnable {
 			if ((nextStatus == Account.STATUS_OFFLINE)&&(account.getStatus() != Account.STATUS_CONNECTING)&&(account.getStatus() != Account.STATUS_ONLINE)&&(account.getStatus() != Account.STATUS_DISABLED)) {
 				return;
 			}
+			if (nextStatus == Account.STATUS_ONLINE) {
+				this.attempt = 0;
+			}
 			account.setStatus(nextStatus);
 			if (statusListener != null) {
 				statusListener.onStatusChanged(account);
@@ -123,6 +128,7 @@ public class XmppConnection implements Runnable {
 	protected void connect() {
 		Log.d(LOGTAG,account.getJid()+ ": connecting");
 		lastConnect = SystemClock.elapsedRealtime();
+		this.attempt++;
 		try {
 			shouldAuthenticate = shouldBind = !account.isOptionSet(Account.OPTION_REGISTER);
 			tagReader = new XmlReader(wakeLock);
@@ -916,4 +922,14 @@ public class XmppConnection implements Runnable {
 		Log.d(LOGTAG,"adding "+jid+" to pending subscriptions");
 		this.pendingSubscriptions.add(jid);
 	}
+
+	public int getTimeToNextAttempt() {
+		int interval = (int) (25 * Math.pow(1.5,attempt));
+		int secondsSinceLast = (int) ((SystemClock.elapsedRealtime() - this.lastConnect) / 1000);
+		return interval - secondsSinceLast;
+	}
+	
+	public int getAttempt() {
+		return this.attempt;
+	}
 }