grpc 0.13.0.pre1.1-x86-mingw32

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.

Files changed (96) hide show
  1. checksums.yaml +7 -0
  2. data/etc/roots.pem +5114 -0
  3. data/grpc_c.32.ruby +0 -0
  4. data/grpc_c.64.ruby +0 -0
  5. data/src/ruby/bin/apis/google/protobuf/empty.rb +44 -0
  6. data/src/ruby/bin/apis/pubsub_demo.rb +256 -0
  7. data/src/ruby/bin/apis/tech/pubsub/proto/pubsub.rb +174 -0
  8. data/src/ruby/bin/apis/tech/pubsub/proto/pubsub_services.rb +103 -0
  9. data/src/ruby/bin/grpc_ruby_interop_client +33 -0
  10. data/src/ruby/bin/grpc_ruby_interop_server +33 -0
  11. data/src/ruby/bin/interop/interop_client.rb +51 -0
  12. data/src/ruby/bin/interop/interop_server.rb +50 -0
  13. data/src/ruby/bin/math.rb +32 -0
  14. data/src/ruby/bin/math_client.rb +147 -0
  15. data/src/ruby/bin/math_server.rb +206 -0
  16. data/src/ruby/bin/math_services.rb +27 -0
  17. data/src/ruby/bin/noproto_client.rb +108 -0
  18. data/src/ruby/bin/noproto_server.rb +112 -0
  19. data/src/ruby/ext/grpc/extconf.rb +129 -0
  20. data/src/ruby/ext/grpc/rb_byte_buffer.c +70 -0
  21. data/src/ruby/ext/grpc/rb_byte_buffer.h +47 -0
  22. data/src/ruby/ext/grpc/rb_call.c +908 -0
  23. data/src/ruby/ext/grpc/rb_call.h +66 -0
  24. data/src/ruby/ext/grpc/rb_call_credentials.c +319 -0
  25. data/src/ruby/ext/grpc/rb_call_credentials.h +46 -0
  26. data/src/ruby/ext/grpc/rb_channel.c +432 -0
  27. data/src/ruby/ext/grpc/rb_channel.h +47 -0
  28. data/src/ruby/ext/grpc/rb_channel_args.c +169 -0
  29. data/src/ruby/ext/grpc/rb_channel_args.h +53 -0
  30. data/src/ruby/ext/grpc/rb_channel_credentials.c +268 -0
  31. data/src/ruby/ext/grpc/rb_channel_credentials.h +47 -0
  32. data/src/ruby/ext/grpc/rb_completion_queue.c +183 -0
  33. data/src/ruby/ext/grpc/rb_completion_queue.h +55 -0
  34. data/src/ruby/ext/grpc/rb_event_thread.c +158 -0
  35. data/src/ruby/ext/grpc/rb_event_thread.h +37 -0
  36. data/src/ruby/ext/grpc/rb_grpc.c +336 -0
  37. data/src/ruby/ext/grpc/rb_grpc.h +85 -0
  38. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +560 -0
  39. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +843 -0
  40. data/src/ruby/ext/grpc/rb_loader.c +72 -0
  41. data/src/ruby/ext/grpc/rb_loader.h +40 -0
  42. data/src/ruby/ext/grpc/rb_server.c +400 -0
  43. data/src/ruby/ext/grpc/rb_server.h +47 -0
  44. data/src/ruby/ext/grpc/rb_server_credentials.c +284 -0
  45. data/src/ruby/ext/grpc/rb_server_credentials.h +47 -0
  46. data/src/ruby/lib/grpc.rb +44 -0
  47. data/src/ruby/lib/grpc/2.0/grpc_c.so +0 -0
  48. data/src/ruby/lib/grpc/2.1/grpc_c.so +0 -0
  49. data/src/ruby/lib/grpc/2.2/grpc_c.so +0 -0
  50. data/src/ruby/lib/grpc/2.3/grpc_c.so +0 -0
  51. data/src/ruby/lib/grpc/core/time_consts.rb +71 -0
  52. data/src/ruby/lib/grpc/errors.rb +62 -0
  53. data/src/ruby/lib/grpc/generic/active_call.rb +488 -0
  54. data/src/ruby/lib/grpc/generic/bidi_call.rb +218 -0
  55. data/src/ruby/lib/grpc/generic/client_stub.rb +471 -0
  56. data/src/ruby/lib/grpc/generic/rpc_desc.rb +147 -0
  57. data/src/ruby/lib/grpc/generic/rpc_server.rb +504 -0
  58. data/src/ruby/lib/grpc/generic/service.rb +234 -0
  59. data/src/ruby/lib/grpc/grpc.rb +34 -0
  60. data/src/ruby/lib/grpc/grpc_c.so +0 -0
  61. data/src/ruby/lib/grpc/logconfig.rb +59 -0
  62. data/src/ruby/lib/grpc/notifier.rb +60 -0
  63. data/src/ruby/lib/grpc/version.rb +33 -0
  64. data/src/ruby/pb/README.md +42 -0
  65. data/src/ruby/pb/generate_proto_ruby.sh +51 -0
  66. data/src/ruby/pb/grpc/health/checker.rb +75 -0
  67. data/src/ruby/pb/grpc/health/v1alpha/health.rb +29 -0
  68. data/src/ruby/pb/grpc/health/v1alpha/health_services.rb +28 -0
  69. data/src/ruby/pb/test/client.rb +469 -0
  70. data/src/ruby/pb/test/proto/empty.rb +15 -0
  71. data/src/ruby/pb/test/proto/messages.rb +80 -0
  72. data/src/ruby/pb/test/proto/test.rb +14 -0
  73. data/src/ruby/pb/test/proto/test_services.rb +64 -0
  74. data/src/ruby/pb/test/server.rb +253 -0
  75. data/src/ruby/spec/call_credentials_spec.rb +57 -0
  76. data/src/ruby/spec/call_spec.rb +163 -0
  77. data/src/ruby/spec/channel_credentials_spec.rb +97 -0
  78. data/src/ruby/spec/channel_spec.rb +177 -0
  79. data/src/ruby/spec/client_server_spec.rb +475 -0
  80. data/src/ruby/spec/completion_queue_spec.rb +42 -0
  81. data/src/ruby/spec/generic/active_call_spec.rb +373 -0
  82. data/src/ruby/spec/generic/client_stub_spec.rb +476 -0
  83. data/src/ruby/spec/generic/rpc_desc_spec.rb +331 -0
  84. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +138 -0
  85. data/src/ruby/spec/generic/rpc_server_spec.rb +576 -0
  86. data/src/ruby/spec/generic/service_spec.rb +345 -0
  87. data/src/ruby/spec/pb/health/checker_spec.rb +232 -0
  88. data/src/ruby/spec/server_credentials_spec.rb +94 -0
  89. data/src/ruby/spec/server_spec.rb +209 -0
  90. data/src/ruby/spec/spec_helper.rb +69 -0
  91. data/src/ruby/spec/testdata/README +1 -0
  92. data/src/ruby/spec/testdata/ca.pem +15 -0
  93. data/src/ruby/spec/testdata/server1.key +16 -0
  94. data/src/ruby/spec/testdata/server1.pem +16 -0
  95. data/src/ruby/spec/time_consts_spec.rb +89 -0
  96. metadata +320 -0
