Repository: qpid-proton Updated Branches: refs/heads/master fe31dcea7 -> 4b0261c10
python wrappers for C reactor/handler infrastructure Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/9f011aa0 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/9f011aa0 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/9f011aa0 Branch: refs/heads/master Commit: 9f011aa0f02b7b577c7d32478c8fd98598161eed Parents: fe31dce Author: Rafael Schloming <r...@alum.mit.edu> Authored: Tue Jan 13 18:35:05 2015 -0500 Committer: Rafael Schloming <r...@alum.mit.edu> Committed: Tue Jan 13 18:35:05 2015 -0500 ---------------------------------------------------------------------- proton-c/bindings/python/proton/__init__.py | 36 ++++++++++++ proton-c/bindings/python/proton/handlers.py | 13 +++++ proton-c/bindings/python/proton/reactors.py | 71 ++++++++++++++++++++++++ 3 files changed, 120 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/9f011aa0/proton-c/bindings/python/proton/__init__.py ---------------------------------------------------------------------- diff --git a/proton-c/bindings/python/proton/__init__.py b/proton-c/bindings/python/proton/__init__.py index cb2dc28..9b6a3c6 100644 --- a/proton-c/bindings/python/proton/__init__.py +++ b/proton-c/bindings/python/proton/__init__.py @@ -3433,6 +3433,42 @@ class Handler(object): def on_unhandled(self, method, args): pass +class _cadapter: + + def __init__(self, handler): + self.handler = handler + + def __call__(self, cevent): + ev = Event.wrap(cevent) + ev.dispatch(self.handler) + if hasattr(self.handler, "handlers"): + for h in self.handler.handlers: + # XXX: if Event wrapped c events rather than copying them, we + # could put this logic in Event.dispatch + if isinstance(h, WrappedHandler): + pn_handler_dispatch(h._impl, cevent) + else: + ev.dispatch(h) + +class WrappedHandler(Wrapper): + + def __init__(self, impl_or_constructor): + Wrapper.__init__(self, impl_or_constructor) + + def add(self, handler): + impl = _chandler(handler) + pn_handler_add(self._impl, impl) + pn_decref(impl) + +def _chandler(obj): + if obj is None: + return None + elif isinstance(obj, WrappedHandler): + impl = obj._impl + pn_incref(impl) + return impl + else: + return pn_pyhandler(_cadapter(obj)) ### # Driver http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/9f011aa0/proton-c/bindings/python/proton/handlers.py ---------------------------------------------------------------------- diff --git a/proton-c/bindings/python/proton/handlers.py b/proton-c/bindings/python/proton/handlers.py index 5a29d14..6659e34 100644 --- a/proton-c/bindings/python/proton/handlers.py +++ b/proton-c/bindings/python/proton/handlers.py @@ -440,3 +440,16 @@ class TransactionalClientHandler(MessagingHandler, TransactionHandler): transaction.accept(delivery) else: super(TransactionalClientHandler, self).accept(delivery) + +from proton import WrappedHandler +from cproton import pn_flowcontroller, pn_handshaker + +class CFlowController(WrappedHandler): + + def __init__(self, window=1024): + WrappedHandler.__init__(self, lambda: pn_flowcontroller(window)) + +class CHandshaker(WrappedHandler): + + def __init__(self): + WrappedHandler.__init__(self, pn_handshaker) http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/9f011aa0/proton-c/bindings/python/proton/reactors.py ---------------------------------------------------------------------- diff --git a/proton-c/bindings/python/proton/reactors.py b/proton-c/bindings/python/proton/reactors.py index 0b5bd7e..2a4fd5a 100644 --- a/proton-c/bindings/python/proton/reactors.py +++ b/proton-c/bindings/python/proton/reactors.py @@ -835,3 +835,74 @@ class Container(object): def do_work(self, timeout=None): return self.loop.do_work(timeout) +from proton import WrappedHandler, _chandler, Connection, secs2millis +from wrapper import Wrapper +from cproton import * + +class Task(Wrapper): + + @staticmethod + def wrap(impl): + if impl is None: + return None + else: + return Task(impl) + + def __init__(self, impl): + Wrapper.__init__(self, impl, pn_task_attachments) + + def _init(self): + pass + +class Acceptor(Wrapper): + + def __init__(self, impl): + Wrapper.__init__(self, impl) + +class Reactor(Wrapper): + + @staticmethod + def wrap(impl): + if impl is None: + return None + else: + return Reactor(impl=impl) + + def __init__(self, *handlers, **kwargs): + Wrapper.__init__(self, kwargs.get("impl", pn_reactor), pn_reactor_attachments) + for h in handlers: + self.handler.add(h) + + def _init(self): + pass + + @property + def handler(self): + return WrappedHandler(pn_reactor_handler(self._impl)) + + def run(self): + pn_reactor_start(self._impl) + while pn_reactor_work(self._impl, 3142): pass + pn_reactor_stop(self._impl) + + def schedule(self, delay, task): + impl = _chandler(task) + task = Task.wrap(pn_reactor_schedule(self._impl, secs2millis(delay), impl)) + pn_decref(impl) + return task + + def acceptor(self, host, port, handler=None): + impl = _chandler(handler) + result = Acceptor(pn_reactor_acceptor(self._impl, host, port, impl)) + pn_decref(impl) + return result + + def connection(self, handler=None): + impl = _chandler(handler) + result = Connection.wrap(pn_reactor_connection(self._impl, impl)) + pn_decref(impl) + return result + +from proton import wrappers as _wrappers +_wrappers["pn_reactor"] = lambda x: Reactor.wrap(pn_cast_pn_reactor(x)) +_wrappers["pn_task"] = lambda x: Task.wrap(pn_cast_pn_task(x)) --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org