19
votes

I am facing an issue with using XEP-0198 stream management.

Basically, I want to enable this for message dropping issue on Android when internet disconect randomly and server still has client presence online.

To solve this issue I want to use XEP-0198 which uses request and acknowledgement process. You can find more here.

Basically I am using sm:3 for this. The problem is that when I set

XMPPConnection.DEBUG_ENABLED=true;

I get sm:3 in log which is internal to asmack but I am unable to get that by ading any packetListner over connection.

This is what inside asmack debug prints:

<r xmlns='urn:xmpp:sm:3'/><message from='[email protected]/Smack' to='[email protected]' id='CQUe6-5'><received xmlns='urn:xmpp:receipts' id='CQUe6-4'/></message><r xmlns='urn:xmpp:sm:3'/>

This is what I get from packetFilter:

<message id="CQUe6-5" to="[email protected]" from="[email protected]/Smack"><received xmlns='urn:xmpp:receipts' id='CQUe6-4'/></message>

I had tried for custom packet filter by seeing the code of chat secure and yaxim as well but I am not getting how can I get this <r xmlns='urn:xmpp:sm:3'/> in my code so that I can return the number of packets received untill now to server so that server can send me back any missing packet.

I had also configured provider manager for this by adding the code below:

        addSimplePacketExtension("sm", URN_SM_3);
        addSimplePacketExtension("r", URN_SM_3);
        addSimplePacketExtension("a", URN_SM_3);
        addSimplePacketExtension("enabled", URN_SM_3);
        addSimplePacketExtension("resumed", URN_SM_3);
        addSimplePacketExtension("failed", URN_SM_3);


private static final String URN_SM_3 = "urn:xmpp:sm:3";
    private static void addSimplePacketExtension(final String name, final String namespace) {
        Log.e("adding simple packet extension", namespace+"---"+name);
        ProviderManager.getInstance().addExtensionProvider(name, namespace,
                new PacketExtensionProvider() {
            public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
                StreamHandlingPacket packet = new StreamHandlingPacket(name, namespace);
                Log.e("Stream ahndling packet ","------>"+packet.toXML());
                int attributeCount = parser.getAttributeCount();
                for (int i = 0 ; i < attributeCount ; i++) {
                    packet.addAttribute(parser.getAttributeName(i), parser.getAttributeValue(i));
                }
                return (PacketExtension) packet;
            }
        });
    }


static class StreamHandlingPacket extends Packet {
        private String name;
        private String namespace;
        Map<String, String> attributes;

        StreamHandlingPacket(String name, String namespace) {
            this.name = name;
            this.namespace = namespace;
            attributes = Collections.emptyMap();
        }

        public void addAttribute(String name, String value) {
            if (attributes == Collections.EMPTY_MAP)
                attributes = new HashMap<String, String>();
            attributes.put(name, value);
        }

        public String getAttribute(String name) {
            return attributes.get(name);
        }

        public String getNamespace() {
            return namespace;
        }

        public String getElementName() {
            return name;
        }

        public String toXML() {
            StringBuilder buf = new StringBuilder();
            buf.append("<").append(getElementName());

            // TODO Xmlns??
            if (getNamespace() != null) {
                buf.append(" xmlns=\"").append(getNamespace()).append("\"");
            }
            for (String key : attributes.keySet()) {
                buf.append(" ").append(key).append("=\"").append(StringUtils.escapeForXML(attributes.get(key))).append("\"");
            }
            buf.append("/>");
            Log.e("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&","&&&&&&&&&&&&&&&&&&&&&&&&&&&&=>"+buf.toString());
            return buf.toString();
        }

    }

Basically I get this idea by seeing chat secure implementation but it extends UnkownPacket rather than Packet. I had taken help from here.

I have also seen a git branch of asmack like this but wasn't able to understand how to implement it.

Please help if you have implemented it in any way with any library like chat secure, yaxim or any other android XMPP client.

3

3 Answers

1
votes

I recommend using Smack 4.1, which runs on Android and supports XEP-198 Stream Management.

0
votes

I can't offer you a solution because I'm having a similar problem but I can tell you from the second link that UnknownPacket extends Packet AND implements PacketExtension, that's why you had to cast your StreamHandlingPacket to (PacketExtension) in the return statement of your parseExtension method.

Are you able to catch the <enabled xmlns='urn:xmpp:sm:3' etc> xml that the XMPP server returns when you send <enable xmlns='urn:xmpp:sm:3' etc>? If so, that would be a beginning for your problem.

Let us know how it goes!

0
votes

The patched asmack yaxim uses can be found here (the creator was kind enought to direct me to it, thanks Ge0rG!):

https://github.com/pfleidi/yaxim/tree/master/libs

Be sure to get libidn as well.