Add ability for passing AnyPointer to restore in RPCClient
This commit is contained in:
@@ -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
|
from non_circular cimport reraise_kj_exception
|
||||||
|
|
||||||
@@ -24,4 +24,6 @@ cdef extern from "../helpers/capabilityHelper.h":
|
|||||||
cdef extern from "../helpers/rpcHelper.h":
|
cdef extern from "../helpers/rpcHelper.h":
|
||||||
Capability.Client restoreHelper(RpcSystem&, MessageBuilder&)
|
Capability.Client restoreHelper(RpcSystem&, MessageBuilder&)
|
||||||
Capability.Client restoreHelper(RpcSystem&, MessageReader&)
|
Capability.Client restoreHelper(RpcSystem&, MessageReader&)
|
||||||
|
Capability.Client restoreHelper(RpcSystem&, AnyPointer.Reader&)
|
||||||
|
Capability.Client restoreHelper(RpcSystem&, AnyPointer.Builder&)
|
||||||
RpcSystem makeRpcClientWithRestorer(TwoPartyVatNetwork&, PyRestorer&)
|
RpcSystem makeRpcClientWithRestorer(TwoPartyVatNetwork&, PyRestorer&)
|
||||||
|
|||||||
@@ -47,6 +47,16 @@ capnp::Capability::Client restoreHelper(capnp::RpcSystem<capnp::rpc::twoparty::S
|
|||||||
hostId.setSide(capnp::rpc::twoparty::Side::SERVER);
|
hostId.setSide(capnp::rpc::twoparty::Side::SERVER);
|
||||||
return client.restore(hostId, objectId.getRoot<capnp::AnyPointer>());
|
return client.restore(hostId, objectId.getRoot<capnp::AnyPointer>());
|
||||||
}
|
}
|
||||||
|
capnp::Capability::Client restoreHelper(capnp::RpcSystem<capnp::rpc::twoparty::SturdyRefHostId>& client, capnp::AnyPointer::Reader & objectId) { capnp::MallocMessageBuilder hostIdMessage(8);
|
||||||
|
auto hostId = hostIdMessage.initRoot<capnp::rpc::twoparty::SturdyRefHostId>();
|
||||||
|
hostId.setSide(capnp::rpc::twoparty::Side::SERVER);
|
||||||
|
return client.restore(hostId, objectId);
|
||||||
|
}
|
||||||
|
capnp::Capability::Client restoreHelper(capnp::RpcSystem<capnp::rpc::twoparty::SturdyRefHostId>& client, capnp::AnyPointer::Builder & objectId) { capnp::MallocMessageBuilder hostIdMessage(8);
|
||||||
|
auto hostId = hostIdMessage.initRoot<capnp::rpc::twoparty::SturdyRefHostId>();
|
||||||
|
hostId.setSide(capnp::rpc::twoparty::Side::SERVER);
|
||||||
|
return client.restore(hostId, objectId);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename SturdyRefHostId, typename ProvisionId,
|
template <typename SturdyRefHostId, typename ProvisionId,
|
||||||
typename RecipientId, typename ThirdPartyCapId, typename JoinAnswer>
|
typename RecipientId, typename ThirdPartyCapId, typename JoinAnswer>
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ cdef extern from "capnp/capability.h" namespace " ::capnp":
|
|||||||
cdef extern from "../helpers/rpcHelper.h":
|
cdef extern from "../helpers/rpcHelper.h":
|
||||||
cdef cppclass PyRestorer:
|
cdef cppclass PyRestorer:
|
||||||
PyRestorer(PyObject *, StructSchema&)
|
PyRestorer(PyObject *, StructSchema&)
|
||||||
|
|
||||||
cdef extern from "capnp/rpc-twoparty.h" namespace " ::capnp":
|
cdef extern from "capnp/rpc-twoparty.h" namespace " ::capnp":
|
||||||
cdef cppclass RpcSystem" ::capnp::RpcSystem<capnp::rpc::twoparty::SturdyRefHostId>":
|
cdef cppclass RpcSystem" ::capnp::RpcSystem<capnp::rpc::twoparty::SturdyRefHostId>":
|
||||||
RpcSystem(RpcSystem&&)
|
RpcSystem(RpcSystem&&)
|
||||||
@@ -252,6 +252,7 @@ cdef extern from "capnp/any.h" namespace " ::capnp":
|
|||||||
cppclass Builder:
|
cppclass Builder:
|
||||||
Builder(Builder)
|
Builder(Builder)
|
||||||
DynamicStruct.Builder getAs"getAs< ::capnp::DynamicStruct>"(StructSchema)
|
DynamicStruct.Builder getAs"getAs< ::capnp::DynamicStruct>"(StructSchema)
|
||||||
|
void setAsText"setAs< ::capnp::Text>"(char*)
|
||||||
|
|
||||||
cdef extern from "capnp/dynamic.h" namespace " ::capnp":
|
cdef extern from "capnp/dynamic.h" namespace " ::capnp":
|
||||||
cdef cppclass DynamicEnum:
|
cdef cppclass DynamicEnum:
|
||||||
|
|||||||
@@ -1138,6 +1138,9 @@ cdef class _DynamicObjectBuilder:
|
|||||||
|
|
||||||
return _DynamicStructBuilder()._init(self.thisptr.getAs(s.thisptr), self._parent)
|
return _DynamicStructBuilder()._init(self.thisptr.getAs(s.thisptr), self._parent)
|
||||||
|
|
||||||
|
cpdef set_as_text(self, text):
|
||||||
|
self.thisptr.setAsText(text)
|
||||||
|
|
||||||
cdef class _EventLoop:
|
cdef class _EventLoop:
|
||||||
cdef capnp.AsyncIoContext * thisptr
|
cdef capnp.AsyncIoContext * thisptr
|
||||||
|
|
||||||
@@ -1529,23 +1532,32 @@ cdef class RpcClient:
|
|||||||
cpdef restore(self, objectId) except +reraise_kj_exception:
|
cpdef restore(self, objectId) except +reraise_kj_exception:
|
||||||
cdef _MessageBuilder builder
|
cdef _MessageBuilder builder
|
||||||
cdef _MessageReader reader
|
cdef _MessageReader reader
|
||||||
|
cdef _DynamicObjectBuilder object_builder
|
||||||
|
cdef _DynamicObjectReader object_reader
|
||||||
|
|
||||||
if not hasattr(objectId, 'is_root'):
|
if type(objectId) is _DynamicObjectBuilder:
|
||||||
raise ValueError("objectId was not a valid Cap'n Proto struct")
|
object_builder = objectId
|
||||||
if not objectId.is_root:
|
return _CapabilityClient()._init(helpers.restoreHelper(deref(self.thisptr), deref(object_builder.thisptr)), self)
|
||||||
raise ValueError("objectId must be the root of a Cap'n Proto message, ie. addressbook_capnp.Person.new_message()")
|
elif type(objectId) is _DynamicObjectReader:
|
||||||
|
object_reader = objectId
|
||||||
try:
|
return _CapabilityClient()._init(helpers.restoreHelper(deref(self.thisptr), object_reader.thisptr), self)
|
||||||
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:
|
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 class RpcServer:
|
||||||
cdef RpcSystem * thisptr
|
cdef RpcSystem * thisptr
|
||||||
@@ -2050,11 +2062,13 @@ cdef class _MessageBuilder:
|
|||||||
|
|
||||||
:rtype: void
|
:rtype: void
|
||||||
"""
|
"""
|
||||||
|
value_type = type(value)
|
||||||
if type(value) is _DynamicStructBuilder:
|
if value_type is _DynamicStructBuilder:
|
||||||
value = value.as_reader();
|
self.thisptr.setRootDynamicStruct((<_DynamicStructReader>value.as_reader()).thisptr)
|
||||||
self.thisptr.setRootDynamicStruct((<_DynamicStructReader>value).thisptr)
|
return self.get_root(value.schema)
|
||||||
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:
|
cpdef new_orphan(self, schema) except +reraise_kj_exception:
|
||||||
"""A method for instantiating Cap'n Proto orphans
|
"""A method for instantiating Cap'n Proto orphans
|
||||||
|
|||||||
@@ -243,7 +243,7 @@ And a corresponding from_bytes function::
|
|||||||
Full Example
|
Full Example
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
Here is a full example reproduced from `examples/example.py <https://github.com/jparyani/pycapnp/blob/master/examples/example.py>`_::
|
Here is a full example reproduced from `examples/example.py <https://github.com/jparyani/pycapnp/blob/master/examples/addressbook.py>`_::
|
||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import os
|
import os
|
||||||
|
|||||||
0
examples/example.py → examples/addressbook.py
Normal file → Executable file
0
examples/example.py → examples/addressbook.py
Normal file → Executable file
@@ -1,58 +0,0 @@
|
|||||||
# Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
|
|
||||||
# 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user