2011-12-05 00:26:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-03-26 21:32:16 +00:00
|
|
|
"""
|
2011-12-05 00:26:14 +00:00
|
|
|
sleekxmpp.xmlstream.handler.waiter
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2010-03-26 21:32:16 +00:00
|
|
|
|
2011-12-05 00:26:14 +00:00
|
|
|
Part of SleekXMPP: The Sleek XMPP Library
|
|
|
|
|
|
|
|
:copyright: (c) 2011 Nathanael C. Fritz
|
|
|
|
:license: MIT, see LICENSE for more details
|
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
|
|
|
|
|
2010-11-17 20:13:09 +00:00
|
|
|
from sleekxmpp.xmlstream import StanzaBase
|
2010-08-27 20:42:26 +00:00
|
|
|
from sleekxmpp.xmlstream.handler.base import BaseHandler
|
|
|
|
|
|
|
|
|
2010-11-06 05:28:59 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2010-08-27 20:42:26 +00:00
|
|
|
class Waiter(BaseHandler):
|
|
|
|
|
|
|
|
"""
|
2011-12-05 00:26:14 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
:param string name: The name of the handler.
|
|
|
|
:param matcher: A :class:`~sleekxmpp.xmlstream.matcher.base.MatcherBase`
|
|
|
|
derived object for matching stanza objects.
|
|
|
|
:param stream: The :class:`~sleekxmpp.xmlstream.xmlstream.XMLStream`
|
|
|
|
instance this handler should monitor.
|
2010-08-27 20:42:26 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, matcher, stream=None):
|
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):
|
2011-12-05 00:26:14 +00:00
|
|
|
"""Store the matched stanza when received during processing.
|
2010-08-27 20:42:26 +00:00
|
|
|
|
2011-12-05 00:26:14 +00:00
|
|
|
:param payload: The matched
|
|
|
|
:class:`~sleekxmpp.xmlstream.stanzabase.ElementBase` object.
|
2010-08-27 20:42:26 +00:00
|
|
|
"""
|
|
|
|
self._payload.put(payload)
|
|
|
|
|
|
|
|
def run(self, payload):
|
2011-12-05 00:26:14 +00:00
|
|
|
"""Do not process this handler during the main event loop."""
|
2010-08-27 20:42:26 +00:00
|
|
|
pass
|
|
|
|
|
2010-11-17 20:13:09 +00:00
|
|
|
def wait(self, timeout=None):
|
2011-12-05 00:26:14 +00:00
|
|
|
"""Block an event handler while waiting for a stanza to arrive.
|
2010-08-27 20:42:26 +00:00
|
|
|
|
|
|
|
Be aware that this will impact performance if called from a
|
|
|
|
non-threaded event handler.
|
|
|
|
|
2011-12-05 00:26:14 +00:00
|
|
|
Will return either the received stanza, or ``False`` if the
|
|
|
|
waiter timed out.
|
2010-08-27 20:42:26 +00:00
|
|
|
|
2011-12-05 00:26:14 +00:00
|
|
|
:param int timeout: The number of seconds to wait for the stanza
|
|
|
|
to arrive. Defaults to the the stream's
|
|
|
|
:class:`~sleekxmpp.xmlstream.xmlstream.XMLStream.response_timeout`
|
|
|
|
value.
|
2010-08-27 20:42:26 +00:00
|
|
|
"""
|
2010-11-17 20:13:09 +00:00
|
|
|
if timeout is None:
|
2011-10-08 21:31:30 +00:00
|
|
|
timeout = self.stream().response_timeout
|
2010-11-17 20:13:09 +00:00
|
|
|
|
2011-11-20 20:15:39 +00:00
|
|
|
elapsed_time = 0
|
|
|
|
stanza = False
|
|
|
|
while elapsed_time < timeout and not self.stream().stop.is_set():
|
|
|
|
try:
|
|
|
|
stanza = self._payload.get(True, 1)
|
|
|
|
break
|
|
|
|
except queue.Empty:
|
|
|
|
elapsed_time += 1
|
|
|
|
if elapsed_time >= timeout:
|
|
|
|
log.warning("Timed out waiting for %s", self.name)
|
2011-10-08 21:31:30 +00:00
|
|
|
self.stream().remove_handler(self.name)
|
2010-08-27 20:42:26 +00:00
|
|
|
return stanza
|
|
|
|
|
2010-10-18 13:06:54 +00:00
|
|
|
def check_delete(self):
|
2011-12-05 00:26:14 +00:00
|
|
|
"""Always remove waiters after use."""
|
2010-08-27 20:42:26 +00:00
|
|
|
return True
|