diff options
author | Lance Stout <lancestout@gmail.com> | 2010-08-27 16:42:26 -0400 |
---|---|---|
committer | Lance Stout <lancestout@gmail.com> | 2010-08-27 16:42:26 -0400 |
commit | 89fb15e8962640a34e24418216e156188032bfa8 (patch) | |
tree | 9a510aef8469c2162456258c6063e21b36a66e07 /sleekxmpp/xmlstream/handler/waiter.py | |
parent | 906aa0bd6896d119bcbabc6e21de31c2171316b9 (diff) | |
download | slixmpp-89fb15e8962640a34e24418216e156188032bfa8.tar.gz slixmpp-89fb15e8962640a34e24418216e156188032bfa8.tar.bz2 slixmpp-89fb15e8962640a34e24418216e156188032bfa8.tar.xz slixmpp-89fb15e8962640a34e24418216e156188032bfa8.zip |
Updated the suite of handler classes with documentation.
Updated XMLStream to return True or False from removeHandler to indicate if the handler
existed and was removed.
Waiter handlers now unregister themselves after timing out.
Diffstat (limited to 'sleekxmpp/xmlstream/handler/waiter.py')
-rw-r--r-- | sleekxmpp/xmlstream/handler/waiter.py | 108 |
1 files changed, 81 insertions, 27 deletions
diff --git a/sleekxmpp/xmlstream/handler/waiter.py b/sleekxmpp/xmlstream/handler/waiter.py index 7c4330a4..0e5206b2 100644 --- a/sleekxmpp/xmlstream/handler/waiter.py +++ b/sleekxmpp/xmlstream/handler/waiter.py @@ -5,32 +5,86 @@ See the file LICENSE for copying permission. """ -from . import base + +import logging try: - import queue + import queue except ImportError: - import Queue as queue -import logging -from .. stanzabase import StanzaBase - -class Waiter(base.BaseHandler): - - def __init__(self, name, matcher): - base.BaseHandler.__init__(self, name, matcher) - self._payload = queue.Queue() - - def prerun(self, payload): - self._payload.put(payload) - - def run(self, payload): - pass - - def wait(self, timeout=60): - try: - return self._payload.get(True, timeout) - except queue.Empty: - logging.warning("Timed out waiting for %s" % self.name) - return False - - def checkDelete(self): - return True + 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): + BaseHandler.__init__(self, name, matcher) + 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 |