@@ -0,0 +1,47 @@
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
+ #ifndef GRPC_RB_CHANNEL_H_
35
+ #define GRPC_RB_CHANNEL_H_
36
+
37
+ #include <ruby/ruby.h>
38
+
39
+ #include <grpc/grpc.h>
40
+
41
+ /* Initializes the Channel class. */
42
+ void Init_grpc_channel();
43
+
44
+ /* Gets the wrapped channel from the ruby wrapper */
45
+ grpc_channel* grpc_rb_get_wrapped_channel(VALUE v);
46
+
47
+ #endif /* GRPC_RB_CHANNEL_H_ */
@@ -0,0 +1,169 @@
1
+ /*
2
+ *
3
+ * Copyright 2015-2016, 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 <ruby/ruby.h>
35
+ #include "rb_grpc_imports.generated.h"
36
+ #include "rb_channel_args.h"
37
+
38
+ #include <ruby/ruby.h>
39
+
40
+ #include <grpc/grpc.h>
41
+
42
+ #include "rb_grpc.h"
43
+
44
+ static rb_data_type_t grpc_rb_channel_args_data_type = {
45
+ "grpc_channel_args",
46
+ {GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, GRPC_RB_MEMSIZE_UNAVAILABLE,
47
+ {NULL, NULL}},
48
+ NULL, NULL,
49
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
50
+ RUBY_TYPED_FREE_IMMEDIATELY
51
+ #endif
52
+ };
53
+
54
+ /* A callback the processes the hash key values in channel_args hash */
55
+ static int grpc_rb_channel_create_in_process_add_args_hash_cb(VALUE key,
56
+ VALUE val,
57
+ VALUE args_obj) {
58
+ const char* the_key;
59
+ grpc_channel_args* args;
60
+
61
+ switch (TYPE(key)) {
62
+ case T_STRING:
63
+ the_key = StringValuePtr(key);
64
+ break;
65
+
66
+ case T_SYMBOL:
67
+ the_key = rb_id2name(SYM2ID(key));
68
+ break;
69
+
70
+ default:
71
+ rb_raise(rb_eTypeError, "bad chan arg: got <%s>, want <String|Symbol>",
72
+ rb_obj_classname(key));
73
+ return ST_STOP;
74
+ }
75
+
76
+ TypedData_Get_Struct(args_obj, grpc_channel_args,
77
+ &grpc_rb_channel_args_data_type, args);
78
+ if (args->num_args <= 0) {
79
+ rb_raise(rb_eRuntimeError, "hash_cb bug: num_args is %lu for key:%s",
80
+ args->num_args, StringValueCStr(key));
81
+ return ST_STOP;
82
+ }
83
+
84
+ args->args[args->num_args - 1].key = (char*)the_key;
85
+ switch (TYPE(val)) {
86
+ case T_SYMBOL:
87
+ args->args[args->num_args - 1].type = GRPC_ARG_STRING;
88
+ args->args[args->num_args - 1].value.string =
89
+ (char*)rb_id2name(SYM2ID(val));
90
+ --args->num_args;
91
+ return ST_CONTINUE;
92
+
93
+ case T_STRING:
94
+ args->args[args->num_args - 1].type = GRPC_ARG_STRING;
95
+ args->args[args->num_args - 1].value.string = StringValueCStr(val);
96
+ --args->num_args;
97
+ return ST_CONTINUE;
98
+
99
+ case T_FIXNUM:
100
+ args->args[args->num_args - 1].type = GRPC_ARG_INTEGER;
101
+ args->args[args->num_args - 1].value.integer = NUM2INT(val);
102
+ --args->num_args;
103
+ return ST_CONTINUE;
104
+
105
+ default:
106
+ rb_raise(rb_eTypeError, "%s: bad value: got <%s>, want <String|Fixnum>",
107
+ StringValueCStr(key), rb_obj_classname(val));
108
+ return ST_STOP;
109
+ }
110
+ rb_raise(rb_eRuntimeError, "impl bug: hash_cb reached to far while on key:%s",
111
+ StringValueCStr(key));
112
+ return ST_STOP;
113
+ }
114
+
115
+ /* channel_convert_params allows the call to
116
+ grpc_rb_hash_convert_to_channel_args to be made within an rb_protect
117
+ exception-handler. This allows any allocated memory to be freed before
118
+ propagating any exception that occurs */
119
+ typedef struct channel_convert_params {
120
+ VALUE src_hash;
121
+ grpc_channel_args* dst;
122
+ } channel_convert_params;
123
+
124
+ static VALUE grpc_rb_hash_convert_to_channel_args0(VALUE as_value) {
125
+ ID id_size = rb_intern("size");
126
+ VALUE grpc_rb_cChannelArgs = rb_define_class("TmpChannelArgs", rb_cObject);
127
+ channel_convert_params* params = (channel_convert_params*)as_value;
128
+ size_t num_args = 0;
129
+
130
+ if (!NIL_P(params->src_hash) && TYPE(params->src_hash) != T_HASH) {
131
+ rb_raise(rb_eTypeError, "bad channel args: got:<%s> want: a hash or nil",
132
+ rb_obj_classname(params->src_hash));
133
+ return Qnil;
134
+ }
135
+
136
+ if (TYPE(params->src_hash) == T_HASH) {
137
+ num_args = NUM2INT(rb_funcall(params->src_hash, id_size, 0));
138
+ params->dst->num_args = num_args;
139
+ params->dst->args = ALLOC_N(grpc_arg, num_args);
140
+ MEMZERO(params->dst->args, grpc_arg, num_args);
141
+ rb_hash_foreach(params->src_hash,
142
+ grpc_rb_channel_create_in_process_add_args_hash_cb,
143
+ TypedData_Wrap_Struct(grpc_rb_cChannelArgs,
144
+ &grpc_rb_channel_args_data_type,
145
+ params->dst));
146
+ /* reset num_args as grpc_rb_channel_create_in_process_add_args_hash_cb
147
+ * decrements it during has processing */
148
+ params->dst->num_args = num_args;
149
+ }
150
+ return Qnil;
151
+ }
152
+
153
+ void grpc_rb_hash_convert_to_channel_args(VALUE src_hash,
154
+ grpc_channel_args* dst) {
155
+ channel_convert_params params;
156
+ int status = 0;
157
+
158
+ /* Make a protected call to grpc_rb_hash_convert_channel_args */
159
+ params.src_hash = src_hash;
160
+ params.dst = dst;
161
+ rb_protect(grpc_rb_hash_convert_to_channel_args0, (VALUE) & params, &status);
162
+ if (status != 0) {
163
+ if (dst->args != NULL) {
164
+ /* Free any allocated memory before propagating the error */
165
+ xfree(dst->args);
166
+ }
167
+ rb_jump_tag(status);
168
+ }
169
+ }
@@ -0,0 +1,53 @@
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
+ #ifndef GRPC_RB_CHANNEL_ARGS_H_
35
+ #define GRPC_RB_CHANNEL_ARGS_H_
36
+
37
+ #include <ruby/ruby.h>
38
+
39
+ #include <grpc/grpc.h>
40
+
41
+ /* Converts a hash object containing channel args to a channel args instance.
42
+ *
43
+ * This func ALLOCs args->args. The caller is responsible for freeing it. If
44
+ * a ruby error is raised during processing of the hash values, the func takes
45
+ * care to deallocate any memory allocated so far, and propagate the error.
46
+ *
47
+ * @param src_hash A ruby hash
48
+ * @param dst the grpc_channel_args that the hash entries will be added to.
49
+ */
50
+ void grpc_rb_hash_convert_to_channel_args(VALUE src_hash,
51
+ grpc_channel_args* dst);
52
+
53
+ #endif /* GRPC_RB_CHANNEL_ARGS_H_ */
@@ -0,0 +1,268 @@
1
+ /*
2
+ *
3
+ * Copyright 2015-2016, 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 <ruby/ruby.h>
35
+ #include "rb_grpc_imports.generated.h"
36
+ #include "rb_channel_credentials.h"
37
+
38
+ #include <ruby/ruby.h>
39
+
40
+ #include <grpc/grpc.h>
41
+ #include <grpc/grpc_security.h>
42
+ #include <grpc/support/log.h>
43
+
44
+ #include "rb_call_credentials.h"
45
+ #include "rb_grpc.h"
46
+
47
+ /* grpc_rb_cChannelCredentials is the ruby class that proxies
48
+ grpc_channel_credentials. */
49
+ static VALUE grpc_rb_cChannelCredentials = Qnil;
50
+
51
+ /* grpc_rb_channel_credentials wraps a grpc_channel_credentials. It provides a
52
+ * peer ruby object, 'mark' to minimize copying when a credential is
53
+ * created from ruby. */
54
+ typedef struct grpc_rb_channel_credentials {
55
+ /* Holder of ruby objects involved in constructing the credentials */
56
+ VALUE mark;
57
+
58
+ /* The actual credentials */
59
+ grpc_channel_credentials *wrapped;
60
+ } grpc_rb_channel_credentials;
61
+
62
+ /* Destroys the credentials instances. */
63
+ static void grpc_rb_channel_credentials_free(void *p) {
64
+ grpc_rb_channel_credentials *wrapper = NULL;
65
+ if (p == NULL) {
66
+ return;
67
+ };
68
+ wrapper = (grpc_rb_channel_credentials *)p;
69
+
70
+ /* Delete the wrapped object if the mark object is Qnil, which indicates that
71
+ * no other object is the actual owner. */
72
+ if (wrapper->wrapped != NULL && wrapper->mark == Qnil) {
73
+ grpc_channel_credentials_release(wrapper->wrapped);
74
+ wrapper->wrapped = NULL;
75
+ }
76
+
77
+ xfree(p);
78
+ }
79
+
80
+ /* Protects the mark object from GC */
81
+ static void grpc_rb_channel_credentials_mark(void *p) {
82
+ grpc_rb_channel_credentials *wrapper = NULL;
83
+ if (p == NULL) {
84
+ return;
85
+ }
86
+ wrapper = (grpc_rb_channel_credentials *)p;
87
+
88
+ /* If it's not already cleaned up, mark the mark object */
89
+ if (wrapper->mark != Qnil) {
90
+ rb_gc_mark(wrapper->mark);
91
+ }
92
+ }
93
+
94
+ static rb_data_type_t grpc_rb_channel_credentials_data_type = {
95
+ "grpc_channel_credentials",
96
+ {grpc_rb_channel_credentials_mark, grpc_rb_channel_credentials_free,
97
+ GRPC_RB_MEMSIZE_UNAVAILABLE, {NULL, NULL}},
98
+ NULL,
99
+ NULL,
100
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
101
+ RUBY_TYPED_FREE_IMMEDIATELY
102
+ #endif
103
+ };
104
+
105
+ /* Allocates ChannelCredential instances.
106
+ Provides safe initial defaults for the instance fields. */
107
+ static VALUE grpc_rb_channel_credentials_alloc(VALUE cls) {
108
+ grpc_rb_channel_credentials *wrapper = ALLOC(grpc_rb_channel_credentials);
109
+ wrapper->wrapped = NULL;
110
+ wrapper->mark = Qnil;
111
+ return TypedData_Wrap_Struct(cls, &grpc_rb_channel_credentials_data_type, wrapper);
112
+ }
113
+
114
+ /* Creates a wrapping object for a given channel credentials. This should only
115
+ * be called with grpc_channel_credentials objects that are not already
116
+ * associated with any Ruby object. */
117
+ VALUE grpc_rb_wrap_channel_credentials(grpc_channel_credentials *c) {
118
+ VALUE rb_wrapper;
119
+ grpc_rb_channel_credentials *wrapper;
120
+ if (c == NULL) {
121
+ return Qnil;
122
+ }
123
+ rb_wrapper = grpc_rb_channel_credentials_alloc(grpc_rb_cChannelCredentials);
124
+ TypedData_Get_Struct(rb_wrapper, grpc_rb_channel_credentials,
125
+ &grpc_rb_channel_credentials_data_type, wrapper);
126
+ wrapper->wrapped = c;
127
+ return rb_wrapper;
128
+ }
129
+
130
+ /* Clones ChannelCredentials instances.
131
+ Gives ChannelCredentials a consistent implementation of Ruby's object copy/dup
132
+ protocol. */
133
+ static VALUE grpc_rb_channel_credentials_init_copy(VALUE copy, VALUE orig) {
134
+ grpc_rb_channel_credentials *orig_cred = NULL;
135
+ grpc_rb_channel_credentials *copy_cred = NULL;
136
+
137
+ if (copy == orig) {
138
+ return copy;
139
+ }
140
+
141
+ /* Raise an error if orig is not a credentials object or a subclass. */
142
+ if (TYPE(orig) != T_DATA ||
143
+ RDATA(orig)->dfree != (RUBY_DATA_FUNC)grpc_rb_channel_credentials_free) {
144
+ rb_raise(rb_eTypeError, "not a %s",
145
+ rb_obj_classname(grpc_rb_cChannelCredentials));
146
+ }
147
+
148
+ TypedData_Get_Struct(orig, grpc_rb_channel_credentials,
149
+ &grpc_rb_channel_credentials_data_type, orig_cred);
150
+ TypedData_Get_Struct(copy, grpc_rb_channel_credentials,
151
+ &grpc_rb_channel_credentials_data_type, copy_cred);
152
+
153
+ /* use ruby's MEMCPY to make a byte-for-byte copy of the credentials
154
+ * wrapper object. */
155
+ MEMCPY(copy_cred, orig_cred, grpc_rb_channel_credentials, 1);
156
+ return copy;
157
+ }
158
+
159
+
160
+ /* The attribute used on the mark object to hold the pem_root_certs. */
161
+ static ID id_pem_root_certs;
162
+
163
+ /* The attribute used on the mark object to hold the pem_private_key. */
164
+ static ID id_pem_private_key;
165
+
166
+ /* The attribute used on the mark object to hold the pem_private_key. */
167
+ static ID id_pem_cert_chain;
168
+
169
+ /*
170
+ call-seq:
171
+ creds1 = Credentials.new()
172
+ ...
173
+ creds2 = Credentials.new(pem_root_certs)
174
+ ...
175
+ creds3 = Credentials.new(pem_root_certs, pem_private_key,
176
+ pem_cert_chain)
177
+ pem_root_certs: (optional) PEM encoding of the server root certificate
178
+ pem_private_key: (optional) PEM encoding of the client's private key
179
+ pem_cert_chain: (optional) PEM encoding of the client's cert chain
180
+ Initializes Credential instances. */
181
+ static VALUE grpc_rb_channel_credentials_init(int argc, VALUE *argv, VALUE self) {
182
+ VALUE pem_root_certs = Qnil;
183
+ VALUE pem_private_key = Qnil;
184
+ VALUE pem_cert_chain = Qnil;
185
+ grpc_rb_channel_credentials *wrapper = NULL;
186
+ grpc_channel_credentials *creds = NULL;
187
+ grpc_ssl_pem_key_cert_pair key_cert_pair;
188
+ const char *pem_root_certs_cstr = NULL;
189
+ MEMZERO(&key_cert_pair, grpc_ssl_pem_key_cert_pair, 1);
190
+ /* "03" == no mandatory arg, 3 optional */
191
+ rb_scan_args(argc, argv, "03", &pem_root_certs, &pem_private_key,
192
+ &pem_cert_chain);
193
+
194
+ TypedData_Get_Struct(self, grpc_rb_channel_credentials,
195
+ &grpc_rb_channel_credentials_data_type, wrapper);
196
+ if (pem_root_certs != Qnil) {
197
+ pem_root_certs_cstr = RSTRING_PTR(pem_root_certs);
198
+ }
199
+ if (pem_private_key == Qnil && pem_cert_chain == Qnil) {
200
+ creds = grpc_ssl_credentials_create(pem_root_certs_cstr, NULL, NULL);
201
+ } else {
202
+ key_cert_pair.private_key = RSTRING_PTR(pem_private_key);
203
+ key_cert_pair.cert_chain = RSTRING_PTR(pem_cert_chain);
204
+ creds = grpc_ssl_credentials_create(pem_root_certs_cstr,
205
+ &key_cert_pair, NULL);
206
+ }
207
+ if (creds == NULL) {
208
+ rb_raise(rb_eRuntimeError, "could not create a credentials, not sure why");
209
+ return Qnil;
210
+ }
211
+ wrapper->wrapped = creds;
212
+
213
+ /* Add the input objects as hidden fields to preserve them. */
214
+ rb_ivar_set(self, id_pem_cert_chain, pem_cert_chain);
215
+ rb_ivar_set(self, id_pem_private_key, pem_private_key);
216
+ rb_ivar_set(self, id_pem_root_certs, pem_root_certs);
217
+
218
+ return self;
219
+ }
220
+
221
+ static VALUE grpc_rb_channel_credentials_compose(int argc, VALUE *argv,
222
+ VALUE self) {
223
+ grpc_channel_credentials *creds;
224
+ grpc_call_credentials *other;
225
+ if (argc == 0) {
226
+ return self;
227
+ }
228
+ creds = grpc_rb_get_wrapped_channel_credentials(self);
229
+ for (int i = 0; i < argc; i++) {
230
+ other = grpc_rb_get_wrapped_call_credentials(argv[i]);
231
+ creds = grpc_composite_channel_credentials_create(creds, other, NULL);
232
+ if (creds == NULL) {
233
+ rb_raise(rb_eRuntimeError,
234
+ "Failed to compose channel and call credentials");
235
+ }
236
+ }
237
+ return grpc_rb_wrap_channel_credentials(creds);
238
+ }
239
+
240
+ void Init_grpc_channel_credentials() {
241
+ grpc_rb_cChannelCredentials =
242
+ rb_define_class_under(grpc_rb_mGrpcCore, "ChannelCredentials", rb_cObject);
243
+
244
+ /* Allocates an object managed by the ruby runtime */
245
+ rb_define_alloc_func(grpc_rb_cChannelCredentials,
246
+ grpc_rb_channel_credentials_alloc);
247
+
248
+ /* Provides a ruby constructor and support for dup/clone. */
249
+ rb_define_method(grpc_rb_cChannelCredentials, "initialize",
250
+ grpc_rb_channel_credentials_init, -1);
251
+ rb_define_method(grpc_rb_cChannelCredentials, "initialize_copy",
252
+ grpc_rb_channel_credentials_init_copy, 1);
253
+ rb_define_method(grpc_rb_cChannelCredentials, "compose",
254
+ grpc_rb_channel_credentials_compose, -1);
255
+
256
+ id_pem_cert_chain = rb_intern("__pem_cert_chain");
257
+ id_pem_private_key = rb_intern("__pem_private_key");
258
+ id_pem_root_certs = rb_intern("__pem_root_certs");
259
+ }
260
+
261
+ /* Gets the wrapped grpc_channel_credentials from the ruby wrapper */
262
+ grpc_channel_credentials *grpc_rb_get_wrapped_channel_credentials(VALUE v) {
263
+ grpc_rb_channel_credentials *wrapper = NULL;
264
+ TypedData_Get_Struct(v, grpc_rb_channel_credentials,
265
+ &grpc_rb_channel_credentials_data_type,
266
+ wrapper);
267
+ return wrapper->wrapped;
268
+ }