grpc 0.13.0.pre1.1-x64-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,72 @@
1
+ /*
2
+ *
3
+ * Copyright 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 "rb_grpc_imports.generated.h"
35
+
36
+ #if GPR_WIN32
37
+ #include <tchar.h>
38
+
39
+ int grpc_rb_load_core() {
40
+ #if GPR_ARCH_64
41
+ TCHAR fname[] = _T("grpc_c.64.ruby");
42
+ #else
43
+ TCHAR fname[] = _T("grpc_c.32.ruby");
44
+ #endif
45
+ HMODULE module = GetModuleHandle(_T("grpc_c.so"));
46
+ TCHAR path[2048 + 32] = _T("");
47
+ LPTSTR seek_back = NULL;
48
+ GetModuleFileName(module, path, 2048);
49
+
50
+ seek_back = _tcsrchr(path, _T('\\'));
51
+
52
+ while (seek_back) {
53
+ HMODULE grpc_c;
54
+ _tcscpy(seek_back + 1, fname);
55
+ grpc_c = LoadLibrary(path);
56
+ if (grpc_c) {
57
+ grpc_rb_load_imports(grpc_c);
58
+ return 1;
59
+ } else {
60
+ *seek_back = _T('\0');
61
+ seek_back = _tcsrchr(path, _T('\\'));
62
+ }
63
+ }
64
+
65
+ return 0;
66
+ }
67
+
68
+ #else
69
+
70
+ int grpc_rb_load_core() { return 1; }
71
+
72
+ #endif
@@ -0,0 +1,40 @@
1
+ /*
2
+ *
3
+ * Copyright 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
+ #ifndef GRPC_RB_LOADER_H_
35
+ #define GRPC_RB_LOADER_H_
36
+
37
+ /* Attempts to load the core if necessary, and return non-zero upon succes. */
38
+ int grpc_rb_load_core();
39
+
40
+ #endif /* GRPC_RB_BYTE_BUFFER_H_ */
@@ -0,0 +1,400 @@
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_server.h"
37
+
38
+ #include <ruby/ruby.h>
39
+
40
+ #include <grpc/grpc.h>
41
+ #include <grpc/grpc_security.h>
42
+ #include "rb_call.h"
43
+ #include "rb_channel_args.h"
44
+ #include "rb_completion_queue.h"
45
+ #include "rb_server_credentials.h"
46
+ #include "rb_grpc.h"
47
+
48
+ /* grpc_rb_cServer is the ruby class that proxies grpc_server. */
49
+ static VALUE grpc_rb_cServer = Qnil;
50
+
51
+ /* id_at is the constructor method of the ruby standard Time class. */
52
+ static ID id_at;
53
+
54
+ /* id_insecure_server is used to indicate that a server is insecure */
55
+ static VALUE id_insecure_server;
56
+
57
+ /* grpc_rb_server wraps a grpc_server. It provides a peer ruby object,
58
+ 'mark' to minimize copying when a server is created from ruby. */
59
+ typedef struct grpc_rb_server {
60
+ /* Holder of ruby objects involved in constructing the server */
61
+ VALUE mark;
62
+ /* The actual server */
63
+ grpc_server *wrapped;
64
+ } grpc_rb_server;
65
+
66
+ /* Destroys server instances. */
67
+ static void grpc_rb_server_free(void *p) {
68
+ grpc_rb_server *svr = NULL;
69
+ if (p == NULL) {
70
+ return;
71
+ };
72
+ svr = (grpc_rb_server *)p;
73
+
74
+ /* Deletes the wrapped object if the mark object is Qnil, which indicates
75
+ that no other object is the actual owner. */
76
+ /* grpc_server_shutdown does not exist. Change this to something that does
77
+ or delete it */
78
+ if (svr->wrapped != NULL && svr->mark == Qnil) {
79
+ // grpc_server_shutdown(svr->wrapped);
80
+ // Aborting to indicate a bug
81
+ abort();
82
+ grpc_server_destroy(svr->wrapped);
83
+ }
84
+
85
+ xfree(p);
86
+ }
87
+
88
+ /* Protects the mark object from GC */
89
+ static void grpc_rb_server_mark(void *p) {
90
+ grpc_rb_server *server = NULL;
91
+ if (p == NULL) {
92
+ return;
93
+ }
94
+ server = (grpc_rb_server *)p;
95
+ if (server->mark != Qnil) {
96
+ rb_gc_mark(server->mark);
97
+ }
98
+ }
99
+
100
+ static const rb_data_type_t grpc_rb_server_data_type = {
101
+ "grpc_server",
102
+ {grpc_rb_server_mark, grpc_rb_server_free, GRPC_RB_MEMSIZE_UNAVAILABLE,
103
+ {NULL, NULL}},
104
+ NULL,
105
+ NULL,
106
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
107
+ /* It is unsafe to specify RUBY_TYPED_FREE_IMMEDIATELY because the free function would block
108
+ * and we might want to unlock GVL
109
+ * TODO(yugui) Unlock GVL?
110
+ */
111
+ 0,
112
+ #endif
113
+ };
114
+
115
+ /* Allocates grpc_rb_server instances. */
116
+ static VALUE grpc_rb_server_alloc(VALUE cls) {
117
+ grpc_rb_server *wrapper = ALLOC(grpc_rb_server);
118
+ wrapper->wrapped = NULL;
119
+ wrapper->mark = Qnil;
120
+ return TypedData_Wrap_Struct(cls, &grpc_rb_server_data_type, wrapper);
121
+ }
122
+
123
+ /*
124
+ call-seq:
125
+ cq = CompletionQueue.new
126
+ server = Server.new(cq, {'arg1': 'value1'})
127
+
128
+ Initializes server instances. */
129
+ static VALUE grpc_rb_server_init(VALUE self, VALUE cqueue, VALUE channel_args) {
130
+ grpc_completion_queue *cq = NULL;
131
+ grpc_rb_server *wrapper = NULL;
132
+ grpc_server *srv = NULL;
133
+ grpc_channel_args args;
134
+ MEMZERO(&args, grpc_channel_args, 1);
135
+ cq = grpc_rb_get_wrapped_completion_queue(cqueue);
136
+ TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type,
137
+ wrapper);
138
+ grpc_rb_hash_convert_to_channel_args(channel_args, &args);
139
+ srv = grpc_server_create(&args, NULL);
140
+
141
+ if (args.args != NULL) {
142
+ xfree(args.args); /* Allocated by grpc_rb_hash_convert_to_channel_args */
143
+ }
144
+ if (srv == NULL) {
145
+ rb_raise(rb_eRuntimeError, "could not create a gRPC server, not sure why");
146
+ }
147
+ grpc_server_register_completion_queue(srv, cq, NULL);
148
+ wrapper->wrapped = srv;
149
+
150
+ /* Add the cq as the server's mark object. This ensures the ruby cq can't be
151
+ GCed before the server */
152
+ wrapper->mark = cqueue;
153
+ return self;
154
+ }
155
+
156
+ /* Clones Server instances.
157
+
158
+ Gives Server a consistent implementation of Ruby's object copy/dup
159
+ protocol. */
160
+ static VALUE grpc_rb_server_init_copy(VALUE copy, VALUE orig) {
161
+ grpc_rb_server *orig_srv = NULL;
162
+ grpc_rb_server *copy_srv = NULL;
163
+
164
+ if (copy == orig) {
165
+ return copy;
166
+ }
167
+
168
+ /* Raise an error if orig is not a server object or a subclass. */
169
+ if (TYPE(orig) != T_DATA ||
170
+ RDATA(orig)->dfree != (RUBY_DATA_FUNC)grpc_rb_server_free) {
171
+ rb_raise(rb_eTypeError, "not a %s", rb_obj_classname(grpc_rb_cServer));
172
+ }
173
+
174
+ TypedData_Get_Struct(orig, grpc_rb_server, &grpc_rb_server_data_type,
175
+ orig_srv);
176
+ TypedData_Get_Struct(copy, grpc_rb_server, &grpc_rb_server_data_type,
177
+ copy_srv);
178
+
179
+ /* use ruby's MEMCPY to make a byte-for-byte copy of the server wrapper
180
+ object. */
181
+ MEMCPY(copy_srv, orig_srv, grpc_rb_server, 1);
182
+ return copy;
183
+ }
184
+
185
+ /* request_call_stack holds various values used by the
186
+ * grpc_rb_server_request_call function */
187
+ typedef struct request_call_stack {
188
+ grpc_call_details details;
189
+ grpc_metadata_array md_ary;
190
+ } request_call_stack;
191
+
192
+ /* grpc_request_call_stack_init ensures the request_call_stack is properly
193
+ * initialized */
194
+ static void grpc_request_call_stack_init(request_call_stack* st) {
195
+ MEMZERO(st, request_call_stack, 1);
196
+ grpc_metadata_array_init(&st->md_ary);
197
+ grpc_call_details_init(&st->details);
198
+ st->details.method = NULL;
199
+ st->details.host = NULL;
200
+ }
201
+
202
+ /* grpc_request_call_stack_cleanup ensures the request_call_stack is properly
203
+ * cleaned up */
204
+ static void grpc_request_call_stack_cleanup(request_call_stack* st) {
205
+ grpc_metadata_array_destroy(&st->md_ary);
206
+ grpc_call_details_destroy(&st->details);
207
+ }
208
+
209
+ /* call-seq:
210
+ cq = CompletionQueue.new
211
+ tag = Object.new
212
+ timeout = 10
213
+ server.request_call(cqueue, tag, timeout)
214
+
215
+ Requests notification of a new call on a server. */
216
+ static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue,
217
+ VALUE tag_new, VALUE timeout) {
218
+ grpc_rb_server *s = NULL;
219
+ grpc_call *call = NULL;
220
+ grpc_event ev;
221
+ grpc_call_error err;
222
+ request_call_stack st;
223
+ VALUE result;
224
+ gpr_timespec deadline;
225
+ TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
226
+ if (s->wrapped == NULL) {
227
+ rb_raise(rb_eRuntimeError, "destroyed!");
228
+ return Qnil;
229
+ } else {
230
+ grpc_request_call_stack_init(&st);
231
+ /* call grpc_server_request_call, then wait for it to complete using
232
+ * pluck_event */
233
+ err = grpc_server_request_call(
234
+ s->wrapped, &call, &st.details, &st.md_ary,
235
+ grpc_rb_get_wrapped_completion_queue(cqueue),
236
+ grpc_rb_get_wrapped_completion_queue(cqueue),
237
+ ROBJECT(tag_new));
238
+ if (err != GRPC_CALL_OK) {
239
+ grpc_request_call_stack_cleanup(&st);
240
+ rb_raise(grpc_rb_eCallError,
241
+ "grpc_server_request_call failed: %s (code=%d)",
242
+ grpc_call_error_detail_of(err), err);
243
+ return Qnil;
244
+ }
245
+
246
+ ev = grpc_rb_completion_queue_pluck_event(cqueue, tag_new, timeout);
247
+ if (ev.type == GRPC_QUEUE_TIMEOUT) {
248
+ grpc_request_call_stack_cleanup(&st);
249
+ return Qnil;
250
+ }
251
+ if (!ev.success) {
252
+ grpc_request_call_stack_cleanup(&st);
253
+ rb_raise(grpc_rb_eCallError, "request_call completion failed");
254
+ return Qnil;
255
+ }
256
+
257
+ /* build the NewServerRpc struct result */
258
+ deadline = gpr_convert_clock_type(st.details.deadline, GPR_CLOCK_REALTIME);
259
+ result = rb_struct_new(
260
+ grpc_rb_sNewServerRpc, rb_str_new2(st.details.method),
261
+ rb_str_new2(st.details.host),
262
+ rb_funcall(rb_cTime, id_at, 2, INT2NUM(deadline.tv_sec),
263
+ INT2NUM(deadline.tv_nsec)),
264
+ grpc_rb_md_ary_to_h(&st.md_ary), grpc_rb_wrap_call(call), NULL);
265
+ grpc_request_call_stack_cleanup(&st);
266
+ return result;
267
+ }
268
+ return Qnil;
269
+ }
270
+
271
+ static VALUE grpc_rb_server_start(VALUE self) {
272
+ grpc_rb_server *s = NULL;
273
+ TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
274
+ if (s->wrapped == NULL) {
275
+ rb_raise(rb_eRuntimeError, "destroyed!");
276
+ } else {
277
+ grpc_server_start(s->wrapped);
278
+ }
279
+ return Qnil;
280
+ }
281
+
282
+ /*
283
+ call-seq:
284
+ cq = CompletionQueue.new
285
+ server = Server.new(cq, {'arg1': 'value1'})
286
+ ... // do stuff with server
287
+ ...
288
+ ... // to shutdown the server
289
+ server.destroy(cq)
290
+
291
+ ... // to shutdown the server with a timeout
292
+ server.destroy(cq, timeout)
293
+
294
+ Destroys server instances. */
295
+ static VALUE grpc_rb_server_destroy(int argc, VALUE *argv, VALUE self) {
296
+ VALUE cqueue = Qnil;
297
+ VALUE timeout = Qnil;
298
+ grpc_completion_queue *cq = NULL;
299
+ grpc_event ev;
300
+ grpc_rb_server *s = NULL;
301
+
302
+ /* "11" == 1 mandatory args, 1 (timeout) is optional */
303
+ rb_scan_args(argc, argv, "11", &cqueue, &timeout);
304
+ cq = grpc_rb_get_wrapped_completion_queue(cqueue);
305
+ TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
306
+
307
+ if (s->wrapped != NULL) {
308
+ grpc_server_shutdown_and_notify(s->wrapped, cq, NULL);
309
+ ev = grpc_rb_completion_queue_pluck_event(cqueue, Qnil, timeout);
310
+ if (!ev.success) {
311
+ rb_warn("server shutdown failed, cancelling the calls, objects may leak");
312
+ grpc_server_cancel_all_calls(s->wrapped);
313
+ return Qfalse;
314
+ }
315
+ grpc_server_destroy(s->wrapped);
316
+ s->wrapped = NULL;
317
+ }
318
+ return Qtrue;
319
+ }
320
+
321
+ /*
322
+ call-seq:
323
+ // insecure port
324
+ insecure_server = Server.new(cq, {'arg1': 'value1'})
325
+ insecure_server.add_http2_port('mydomain:50051', :this_port_is_insecure)
326
+
327
+ // secure port
328
+ server_creds = ...
329
+ secure_server = Server.new(cq, {'arg1': 'value1'})
330
+ secure_server.add_http_port('mydomain:50051', server_creds)
331
+
332
+ Adds a http2 port to server */
333
+ static VALUE grpc_rb_server_add_http2_port(VALUE self, VALUE port,
334
+ VALUE rb_creds) {
335
+ grpc_rb_server *s = NULL;
336
+ grpc_server_credentials *creds = NULL;
337
+ int recvd_port = 0;
338
+
339
+ TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
340
+ if (s->wrapped == NULL) {
341
+ rb_raise(rb_eRuntimeError, "destroyed!");
342
+ return Qnil;
343
+ } else if (TYPE(rb_creds) == T_SYMBOL) {
344
+ if (id_insecure_server != SYM2ID(rb_creds)) {
345
+ rb_raise(rb_eTypeError,
346
+ "bad creds symbol, want :this_port_is_insecure");
347
+ return Qnil;
348
+ }
349
+ recvd_port =
350
+ grpc_server_add_insecure_http2_port(s->wrapped, StringValueCStr(port));
351
+ if (recvd_port == 0) {
352
+ rb_raise(rb_eRuntimeError,
353
+ "could not add port %s to server, not sure why",
354
+ StringValueCStr(port));
355
+ }
356
+ } else {
357
+ creds = grpc_rb_get_wrapped_server_credentials(rb_creds);
358
+ recvd_port =
359
+ grpc_server_add_secure_http2_port(s->wrapped, StringValueCStr(port),
360
+ creds);
361
+ if (recvd_port == 0) {
362
+ rb_raise(rb_eRuntimeError,
363
+ "could not add secure port %s to server, not sure why",
364
+ StringValueCStr(port));
365
+ }
366
+ }
367
+ return INT2NUM(recvd_port);
368
+ }
369
+
370
+ void Init_grpc_server() {
371
+ grpc_rb_cServer =
372
+ rb_define_class_under(grpc_rb_mGrpcCore, "Server", rb_cObject);
373
+
374
+ /* Allocates an object managed by the ruby runtime */
375
+ rb_define_alloc_func(grpc_rb_cServer, grpc_rb_server_alloc);
376
+
377
+ /* Provides a ruby constructor and support for dup/clone. */
378
+ rb_define_method(grpc_rb_cServer, "initialize", grpc_rb_server_init, 2);
379
+ rb_define_method(grpc_rb_cServer, "initialize_copy",
380
+ grpc_rb_server_init_copy, 1);
381
+
382
+ /* Add the server methods. */
383
+ rb_define_method(grpc_rb_cServer, "request_call",
384
+ grpc_rb_server_request_call, 3);
385
+ rb_define_method(grpc_rb_cServer, "start", grpc_rb_server_start, 0);
386
+ rb_define_method(grpc_rb_cServer, "destroy", grpc_rb_server_destroy, -1);
387
+ rb_define_alias(grpc_rb_cServer, "close", "destroy");
388
+ rb_define_method(grpc_rb_cServer, "add_http2_port",
389
+ grpc_rb_server_add_http2_port,
390
+ 2);
391
+ id_at = rb_intern("at");
392
+ id_insecure_server = rb_intern("this_port_is_insecure");
393
+ }
394
+
395
+ /* Gets the wrapped server from the ruby wrapper */
396
+ grpc_server *grpc_rb_get_wrapped_server(VALUE v) {
397
+ grpc_rb_server *wrapper = NULL;
398
+ TypedData_Get_Struct(v, grpc_rb_server, &grpc_rb_server_data_type, wrapper);
399
+ return wrapper->wrapped;
400
+ }