Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Stream Management asmack android

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.

like image 291
Akhil Dad Avatar asked Apr 22 '14 14:04

Akhil Dad


1 Answers

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

like image 186
Flow Avatar answered Nov 14 '22 04:11

Flow