1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-28 21:48:00 +01:00
Smack/documentation/processing.md

61 lines
3 KiB
Markdown
Raw Normal View History

Processing Incoming Stanzas
2014-08-16 00:09:55 +02:00
===========================
[Back](index.md)
2014-08-16 00:09:55 +02:00
2018-04-03 15:14:45 +02:00
Smack provides a flexible framework for processing incoming stanzas using two
2014-08-16 00:09:55 +02:00
constructs:
2018-04-03 15:14:45 +02:00
* `org.jivesoftware.smack.StanzaCollector` -- a class that lets you synchronously wait for new stanzas.
* `org.jivesoftware.smack.StanzaListener` -- an interface for asynchronously notifying you of incoming stanzas. A stanza listener is used for event style programming, while a stanza collector has a result queue of stanzas that you can do polling and blocking operations on. So, a stanza listener is useful when you want to take some action whenever a stanza happens to come in, while a stanza collector is useful when you want to wait for a specific stanza to arrive. Stanza collectors and listeners can be created using an `XMPPConnection` instance.
2014-08-16 00:09:55 +02:00
The `org.jivesoftware.smack.filter.StanzaFilter` interface determines which
2018-04-03 15:14:45 +02:00
specific stanzas will be delivered to a `StanzaCollector` or `StanzaListener`.
2014-08-16 00:09:55 +02:00
Many pre-defined filters can be found in the `org.jivesoftware.smack.filter`
package.
2018-04-03 15:14:45 +02:00
The following code snippet demonstrates registering both a stanza collector
and a stanza listener:
2014-08-16 00:09:55 +02:00
```
2018-04-03 15:14:45 +02:00
// Create a stanza filter to listen for new messages from a particular
2014-08-16 00:09:55 +02:00
// user. We use an AndFilter to combine two other filters._
2019-08-03 11:44:46 +02:00
StanzaFilter filter = new AndFilter(StanzaTypeFilter.MESSAGE,
FromMatchesFilter.create(JidCreate.entityBareFrom("mary@jivesoftware.com")));
2018-04-03 15:14:45 +02:00
// Assume we've created an XMPPConnection named "connection".
2014-08-16 00:09:55 +02:00
2018-04-03 15:14:45 +02:00
// First, register a stanza collector using the filter we created.
StanzaCollector myCollector = connection.createStanzaCollector(filter);
2014-08-16 00:09:55 +02:00
// Normally, you'd do something with the collector, like wait for new packets.
2018-04-03 15:14:45 +02:00
// Next, create a stanza listener. We use an anonymous inner class for brevity.
StanzaListener myListener = new StanzaListener() {
2019-08-03 11:44:46 +02:00
public void processStanza(Stanza stanza) {
// Do something with the incoming stanza here._
2014-08-16 00:09:55 +02:00
}
};
// Register the listener._
2018-04-03 15:14:45 +02:00
connection.addAsyncStanzaListener(myListener, filter);
// or for a synchronous stanza listener use
connection.addSyncStanzaListener(myListener, filter);
2014-08-16 00:09:55 +02:00
```
Standard Stanza Filters
2014-08-16 00:09:55 +02:00
-----------------------
2018-04-03 15:14:45 +02:00
A rich set of stanza filters are included with Smack, or you can create your
own filters by coding to the `StanzaFilter` interface. The default set of
2014-08-16 00:09:55 +02:00
filters includes:
2018-04-03 15:14:45 +02:00
* `StanzaTypeFilter` -- filters for stanzas that are a particular Class type.
* `StanzaIdFilter` -- filters for stanzas with a particular packet ID.
* `ThreadFilter` -- filters for message stanzas with a particular thread ID.
* `ToMatchesFilter` -- filters for stanzas that are sent to a particular address.
* `FromMatchesFilter` -- filters for stanzas that are sent from a particular address.
* `StanzaExtensionFilter` -- filters for stanzas that have a particular stanza extension.
2014-08-16 00:09:55 +02:00
* `AndFilter` -- implements the logical AND operation over two filters.
* `OrFilter` -- implements the logical OR operation over two filters.
* `NotFilter` -- implements the logical NOT operation on a filter.
Copyright (C) Jive Software 2002-2008