grpc 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of grpc might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +1 -0
- data/.rubocop.yml +10 -0
- data/.rubocop_todo.yml +52 -0
- data/Gemfile +4 -0
- data/README.md +82 -0
- data/Rakefile +54 -0
- data/bin/apis/google/protobuf/empty.rb +44 -0
- data/bin/apis/pubsub_demo.rb +267 -0
- data/bin/apis/tech/pubsub/proto/pubsub.rb +174 -0
- data/bin/apis/tech/pubsub/proto/pubsub_services.rb +103 -0
- data/bin/interop/README.md +8 -0
- data/bin/interop/interop_client.rb +334 -0
- data/bin/interop/interop_server.rb +192 -0
- data/bin/interop/test/cpp/interop/empty.rb +44 -0
- data/bin/interop/test/cpp/interop/messages.rb +89 -0
- data/bin/interop/test/cpp/interop/test.rb +43 -0
- data/bin/interop/test/cpp/interop/test_services.rb +60 -0
- data/bin/math.proto +80 -0
- data/bin/math.rb +61 -0
- data/bin/math_client.rb +147 -0
- data/bin/math_server.rb +190 -0
- data/bin/math_services.rb +56 -0
- data/bin/noproto_client.rb +108 -0
- data/bin/noproto_server.rb +112 -0
- data/ext/grpc/extconf.rb +76 -0
- data/ext/grpc/rb_byte_buffer.c +241 -0
- data/ext/grpc/rb_byte_buffer.h +54 -0
- data/ext/grpc/rb_call.c +569 -0
- data/ext/grpc/rb_call.h +59 -0
- data/ext/grpc/rb_channel.c +264 -0
- data/ext/grpc/rb_channel.h +49 -0
- data/ext/grpc/rb_channel_args.c +154 -0
- data/ext/grpc/rb_channel_args.h +52 -0
- data/ext/grpc/rb_completion_queue.c +185 -0
- data/ext/grpc/rb_completion_queue.h +50 -0
- data/ext/grpc/rb_credentials.c +281 -0
- data/ext/grpc/rb_credentials.h +50 -0
- data/ext/grpc/rb_event.c +361 -0
- data/ext/grpc/rb_event.h +53 -0
- data/ext/grpc/rb_grpc.c +274 -0
- data/ext/grpc/rb_grpc.h +74 -0
- data/ext/grpc/rb_metadata.c +215 -0
- data/ext/grpc/rb_metadata.h +53 -0
- data/ext/grpc/rb_server.c +278 -0
- data/ext/grpc/rb_server.h +50 -0
- data/ext/grpc/rb_server_credentials.c +210 -0
- data/ext/grpc/rb_server_credentials.h +50 -0
- data/grpc.gemspec +41 -0
- data/lib/grpc.rb +39 -0
- data/lib/grpc/core/event.rb +44 -0
- data/lib/grpc/core/time_consts.rb +71 -0
- data/lib/grpc/errors.rb +61 -0
- data/lib/grpc/generic/active_call.rb +536 -0
- data/lib/grpc/generic/bidi_call.rb +221 -0
- data/lib/grpc/generic/client_stub.rb +413 -0
- data/lib/grpc/generic/rpc_desc.rb +150 -0
- data/lib/grpc/generic/rpc_server.rb +404 -0
- data/lib/grpc/generic/service.rb +235 -0
- data/lib/grpc/logconfig.rb +40 -0
- data/lib/grpc/version.rb +33 -0
- data/spec/alloc_spec.rb +44 -0
- data/spec/byte_buffer_spec.rb +67 -0
- data/spec/call_spec.rb +163 -0
- data/spec/channel_spec.rb +181 -0
- data/spec/client_server_spec.rb +372 -0
- data/spec/completion_queue_spec.rb +74 -0
- data/spec/credentials_spec.rb +71 -0
- data/spec/event_spec.rb +53 -0
- data/spec/generic/active_call_spec.rb +373 -0
- data/spec/generic/client_stub_spec.rb +519 -0
- data/spec/generic/rpc_desc_spec.rb +357 -0
- data/spec/generic/rpc_server_pool_spec.rb +139 -0
- data/spec/generic/rpc_server_spec.rb +404 -0
- data/spec/generic/service_spec.rb +342 -0
- data/spec/metadata_spec.rb +64 -0
- data/spec/server_credentials_spec.rb +69 -0
- data/spec/server_spec.rb +212 -0
- data/spec/spec_helper.rb +51 -0
- data/spec/testdata/README +1 -0
- data/spec/testdata/ca.pem +15 -0
- data/spec/testdata/server1.key +16 -0
- data/spec/testdata/server1.pem +16 -0
- data/spec/time_consts_spec.rb +89 -0
- metadata +353 -0
@@ -0,0 +1,108 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Copyright 2015, Google Inc.
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are
|
8
|
+
# met:
|
9
|
+
#
|
10
|
+
# * Redistributions of source code must retain the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer.
|
12
|
+
# * Redistributions in binary form must reproduce the above
|
13
|
+
# copyright notice, this list of conditions and the following disclaimer
|
14
|
+
# in the documentation and/or other materials provided with the
|
15
|
+
# distribution.
|
16
|
+
# * Neither the name of Google Inc. nor the names of its
|
17
|
+
# contributors may be used to endorse or promote products derived from
|
18
|
+
# this software without specific prior written permission.
|
19
|
+
#
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
24
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
26
|
+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
28
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
29
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
|
32
|
+
# Sample app that helps validate RpcServer without protobuf serialization.
|
33
|
+
#
|
34
|
+
# Usage: $ ruby -S path/to/noproto_client.rb
|
35
|
+
|
36
|
+
this_dir = File.expand_path(File.dirname(__FILE__))
|
37
|
+
lib_dir = File.join(File.dirname(this_dir), 'lib')
|
38
|
+
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
|
39
|
+
|
40
|
+
require 'grpc'
|
41
|
+
require 'optparse'
|
42
|
+
|
43
|
+
# a simple non-protobuf message class.
|
44
|
+
class NoProtoMsg
|
45
|
+
def self.marshal(_o)
|
46
|
+
''
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.unmarshal(_o)
|
50
|
+
NoProtoMsg.new
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# service the uses the non-protobuf message class.
|
55
|
+
class NoProtoService
|
56
|
+
include GRPC::GenericService
|
57
|
+
rpc :AnRPC, NoProtoMsg, NoProtoMsg
|
58
|
+
end
|
59
|
+
|
60
|
+
NoProtoStub = NoProtoService.rpc_stub_class
|
61
|
+
|
62
|
+
def load_test_certs
|
63
|
+
this_dir = File.expand_path(File.dirname(__FILE__))
|
64
|
+
data_dir = File.join(File.dirname(this_dir), 'spec/testdata')
|
65
|
+
files = ['ca.pem', 'server1.key', 'server1.pem']
|
66
|
+
files.map { |f| File.open(File.join(data_dir, f)).read }
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_creds
|
70
|
+
certs = load_test_certs
|
71
|
+
GRPC::Core::Credentials.new(certs[0])
|
72
|
+
end
|
73
|
+
|
74
|
+
def main
|
75
|
+
options = {
|
76
|
+
'host' => 'localhost:7071',
|
77
|
+
'secure' => false
|
78
|
+
}
|
79
|
+
OptionParser.new do |opts|
|
80
|
+
opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]'
|
81
|
+
opts.on('--host HOST', '<hostname>:<port>') do |v|
|
82
|
+
options['host'] = v
|
83
|
+
end
|
84
|
+
opts.on('-s', '--secure', 'access using test creds') do |v|
|
85
|
+
options['secure'] = v
|
86
|
+
end
|
87
|
+
end.parse!
|
88
|
+
|
89
|
+
if options['secure']
|
90
|
+
stub_opts = {
|
91
|
+
:creds => test_creds,
|
92
|
+
GRPC::Core::Channel::SSL_TARGET => 'foo.test.google.fr'
|
93
|
+
}
|
94
|
+
p stub_opts
|
95
|
+
p options['host']
|
96
|
+
stub = NoProtoStub.new(options['host'], **stub_opts)
|
97
|
+
logger.info("... connecting securely on #{options['host']}")
|
98
|
+
else
|
99
|
+
stub = NoProtoStub.new(options['host'])
|
100
|
+
logger.info("... connecting insecurely on #{options['host']}")
|
101
|
+
end
|
102
|
+
|
103
|
+
logger.info('sending a NoProto rpc')
|
104
|
+
resp = stub.an_rpc(NoProtoMsg.new)
|
105
|
+
logger.info("got a response: #{resp}")
|
106
|
+
end
|
107
|
+
|
108
|
+
main
|
@@ -0,0 +1,112 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Copyright 2015, Google Inc.
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are
|
8
|
+
# met:
|
9
|
+
#
|
10
|
+
# * Redistributions of source code must retain the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer.
|
12
|
+
# * Redistributions in binary form must reproduce the above
|
13
|
+
# copyright notice, this list of conditions and the following disclaimer
|
14
|
+
# in the documentation and/or other materials provided with the
|
15
|
+
# distribution.
|
16
|
+
# * Neither the name of Google Inc. nor the names of its
|
17
|
+
# contributors may be used to endorse or promote products derived from
|
18
|
+
# this software without specific prior written permission.
|
19
|
+
#
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
24
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
26
|
+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
28
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
29
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
|
32
|
+
# Sample app that helps validate RpcServer without protobuf serialization.
|
33
|
+
#
|
34
|
+
# Usage: $ path/to/noproto_server.rb
|
35
|
+
|
36
|
+
this_dir = File.expand_path(File.dirname(__FILE__))
|
37
|
+
lib_dir = File.join(File.dirname(this_dir), 'lib')
|
38
|
+
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
|
39
|
+
|
40
|
+
require 'grpc'
|
41
|
+
require 'optparse'
|
42
|
+
|
43
|
+
# a simple non-protobuf message class.
|
44
|
+
class NoProtoMsg
|
45
|
+
def self.marshal(_o)
|
46
|
+
''
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.unmarshal(_o)
|
50
|
+
NoProtoMsg.new
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# service the uses the non-protobuf message class.
|
55
|
+
class NoProtoService
|
56
|
+
include GRPC::GenericService
|
57
|
+
rpc :AnRPC, NoProtoMsg, NoProtoMsg
|
58
|
+
end
|
59
|
+
|
60
|
+
# an implementation of the non-protobuf service.
|
61
|
+
class NoProto < NoProtoService
|
62
|
+
def initialize(_default_var = 'ignored')
|
63
|
+
end
|
64
|
+
|
65
|
+
def an_rpc(req, _call)
|
66
|
+
logger.info('echo service received a request')
|
67
|
+
req
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def load_test_certs
|
72
|
+
this_dir = File.expand_path(File.dirname(__FILE__))
|
73
|
+
data_dir = File.join(File.dirname(this_dir), 'spec/testdata')
|
74
|
+
files = ['ca.pem', 'server1.key', 'server1.pem']
|
75
|
+
files.map { |f| File.open(File.join(data_dir, f)).read }
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_server_creds
|
79
|
+
certs = load_test_certs
|
80
|
+
GRPC::Core::ServerCredentials.new(nil, certs[1], certs[2])
|
81
|
+
end
|
82
|
+
|
83
|
+
def main
|
84
|
+
options = {
|
85
|
+
'host' => 'localhost:9090',
|
86
|
+
'secure' => false
|
87
|
+
}
|
88
|
+
OptionParser.new do |opts|
|
89
|
+
opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]'
|
90
|
+
opts.on('--host HOST', '<hostname>:<port>') do |v|
|
91
|
+
options['host'] = v
|
92
|
+
end
|
93
|
+
opts.on('-s', '--secure', 'access using test creds') do |v|
|
94
|
+
options['secure'] = v
|
95
|
+
end
|
96
|
+
end.parse!
|
97
|
+
|
98
|
+
if options['secure']
|
99
|
+
s = GRPC::RpcServer.new(creds: test_server_creds)
|
100
|
+
s.add_http2_port(options['host'], true)
|
101
|
+
logger.info("... running securely on #{options['host']}")
|
102
|
+
else
|
103
|
+
s = GRPC::RpcServer.new
|
104
|
+
s.add_http2_port(options['host'])
|
105
|
+
logger.info("... running insecurely on #{options['host']}")
|
106
|
+
end
|
107
|
+
|
108
|
+
s.handle(NoProto)
|
109
|
+
s.run
|
110
|
+
end
|
111
|
+
|
112
|
+
main
|
data/ext/grpc/extconf.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Copyright 2015, Google Inc.
|
2
|
+
# All rights reserved.
|
3
|
+
#
|
4
|
+
# Redistribution and use in source and binary forms, with or without
|
5
|
+
# modification, are permitted provided that the following conditions are
|
6
|
+
# met:
|
7
|
+
#
|
8
|
+
# * Redistributions of source code must retain the above copyright
|
9
|
+
# notice, this list of conditions and the following disclaimer.
|
10
|
+
# * Redistributions in binary form must reproduce the above
|
11
|
+
# copyright notice, this list of conditions and the following disclaimer
|
12
|
+
# in the documentation and/or other materials provided with the
|
13
|
+
# distribution.
|
14
|
+
# * Neither the name of Google Inc. nor the names of its
|
15
|
+
# contributors may be used to endorse or promote products derived from
|
16
|
+
# this software without specific prior written permission.
|
17
|
+
#
|
18
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
22
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24
|
+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
25
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
26
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
+
|
30
|
+
require 'mkmf'
|
31
|
+
|
32
|
+
LIBDIR = RbConfig::CONFIG['libdir']
|
33
|
+
INCLUDEDIR = RbConfig::CONFIG['includedir']
|
34
|
+
|
35
|
+
HEADER_DIRS = [
|
36
|
+
# Search /opt/local (Mac source install)
|
37
|
+
'/opt/local/include',
|
38
|
+
|
39
|
+
# Search /usr/local (Source install)
|
40
|
+
'/usr/local/include',
|
41
|
+
|
42
|
+
# Check the ruby install locations
|
43
|
+
INCLUDEDIR
|
44
|
+
]
|
45
|
+
|
46
|
+
LIB_DIRS = [
|
47
|
+
# Search /opt/local (Mac source install)
|
48
|
+
'/opt/local/lib',
|
49
|
+
|
50
|
+
# Search /usr/local (Source install)
|
51
|
+
'/usr/local/lib',
|
52
|
+
|
53
|
+
# Check the ruby install locations
|
54
|
+
LIBDIR
|
55
|
+
]
|
56
|
+
|
57
|
+
def crash(msg)
|
58
|
+
print(" extconf failure: #{msg}\n")
|
59
|
+
exit 1
|
60
|
+
end
|
61
|
+
|
62
|
+
dir_config('grpc', HEADER_DIRS, LIB_DIRS)
|
63
|
+
|
64
|
+
$CFLAGS << ' -std=c89 '
|
65
|
+
$CFLAGS << ' -Wno-implicit-function-declaration '
|
66
|
+
$CFLAGS << ' -Wno-pointer-sign '
|
67
|
+
$CFLAGS << ' -Wno-return-type '
|
68
|
+
$CFLAGS << ' -Wall '
|
69
|
+
$CFLAGS << ' -pedantic '
|
70
|
+
|
71
|
+
$LDFLAGS << ' -lgrpc -lgpr -ldl'
|
72
|
+
|
73
|
+
crash('need grpc lib') unless have_library('grpc', 'grpc_channel_destroy')
|
74
|
+
have_library('grpc', 'grpc_channel_destroy')
|
75
|
+
crash('need gpr lib') unless have_library('gpr', 'gpr_now')
|
76
|
+
create_makefile('grpc/grpc')
|
@@ -0,0 +1,241 @@
|
|
1
|
+
/*
|
2
|
+
*
|
3
|
+
* Copyright 2015, Google Inc.
|
4
|
+
* All rights reserved.
|
5
|
+
*
|
6
|
+
* Redistribution and use in source and binary forms, with or without
|
7
|
+
* modification, are permitted provided that the following conditions are
|
8
|
+
* met:
|
9
|
+
*
|
10
|
+
* * Redistributions of source code must retain the above copyright
|
11
|
+
* notice, this list of conditions and the following disclaimer.
|
12
|
+
* * Redistributions in binary form must reproduce the above
|
13
|
+
* copyright notice, this list of conditions and the following disclaimer
|
14
|
+
* in the documentation and/or other materials provided with the
|
15
|
+
* distribution.
|
16
|
+
* * Neither the name of Google Inc. nor the names of its
|
17
|
+
* contributors may be used to endorse or promote products derived from
|
18
|
+
* this software without specific prior written permission.
|
19
|
+
*
|
20
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
24
|
+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
26
|
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
28
|
+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
29
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*
|
32
|
+
*/
|
33
|
+
|
34
|
+
#include "rb_byte_buffer.h"
|
35
|
+
|
36
|
+
#include <ruby.h>
|
37
|
+
|
38
|
+
#include <grpc/grpc.h>
|
39
|
+
#include <grpc/support/slice.h>
|
40
|
+
#include "rb_grpc.h"
|
41
|
+
|
42
|
+
/* grpc_rb_byte_buffer wraps a grpc_byte_buffer. It provides a peer ruby
|
43
|
+
* object, 'mark' to minimize copying when a byte_buffer is created from
|
44
|
+
* ruby. */
|
45
|
+
typedef struct grpc_rb_byte_buffer {
|
46
|
+
/* Holder of ruby objects involved in constructing the status */
|
47
|
+
VALUE mark;
|
48
|
+
/* The actual status */
|
49
|
+
grpc_byte_buffer *wrapped;
|
50
|
+
} grpc_rb_byte_buffer;
|
51
|
+
|
52
|
+
/* Destroys ByteBuffer instances. */
|
53
|
+
static void grpc_rb_byte_buffer_free(void *p) {
|
54
|
+
grpc_rb_byte_buffer *bb = NULL;
|
55
|
+
if (p == NULL) {
|
56
|
+
return;
|
57
|
+
};
|
58
|
+
bb = (grpc_rb_byte_buffer *)p;
|
59
|
+
|
60
|
+
/* Deletes the wrapped object if the mark object is Qnil, which indicates
|
61
|
+
* that no other object is the actual owner. */
|
62
|
+
if (bb->wrapped != NULL && bb->mark == Qnil) {
|
63
|
+
grpc_byte_buffer_destroy(bb->wrapped);
|
64
|
+
}
|
65
|
+
|
66
|
+
xfree(p);
|
67
|
+
}
|
68
|
+
|
69
|
+
/* Protects the mark object from GC */
|
70
|
+
static void grpc_rb_byte_buffer_mark(void *p) {
|
71
|
+
grpc_rb_byte_buffer *bb = NULL;
|
72
|
+
if (p == NULL) {
|
73
|
+
return;
|
74
|
+
}
|
75
|
+
bb = (grpc_rb_byte_buffer *)p;
|
76
|
+
|
77
|
+
/* If it's not already cleaned up, mark the mark object */
|
78
|
+
if (bb->mark != Qnil && BUILTIN_TYPE(bb->mark) != T_NONE) {
|
79
|
+
rb_gc_mark(bb->mark);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
/* id_source is the name of the hidden ivar the preserves the original
|
84
|
+
* byte_buffer source string */
|
85
|
+
static ID id_source;
|
86
|
+
|
87
|
+
/* Allocates ByteBuffer instances.
|
88
|
+
|
89
|
+
Provides safe default values for the byte_buffer fields. */
|
90
|
+
static VALUE grpc_rb_byte_buffer_alloc(VALUE cls) {
|
91
|
+
grpc_rb_byte_buffer *wrapper = ALLOC(grpc_rb_byte_buffer);
|
92
|
+
wrapper->wrapped = NULL;
|
93
|
+
wrapper->mark = Qnil;
|
94
|
+
return Data_Wrap_Struct(cls, grpc_rb_byte_buffer_mark,
|
95
|
+
grpc_rb_byte_buffer_free, wrapper);
|
96
|
+
}
|
97
|
+
|
98
|
+
/* Clones ByteBuffer instances.
|
99
|
+
|
100
|
+
Gives ByteBuffer a consistent implementation of Ruby's object copy/dup
|
101
|
+
protocol. */
|
102
|
+
static VALUE grpc_rb_byte_buffer_init_copy(VALUE copy, VALUE orig) {
|
103
|
+
grpc_rb_byte_buffer *orig_bb = NULL;
|
104
|
+
grpc_rb_byte_buffer *copy_bb = NULL;
|
105
|
+
|
106
|
+
if (copy == orig) {
|
107
|
+
return copy;
|
108
|
+
}
|
109
|
+
|
110
|
+
/* Raise an error if orig is not a metadata object or a subclass. */
|
111
|
+
if (TYPE(orig) != T_DATA ||
|
112
|
+
RDATA(orig)->dfree != (RUBY_DATA_FUNC)grpc_rb_byte_buffer_free) {
|
113
|
+
rb_raise(rb_eTypeError, "not a %s", rb_obj_classname(rb_cByteBuffer));
|
114
|
+
}
|
115
|
+
|
116
|
+
Data_Get_Struct(orig, grpc_rb_byte_buffer, orig_bb);
|
117
|
+
Data_Get_Struct(copy, grpc_rb_byte_buffer, copy_bb);
|
118
|
+
|
119
|
+
/* use ruby's MEMCPY to make a byte-for-byte copy of the metadata wrapper
|
120
|
+
* object. */
|
121
|
+
MEMCPY(copy_bb, orig_bb, grpc_rb_byte_buffer, 1);
|
122
|
+
return copy;
|
123
|
+
}
|
124
|
+
|
125
|
+
/* id_empty is used to return the empty string from to_s when necessary. */
|
126
|
+
static ID id_empty;
|
127
|
+
|
128
|
+
static VALUE grpc_rb_byte_buffer_to_s(VALUE self) {
|
129
|
+
grpc_rb_byte_buffer *wrapper = NULL;
|
130
|
+
grpc_byte_buffer *bb = NULL;
|
131
|
+
grpc_byte_buffer_reader *reader = NULL;
|
132
|
+
char *output = NULL;
|
133
|
+
size_t length = 0;
|
134
|
+
size_t offset = 0;
|
135
|
+
VALUE output_obj = Qnil;
|
136
|
+
gpr_slice next;
|
137
|
+
|
138
|
+
Data_Get_Struct(self, grpc_rb_byte_buffer, wrapper);
|
139
|
+
output_obj = rb_ivar_get(wrapper->mark, id_source);
|
140
|
+
if (output_obj != Qnil) {
|
141
|
+
/* From ruby, ByteBuffers are immutable so if a source is set, return that
|
142
|
+
* as the to_s value */
|
143
|
+
return output_obj;
|
144
|
+
}
|
145
|
+
|
146
|
+
/* Read the bytes. */
|
147
|
+
bb = wrapper->wrapped;
|
148
|
+
if (bb == NULL) {
|
149
|
+
return rb_id2str(id_empty);
|
150
|
+
}
|
151
|
+
length = grpc_byte_buffer_length(bb);
|
152
|
+
if (length == 0) {
|
153
|
+
return rb_id2str(id_empty);
|
154
|
+
}
|
155
|
+
reader = grpc_byte_buffer_reader_create(bb);
|
156
|
+
output = xmalloc(length);
|
157
|
+
while (grpc_byte_buffer_reader_next(reader, &next) != 0) {
|
158
|
+
memcpy(output + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next));
|
159
|
+
offset += GPR_SLICE_LENGTH(next);
|
160
|
+
}
|
161
|
+
output_obj = rb_str_new(output, length);
|
162
|
+
|
163
|
+
/* Save a references to the computed string in the mark object so that the
|
164
|
+
* calling to_s does not do any allocations. */
|
165
|
+
wrapper->mark = rb_class_new_instance(0, NULL, rb_cObject);
|
166
|
+
rb_ivar_set(wrapper->mark, id_source, output_obj);
|
167
|
+
|
168
|
+
return output_obj;
|
169
|
+
}
|
170
|
+
|
171
|
+
/* Initializes ByteBuffer instances. */
|
172
|
+
static VALUE grpc_rb_byte_buffer_init(VALUE self, VALUE src) {
|
173
|
+
gpr_slice a_slice;
|
174
|
+
grpc_rb_byte_buffer *wrapper = NULL;
|
175
|
+
grpc_byte_buffer *byte_buffer = NULL;
|
176
|
+
|
177
|
+
if (TYPE(src) != T_STRING) {
|
178
|
+
rb_raise(rb_eTypeError, "bad byte_buffer arg: got <%s>, want <String>",
|
179
|
+
rb_obj_classname(src));
|
180
|
+
return Qnil;
|
181
|
+
}
|
182
|
+
Data_Get_Struct(self, grpc_rb_byte_buffer, wrapper);
|
183
|
+
a_slice = gpr_slice_malloc(RSTRING_LEN(src));
|
184
|
+
memcpy(GPR_SLICE_START_PTR(a_slice), RSTRING_PTR(src), RSTRING_LEN(src));
|
185
|
+
byte_buffer = grpc_byte_buffer_create(&a_slice, 1);
|
186
|
+
gpr_slice_unref(a_slice);
|
187
|
+
|
188
|
+
if (byte_buffer == NULL) {
|
189
|
+
rb_raise(rb_eArgError, "could not create a byte_buffer, not sure why");
|
190
|
+
return Qnil;
|
191
|
+
}
|
192
|
+
wrapper->wrapped = byte_buffer;
|
193
|
+
|
194
|
+
/* Save a references to the original string in the mark object so that the
|
195
|
+
* pointers used there is valid for the lifetime of the object. */
|
196
|
+
wrapper->mark = rb_class_new_instance(0, NULL, rb_cObject);
|
197
|
+
rb_ivar_set(wrapper->mark, id_source, src);
|
198
|
+
|
199
|
+
return self;
|
200
|
+
}
|
201
|
+
|
202
|
+
/* rb_cByteBuffer is the ruby class that proxies grpc_byte_buffer. */
|
203
|
+
VALUE rb_cByteBuffer = Qnil;
|
204
|
+
|
205
|
+
void Init_grpc_byte_buffer() {
|
206
|
+
rb_cByteBuffer =
|
207
|
+
rb_define_class_under(rb_mGrpcCore, "ByteBuffer", rb_cObject);
|
208
|
+
|
209
|
+
/* Allocates an object managed by the ruby runtime */
|
210
|
+
rb_define_alloc_func(rb_cByteBuffer, grpc_rb_byte_buffer_alloc);
|
211
|
+
|
212
|
+
/* Provides a ruby constructor and support for dup/clone. */
|
213
|
+
rb_define_method(rb_cByteBuffer, "initialize", grpc_rb_byte_buffer_init, 1);
|
214
|
+
rb_define_method(rb_cByteBuffer, "initialize_copy",
|
215
|
+
grpc_rb_byte_buffer_init_copy, 1);
|
216
|
+
|
217
|
+
/* Provides a to_s method that returns the buffer value */
|
218
|
+
rb_define_method(rb_cByteBuffer, "to_s", grpc_rb_byte_buffer_to_s, 0);
|
219
|
+
|
220
|
+
id_source = rb_intern("__source");
|
221
|
+
id_empty = rb_intern("");
|
222
|
+
}
|
223
|
+
|
224
|
+
VALUE grpc_rb_byte_buffer_create_with_mark(VALUE mark, grpc_byte_buffer *bb) {
|
225
|
+
grpc_rb_byte_buffer *byte_buffer = NULL;
|
226
|
+
if (bb == NULL) {
|
227
|
+
return Qnil;
|
228
|
+
}
|
229
|
+
byte_buffer = ALLOC(grpc_rb_byte_buffer);
|
230
|
+
byte_buffer->wrapped = bb;
|
231
|
+
byte_buffer->mark = mark;
|
232
|
+
return Data_Wrap_Struct(rb_cByteBuffer, grpc_rb_byte_buffer_mark,
|
233
|
+
grpc_rb_byte_buffer_free, byte_buffer);
|
234
|
+
}
|
235
|
+
|
236
|
+
/* Gets the wrapped byte_buffer from the ruby wrapper */
|
237
|
+
grpc_byte_buffer *grpc_rb_get_wrapped_byte_buffer(VALUE v) {
|
238
|
+
grpc_rb_byte_buffer *wrapper = NULL;
|
239
|
+
Data_Get_Struct(v, grpc_rb_byte_buffer, wrapper);
|
240
|
+
return wrapper->wrapped;
|
241
|
+
}
|