dispatcher — Dispatch stanzas to callbacks¶
New in version 0.9: The whole module was added in 0.9.
Stanza Dispatchers for Messages and Presences¶
-
class
aioxmpp.dispatcher.SimpleMessageDispatcher(client, *, logger_base=None, dependencies={}, service_order_index=0)[source]¶ Dispatch messages to callbacks.
This
ServicedispatchesMessagestanzas to callbacks. Callbacks registrations are managed with theSimpleStanzaDispatcher.register_callback()andSimpleStanzaDispatcher.unregister_callback()methods of the base class. The type_ argument to these methods must be aaioxmpp.MessageTypeorNoneto make any sense.Note
It is not recommended to mix the use of a
SimpleMessageDispatcherwith the modern Instant Messaging features provided by theaioxmpp.immodule. Both will receive the messages and this may thus lead to duplicate messages.
-
class
aioxmpp.dispatcher.SimplePresenceDispatcher(client, *, logger_base=None, dependencies={}, service_order_index=0)[source]¶ Dispatch presences to callbacks.
This
ServicedispatchesPresencestanzas to callbacks. Callbacks registrations are managed with theSimpleStanzaDispatcher.register_callback()andSimpleStanzaDispatcher.unregister_callback()methods of the base class. The type_ argument to these methods must be aaioxmpp.MessageTypeorNoneto make any sense.Warning
It is not recommended to mix the use of a
SimplePresenceDispatcherwithaioxmpp.RosterClientandaioxmpp.PresenceClient. Both of these register callbacks at theSimplePresenceDispatcher. Registering callbacks for different slots will either make those callbacks not be called at all or will make the services miss stanzas.
Decorators for aioxmpp.service.Service Methods¶
-
@aioxmpp.dispatcher.message_handler(type_, from_)[source]¶ Register the decorated function as message handler.
- Parameters
type (
MessageType) – Message type to listen forfrom (
aioxmpp.JIDorNone) – Sender JIDs to listen for
- Raises
TypeError – if the decorated object is a coroutine function
See also
register_message_callback()for more details on the type_ and from_ arguments
Changed in version 0.9: This is now based on
aioxmpp.dispatcher.SimpleMessageDispatcher.
-
@aioxmpp.dispatcher.presence_handler(type_, from_)[source]¶ Register the decorated function as presence stanza handler.
- Parameters
type (
PresenceType) – Presence type to listen forfrom (
aioxmpp.JIDorNone) – Sender JIDs to listen for
- Raises
TypeError – if the decorated object is a coroutine function
See also
register_presence_callback()for more details on the type_ and from_ arguments
Changed in version 0.9: This is now based on
aioxmpp.dispatcher.SimplePresenceDispatcher.
Test Functions¶
-
aioxmpp.dispatcher.is_message_handler(type_, from_, cb)[source]¶ Return true if cb has been decorated with
message_handler()for the given type_ and from_.
-
aioxmpp.dispatcher.is_presence_handler(type_, from_, cb)[source]¶ Return true if cb has been decorated with
presence_handler()for the given type_ and from_.
Base Class for Stanza Dispatchers¶
-
class
aioxmpp.dispatcher.SimpleStanzaDispatcher(**kwargs)[source]¶ Dispatch stanzas based on their sender and type.
This is a service base class (not a service you should summon) which can be used to implement simple, pre-0.9 presence and message dispatching.
For users, the following methods are relevant:
-
register_callback(type_, from_, cb, *, wildcard_resource=True)[source]¶ Register a callback function.
- Parameters
type – Stanza type to listen for, or
Nonefor a wildcard match.from (
aioxmpp.JIDorNone) – Sender to listen for, orNonefor a full wildcard match.cb – Callback function to register
wildcard_resource (
bool) – Whether to wildcard the resourcepart of the JID.
- Raises
ValueError – if another function is already registered for the callback slot.
cb will be called whenever a stanza with the matching type_ and from_ is processed. The following wildcarding rules apply:
If the
from_attribute of the stanza has a resourcepart, the following lookup order for callbacks is used:type_from_wildcard_resourcetype_any
type_bare
from_any
bare
from_type_any
any
If the
from_attribute of the stanza does not have a resourcepart, the following lookup order for callbacks is used:type_from_wildcard_resourcetype_type_any
any
Only the first callback which matches is called. wildcard_resource is ignored if from_ is a full JID or
None.
-
unregister_callback(type_, from_, *, wildcard_resource=True)[source]¶ Unregister a callback function.
- Parameters
type – Stanza type to listen for, or
Nonefor a wildcard match.from (
aioxmpp.JIDorNone) – Sender to listen for, orNonefor a full wildcard match.wildcard_resource (
bool) – Whether to wildcard the resourcepart of the JID.
The callback must be disconnected with the same arguments as were used to connect it.
-
handler_context(type_, from_, cb, *, wildcard_resource=True)[source]¶ Context manager which temporarily registers a callback.
The arguments are the same as for
register_callback().When the context is entered, the callback cb is registered. When the context is exited, no matter if an exception is raised or not, the callback is unregistered.
For deriving classes, the following methods are relevant:
-
_feed(stanza)[source]¶ Dispatch the given stanza.
- Parameters
stanza (
StanzaBase) – Stanza to dispatch- Return type
- Returns
true if the stanza was dispatched, false otherwise.
Dispatch the stanza to up to one handler registered on the dispatcher. If no handler is found for the stanza,
Falseis returned. Otherwise,Trueis returned.
Subclasses must also provide the following property:
-
local_jid¶ The bare JID of the client for which this dispatcher is used.
This is required to map missing
@fromattributes to this JID. The attribute must be provided by implementing subclasses.
-