5

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!

matthias krull
  • 4,389
  • 3
  • 34
  • 54

2 Answers2

2

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);
        }
    }
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
1

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.

Paolo Brandoli
  • 4,681
  • 26
  • 38
  • I have tested this, and the packet ID always matches the filter ID. I have also tried using a constant as the ID and using a different type of filter. All three filter settings get the same result. Note: When entering the activity, if the packet is received the first time, then no matter how many times I query the IQ and send the packet, it will work every time. The inverse is true as well, when I enter the activity and the packet doesn't get picked up by the listener, all future IQ packets will not get received by the listener. – Jonathan Cornwell Nov 19 '12 at 16:53
  • @JonathanCornwell Have you checked if the connection where you register the listener is the same that receives the packet? (for instance, the client disconnects and then reconnect because the connectivity changes, but the new connection doesn't have the listener) – Paolo Brandoli Nov 19 '12 at 17:16
  • I have tested this aswell, but the connection never changes. I even made it so that all activities had access to a public version of the connection to ensure the connection never changes with the same results. Update: It seems as though when I open the activity and the listener runs, it will run correctly for every packet sent to it. But when the listener doesn't run, it won't run for every packet sent to it. Closing and re-opening the activity can change this (working/not working), but the weird thing is that opening/closing the activity doesn't change anything that effects the listener. – Jonathan Cornwell Nov 26 '12 at 16:42