Add ability for passing AnyPointer to restore in RPCClient

This commit is contained in:
Jason Paryani
2013-12-09 19:18:54 -08:00
parent 0892a63b23
commit 618097dec1
7 changed files with 50 additions and 81 deletions

View File

@@ -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&)

View File

@@ -47,6 +47,16 @@ capnp::Capability::Client restoreHelper(capnp::RpcSystem<capnp::rpc::twoparty::S
hostId.setSide(capnp::rpc::twoparty::Side::SERVER);
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,
typename RecipientId, typename ThirdPartyCapId, typename JoinAnswer>

View File

@@ -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:

View File

@@ -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,7 +1532,16 @@ 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 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:
if not hasattr(objectId, 'is_root'):
raise ValueError("objectId was not a valid Cap'n Proto struct")
if not objectId.is_root:
@@ -2050,9 +2062,11 @@ cdef class _MessageBuilder:
:rtype: void
"""
if type(value) is _DynamicStructBuilder:
value = value.as_reader();
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)

View File

@@ -243,7 +243,7 @@ And a corresponding from_bytes function::
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
import os

0
examples/example.py → examples/addressbook.py Normal file → Executable file
View File

View 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;
}
}