modified clearIqCallbacks into 2-step process

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/parser/IqParser.java     | 12 +-
src/main/java/eu/siacs/conversations/xml/Element.java         |  5 
src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java | 25 +++--
3 files changed, 23 insertions(+), 19 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/parser/IqParser.java 🔗

@@ -236,7 +236,9 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
 
 	@Override
 	public void onIqPacketReceived(final Account account, final IqPacket packet) {
-		if (packet.hasChild("query", Xmlns.ROSTER) && packet.fromServer(account)) {
+		if (packet.getType() == IqPacket.TYPE.ERROR) {
+			return;
+		} else if (packet.hasChild("query", Xmlns.ROSTER) && packet.fromServer(account)) {
 			final Element query = packet.findChild("query");
 			// If this is in response to a query for the whole roster:
 			if (packet.getType() == IqPacket.TYPE.RESULT) {
@@ -306,15 +308,13 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
 			final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
 			mXmppConnectionService.sendIqPacket(account, response, null);
 		} else {
-			if ((packet.getType() == IqPacket.TYPE.GET)
-					|| (packet.getType() == IqPacket.TYPE.SET)) {
+			if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) {
 				final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
 				final Element error = response.addChild("error");
 				error.setAttribute("type", "cancel");
-				error.addChild("feature-not-implemented",
-						"urn:ietf:params:xml:ns:xmpp-stanzas");
+				error.addChild("feature-not-implemented","urn:ietf:params:xml:ns:xmpp-stanzas");
 				account.getXmppConnection().sendIqPacket(response, null);
-					}
+			}
 		}
 	}
 

src/main/java/eu/siacs/conversations/xml/Element.java 🔗

@@ -69,10 +69,9 @@ public class Element {
 
 	public Element findChild(String name, String xmlns) {
 		for (Element child : this.children) {
-			if (child.getName().equals(name)
-					&& (child.getAttribute("xmlns").equals(xmlns))) {
+			if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) {
 				return child;
-					}
+			}
 		}
 		return null;
 	}

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

@@ -1,13 +1,10 @@
 package eu.siacs.conversations.xmpp;
 
-import android.content.Context;
-import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.os.Parcelable;
 import android.os.PowerManager;
 import android.os.PowerManager.WakeLock;
 import android.os.SystemClock;
-import android.preference.PreferenceManager;
 import android.util.Log;
 import android.util.Pair;
 import android.util.SparseArray;
@@ -37,7 +34,6 @@ import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Map;
 import java.util.Map.Entry;
 
 import javax.net.ssl.HostnameVerifier;
@@ -103,7 +99,7 @@ public class XmppConnection implements Runnable {
 	private long lastConnect = 0;
 	private long lastSessionStarted = 0;
 	private int attempt = 0;
-	private final Map<String, Pair<IqPacket, OnIqPacketReceived>> packetCallbacks = new Hashtable<>();
+	private final Hashtable<String, Pair<IqPacket, OnIqPacketReceived>> packetCallbacks = new Hashtable<>();
 	private OnPresencePacketReceived presenceListener = null;
 	private OnJinglePacketReceived jingleListener = null;
 	private OnIqPacketReceived unregisteredIqListener = null;
@@ -727,9 +723,11 @@ public class XmppConnection implements Runnable {
 							sendPostBindInitialization();
 						}
 					} else {
+						Log.d(Config.LOGTAG,account.getJid()+": disconnecting because of bind failure");
 						disconnect(true);
 					}
 				} else {
+					Log.d(Config.LOGTAG,account.getJid()+": disconnecting because of bind failure");
 					disconnect(true);
 				}
 			}
@@ -737,15 +735,19 @@ public class XmppConnection implements Runnable {
 	}
 
 	private void clearIqCallbacks() {
-		Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": clearing iq iq callbacks");
+		Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": clearing "+this.packetCallbacks.size()+" iq callbacks");
 		final IqPacket failurePacket = new IqPacket(IqPacket.TYPE.ERROR);
+		final ArrayList<OnIqPacketReceived> callbacks = new ArrayList<>();
 		synchronized (this.packetCallbacks) {
-			Iterator<Entry<String, Pair<IqPacket, OnIqPacketReceived>>> iterator = this.packetCallbacks.entrySet().iterator();
+			final Iterator<Pair<IqPacket, OnIqPacketReceived>> iterator = this.packetCallbacks.values().iterator();
 			while (iterator.hasNext()) {
-				Entry<String, Pair<IqPacket, OnIqPacketReceived>> entry = iterator.next();
-				entry.getValue().second.onIqPacketReceived(account, failurePacket);
-				iterator.remove();
+				Pair<IqPacket, OnIqPacketReceived> entry = iterator.next();
+				callbacks.add(entry.second);
 			}
+			this.packetCallbacks.clear();
+		}
+		for(OnIqPacketReceived callback : callbacks) {
+			callback.onIqPacketReceived(account,failurePacket);
 		}
 	}
 
@@ -758,6 +760,7 @@ public class XmppConnection implements Runnable {
 				if (packet.getType() == IqPacket.TYPE.RESULT) {
 					sendPostBindInitialization();
 				} else {
+					Log.d(Config.LOGTAG,account.getJid().toBareJid()+": could not init sessions");
 					disconnect(true);
 				}
 			}
@@ -887,6 +890,8 @@ public class XmppConnection implements Runnable {
 			Log.d(Config.LOGTAG,
 					account.getJid().toBareJid() + ": switching resource due to conflict ("
 					+ account.getResource() + ")");
+		} else if (streamError != null) {
+			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": stream error "+streamError.toString());
 		}
 	}