addPacketListener adds ONLY the last PacketTypeFilter

Folks,

I recently upgraded to Smack 3.0.4 (I know I should have done it earlier! ) and openfire to 3.4.2.

After creating the XMPPConnection object I do addPacketListner to listen to Presence.class , Message.class packetypes.

I do this by doing the following:

conn.addPacketListener(listener, new PacketTypeFilter (Presence.class));

conn.addPacketListener(listener, new PacketTypeFilter (Message.class));

After doing this, whatever I add LAST is used for packetfiltering. Meaning, in the above case only Message packets will be handled by the listener.

I am not able to add both Presence.class and Message.class in the conn object to be handled by listener.

What should be the expected behavior ? Am I missing something?

Thanks.

PS: (I was able to add both Presence.class and Message.class as packetfilter with earlier version of smack)

I’m not sure what the behavior should be, but the reason it is happening is that the listeners are stored using a map of listener instance to packet type. So when you are adding the second packet type it is replacing the map entry with the most recent one. You can work around the issue pretty easily by registering both packet types at the same time using an OrFilter class.

conn.addPacketListener(listener, new OrFilter(new PacketTypeFilter (Presence.class), new PacketTypeFilter(Message.class)));

Chris

Thanks Chris, OrFilter did solve the problem.

But, isn’t this a bug in smack as it is overwriting an earlier map entry of listener instance to packet type filter.

I guess it’s ultimately up to the developers of Smack to decide that, but I would probably expect it to work the way you were trying to use it. Plus if it worked in the old version it should probably continue to work that way. The current behavior is in fact documented though for the addPacketListener method, so it seems that the change was intentional.

Chris

you are right. it did used to work in the earlier version.

Thanks for your reply!

Hi

I am trying to develop a chat client based on Smack,I am getting prblm while getting the subscription request from users.How i can get subscription request

roster.getPresence(rentry).setType(Presence.Type.subscribed);

// presence.setType(Presence.Type.subscribed);

roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);

roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);

I added dis statement but still i am not getiing any athing

and How i can subscrib to get the presence of my roster elements