grpc 1.42.0.pre1-x86_64-darwin

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 (128) hide show
  1. checksums.yaml +7 -0
  2. data/etc/roots.pem +4337 -0
  3. data/grpc_c.32.ruby +0 -0
  4. data/grpc_c.64.ruby +0 -0
  5. data/src/ruby/bin/math_client.rb +140 -0
  6. data/src/ruby/bin/math_pb.rb +34 -0
  7. data/src/ruby/bin/math_server.rb +191 -0
  8. data/src/ruby/bin/math_services_pb.rb +51 -0
  9. data/src/ruby/bin/noproto_client.rb +93 -0
  10. data/src/ruby/bin/noproto_server.rb +97 -0
  11. data/src/ruby/ext/grpc/ext-export.clang +1 -0
  12. data/src/ruby/ext/grpc/ext-export.gcc +6 -0
  13. data/src/ruby/ext/grpc/extconf.rb +123 -0
  14. data/src/ruby/ext/grpc/rb_byte_buffer.c +65 -0
  15. data/src/ruby/ext/grpc/rb_byte_buffer.h +35 -0
  16. data/src/ruby/ext/grpc/rb_call.c +1051 -0
  17. data/src/ruby/ext/grpc/rb_call.h +57 -0
  18. data/src/ruby/ext/grpc/rb_call_credentials.c +341 -0
  19. data/src/ruby/ext/grpc/rb_call_credentials.h +31 -0
  20. data/src/ruby/ext/grpc/rb_channel.c +846 -0
  21. data/src/ruby/ext/grpc/rb_channel.h +34 -0
  22. data/src/ruby/ext/grpc/rb_channel_args.c +155 -0
  23. data/src/ruby/ext/grpc/rb_channel_args.h +38 -0
  24. data/src/ruby/ext/grpc/rb_channel_credentials.c +286 -0
  25. data/src/ruby/ext/grpc/rb_channel_credentials.h +37 -0
  26. data/src/ruby/ext/grpc/rb_completion_queue.c +101 -0
  27. data/src/ruby/ext/grpc/rb_completion_queue.h +36 -0
  28. data/src/ruby/ext/grpc/rb_compression_options.c +471 -0
  29. data/src/ruby/ext/grpc/rb_compression_options.h +29 -0
  30. data/src/ruby/ext/grpc/rb_enable_cpp.cc +22 -0
  31. data/src/ruby/ext/grpc/rb_event_thread.c +145 -0
  32. data/src/ruby/ext/grpc/rb_event_thread.h +21 -0
  33. data/src/ruby/ext/grpc/rb_grpc.c +333 -0
  34. data/src/ruby/ext/grpc/rb_grpc.h +77 -0
  35. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +605 -0
  36. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +913 -0
  37. data/src/ruby/ext/grpc/rb_loader.c +57 -0
  38. data/src/ruby/ext/grpc/rb_loader.h +25 -0
  39. data/src/ruby/ext/grpc/rb_server.c +385 -0
  40. data/src/ruby/ext/grpc/rb_server.h +32 -0
  41. data/src/ruby/ext/grpc/rb_server_credentials.c +259 -0
  42. data/src/ruby/ext/grpc/rb_server_credentials.h +37 -0
  43. data/src/ruby/ext/grpc/rb_xds_channel_credentials.c +218 -0
  44. data/src/ruby/ext/grpc/rb_xds_channel_credentials.h +37 -0
  45. data/src/ruby/ext/grpc/rb_xds_server_credentials.c +170 -0
  46. data/src/ruby/ext/grpc/rb_xds_server_credentials.h +37 -0
  47. data/src/ruby/lib/grpc/2.4/grpc_c.bundle +0 -0
  48. data/src/ruby/lib/grpc/2.5/grpc_c.bundle +0 -0
  49. data/src/ruby/lib/grpc/2.6/grpc_c.bundle +0 -0
  50. data/src/ruby/lib/grpc/2.7/grpc_c.bundle +0 -0
  51. data/src/ruby/lib/grpc/3.0/grpc_c.bundle +0 -0
  52. data/src/ruby/lib/grpc/core/status_codes.rb +135 -0
  53. data/src/ruby/lib/grpc/core/time_consts.rb +56 -0
  54. data/src/ruby/lib/grpc/errors.rb +277 -0
  55. data/src/ruby/lib/grpc/generic/active_call.rb +669 -0
  56. data/src/ruby/lib/grpc/generic/bidi_call.rb +233 -0
  57. data/src/ruby/lib/grpc/generic/client_stub.rb +503 -0
  58. data/src/ruby/lib/grpc/generic/interceptor_registry.rb +53 -0
  59. data/src/ruby/lib/grpc/generic/interceptors.rb +186 -0
  60. data/src/ruby/lib/grpc/generic/rpc_desc.rb +204 -0
  61. data/src/ruby/lib/grpc/generic/rpc_server.rb +551 -0
  62. data/src/ruby/lib/grpc/generic/service.rb +211 -0
  63. data/src/ruby/lib/grpc/google_rpc_status_utils.rb +40 -0
  64. data/src/ruby/lib/grpc/grpc.rb +24 -0
  65. data/src/ruby/lib/grpc/logconfig.rb +44 -0
  66. data/src/ruby/lib/grpc/notifier.rb +45 -0
  67. data/src/ruby/lib/grpc/structs.rb +15 -0
  68. data/src/ruby/lib/grpc/version.rb +18 -0
  69. data/src/ruby/lib/grpc.rb +37 -0
  70. data/src/ruby/pb/README.md +42 -0
  71. data/src/ruby/pb/generate_proto_ruby.sh +51 -0
  72. data/src/ruby/pb/grpc/health/checker.rb +75 -0
  73. data/src/ruby/pb/grpc/health/v1/health_pb.rb +31 -0
  74. data/src/ruby/pb/grpc/health/v1/health_services_pb.rb +62 -0
  75. data/src/ruby/pb/grpc/testing/duplicate/echo_duplicate_services_pb.rb +44 -0
  76. data/src/ruby/pb/grpc/testing/metrics_pb.rb +28 -0
  77. data/src/ruby/pb/grpc/testing/metrics_services_pb.rb +49 -0
  78. data/src/ruby/pb/src/proto/grpc/testing/empty_pb.rb +17 -0
  79. data/src/ruby/pb/src/proto/grpc/testing/messages_pb.rb +145 -0
  80. data/src/ruby/pb/src/proto/grpc/testing/test_pb.rb +16 -0
  81. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +152 -0
  82. data/src/ruby/pb/test/client.rb +769 -0
  83. data/src/ruby/pb/test/server.rb +252 -0
  84. data/src/ruby/pb/test/xds_client.rb +415 -0
  85. data/src/ruby/spec/call_credentials_spec.rb +42 -0
  86. data/src/ruby/spec/call_spec.rb +180 -0
  87. data/src/ruby/spec/channel_connection_spec.rb +126 -0
  88. data/src/ruby/spec/channel_credentials_spec.rb +124 -0
  89. data/src/ruby/spec/channel_spec.rb +245 -0
  90. data/src/ruby/spec/client_auth_spec.rb +152 -0
  91. data/src/ruby/spec/client_server_spec.rb +664 -0
  92. data/src/ruby/spec/compression_options_spec.rb +149 -0
  93. data/src/ruby/spec/debug_message_spec.rb +134 -0
  94. data/src/ruby/spec/error_sanity_spec.rb +49 -0
  95. data/src/ruby/spec/errors_spec.rb +142 -0
  96. data/src/ruby/spec/generic/active_call_spec.rb +683 -0
  97. data/src/ruby/spec/generic/client_interceptors_spec.rb +153 -0
  98. data/src/ruby/spec/generic/client_stub_spec.rb +1083 -0
  99. data/src/ruby/spec/generic/interceptor_registry_spec.rb +65 -0
  100. data/src/ruby/spec/generic/rpc_desc_spec.rb +374 -0
  101. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +127 -0
  102. data/src/ruby/spec/generic/rpc_server_spec.rb +748 -0
  103. data/src/ruby/spec/generic/server_interceptors_spec.rb +218 -0
  104. data/src/ruby/spec/generic/service_spec.rb +263 -0
  105. data/src/ruby/spec/google_rpc_status_utils_spec.rb +282 -0
  106. data/src/ruby/spec/pb/codegen/grpc/testing/package_options.proto +28 -0
  107. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_import.proto +22 -0
  108. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_import2.proto +23 -0
  109. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_ruby_style.proto +41 -0
  110. data/src/ruby/spec/pb/codegen/grpc/testing/same_package_service_name.proto +27 -0
  111. data/src/ruby/spec/pb/codegen/grpc/testing/same_ruby_package_service_name.proto +29 -0
  112. data/src/ruby/spec/pb/codegen/package_option_spec.rb +98 -0
  113. data/src/ruby/spec/pb/duplicate/codegen_spec.rb +57 -0
  114. data/src/ruby/spec/pb/health/checker_spec.rb +236 -0
  115. data/src/ruby/spec/server_credentials_spec.rb +104 -0
  116. data/src/ruby/spec/server_spec.rb +231 -0
  117. data/src/ruby/spec/spec_helper.rb +61 -0
  118. data/src/ruby/spec/support/helpers.rb +107 -0
  119. data/src/ruby/spec/support/services.rb +160 -0
  120. data/src/ruby/spec/testdata/README +1 -0
  121. data/src/ruby/spec/testdata/ca.pem +20 -0
  122. data/src/ruby/spec/testdata/client.key +28 -0
  123. data/src/ruby/spec/testdata/client.pem +20 -0
  124. data/src/ruby/spec/testdata/server1.key +28 -0
  125. data/src/ruby/spec/testdata/server1.pem +22 -0
  126. data/src/ruby/spec/time_consts_spec.rb +74 -0
  127. data/src/ruby/spec/user_agent_spec.rb +74 -0
  128. metadata +404 -0
