Project

General

Profile

Plugin processes a packet 2 times from sender

Igor Khomenko
Added over 3 years ago

Hi there,

I develop a plugin to track message statuses (delivered, read) http://xmpp.org/extensions/xep-0333.html

Here is my simple implementation:

public class MessageReadDelivered extends XMPPProcessor implements XMPPProcessorIfc {

    private static final String ID = "message-read-delivered";
    private static final String  XMLNS  = "urn:xmpp:chat-markers:0";

    public static final String RECEIVED_KEY = "received";
    public static final String DISPLAYED_KEY = "displayed";
    public static final String[] MESSAGE_RECEIVED_PATH = { Message.ELEM_NAME, RECEIVED_KEY};
    public static final String[] MESSAGE_DISPLAYED_PATH = { Message.ELEM_NAME, DISPLAYED_KEY};

    private static final Logger log = Logger.getLogger(MessageReadDelivered.class.getName());

    @Override
    public String id() {
        return ID;
    }


    @Override
    public String[] supNamespaces() {
        return new String[]{XMLNS, XMLNS};
    }

    @Override
    public String[][] supElementNamePaths() {
        return new String[][]{MESSAGE_RECEIVED_PATH, MESSAGE_DISPLAYED_PATH};
    }

    @Override
    public void process(Packet packet, XMPPResourceConnection session, NonAuthUserRepository repo, Queue<Packet> results, Map<String, Object> settings) throws XMPPException {

        // You may want to skip processing completely if the user is offline.
        if (session == null) {
            return;
        }

        BareJID id = (packet.getStanzaFrom() != null) ? packet.getStanzaFrom().getBareJID() : null;

        if (session.isUserId(id) {
            if (log.isLoggable(Level.INFO)) {
                log.log(Level.INFO, "packet: {0}, session: {1}", new Object[] { packet, session});
            }

           // do something with status
           // ...
        }
    }
}

My test is simple: UserA sends a status to UserB

By some reason I see above log 2 times:

[message-read-delivered Queue Worker 0]  MessageReadDelivered.process()  INFO: packet: from=c2s@qq-chat1/174.30.0.248_5222_92.117.252.236_52793, to=sess-man@qq-chat1...
[message-read-delivered Queue Worker 0]  MessageReadDelivered.process()  INFO: packet: from=sess-man@qq-chat1, to=sess-man@qq-chat1...

This is not what I expect - my logic works 2 times then.

According to this doc http://docs.tigase.org/tigase-server/7.0.0/Development_Guide/html/#_introduction

The packet has to be processed twice by the session manager. The first time it is processed on behalf of the User A as an outgoing packet and the second time it is processed on behalf of the User B as an incoming packet.

so only once on each side, not 2

To fix this I add:

if (session.isUserId(id) &&  packet.getPacketFrom().getLocalpart().equals("c2s")) {...

Is it a right solution or should I rewrite this in a some other way?

thank you


Replies (2)

Avatar?id=6023&size=32x32

Added by Artur Hefczyc TigaseTeam over 3 years ago

The second part of your if statement should not be necessary. You are correct that with your code you should see the log entry only once, unless, the sender and receiver of the message packet is the same. Do you have the exact message stanza you are sending?

Added by Igor Khomenko over 2 years ago

the sender and receiver of the message packet is the same

you were right with this, thank you

    (1-2/2)