Filtering for the right Packet

Hi,

Here is the other problem I have. I try to filter out the right sort of Packets so I can print out the GroupChat messages. This listed code does not work and does not printout messages I get from the server. (I see them coming on the debug)

Any idea how to filter it right?

Thanks,

(Boy will I be happy if there will be a solution for my login bug too)

Ted

public void processPacket(Packet packet){

try{

if(packet.getClass().isInstance(“org.jivesoftware.smack.packet.Presence”))

setRoster();

else if(packet.getClass().isInstance(“org.jivesoftware.smack.packet.Message”)){

Message msg = (Message)packet;

client.timedCya(jabbing,"[ "msg.getFrom() " ] "+msg.getBody());

}

}

catch(Exception e)

{

e.printStackTrace(System.out);

System.out.println("processPacket error "+e);

}

}

What packet filter are you using when registering the listener?

Regards,

Matt

Oh maybe we are on on somthing.

I do not register a filter I thought groupChat.addMessageListener(this)takes care of that. I must be missing somthing here.

All I do is: my groupChat client implements PacketListener

Then it have a processPacket(Packet packet)that I listed. Should I register this client with the connection also what filter do I use to get the roster and messages for this room then?

Thanks,

Ted

Ted,

Here’'s the filter that gets used when you use that code:

messageFilter = new AndFilter(messageFilter, new PacketFilter() {
            public boolean accept(Packet packet) {
                Message msg = (Message)packet;
                return msg.getType() == Message.Type.GROUP_CHAT;
            }
        });

This is NOT what you want if you want to look for presence packets or other types of chat messages. Instead, you should register a packet listener with the appropriate filter directly with the connection. I’'ll try to add more info to the Javadocs to clarify this behavior.

Regards,

Matt

Thanks Matt,

I am looking at this filter and do not understand the messageFilter statment.

messageFilter = new AndFilter(messageFilter, …

I thought to have AndFilter with just two PacketTypeFilters(Class packetType)

myFilter = new AndFilter(new PacketTypeFilters(Message.class), new PacketTypeFilters(Presence.class ))

Does it look right?

Thanks Matt,

Ted

Well that didn’'t work,

Same Exception.

Here is what I tried

connection.addPacketListener(this,new AndFilter(new PacketTypeFilter(Message.class), new PacketTypeFilter(Presence.class )));

groupChat = connection.createGroupChat(room);

groupChat.addMessageListener(this);

groupChat.join(client.login.userName);

Do I have to define the accept for each?

Thanks,

Ted

Ted,

Please read the Javadocs – the and filter is the logicial AND operation. You’'re looking for the OR operation so the OrFilter.

Regards,

Matt

Sorry you are right now with OrFilter I finaly get the room messages.

It does throw a class cast exception now and still no Roster

I think I will go to have a soup…

Buildfile: build.xml

run:

java.lang.ClassCastException

at cscie258.TWeitz.proj.GroupChatClient.processPacket(Unknown So

urce)

at org.jivesoftware.smack.PacketReader$ListenerWrapper.notifyLis

tener(PacketReader.java:742)

at org.jivesoftware.smack.PacketReader.processListeners(PacketRe

ader.java:236)

at org.jivesoftware.smack.PacketReader.access$100(PacketReader.j

ava:74)

Well, your listener is obviously tring to cast a packet to a class that won’'t work.

-Matt

Ok,

That is what I use

connection.addPacketListener(this,new OrFilter(new PacketTypeFilter(Message.class), new PacketTypeFilter(Presence.class )));

I thought the PacketTypeFilter(AClass.class), takes care of the details. Do I need to write it explicitly?

You’'ll be getting packets that are EITHER messages or presence packets. So, you need to do an instanceof check before trying to cast it to one class or another.

-Matt