It seems clear. The PacketFilter
filters packets for processing by the PacketListener.
What is the difference between packetFilter and accept?
Question
What is the difference between these two methods :
processPacket :
PacketListener pListener = new PacketListener() {
@Override
public void processPacket(Packet packet) {
if(packet instanceof Presence) {
//..
}
}
};
and accept :
PacketFilter pFilter = new PacketFilter() {
@Override
public boolean accept(Packet packet) {
return true;
}
};
Aren't they capable of doing the same thing ?
note :
con.addPacketListener(pListener, pFilter);
Solution
OTHER TIPS
As you understand, PacketFilter is used before PacketListener to filter the packet data of the matching types. Only packet that satisfies PacketFilter can enter PacketListener processing.
And PacketFilter is an interface that only declares a method: accept.
PacketListener Provides a mechanism to listen for packets that pass a specified filter. This allows event-style programming -- every time a new packet is found, The processPacket(Packet) method will be called. This is the opposite approach to the functionality provided by a PacketCollector which lets you block while waiting for results.
PacketFilter Defines a way to filter packets for particular attributes. Packet filters are used when constructing packet listeners or collectors -- the filter defines what packets match the criteria of the collector or listener for further packet processing.
Several pre-defined filters are defined. These filters can be logically combined for more complex packet filtering by using the AndFilter and OrFilter filters. It's also possible to define your own filters by implementing this interface. The code example below creates a trivial filter for packets with a specific ID.
// Use an anonymous inner class to define a packet filter that returns
// all packets that have a packet ID of "RS145".
PacketFilter myFilter = new PacketFilter() {
public boolean accept(Packet packet) {
return "RS145".equals(packet.getPacketID());
}
};
// Create a new packet collector using the filter we created.
PacketCollector myCollector = packetReader.createPacketCollector(myFilter);
Above statements are from java doc.