From 618097dec1295d97946a2b1b4601747be978082f Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Mon, 9 Dec 2013 19:18:54 -0800 Subject: [PATCH] Add ability for passing AnyPointer to restore in RPCClient --- capnp/helpers/helpers.pxd | 4 +- capnp/helpers/rpcHelper.h | 10 +++++ capnp/includes/capnp_cpp.pxd | 3 +- capnp/lib/capnp.pyx | 54 ++++++++++++++--------- docs/quickstart.rst | 2 +- examples/{example.py => addressbook.py} | 0 examples/example_capability.capnp | 58 ------------------------- 7 files changed, 50 insertions(+), 81 deletions(-) rename examples/{example.py => addressbook.py} (100%) mode change 100644 => 100755 delete mode 100644 examples/example_capability.capnp diff --git a/capnp/helpers/helpers.pxd b/capnp/helpers/helpers.pxd index 86d7679..8b25aa6 100644 --- a/capnp/helpers/helpers.pxd +++ b/capnp/helpers/helpers.pxd @@ -1,4 +1,4 @@ -from .capnp.includes.capnp_cpp cimport Maybe, DynamicStruct, Request, PyPromise, VoidPromise, RemotePromise, DynamicCapability, InterfaceSchema, EnumSchema, StructSchema, DynamicValue, Capability, RpcSystem, MessageBuilder, MessageReader, TwoPartyVatNetwork, PyRestorer +from .capnp.includes.capnp_cpp cimport Maybe, DynamicStruct, Request, PyPromise, VoidPromise, RemotePromise, DynamicCapability, InterfaceSchema, EnumSchema, StructSchema, DynamicValue, Capability, RpcSystem, MessageBuilder, MessageReader, TwoPartyVatNetwork, PyRestorer, AnyPointer from non_circular cimport reraise_kj_exception @@ -24,4 +24,6 @@ cdef extern from "../helpers/capabilityHelper.h": cdef extern from "../helpers/rpcHelper.h": Capability.Client restoreHelper(RpcSystem&, MessageBuilder&) Capability.Client restoreHelper(RpcSystem&, MessageReader&) + Capability.Client restoreHelper(RpcSystem&, AnyPointer.Reader&) + Capability.Client restoreHelper(RpcSystem&, AnyPointer.Builder&) RpcSystem makeRpcClientWithRestorer(TwoPartyVatNetwork&, PyRestorer&) diff --git a/capnp/helpers/rpcHelper.h b/capnp/helpers/rpcHelper.h index a0dab53..d40af54 100644 --- a/capnp/helpers/rpcHelper.h +++ b/capnp/helpers/rpcHelper.h @@ -47,6 +47,16 @@ capnp::Capability::Client restoreHelper(capnp::RpcSystem()); } +capnp::Capability::Client restoreHelper(capnp::RpcSystem& client, capnp::AnyPointer::Reader & objectId) { capnp::MallocMessageBuilder hostIdMessage(8); + auto hostId = hostIdMessage.initRoot(); + hostId.setSide(capnp::rpc::twoparty::Side::SERVER); + return client.restore(hostId, objectId); +} +capnp::Capability::Client restoreHelper(capnp::RpcSystem& client, capnp::AnyPointer::Builder & objectId) { capnp::MallocMessageBuilder hostIdMessage(8); + auto hostId = hostIdMessage.initRoot(); + hostId.setSide(capnp::rpc::twoparty::Side::SERVER); + return client.restore(hostId, objectId); +} template diff --git a/capnp/includes/capnp_cpp.pxd b/capnp/includes/capnp_cpp.pxd index 737050c..6b24193 100644 --- a/capnp/includes/capnp_cpp.pxd +++ b/capnp/includes/capnp_cpp.pxd @@ -218,7 +218,7 @@ cdef extern from "capnp/capability.h" namespace " ::capnp": cdef extern from "../helpers/rpcHelper.h": cdef cppclass PyRestorer: PyRestorer(PyObject *, StructSchema&) - + cdef extern from "capnp/rpc-twoparty.h" namespace " ::capnp": cdef cppclass RpcSystem" ::capnp::RpcSystem": RpcSystem(RpcSystem&&) @@ -252,6 +252,7 @@ cdef extern from "capnp/any.h" namespace " ::capnp": cppclass Builder: Builder(Builder) DynamicStruct.Builder getAs"getAs< ::capnp::DynamicStruct>"(StructSchema) + void setAsText"setAs< ::capnp::Text>"(char*) cdef extern from "capnp/dynamic.h" namespace " ::capnp": cdef cppclass DynamicEnum: diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index a95b51b..280ee3d 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -1138,6 +1138,9 @@ cdef class _DynamicObjectBuilder: return _DynamicStructBuilder()._init(self.thisptr.getAs(s.thisptr), self._parent) + cpdef set_as_text(self, text): + self.thisptr.setAsText(text) + cdef class _EventLoop: cdef capnp.AsyncIoContext * thisptr @@ -1529,23 +1532,32 @@ cdef class RpcClient: cpdef restore(self, objectId) except +reraise_kj_exception: cdef _MessageBuilder builder cdef _MessageReader reader + cdef _DynamicObjectBuilder object_builder + cdef _DynamicObjectReader object_reader - if not hasattr(objectId, 'is_root'): - raise ValueError("objectId was not a valid Cap'n Proto struct") - if not objectId.is_root: - raise ValueError("objectId must be the root of a Cap'n Proto message, ie. addressbook_capnp.Person.new_message()") - - try: - builder = objectId._parent - except: - reader = objectId._parent - - if builder is not None: - return _CapabilityClient()._init(helpers.restoreHelper(deref(self.thisptr), deref(builder.thisptr)), self) - elif reader is not None: - return _CapabilityClient()._init(helpers.restoreHelper(deref(self.thisptr), deref(reader.thisptr)), self) + if type(objectId) is _DynamicObjectBuilder: + object_builder = objectId + return _CapabilityClient()._init(helpers.restoreHelper(deref(self.thisptr), deref(object_builder.thisptr)), self) + elif type(objectId) is _DynamicObjectReader: + object_reader = objectId + return _CapabilityClient()._init(helpers.restoreHelper(deref(self.thisptr), object_reader.thisptr), self) else: - raise ValueError("objectId unexpectedly was not convertible to the proper type") + if not hasattr(objectId, 'is_root'): + raise ValueError("objectId was not a valid Cap'n Proto struct") + if not objectId.is_root: + raise ValueError("objectId must be the root of a Cap'n Proto message, ie. addressbook_capnp.Person.new_message()") + + try: + builder = objectId._parent + except: + reader = objectId._parent + + if builder is not None: + return _CapabilityClient()._init(helpers.restoreHelper(deref(self.thisptr), deref(builder.thisptr)), self) + elif reader is not None: + return _CapabilityClient()._init(helpers.restoreHelper(deref(self.thisptr), deref(reader.thisptr)), self) + else: + raise ValueError("objectId unexpectedly was not convertible to the proper type") cdef class RpcServer: cdef RpcSystem * thisptr @@ -2050,11 +2062,13 @@ cdef class _MessageBuilder: :rtype: void """ - - if type(value) is _DynamicStructBuilder: - value = value.as_reader(); - self.thisptr.setRootDynamicStruct((<_DynamicStructReader>value).thisptr) - return self.get_root(value.schema) + value_type = type(value) + if value_type is _DynamicStructBuilder: + self.thisptr.setRootDynamicStruct((<_DynamicStructReader>value.as_reader()).thisptr) + return self.get_root(value.schema) + elif value_type is _DynamicStructReader: + self.thisptr.setRootDynamicStruct((<_DynamicStructReader>value).thisptr) + return self.get_root(value.schema) cpdef new_orphan(self, schema) except +reraise_kj_exception: """A method for instantiating Cap'n Proto orphans diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 4f5d561..3644edd 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -243,7 +243,7 @@ And a corresponding from_bytes function:: Full Example ------------------ -Here is a full example reproduced from `examples/example.py `_:: +Here is a full example reproduced from `examples/example.py `_:: from __future__ import print_function import os diff --git a/examples/example.py b/examples/addressbook.py old mode 100644 new mode 100755 similarity index 100% rename from examples/example.py rename to examples/addressbook.py diff --git a/examples/example_capability.capnp b/examples/example_capability.capnp deleted file mode 100644 index 6df9cdb..0000000 --- a/examples/example_capability.capnp +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright (c) 2013, Kenton Varda -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. -# 2. Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -@0xd508eefec2dc42b8; - -interface TestInterface { - foo @0 (i :UInt32, j :Bool) -> (x: Text); - bar @1 () -> (); - # baz @2 (s: TestAllTypes); -} - -interface TestExtends extends(TestInterface) { - qux @0 (); -# corge @1 TestAllTypes -> (); -# grault @2 () -> TestAllTypes; -} - -interface TestPipeline { - getCap @0 (n: UInt32, inCap :TestInterface) -> (s: Text, outBox :Box); - testPointers @1 (cap :TestInterface, obj :AnyPointer, list :List(TestInterface)) -> (); - - struct Box { - cap @0 :TestInterface; - } -} - -struct TestSturdyRefHostId { - host @0 :Text; -} - -struct TestSturdyRefObjectId { - tag @0 :Tag; - enum Tag { - testInterface @0; - testExtends @1; - testPipeline @2; - } -} \ No newline at end of file