Jonathan Cornwell
Jonathan Cornwell

Reputation: 81

Smack Packet Listener Not Running

I am trying to write a listener for a asmack packet. What ends up happening is the listener sometimes just doesn't get the packet and process it. Here is some of the code:

try {

            XMPPMethods.getConnection().addPacketListener( new PacketListener() {
                    @Override
                    public synchronized void processPacket(Packet packet) {

                        if (packet.getPacketID().equals(lastCustomIQId)) {

                            android.os.Message msg = new android.os.Message();
                            msg.obj = privateData;
                            msg.what = XMPPMethods.ADD_CONTACT_RESULTS;
                            AddContact.addContactHandler.sendMessage(msg);
                        }
                    }
                }, new PacketIDFilter(lastCustomIQId));

Note that this IS inside of the doInBackground(string... params) part of an asynctask.

The packet is being sent using:

JIDIQ.setPacketID(lastCustomIQId);
JIDIQ.setFrom(XMPPMethods.getCurrentUserFullUserName());
JIDIQ.setType(Type.GET);
XMPPMethods.getConnection().sendPacket(JIDIQ);

Where the JIDIQ is an asmack IQ. This code all runs correctly MOST of the time. But sometimes the PacketListener just doesn't receive the packet sent. I'm wondering if I should be using a PacketCollector instead, or if the listener is somehow dying. Does anyone know why this wouldn't receive the packet? Any knowledge of this subject would be greatly appreciated!

Upvotes: 5

Views: 2585

Answers (2)

Michal Palczewski
Michal Palczewski

Reputation: 918

This will happen in smack if one of your other packet listeners is throwing an exception.
Whenever a packet comes in smack spawns of a hit to all the packet listeners in one thread, inside of one for loop. If an exception is thrown in a packet listener this will abort that thread and no further packet listeners will get triggered.

The most thorough way of detecting where this is happening is by recompiling smack and adding an error handler in PacketReader.java.

Here is the relevant section of code. You can see that any exception that is thrown will cause the thread to abort since there is no error handling.

private class ListenerNotification implements Runnable {

    private Packet packet;

    public ListenerNotification(Packet packet) {
        this.packet = packet;
    }

    public void run() {
        for (ListenerWrapper listenerWrapper : connection.recvListeners.values()) {
            listenerWrapper.notifyListener(packet);
        }
    }
}

Upvotes: 2

Paolo Brandoli
Paolo Brandoli

Reputation: 4750

I think that the PacketIDFilter is filtering the packets, so your packet listener doesn't get them.

When your packet listener doesn't get the packet then check the smack log to see if the packet ID is what the packet filter expects.

Upvotes: 1

Related Questions