From 4a6a6c84031585ec7985fa16cbc86988d58e8b43 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Tue, 26 Aug 2014 16:52:42 +0200 Subject: basic message acking --- .../conversations/xmpp/OnMessageAcknowledged.java | 7 ++++++ .../siacs/conversations/xmpp/XmppConnection.java | 28 ++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 src/eu/siacs/conversations/xmpp/OnMessageAcknowledged.java (limited to 'src/eu/siacs/conversations/xmpp') diff --git a/src/eu/siacs/conversations/xmpp/OnMessageAcknowledged.java b/src/eu/siacs/conversations/xmpp/OnMessageAcknowledged.java new file mode 100644 index 000000000..5f670d933 --- /dev/null +++ b/src/eu/siacs/conversations/xmpp/OnMessageAcknowledged.java @@ -0,0 +1,7 @@ +package eu.siacs.conversations.xmpp; + +import eu.siacs.conversations.entities.Account; + +public interface OnMessageAcknowledged { + public void onMessageAcknowledged(Account account, String id); +} diff --git a/src/eu/siacs/conversations/xmpp/XmppConnection.java b/src/eu/siacs/conversations/xmpp/XmppConnection.java index ba7a9245d..378e7d62a 100644 --- a/src/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/eu/siacs/conversations/xmpp/XmppConnection.java @@ -31,6 +31,7 @@ import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.os.SystemClock; import android.util.Log; +import android.util.SparseArray; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.utils.CryptoHelper; @@ -74,6 +75,7 @@ public class XmppConnection implements Runnable { private String streamId = null; private int smVersion = 3; + private SparseArray messageReceipts = new SparseArray(); private boolean usingCompression = false; @@ -98,6 +100,7 @@ public class XmppConnection implements Runnable { private OnMessagePacketReceived messageListener = null; private OnStatusChanged statusListener = null; private OnBindListener bindListener = null; + private OnMessageAcknowledged acknowledgedListener = null; private MemorizingTrustManager mMemorizingTrustManager; public XmppConnection(Account account, XmppConnectionService service) { @@ -248,7 +251,6 @@ public class XmppConnection implements Runnable { challange,mRandom)); tagWriter.writeElement(response); } else if (nextTag.isStart("enabled")) { - this.stanzasSent = 0; Element enabled = tagReader.readElement(nextTag); if ("true".equals(enabled.getAttribute("resume"))) { this.streamId = enabled.getAttribute("id"); @@ -276,8 +278,12 @@ public class XmppConnection implements Runnable { Element ack = tagReader.readElement(nextTag); lastPaketReceived = SystemClock.elapsedRealtime(); int serverSequence = Integer.parseInt(ack.getAttribute("h")); - if (serverSequence > this.stanzasSent) { - this.stanzasSent = serverSequence; + String msgId = this.messageReceipts.get(serverSequence); + if (msgId != null) { + if (this.acknowledgedListener != null) { + this.acknowledgedListener.onMessageAcknowledged(account, msgId); + } + this.messageReceipts.remove(serverSequence); } } else if (nextTag.isStart("failed")) { tagReader.readElement(nextTag); @@ -610,10 +616,12 @@ public class XmppConnection implements Runnable { smVersion = 3; EnablePacket enable = new EnablePacket(smVersion); tagWriter.writeStanzaAsync(enable); + stanzasSent = 0; } else if (streamFeatures.hasChild("sm", "urn:xmpp:sm:2")) { smVersion = 2; EnablePacket enable = new EnablePacket(smVersion); tagWriter.writeStanzaAsync(enable); + stanzasSent = 0; } sendServiceDiscoveryInfo(account.getServer()); sendServiceDiscoveryItems(account.getServer()); @@ -751,9 +759,15 @@ public class XmppConnection implements Runnable { private synchronized void sendPacket(final AbstractStanza packet, PacketReceived callback) { - // TODO dont increment stanza count if packet = request packet or ack; - ++stanzasSent; + if (packet.getName().equals("iq") || packet.getName().equals("message") || packet.getName().equals("presence")) { + ++stanzasSent; + } tagWriter.writeStanzaAsync(packet); + if (packet instanceof MessagePacket && packet.getId() != null && this.streamId != null) { + Log.d(LOGTAG,"request delivery report for stanza "+stanzasSent); + this.messageReceipts.put(stanzasSent, packet.getId()); + tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion)); + } if (callback != null) { if (packet.getId() == null) { packet.setId(nextRandomId()); @@ -801,6 +815,10 @@ public class XmppConnection implements Runnable { public void setOnBindListener(OnBindListener listener) { this.bindListener = listener; } + + public void setOnMessageAcknowledgeListener(OnMessageAcknowledged listener) { + this.acknowledgedListener = listener; + } public void disconnect(boolean force) { changeStatus(Account.STATUS_OFFLINE); -- cgit v1.2.3 From 9075d3854dc8fd02a35bf628807b3982815ef7ad Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Wed, 27 Aug 2014 14:51:00 +0200 Subject: properly set stanza count on session resume --- .../siacs/conversations/xmpp/XmppConnection.java | 24 +++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src/eu/siacs/conversations/xmpp') diff --git a/src/eu/siacs/conversations/xmpp/XmppConnection.java b/src/eu/siacs/conversations/xmpp/XmppConnection.java index 378e7d62a..bc1cd7088 100644 --- a/src/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/eu/siacs/conversations/xmpp/XmppConnection.java @@ -266,9 +266,27 @@ public class XmppConnection implements Runnable { tagWriter.writeStanzaAsync(r); } else if (nextTag.isStart("resumed")) { lastPaketReceived = SystemClock.elapsedRealtime(); - Log.d(LOGTAG, account.getJid() + ": session resumed"); - tagReader.readElement(nextTag); - sendPing(); + Element resumed = tagReader.readElement(nextTag); + String h = resumed.getAttribute("h"); + try { + int serverCount = Integer.parseInt(h); + if (serverCount!=stanzasSent) { + Log.d(LOGTAG,account.getJid() + ": session resumed with lost packages"); + stanzasSent = serverCount; + } else { + Log.d(LOGTAG, account.getJid() + ": session resumed"); + } + if (acknowledgedListener!=null) { + for(int i = 0; i < messageReceipts.size(); ++i) { + if (serverCount>=messageReceipts.keyAt(i)) { + acknowledgedListener.onMessageAcknowledged(account, messageReceipts.valueAt(i)); + } + } + } + messageReceipts.clear(); + } catch (NumberFormatException e) { + + } changeStatus(Account.STATUS_ONLINE); } else if (nextTag.isStart("r")) { tagReader.readElement(nextTag); -- cgit v1.2.3 From 4435b0055e9a8b8014a89f3bdec602b8106f992e Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Wed, 27 Aug 2014 19:25:58 +0200 Subject: reset unsend messages to waiting --- src/eu/siacs/conversations/xmpp/XmppConnection.java | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/eu/siacs/conversations/xmpp') diff --git a/src/eu/siacs/conversations/xmpp/XmppConnection.java b/src/eu/siacs/conversations/xmpp/XmppConnection.java index bc1cd7088..961611fd8 100644 --- a/src/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/eu/siacs/conversations/xmpp/XmppConnection.java @@ -635,11 +635,13 @@ public class XmppConnection implements Runnable { EnablePacket enable = new EnablePacket(smVersion); tagWriter.writeStanzaAsync(enable); stanzasSent = 0; + messageReceipts.clear(); } else if (streamFeatures.hasChild("sm", "urn:xmpp:sm:2")) { smVersion = 2; EnablePacket enable = new EnablePacket(smVersion); tagWriter.writeStanzaAsync(enable); stanzasSent = 0; + messageReceipts.clear(); } sendServiceDiscoveryInfo(account.getServer()); sendServiceDiscoveryItems(account.getServer()); -- cgit v1.2.3