capn_proto-rpc 0.1.1.alpha.rpc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +19 -0
  4. data/Gemfile +2 -0
  5. data/LICENSE +19 -0
  6. data/README.md +249 -0
  7. data/Rakefile +44 -0
  8. data/capn_proto.gemspec +37 -0
  9. data/examples/addressbook.bin +0 -0
  10. data/examples/addressbook.capnp +31 -0
  11. data/examples/create_test_data.py +39 -0
  12. data/examples/example.rb +38 -0
  13. data/ext/capn_proto/.ycm_extra_conf.py +65 -0
  14. data/ext/capn_proto/EzRpc_client.cc +52 -0
  15. data/ext/capn_proto/EzRpc_client.h +21 -0
  16. data/ext/capn_proto/EzRpc_server.cc +79 -0
  17. data/ext/capn_proto/EzRpc_server.h +32 -0
  18. data/ext/capn_proto/call_context.cc +67 -0
  19. data/ext/capn_proto/call_context.h +25 -0
  20. data/ext/capn_proto/capability_client.cc +47 -0
  21. data/ext/capn_proto/capability_client.h +21 -0
  22. data/ext/capn_proto/class_builder.cc +80 -0
  23. data/ext/capn_proto/class_builder.h +72 -0
  24. data/ext/capn_proto/cxx_compiler.rb +126 -0
  25. data/ext/capn_proto/dynamic_capability_client.cc +73 -0
  26. data/ext/capn_proto/dynamic_capability_client.h +22 -0
  27. data/ext/capn_proto/dynamic_list_builder.cc +123 -0
  28. data/ext/capn_proto/dynamic_list_builder.h +27 -0
  29. data/ext/capn_proto/dynamic_list_reader.cc +63 -0
  30. data/ext/capn_proto/dynamic_list_reader.h +25 -0
  31. data/ext/capn_proto/dynamic_object_builder.cc +57 -0
  32. data/ext/capn_proto/dynamic_object_builder.h +22 -0
  33. data/ext/capn_proto/dynamic_object_reader.cc +56 -0
  34. data/ext/capn_proto/dynamic_object_reader.h +22 -0
  35. data/ext/capn_proto/dynamic_struct_builder.cc +180 -0
  36. data/ext/capn_proto/dynamic_struct_builder.h +34 -0
  37. data/ext/capn_proto/dynamic_struct_reader.cc +69 -0
  38. data/ext/capn_proto/dynamic_struct_reader.h +25 -0
  39. data/ext/capn_proto/dynamic_value_builder.cc +53 -0
  40. data/ext/capn_proto/dynamic_value_builder.h +13 -0
  41. data/ext/capn_proto/dynamic_value_reader.cc +55 -0
  42. data/ext/capn_proto/dynamic_value_reader.h +13 -0
  43. data/ext/capn_proto/exception.cc +34 -0
  44. data/ext/capn_proto/exception.h +20 -0
  45. data/ext/capn_proto/extconf.rb +32 -0
  46. data/ext/capn_proto/field_list.cc +51 -0
  47. data/ext/capn_proto/field_list.h +23 -0
  48. data/ext/capn_proto/flat_array_message_reader.cc +61 -0
  49. data/ext/capn_proto/flat_array_message_reader.h +21 -0
  50. data/ext/capn_proto/init.cc +71 -0
  51. data/ext/capn_proto/interface_method.cc +38 -0
  52. data/ext/capn_proto/interface_method.h +21 -0
  53. data/ext/capn_proto/interface_schema.cc +51 -0
  54. data/ext/capn_proto/interface_schema.h +26 -0
  55. data/ext/capn_proto/list_nested_node_reader.cc +53 -0
  56. data/ext/capn_proto/list_nested_node_reader.h +24 -0
  57. data/ext/capn_proto/malloc_message_builder.cc +51 -0
  58. data/ext/capn_proto/malloc_message_builder.h +21 -0
  59. data/ext/capn_proto/message_builder.cc +22 -0
  60. data/ext/capn_proto/message_builder.h +17 -0
  61. data/ext/capn_proto/message_reader.cc +30 -0
  62. data/ext/capn_proto/message_reader.h +17 -0
  63. data/ext/capn_proto/nested_node_reader.cc +42 -0
  64. data/ext/capn_proto/nested_node_reader.h +21 -0
  65. data/ext/capn_proto/parsed_schema.cc +65 -0
  66. data/ext/capn_proto/parsed_schema.h +24 -0
  67. data/ext/capn_proto/rb.cc +0 -0
  68. data/ext/capn_proto/rb.h +0 -0
  69. data/ext/capn_proto/remote_promise.cc +116 -0
  70. data/ext/capn_proto/remote_promise.h +30 -0
  71. data/ext/capn_proto/ruby_capability_server.cc +23 -0
  72. data/ext/capn_proto/ruby_capability_server.h +21 -0
  73. data/ext/capn_proto/ruby_capn_proto.cc +4 -0
  74. data/ext/capn_proto/ruby_capn_proto.h +42 -0
  75. data/ext/capn_proto/schema_node_reader.cc +59 -0
  76. data/ext/capn_proto/schema_node_reader.h +23 -0
  77. data/ext/capn_proto/schema_parser.cc +61 -0
  78. data/ext/capn_proto/schema_parser.h +20 -0
  79. data/ext/capn_proto/stream_fd_message_reader.cc +59 -0
  80. data/ext/capn_proto/stream_fd_message_reader.h +21 -0
  81. data/ext/capn_proto/struct_schema.cc +59 -0
  82. data/ext/capn_proto/struct_schema.h +23 -0
  83. data/ext/capn_proto/util.cc +30 -0
  84. data/ext/capn_proto/util.h +17 -0
  85. data/lib/capn_proto.rb +231 -0
  86. data/lib/capn_proto/version.rb +3 -0
  87. data/media/captain_proto.png +0 -0
  88. data/media/captain_proto_small.png +0 -0
  89. data/spec/addressbook.bin +0 -0
  90. data/spec/addressbook.capnp +31 -0
  91. data/spec/capn_proto_spec.rb +80 -0
  92. data/spec/create_test_data.py +38 -0
  93. data/spec/spec_helper.rb +8 -0
  94. data/tests/hidraCordatus.capnp +16 -0
  95. data/tests/hidraCordatusEmployer.rb +33 -0
  96. data/tests/hidraCordatusMaster.rb +51 -0
  97. metadata +216 -0
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.expand_path("../../lib", __FILE__)
3
+ $: << File.expand_path("../../ext", __FILE__)
4
+ require 'capn_proto'
5
+
6
+ ADDRESSBOOK_SCHEMA = File.expand_path("../addressbook.capnp", __FILE__);
7
+ MESSAGE = File.expand_path("../addressbook.bin", __FILE__);
8
+
9
+ module AddressBook extend CapnProto::SchemaLoader
10
+ load_schema(ADDRESSBOOK_SCHEMA)
11
+ end
12
+
13
+ def print_address_book(file)
14
+ addresses = AddressBook::AddressBook.read_from(file)
15
+
16
+ addresses.people.each do |person|
17
+ puts "#{person.name} : #{person.email}"
18
+ person.phones.each do |phone|
19
+ puts "#{phone.type} : #{phone.number}"
20
+ end
21
+
22
+ which = person.employment.which
23
+ puts which
24
+
25
+ if which == "unemployed"
26
+ puts "unemployed"
27
+ elsif which == 'employer'
28
+ puts "employer: #{person.employment.employer}"
29
+ elsif which == "school"
30
+ puts "student at: #{person.employment.school}"
31
+ elsif which == "selfEmployed"
32
+ puts "self employed"
33
+ end
34
+ end
35
+ end
36
+
37
+ file = File.open(MESSAGE, "rb")
38
+ print_address_book(file)
@@ -0,0 +1,65 @@
1
+ # vim: ts=2 et sw=2 sts=2
2
+
3
+ import os
4
+ import ycm_core
5
+ import glob
6
+ import subprocess
7
+
8
+ def RubyInclude():
9
+ cmd = ["ruby", "-e", "print RbConfig::CONFIG['rubyhdrdir']"]
10
+ process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
11
+ rubyhdrdir = process.stdout.read()
12
+ return rubyhdrdir
13
+
14
+ flags = [
15
+ '-Wall',
16
+ '-Wextra',
17
+ '-Werror',
18
+ '-fexceptions',
19
+ '-Wc++98-compat',
20
+ '-std=c++11',
21
+ '-x', 'c++',
22
+ '-I', '.',
23
+ '-I', '/usr/local/include',
24
+ '-I', RubyInclude(),
25
+ ]
26
+
27
+ def DirectoryOfThisScript():
28
+ return os.path.dirname( os.path.abspath( __file__ ) )
29
+
30
+ def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
31
+ if not working_directory:
32
+ return list( flags )
33
+ new_flags = []
34
+ make_next_absolute = False
35
+ path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
36
+ for flag in flags:
37
+ new_flag = flag
38
+
39
+ if make_next_absolute:
40
+ make_next_absolute = False
41
+ if not flag.startswith( '/' ):
42
+ new_flag = os.path.join( working_directory, flag )
43
+
44
+ for path_flag in path_flags:
45
+ if flag == path_flag:
46
+ make_next_absolute = True
47
+ break
48
+
49
+ if flag.startswith( path_flag ):
50
+ path = flag[ len( path_flag ): ]
51
+ new_flag = path_flag + os.path.join( working_directory, path )
52
+ break
53
+
54
+ if new_flag:
55
+ new_flags.append( new_flag )
56
+ return new_flags
57
+
58
+ def FlagsForFile( filename ):
59
+ relative_to = DirectoryOfThisScript()
60
+ final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
61
+
62
+ return {
63
+ 'flags': final_flags,
64
+ 'do_cache': True
65
+ }
@@ -0,0 +1,52 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "EzRpc_client.h"
3
+ #include "capability_client.h"
4
+ #include "dynamic_capability_client.h"
5
+ #include "class_builder.h"
6
+ #include "exception.h"
7
+ #include "util.h"
8
+
9
+ namespace ruby_capn_proto {
10
+ using WrappedType = capnp::EzRpcClient;
11
+ VALUE EzRpcCapabilityClient::Class;
12
+
13
+ void EzRpcCapabilityClient::Init() {
14
+ ClassBuilder("EzRpcClient", rb_cObject).
15
+ defineAlloc(&alloc).
16
+ defineMethod("client", &make_dynamic).
17
+ defineMethod("initialize" , &create).
18
+ store(&Class);
19
+ }
20
+
21
+ void EzRpcCapabilityClient::free(WrappedType* p) {
22
+ ruby_xfree(p);
23
+ }
24
+
25
+ VALUE EzRpcCapabilityClient::alloc(VALUE klass) {
26
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
27
+ }
28
+
29
+ WrappedType* EzRpcCapabilityClient::unwrap(VALUE self) {
30
+ WrappedType* p;
31
+ Data_Get_Struct(self, WrappedType, p);
32
+ return p;
33
+ }
34
+
35
+ VALUE EzRpcCapabilityClient::create(VALUE self, VALUE dir, VALUE interschema) {
36
+
37
+ WrappedType* rb_self = unwrap(self);
38
+ new (rb_self) capnp::EzRpcClient(Util::toString(dir));
39
+
40
+ //store the InterfaceSchema
41
+ rb_iv_set(self,"schema",interschema);
42
+
43
+ return self;
44
+ }
45
+
46
+ VALUE EzRpcCapabilityClient::make_dynamic(VALUE self){
47
+ VALUE rb_schema = rb_iv_get(self,"schema");
48
+ VALUE rb_cap = CapabilityClient::create(unwrap(self)->getMain());
49
+ return DynamicCapabilityClient::create(rb_cap,rb_schema);
50
+ }
51
+
52
+ }
@@ -0,0 +1,21 @@
1
+ #ifndef EZRPC_CAPABILITY_CLIENT_H
2
+ #define EXRPC_CAPABILITY_CLIENT_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class EzRpcCapabilityClient {
8
+ public:
9
+ using WrappedType = capnp::EzRpcClient;
10
+ static void Init();
11
+ static VALUE alloc(VALUE klass);
12
+ static VALUE create(VALUE self, VALUE dir, VALUE schema);
13
+ static void free(WrappedType* p);
14
+ static WrappedType* unwrap(VALUE self);
15
+ static VALUE make_dynamic(VALUE self);
16
+ static VALUE Class;
17
+ };
18
+ }
19
+
20
+
21
+ #endif /* EZRPC_CAPABILITY_CLIENT_H */
@@ -0,0 +1,79 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "EzRpc_server.h"
3
+ #include "ruby_capability_server.h"
4
+ #include "interface_schema.h"
5
+ #include "exception.h"
6
+ #include "class_builder.h"
7
+ #include <ruby/thread.h>
8
+ #include "util.h"
9
+
10
+ namespace ruby_capn_proto {
11
+ using WrappedType = capnp::EzRpcServer;
12
+ VALUE EzRpcCapabilityServer::Class;
13
+
14
+ void EzRpcCapabilityServer::Init() {
15
+ ClassBuilder("EzRpcServer", rb_cObject).
16
+ defineAlloc(&alloc).
17
+ defineMethod("run" , &process).
18
+ defineMethod("initialize" , &create).
19
+ store(&Class);
20
+ }
21
+
22
+ void EzRpcCapabilityServer::free(WrappedType* p) {
23
+ ruby_xfree(p);
24
+ }
25
+
26
+ VALUE EzRpcCapabilityServer::alloc(VALUE klass) {
27
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
28
+ }
29
+
30
+ WrappedType* EzRpcCapabilityServer::unwrap(VALUE self) {
31
+ WrappedType* p;
32
+ Data_Get_Struct(self, WrappedType, p);
33
+ return p;
34
+ }
35
+
36
+ VALUE EzRpcCapabilityServer::create(VALUE self, VALUE rb_capServer, VALUE dir) {
37
+
38
+ VALUE interschema = rb_iv_get(rb_capServer,"@schema");
39
+ auto schema = InterfaceSchema::unwrap(interschema);
40
+
41
+ WrappedType* rb_self = unwrap(self);
42
+ new (rb_self) capnp::EzRpcServer( kj::heap<capnp::RubyCapabilityServer>(*schema, rb_capServer) , Util::toString(dir) );
43
+
44
+ return self;
45
+ }
46
+
47
+ VALUE EzRpcCapabilityServer::process(VALUE self){
48
+ try{
49
+ loopCall l;
50
+ auto server = unwrap(self);
51
+ auto to_fulfill = kj::heap<kj::PromiseFulfillerPair<void>>(kj::newPromiseAndFulfiller<void>());
52
+ l.waitscope = &server->getWaitScope();
53
+ l.promisepair = to_fulfill.get();
54
+ rb_thread_call_without_gvl(loopServer, &l, stopLoopServer , l.promisepair);
55
+ }catch ( kj::Exception t ){
56
+ Exception::raise(t);
57
+ }
58
+ return Qtrue;
59
+ }
60
+
61
+ void * EzRpcCapabilityServer::loopServer(void * p){
62
+ try {
63
+ auto* loopcall = (loopCall*) p;
64
+ loopcall->promisepair->promise.wait(*(loopcall->waitscope));
65
+ }catch( kj::Exception t ){
66
+ //adquire the lock to raise a ruby exception
67
+ rb_thread_call_with_gvl(&Exception::raise,&t);
68
+ }
69
+ }
70
+
71
+ void EzRpcCapabilityServer::stopLoopServer(void *p){
72
+ try {
73
+ auto* promisefulfiller = (kj::PromiseFulfillerPair<void>*) p;
74
+ promisefulfiller->fulfiller->fulfill();
75
+ }catch( kj::Exception t ){
76
+ Exception::raise(t);
77
+ }
78
+ }
79
+ }
@@ -0,0 +1,32 @@
1
+ #ifndef EZRPC_CAPABILITY_SERVER_H
2
+ #define EZRPC_CAPABILITY_SERVER_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+
8
+ typedef struct _loopCall{
9
+ kj::WaitScope* waitscope;
10
+ kj::PromiseFulfillerPair<void>* promisepair;
11
+ } loopCall;
12
+
13
+ class EzRpcCapabilityServer {
14
+ public:
15
+ using WrappedType = capnp::EzRpcServer;
16
+ static void Init();
17
+ static VALUE alloc(VALUE klass);
18
+ static VALUE create(VALUE self, VALUE rb_server, VALUE dir);
19
+ static void free(WrappedType* p);
20
+ static WrappedType* unwrap(VALUE self);
21
+ static VALUE process(VALUE self);
22
+ static void * loopServer(void* p);
23
+ static void stopLoopServer(void* p);
24
+ // schema and rb_server to make new RubyCapabilityServer on the fly
25
+ static capnp::InterfaceSchema* schema(VALUE self);
26
+ static VALUE rb_server(VALUE self);
27
+ static VALUE Class;
28
+ };
29
+ }
30
+
31
+
32
+ #endif /* EZRPC_CAPABILITY_SERVER_H */
@@ -0,0 +1,67 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "call_context.h"
3
+ #include "dynamic_struct_reader.h"
4
+ #include "dynamic_struct_builder.h"
5
+ #include "class_builder.h"
6
+ #include "util.h"
7
+
8
+ namespace ruby_capn_proto {
9
+ using WrappedType = capnp::CallContext<capnp::DynamicStruct, capnp::DynamicStruct>;
10
+ VALUE CallContext::Class;
11
+
12
+ void CallContext::Init() {
13
+ ClassBuilder("CallContext" , rb_cObject).
14
+ defineAlloc(&alloc).
15
+ defineMethod("getParams" , &getParams).
16
+ defineMethod("releaseParams" , &releaseParams).
17
+ defineMethod("getResults" , &getResults).
18
+ defineMethod("initResults" , &initResults).
19
+ defineMethod("setResults" , &setResults).
20
+ store(&Class);
21
+ }
22
+
23
+ void CallContext::free(WrappedType* p) {
24
+ p->~CallContext();
25
+ ruby_xfree(p);
26
+ }
27
+
28
+ VALUE CallContext::alloc(VALUE klass) {
29
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
30
+ }
31
+
32
+ WrappedType* CallContext::unwrap(VALUE self) {
33
+ WrappedType* p;
34
+ Data_Get_Struct(self, WrappedType, p);
35
+ return p;
36
+ }
37
+
38
+ VALUE CallContext::create(WrappedType context) {
39
+ VALUE rb_obj = alloc(Class);
40
+ WrappedType* wrapped_context = unwrap(rb_obj);
41
+ *wrapped_context = kj::mv(context);
42
+ return rb_obj;
43
+ }
44
+
45
+ VALUE CallContext::getParams(VALUE self) {
46
+ return DynamicStructReader::create(unwrap(self)->getParams(),Qnil);
47
+ }
48
+
49
+ VALUE CallContext::releaseParams(VALUE self) {
50
+ unwrap(self)->releaseParams();
51
+ return Qtrue;
52
+ }
53
+
54
+ VALUE CallContext::getResults(VALUE self) {
55
+ return DynamicStructBuilder::create(unwrap(self)->getResults(), Qnil, Qtrue);
56
+ }
57
+
58
+ VALUE CallContext::initResults(VALUE self) {
59
+ return DynamicStructBuilder::create(unwrap(self)->initResults(), Qnil, Qtrue );
60
+ }
61
+
62
+ VALUE CallContext::setResults(VALUE self, VALUE structReader) {
63
+ // maybe check the type of structReader?
64
+ unwrap(self)->setResults( *DynamicStructReader::unwrap(structReader) );
65
+ return Qtrue;
66
+ }
67
+ }
@@ -0,0 +1,25 @@
1
+ #ifndef CONTEXT_H
2
+ #define CONTEXT_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class CallContext {
8
+ public:
9
+ using WrappedType = capnp::CallContext<capnp::DynamicStruct, capnp::DynamicStruct>;
10
+ static void Init();
11
+ static VALUE alloc(VALUE klass);
12
+ static void free(WrappedType* p);
13
+ static VALUE create(WrappedType context);
14
+ static WrappedType* unwrap(VALUE self);
15
+ static VALUE getParams(VALUE self);
16
+ static VALUE releaseParams(VALUE self);
17
+ static VALUE getResults(VALUE self);
18
+ static VALUE initResults(VALUE self);
19
+ static VALUE setResults(VALUE self, VALUE structBuilder);
20
+
21
+ static VALUE Class;
22
+ };
23
+ }
24
+
25
+ #endif /* CONTEXT_H */
@@ -0,0 +1,47 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "capability_client.h"
3
+ #include "dynamic_capability_client.h"
4
+ #include "exception.h"
5
+ #include "class_builder.h"
6
+ #include "util.h"
7
+
8
+ namespace ruby_capn_proto {
9
+ using WrappedType = capnp::Capability::Client;
10
+ VALUE CapabilityClient::Class;
11
+
12
+ void CapabilityClient::Init() {
13
+ ClassBuilder("CapabilityClient", rb_cObject).
14
+ defineAlloc(&alloc).
15
+ defineMethod("to_dynamic" , &to_dynamic).
16
+ store(&Class);
17
+ }
18
+
19
+ void CapabilityClient::free(WrappedType* p) {
20
+ p->~Client();
21
+ ruby_xfree(p);
22
+ }
23
+
24
+ VALUE CapabilityClient::alloc(VALUE klass) {
25
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
26
+ }
27
+
28
+ WrappedType* CapabilityClient::unwrap(VALUE self) {
29
+ WrappedType* p;
30
+ Data_Get_Struct(self, WrappedType, p);
31
+ return p;
32
+ }
33
+
34
+ VALUE CapabilityClient::create(WrappedType native_client ) {
35
+
36
+ VALUE self = alloc(Class);
37
+ WrappedType* rb_self = unwrap(self);
38
+ new (rb_self) capnp::Capability::Client(native_client);
39
+
40
+ return self;
41
+ }
42
+
43
+ VALUE CapabilityClient::to_dynamic(VALUE self, VALUE schema){
44
+ return DynamicCapabilityClient::create(self,schema);
45
+ }
46
+
47
+ }
@@ -0,0 +1,21 @@
1
+ #ifndef CAPABILITY_CLIENT_H
2
+ #define CAPABILITY_CLIENT_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class CapabilityClient {
8
+ public:
9
+ using WrappedType = capnp::Capability::Client;
10
+ static void Init();
11
+ static VALUE alloc(VALUE klass);
12
+ static VALUE create(WrappedType native_client);
13
+ static void free(WrappedType* p);
14
+ static WrappedType* unwrap(VALUE self);
15
+ static VALUE to_dynamic(VALUE self, VALUE schema);
16
+ static VALUE Class;
17
+ };
18
+ }
19
+
20
+
21
+ #endif /* CAPABILITY_CLIENT_H */