@@ -0,0 +1,57 @@
1
+ /*
2
+ *
3
+ * Copyright 2016 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #include "rb_grpc_imports.generated.h"
20
+
21
+ #if GPR_WINDOWS
22
+ #include <tchar.h>
23
+
24
+ int grpc_rb_load_core() {
25
+ #if GPR_ARCH_64
26
+ TCHAR fname[] = _T("grpc_c.64.ruby");
27
+ #else
28
+ TCHAR fname[] = _T("grpc_c.32.ruby");
29
+ #endif
30
+ HMODULE module = GetModuleHandle(_T("grpc_c.so"));
31
+ TCHAR path[2048 + 32] = _T("");
32
+ LPTSTR seek_back = NULL;
33
+ GetModuleFileName(module, path, 2048);
34
+
35
+ seek_back = _tcsrchr(path, _T('\\'));
36
+
37
+ while (seek_back) {
38
+ HMODULE grpc_c;
39
+ _tcscpy(seek_back + 1, fname);
40
+ grpc_c = LoadLibrary(path);
41
+ if (grpc_c) {
42
+ grpc_rb_load_imports(grpc_c);
43
+ return 1;
44
+ } else {
45
+ *seek_back = _T('\0');
46
+ seek_back = _tcsrchr(path, _T('\\'));
47
+ }
48
+ }
49
+
50
+ return 0;
51
+ }
52
+
53
+ #else
54
+
55
+ int grpc_rb_load_core() { return 1; }
56
+
57
+ #endif
@@ -0,0 +1,25 @@
1
+ /*
2
+ *
3
+ * Copyright 2016 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #ifndef GRPC_RB_LOADER_H_
20
+ #define GRPC_RB_LOADER_H_
21
+
22
+ /* Attempts to load the core if necessary, and return non-zero upon succes. */
23
+ int grpc_rb_load_core();
24
+
25
+ #endif /* GRPC_RB_BYTE_BUFFER_H_ */
@@ -0,0 +1,385 @@
1
+ /*
2
+ *
3
+ * Copyright 2015 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #include <ruby/ruby.h>
20
+
21
+ #include "rb_server.h"
22
+
23
+ #include "rb_byte_buffer.h"
24
+ #include "rb_call.h"
25
+ #include "rb_channel_args.h"
26
+ #include "rb_completion_queue.h"
27
+ #include "rb_grpc.h"
28
+ #include "rb_grpc_imports.generated.h"
29
+ #include "rb_server_credentials.h"
30
+ #include "rb_xds_server_credentials.h"
31
+
32
+ #include <grpc/grpc.h>
33
+ #include <grpc/grpc_security.h>
34
+ #include <grpc/support/atm.h>
35
+ #include <grpc/support/log.h>
36
+
37
+ /* grpc_rb_cServer is the ruby class that proxies grpc_server. */
38
+ static VALUE grpc_rb_cServer = Qnil;
39
+
40
+ /* id_at is the constructor method of the ruby standard Time class. */
41
+ static ID id_at;
42
+
43
+ /* id_insecure_server is used to indicate that a server is insecure */
44
+ static VALUE id_insecure_server;
45
+
46
+ /* grpc_rb_server wraps a grpc_server. */
47
+ typedef struct grpc_rb_server {
48
+ /* The actual server */
49
+ grpc_server* wrapped;
50
+ grpc_completion_queue* queue;
51
+ int shutdown_and_notify_done;
52
+ int destroy_done;
53
+ } grpc_rb_server;
54
+
55
+ static void grpc_rb_server_maybe_shutdown_and_notify(grpc_rb_server* server,
56
+ gpr_timespec deadline) {
57
+ grpc_event ev;
58
+ void* tag = &ev;
59
+ if (!server->shutdown_and_notify_done) {
60
+ server->shutdown_and_notify_done = 1;
61
+ if (server->wrapped != NULL) {
62
+ grpc_server_shutdown_and_notify(server->wrapped, server->queue, tag);
63
+ ev = rb_completion_queue_pluck(server->queue, tag, deadline, NULL);
64
+ if (ev.type == GRPC_QUEUE_TIMEOUT) {
65
+ grpc_server_cancel_all_calls(server->wrapped);
66
+ ev = rb_completion_queue_pluck(
67
+ server->queue, tag, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
68
+ }
69
+ if (ev.type != GRPC_OP_COMPLETE) {
70
+ gpr_log(GPR_INFO,
71
+ "GRPC_RUBY: bad grpc_server_shutdown_and_notify result:%d",
72
+ ev.type);
73
+ }
74
+ }
75
+ }
76
+ }
77
+
78
+ static void grpc_rb_server_maybe_destroy(grpc_rb_server* server) {
79
+ // This can be started by app or implicitly by GC. Avoid a race between these.
80
+ if (!server->destroy_done) {
81
+ server->destroy_done = 1;
82
+ if (server->wrapped != NULL) {
83
+ grpc_server_destroy(server->wrapped);
84
+ grpc_rb_completion_queue_destroy(server->queue);
85
+ server->wrapped = NULL;
86
+ server->queue = NULL;
87
+ }
88
+ }
89
+ }
90
+
91
+ static void grpc_rb_server_free_internal(void* p) {
92
+ grpc_rb_server* svr = NULL;
93
+ gpr_timespec deadline;
94
+ if (p == NULL) {
95
+ return;
96
+ };
97
+ svr = (grpc_rb_server*)p;
98
+
99
+ deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
100
+ gpr_time_from_seconds(2, GPR_TIMESPAN));
101
+
102
+ grpc_rb_server_maybe_shutdown_and_notify(svr, deadline);
103
+ grpc_rb_server_maybe_destroy(svr);
104
+
105
+ xfree(p);
106
+ }
107
+
108
+ /* Destroys server instances. */
109
+ static void grpc_rb_server_free(void* p) {
110
+ grpc_rb_server_free_internal(p);
111
+ grpc_ruby_shutdown();
112
+ }
113
+
114
+ static const rb_data_type_t grpc_rb_server_data_type = {
115
+ "grpc_server",
116
+ {GRPC_RB_GC_NOT_MARKED,
117
+ grpc_rb_server_free,
118
+ GRPC_RB_MEMSIZE_UNAVAILABLE,
119
+ {NULL, NULL}},
120
+ NULL,
121
+ NULL,
122
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
123
+ /* It is unsafe to specify RUBY_TYPED_FREE_IMMEDIATELY because the free
124
+ * function would block and we might want to unlock GVL
125
+ * TODO(yugui) Unlock GVL?
126
+ */
127
+ 0,
128
+ #endif
129
+ };
130
+
131
+ /* Allocates grpc_rb_server instances. */
132
+ static VALUE grpc_rb_server_alloc(VALUE cls) {
133
+ grpc_ruby_init();
134
+ grpc_rb_server* wrapper = ALLOC(grpc_rb_server);
135
+ wrapper->wrapped = NULL;
136
+ wrapper->destroy_done = 0;
137
+ wrapper->shutdown_and_notify_done = 0;
138
+ return TypedData_Wrap_Struct(cls, &grpc_rb_server_data_type, wrapper);
139
+ }
140
+
141
+ /*
142
+ call-seq:
143
+ server = Server.new({'arg1': 'value1'})
144
+
145
+ Initializes server instances. */
146
+ static VALUE grpc_rb_server_init(VALUE self, VALUE channel_args) {
147
+ grpc_completion_queue* cq = NULL;
148
+ grpc_rb_server* wrapper = NULL;
149
+ grpc_server* srv = NULL;
150
+ grpc_channel_args args;
151
+ MEMZERO(&args, grpc_channel_args, 1);
152
+
153
+ cq = grpc_completion_queue_create_for_pluck(NULL);
154
+ TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type,
155
+ wrapper);
156
+ grpc_rb_hash_convert_to_channel_args(channel_args, &args);
157
+ srv = grpc_server_create(&args, NULL);
158
+
159
+ if (args.args != NULL) {
160
+ xfree(args.args); /* Allocated by grpc_rb_hash_convert_to_channel_args */
161
+ }
162
+ if (srv == NULL) {
163
+ rb_raise(rb_eRuntimeError, "could not create a gRPC server, not sure why");
164
+ }
165
+ grpc_server_register_completion_queue(srv, cq, NULL);
166
+ wrapper->wrapped = srv;
167
+ wrapper->queue = cq;
168
+
169
+ return self;
170
+ }
171
+
172
+ /* request_call_stack holds various values used by the
173
+ * grpc_rb_server_request_call function */
174
+ typedef struct request_call_stack {
175
+ grpc_call_details details;
176
+ grpc_metadata_array md_ary;
177
+ } request_call_stack;
178
+
179
+ /* grpc_request_call_stack_init ensures the request_call_stack is properly
180
+ * initialized */
181
+ static void grpc_request_call_stack_init(request_call_stack* st) {
182
+ MEMZERO(st, request_call_stack, 1);
183
+ grpc_metadata_array_init(&st->md_ary);
184
+ grpc_call_details_init(&st->details);
185
+ }
186
+
187
+ /* grpc_request_call_stack_cleanup ensures the request_call_stack is properly
188
+ * cleaned up */
189
+ static void grpc_request_call_stack_cleanup(request_call_stack* st) {
190
+ grpc_metadata_array_destroy(&st->md_ary);
191
+ grpc_call_details_destroy(&st->details);
192
+ }
193
+
194
+ /* call-seq:
195
+ server.request_call
196
+
197
+ Requests notification of a new call on a server. */
198
+ static VALUE grpc_rb_server_request_call(VALUE self) {
199
+ grpc_rb_server* s = NULL;
200
+ grpc_call* call = NULL;
201
+ grpc_event ev;
202
+ grpc_call_error err;
203
+ request_call_stack st;
204
+ VALUE result;
205
+ void* tag = (void*)&st;
206
+ grpc_completion_queue* call_queue =
207
+ grpc_completion_queue_create_for_pluck(NULL);
208
+ gpr_timespec deadline;
209
+
210
+ TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
211
+ if (s->wrapped == NULL) {
212
+ rb_raise(rb_eRuntimeError, "destroyed!");
213
+ return Qnil;
214
+ }
215
+ grpc_request_call_stack_init(&st);
216
+ /* call grpc_server_request_call, then wait for it to complete using
217
+ * pluck_event */
218
+ err = grpc_server_request_call(s->wrapped, &call, &st.details, &st.md_ary,
219
+ call_queue, s->queue, tag);
220
+ if (err != GRPC_CALL_OK) {
221
+ grpc_request_call_stack_cleanup(&st);
222
+ rb_raise(grpc_rb_eCallError,
223
+ "grpc_server_request_call failed: %s (code=%d)",
224
+ grpc_call_error_detail_of(err), err);
225
+ return Qnil;
226
+ }
227
+
228
+ ev = rb_completion_queue_pluck(s->queue, tag,
229
+ gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
230
+ if (!ev.success) {
231
+ grpc_request_call_stack_cleanup(&st);
232
+ rb_raise(grpc_rb_eCallError, "request_call completion failed");
233
+ return Qnil;
234
+ }
235
+
236
+ /* build the NewServerRpc struct result */
237
+ deadline = gpr_convert_clock_type(st.details.deadline, GPR_CLOCK_REALTIME);
238
+ result = rb_struct_new(
239
+ grpc_rb_sNewServerRpc, grpc_rb_slice_to_ruby_string(st.details.method),
240
+ grpc_rb_slice_to_ruby_string(st.details.host),
241
+ rb_funcall(rb_cTime, id_at, 2, INT2NUM(deadline.tv_sec),
242
+ INT2NUM(deadline.tv_nsec / 1000)),
243
+ grpc_rb_md_ary_to_h(&st.md_ary), grpc_rb_wrap_call(call, call_queue),
244
+ NULL);
245
+ grpc_request_call_stack_cleanup(&st);
246
+ return result;
247
+ }
248
+
249
+ static VALUE grpc_rb_server_start(VALUE self) {
250
+ grpc_rb_server* s = NULL;
251
+ TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
252
+
253
+ grpc_ruby_fork_guard();
254
+ if (s->wrapped == NULL) {
255
+ rb_raise(rb_eRuntimeError, "destroyed!");
256
+ } else {
257
+ grpc_server_start(s->wrapped);
258
+ }
259
+ return Qnil;
260
+ }
261
+
262
+ static VALUE grpc_rb_server_shutdown_and_notify(VALUE self, VALUE timeout) {
263
+ gpr_timespec deadline;
264
+ grpc_rb_server* s = NULL;
265
+
266
+ TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
267
+ if (TYPE(timeout) == T_NIL) {
268
+ deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
269
+ } else {
270
+ deadline = grpc_rb_time_timeval(timeout, /* absolute time*/ 0);
271
+ }
272
+
273
+ grpc_rb_server_maybe_shutdown_and_notify(s, deadline);
274
+
275
+ return Qnil;
276
+ }
277
+
278
+ /*
279
+ call-seq:
280
+ server = Server.new({'arg1': 'value1'})
281
+ ... // do stuff with server
282
+ ...
283
+ ... // initiate server shutdown
284
+ server.shutdown_and_notify(timeout)
285
+ ... // to shutdown the server
286
+ server.destroy()
287
+
288
+ Destroys server instances. */
289
+ static VALUE grpc_rb_server_destroy(VALUE self) {
290
+ grpc_rb_server* s = NULL;
291
+ TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
292
+ grpc_rb_server_maybe_destroy(s);
293
+ return Qnil;
294
+ }
295
+
296
+ /*
297
+ call-seq:
298
+ // insecure port
299
+ insecure_server = Server.new(cq, {'arg1': 'value1'})
300
+ insecure_server.add_http2_port('mydomain:50051', :this_port_is_insecure)
301
+
302
+ // secure port
303
+ server_creds = ...
304
+ secure_server = Server.new(cq, {'arg1': 'value1'})
305
+ secure_server.add_http_port('mydomain:50051', server_creds)
306
+
307
+ Adds a http2 port to server */
308
+ static VALUE grpc_rb_server_add_http2_port(VALUE self, VALUE port,
309
+ VALUE rb_creds) {
310
+ grpc_rb_server* s = NULL;
311
+ grpc_server_credentials* creds = NULL;
312
+ int recvd_port = 0;
313
+
314
+ TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s);
315
+ if (s->wrapped == NULL) {
316
+ rb_raise(rb_eRuntimeError, "destroyed!");
317
+ return Qnil;
318
+ } else if (TYPE(rb_creds) == T_SYMBOL) {
319
+ if (id_insecure_server != SYM2ID(rb_creds)) {
320
+ rb_raise(rb_eTypeError, "bad creds symbol, want :this_port_is_insecure");
321
+ return Qnil;
322
+ }
323
+ recvd_port =
324
+ grpc_server_add_insecure_http2_port(s->wrapped, StringValueCStr(port));
325
+ if (recvd_port == 0) {
326
+ rb_raise(rb_eRuntimeError,
327
+ "could not add port %s to server, not sure why",
328
+ StringValueCStr(port));
329
+ }
330
+ } else {
331
+ // TODO: create a common parent class for all server-side credentials,
332
+ // then we can have a single method to retrieve the underlying
333
+ // grpc_server_credentials object, and avoid the need for this reflection
334
+ if (grpc_rb_is_server_credentials(rb_creds)) {
335
+ creds = grpc_rb_get_wrapped_server_credentials(rb_creds);
336
+ } else if (grpc_rb_is_xds_server_credentials(rb_creds)) {
337
+ creds = grpc_rb_get_wrapped_xds_server_credentials(rb_creds);
338
+ } else {
339
+ rb_raise(rb_eTypeError,
340
+ "failed to create server because credentials parameter has an "
341
+ "invalid type, want ServerCredentials or XdsServerCredentials");
342
+ }
343
+ recvd_port = grpc_server_add_secure_http2_port(
344
+ s->wrapped, StringValueCStr(port), creds);
345
+ if (recvd_port == 0) {
346
+ rb_raise(rb_eRuntimeError,
347
+ "could not add secure port %s to server, not sure why",
348
+ StringValueCStr(port));
349
+ }
350
+ }
351
+ return INT2NUM(recvd_port);
352
+ }
353
+
354
+ void Init_grpc_server() {
355
+ grpc_rb_cServer =
356
+ rb_define_class_under(grpc_rb_mGrpcCore, "Server", rb_cObject);
357
+
358
+ /* Allocates an object managed by the ruby runtime */
359
+ rb_define_alloc_func(grpc_rb_cServer, grpc_rb_server_alloc);
360
+
361
+ /* Provides a ruby constructor and support for dup/clone. */
362
+ rb_define_method(grpc_rb_cServer, "initialize", grpc_rb_server_init, 1);
363
+ rb_define_method(grpc_rb_cServer, "initialize_copy", grpc_rb_cannot_init_copy,
364
+ 1);
365
+
366
+ /* Add the server methods. */
367
+ rb_define_method(grpc_rb_cServer, "request_call", grpc_rb_server_request_call,
368
+ 0);
369
+ rb_define_method(grpc_rb_cServer, "start", grpc_rb_server_start, 0);
370
+ rb_define_method(grpc_rb_cServer, "shutdown_and_notify",
371
+ grpc_rb_server_shutdown_and_notify, 1);
372
+ rb_define_method(grpc_rb_cServer, "destroy", grpc_rb_server_destroy, 0);
373
+ rb_define_alias(grpc_rb_cServer, "close", "destroy");
374
+ rb_define_method(grpc_rb_cServer, "add_http2_port",
375
+ grpc_rb_server_add_http2_port, 2);
376
+ id_at = rb_intern("at");
377
+ id_insecure_server = rb_intern("this_port_is_insecure");
378
+ }
379
+
380
+ /* Gets the wrapped server from the ruby wrapper */
381
+ grpc_server* grpc_rb_get_wrapped_server(VALUE v) {
382
+ grpc_rb_server* wrapper = NULL;
383
+ TypedData_Get_Struct(v, grpc_rb_server, &grpc_rb_server_data_type, wrapper);
384
+ return wrapper->wrapped;
385
+ }
@@ -0,0 +1,32 @@
1
+ /*
2
+ *
3
+ * Copyright 2015 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #ifndef GRPC_RB_SERVER_H_
20
+ #define GRPC_RB_SERVER_H_
21
+
22
+ #include <ruby/ruby.h>
23
+
24
+ #include <grpc/grpc.h>
25
+
26
+ /* Initializes the Server class. */
27
+ void Init_grpc_server();
28
+
29
+ /* Gets the wrapped server from the ruby wrapper */
30
+ grpc_server* grpc_rb_get_wrapped_server(VALUE v);
31
+
32
+ #endif /* GRPC_RB_SERVER_H_ */