From 18c6d41f96cad5d46440437a1cd872a4a376f06a Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Tue, 28 Jan 2014 21:42:57 -0800 Subject: [PATCH] Add beginnings of EventPort wrapping --- capnp/helpers/asyncHelper.h | 29 +++++++++++++++++++++++++++++ capnp/helpers/non_circular.pxd | 10 +++++++++- capnp/includes/capnp_cpp.pxd | 16 ++-------------- 3 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 capnp/helpers/asyncHelper.h diff --git a/capnp/helpers/asyncHelper.h b/capnp/helpers/asyncHelper.h new file mode 100644 index 0000000..40711b8 --- /dev/null +++ b/capnp/helpers/asyncHelper.h @@ -0,0 +1,29 @@ +#pragma once + +#include "kj/async.h" +#include "Python.h" + +class PyEventPort: public kj::EventPort { +public: + PyEventPort(PyObject * _py_event_port): py_event_port(_py_event_port) { + // We don't need to incref/decref, since this C++ class will be owned by the Python wrapper class, and we'll make sure the python class doesn't refcount to 0 elsewhere. + // Py_INCREF(py_event_port); + } + virtual void wait() { + PyObject_CallMethod(py_event_port, const_cast("wait"), NULL); + } + + virtual void poll() { + PyObject_CallMethod(py_event_port, const_cast("poll"), NULL); + } + + virtual void setRunnable(bool runnable) { + PyObject * arg = Py_False; + if (runnable) + arg = Py_True; + PyObject_CallMethod(py_event_port, const_cast("set_runnable"), const_cast("o"), arg); + } + +private: + PyObject * py_event_port; +}; diff --git a/capnp/helpers/non_circular.pxd b/capnp/helpers/non_circular.pxd index 9c51e24..79e4043 100644 --- a/capnp/helpers/non_circular.pxd +++ b/capnp/helpers/non_circular.pxd @@ -7,4 +7,12 @@ cdef extern from "../helpers/capabilityHelper.h": cdef extern from "../helpers/capabilityHelper.h": void reraise_kj_exception() cdef cppclass PyRefCounter: - PyRefCounter(PyObject *) \ No newline at end of file + PyRefCounter(PyObject *) + +cdef extern from "../helpers/rpcHelper.h": + cdef cppclass PyRestorer: + PyRestorer(PyObject *) + +cdef extern from "../helpers/asyncHelper.h": + cdef cppclass PyEventPort: + PyEventPort(PyObject *) \ No newline at end of file diff --git a/capnp/includes/capnp_cpp.pxd b/capnp/includes/capnp_cpp.pxd index f73aa8a..c079070 100644 --- a/capnp/includes/capnp_cpp.pxd +++ b/capnp/includes/capnp_cpp.pxd @@ -5,7 +5,7 @@ cdef extern from "../helpers/checkCompiler.h": pass from schema_cpp cimport Node, Data, StructNode, EnumNode, InterfaceNode, MessageBuilder, MessageReader -from .capnp.helpers.non_circular cimport PythonInterfaceDynamicImpl, reraise_kj_exception, PyRefCounter +from .capnp.helpers.non_circular cimport PythonInterfaceDynamicImpl, reraise_kj_exception, PyRefCounter, PyRestorer, PyEventPort from .capnp.includes.types cimport * cdef extern from "capnp/common.h" namespace " ::capnp": @@ -245,10 +245,6 @@ cdef extern from "capnp/capability.h" namespace " ::capnp": Client(Client&) DynamicCapability.Client castAs"castAs< ::capnp::DynamicCapability>"(InterfaceSchema) -cdef extern from "../helpers/rpcHelper.h": - cdef cppclass PyRestorer: - PyRestorer(PyObject *) - cdef extern from "capnp/rpc-twoparty.h" namespace " ::capnp": cdef cppclass RpcSystem" ::capnp::RpcSystem": RpcSystem(RpcSystem&&) @@ -400,15 +396,7 @@ cdef extern from "capnp/capability.h" namespace " ::capnp": cdef extern from "kj/async.h" namespace " ::kj": cdef cppclass EventLoop: EventLoop() - # Promise[void] yield_end'yield'() - # object wait(PyPromise) except +reraise_kj_exception - # Response wait_remote'wait'(RemotePromise) - # void wait_void'wait'(VoidPromise) - # object there(PyPromise) except +reraise_kj_exception - # PyPromise evalLater(PyObject * func) - # PyPromise there(PyPromise, PyObject * func) - cdef cppclass SimpleEventLoop(EventLoop): - pass + EventLoop(PyEventPort &) cdef cppclass PromiseFulfiller: void fulfill() cdef cppclass PromiseFulfillerPair" ::kj::PromiseFulfillerPair":