2010-03-26 21:32:16 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
2010-07-20 15:19:49 +00:00
|
|
|
See the file LICENSE for copying permission.
|
2010-03-26 21:32:16 +00:00
|
|
|
"""
|
2010-08-27 20:42:26 +00:00
|
|
|
|
|
|
|
import logging
|
2010-01-08 06:03:02 +00:00
|
|
|
try:
|
2010-08-27 20:42:26 +00:00
|
|
|
import queue
|
2010-01-08 06:03:02 +00:00
|
|
|
except ImportError:
|
2010-08-27 20:42:26 +00:00
|
|
|
import Queue as queue
|
|
|
|
|
|
|
|
from sleekxmpp.xmlstream import StanzaBase, RESPONSE_TIMEOUT
|
|
|
|
from sleekxmpp.xmlstream.handler.base import BaseHandler
|
|
|
|
|
|
|
|
|
|
|
|
class Waiter(BaseHandler):
|
|
|
|
|
|
|
|
"""
|
|
|
|
The Waiter handler allows an event handler to block
|
|
|
|
until a particular stanza has been received. The handler
|
|
|
|
will either be given the matched stanza, or False if the
|
|
|
|
waiter has timed out.
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
checkDelete -- Overrides BaseHandler.checkDelete
|
|
|
|
prerun -- Overrides BaseHandler.prerun
|
|
|
|
run -- Overrides BaseHandler.run
|
|
|
|
wait -- Wait for a stanza to arrive and return it to
|
|
|
|
an event handler.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, matcher, stream=None):
|
2010-08-27 22:16:09 +00:00
|
|
|
"""
|
|
|
|
Create a new Waiter.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
name -- The name of the waiter.
|
|
|
|
matcher -- A matcher object to detect the desired stanza.
|
|
|
|
stream -- Optional XMLStream instance to monitor.
|
|
|
|
"""
|
2010-09-01 18:20:34 +00:00
|
|
|
BaseHandler.__init__(self, name, matcher, stream=stream)
|
2010-08-27 20:42:26 +00:00
|
|
|
self._payload = queue.Queue()
|
|
|
|
|
|
|
|
def prerun(self, payload):
|
|
|
|
"""
|
|
|
|
Store the matched stanza.
|
|
|
|
|
|
|
|
Overrides BaseHandler.prerun
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
payload -- The matched stanza object.
|
|
|
|
"""
|
|
|
|
self._payload.put(payload)
|
|
|
|
|
|
|
|
def run(self, payload):
|
|
|
|
"""
|
|
|
|
Do not process this handler during the main event loop.
|
|
|
|
|
|
|
|
Overrides BaseHandler.run
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
payload -- The matched stanza object.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def wait(self, timeout=RESPONSE_TIMEOUT):
|
|
|
|
"""
|
|
|
|
Block an event handler while waiting for a stanza to arrive.
|
|
|
|
|
|
|
|
Be aware that this will impact performance if called from a
|
|
|
|
non-threaded event handler.
|
|
|
|
|
|
|
|
Will return either the received stanza, or False if the waiter
|
|
|
|
timed out.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
timeout -- The number of seconds to wait for the stanza to
|
|
|
|
arrive. Defaults to the global default timeout
|
|
|
|
value sleekxmpp.xmlstream.RESPONSE_TIMEOUT.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
stanza = self._payload.get(True, timeout)
|
|
|
|
except queue.Empty:
|
|
|
|
stanza = False
|
|
|
|
logging.warning("Timed out waiting for %s" % self.name)
|
|
|
|
self.stream.removeHandler(self.name)
|
|
|
|
return stanza
|
|
|
|
|
|
|
|
def checkDelete(self):
|
|
|
|
"""
|
|
|
|
Always remove waiters after use.
|
|
|
|
|
|
|
|
Overrides BaseHandler.checkDelete
|
|
|
|
"""
|
|
|
|
return True
|