Smack provides a flexible framework for processing incoming packets using two constructs:
The org.jivesoftware.smack.filter.PacketFilter interface determines which specific packets will be delivered to a PacketCollector or PacketListener. Many pre-defined filters can be found in the org.jivesoftware.smack.filter package.
The following code snippet demonstrates registering both a packet collector and a packet listener:
// Create a packet filter to listen for new messages from a particular // user. We use an AndFilter to combine two other filters. PacketFilter filter = new AndFilter(new PacketTypeTypeFilter(Message.class), new FromContainsFilter("mary@jivesoftware.com")); // Assume we've created an XMPPConnection name "connection". // First, register a packet collector using the filter we created. PacketCollector myCollector = connection.createPacketCollector(filter); // Normally, you'd do something with the collector, like wait for new packets. // Next, create a packet listener. We use an anonymous inner class for brevity. PacketListener myListener = new PacketListener() { public void processPacket(Packet packet) { // Do something with the incoming packet here. } }; // Register the listener. connection.addPacketListener(myListener, filter);
Standard Packet Filters
A rich set of packet filters are included with Smack, or you can create your own filters by coding to the PacketFilter interface. The default set of filters includes: