grpc 1.30.2-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 (121) hide show
  1. checksums.yaml +7 -0
  2. data/etc/roots.pem +4644 -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 +107 -0
  14. data/src/ruby/ext/grpc/rb_byte_buffer.c +64 -0
  15. data/src/ruby/ext/grpc/rb_byte_buffer.h +35 -0
  16. data/src/ruby/ext/grpc/rb_call.c +1050 -0
  17. data/src/ruby/ext/grpc/rb_call.h +53 -0
  18. data/src/ruby/ext/grpc/rb_call_credentials.c +297 -0
  19. data/src/ruby/ext/grpc/rb_call_credentials.h +31 -0
  20. data/src/ruby/ext/grpc/rb_channel.c +835 -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 +267 -0
  25. data/src/ruby/ext/grpc/rb_channel_credentials.h +32 -0
  26. data/src/ruby/ext/grpc/rb_completion_queue.c +100 -0
  27. data/src/ruby/ext/grpc/rb_completion_queue.h +36 -0
  28. data/src/ruby/ext/grpc/rb_compression_options.c +470 -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 +143 -0
  32. data/src/ruby/ext/grpc/rb_event_thread.h +21 -0
  33. data/src/ruby/ext/grpc/rb_grpc.c +328 -0
  34. data/src/ruby/ext/grpc/rb_grpc.h +76 -0
  35. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +573 -0
  36. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +865 -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 +372 -0
  40. data/src/ruby/ext/grpc/rb_server.h +32 -0
  41. data/src/ruby/ext/grpc/rb_server_credentials.c +243 -0
  42. data/src/ruby/ext/grpc/rb_server_credentials.h +32 -0
  43. data/src/ruby/lib/grpc.rb +37 -0
  44. data/src/ruby/lib/grpc/2.3/grpc_c.so +0 -0
  45. data/src/ruby/lib/grpc/2.4/grpc_c.so +0 -0
  46. data/src/ruby/lib/grpc/2.5/grpc_c.so +0 -0
  47. data/src/ruby/lib/grpc/2.6/grpc_c.so +0 -0
  48. data/src/ruby/lib/grpc/2.7/grpc_c.so +0 -0
  49. data/src/ruby/lib/grpc/core/status_codes.rb +135 -0
  50. data/src/ruby/lib/grpc/core/time_consts.rb +56 -0
  51. data/src/ruby/lib/grpc/errors.rb +277 -0
  52. data/src/ruby/lib/grpc/generic/active_call.rb +669 -0
  53. data/src/ruby/lib/grpc/generic/bidi_call.rb +233 -0
  54. data/src/ruby/lib/grpc/generic/client_stub.rb +501 -0
  55. data/src/ruby/lib/grpc/generic/interceptor_registry.rb +53 -0
  56. data/src/ruby/lib/grpc/generic/interceptors.rb +186 -0
  57. data/src/ruby/lib/grpc/generic/rpc_desc.rb +204 -0
  58. data/src/ruby/lib/grpc/generic/rpc_server.rb +551 -0
  59. data/src/ruby/lib/grpc/generic/service.rb +211 -0
  60. data/src/ruby/lib/grpc/google_rpc_status_utils.rb +40 -0
  61. data/src/ruby/lib/grpc/grpc.rb +24 -0
  62. data/src/ruby/lib/grpc/logconfig.rb +44 -0
  63. data/src/ruby/lib/grpc/notifier.rb +45 -0
  64. data/src/ruby/lib/grpc/structs.rb +15 -0
  65. data/src/ruby/lib/grpc/version.rb +18 -0
  66. data/src/ruby/pb/README.md +42 -0
  67. data/src/ruby/pb/generate_proto_ruby.sh +51 -0
  68. data/src/ruby/pb/grpc/health/checker.rb +75 -0
  69. data/src/ruby/pb/grpc/health/v1/health_pb.rb +31 -0
  70. data/src/ruby/pb/grpc/health/v1/health_services_pb.rb +62 -0
  71. data/src/ruby/pb/grpc/testing/duplicate/echo_duplicate_services_pb.rb +44 -0
  72. data/src/ruby/pb/grpc/testing/metrics_pb.rb +28 -0
  73. data/src/ruby/pb/grpc/testing/metrics_services_pb.rb +49 -0
  74. data/src/ruby/pb/src/proto/grpc/testing/empty_pb.rb +17 -0
  75. data/src/ruby/pb/src/proto/grpc/testing/messages_pb.rb +105 -0
  76. data/src/ruby/pb/src/proto/grpc/testing/test_pb.rb +16 -0
  77. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +118 -0
  78. data/src/ruby/pb/test/client.rb +769 -0
  79. data/src/ruby/pb/test/server.rb +252 -0
  80. data/src/ruby/pb/test/xds_client.rb +213 -0
  81. data/src/ruby/spec/call_credentials_spec.rb +42 -0
  82. data/src/ruby/spec/call_spec.rb +180 -0
  83. data/src/ruby/spec/channel_connection_spec.rb +126 -0
  84. data/src/ruby/spec/channel_credentials_spec.rb +82 -0
  85. data/src/ruby/spec/channel_spec.rb +234 -0
  86. data/src/ruby/spec/client_auth_spec.rb +126 -0
  87. data/src/ruby/spec/client_server_spec.rb +664 -0
  88. data/src/ruby/spec/compression_options_spec.rb +149 -0
  89. data/src/ruby/spec/debug_message_spec.rb +134 -0
  90. data/src/ruby/spec/error_sanity_spec.rb +49 -0
  91. data/src/ruby/spec/errors_spec.rb +142 -0
  92. data/src/ruby/spec/generic/active_call_spec.rb +672 -0
  93. data/src/ruby/spec/generic/client_interceptors_spec.rb +153 -0
  94. data/src/ruby/spec/generic/client_stub_spec.rb +1083 -0
  95. data/src/ruby/spec/generic/interceptor_registry_spec.rb +65 -0
  96. data/src/ruby/spec/generic/rpc_desc_spec.rb +374 -0
  97. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +127 -0
  98. data/src/ruby/spec/generic/rpc_server_spec.rb +748 -0
  99. data/src/ruby/spec/generic/server_interceptors_spec.rb +218 -0
  100. data/src/ruby/spec/generic/service_spec.rb +263 -0
  101. data/src/ruby/spec/google_rpc_status_utils_spec.rb +282 -0
  102. data/src/ruby/spec/pb/codegen/grpc/testing/package_options.proto +28 -0
  103. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_import.proto +22 -0
  104. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_import2.proto +23 -0
  105. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_ruby_style.proto +41 -0
  106. data/src/ruby/spec/pb/codegen/package_option_spec.rb +82 -0
  107. data/src/ruby/spec/pb/duplicate/codegen_spec.rb +57 -0
  108. data/src/ruby/spec/pb/health/checker_spec.rb +236 -0
  109. data/src/ruby/spec/server_credentials_spec.rb +79 -0
  110. data/src/ruby/spec/server_spec.rb +209 -0
  111. data/src/ruby/spec/spec_helper.rb +61 -0
  112. data/src/ruby/spec/support/helpers.rb +107 -0
  113. data/src/ruby/spec/support/services.rb +160 -0
  114. data/src/ruby/spec/testdata/README +1 -0
  115. data/src/ruby/spec/testdata/ca.pem +20 -0
  116. data/src/ruby/spec/testdata/client.key +28 -0
  117. data/src/ruby/spec/testdata/client.pem +20 -0
  118. data/src/ruby/spec/testdata/server1.key +28 -0
  119. data/src/ruby/spec/testdata/server1.pem +22 -0
  120. data/src/ruby/spec/time_consts_spec.rb +74 -0
  121. metadata +394 -0
@@ -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_CREDENTIALS_H_
20
+ #define GRPC_RB_CREDENTIALS_H_
21
+
22
+ #include <ruby/ruby.h>
23
+
24
+ #include <grpc/grpc_security.h>
25
+
26
+ /* Initializes the ruby ChannelCredentials class. */
27
+ void Init_grpc_channel_credentials();
28
+
29
+ /* Gets the wrapped credentials from the ruby wrapper */
30
+ grpc_channel_credentials* grpc_rb_get_wrapped_channel_credentials(VALUE v);
31
+
32
+ #endif /* GRPC_RB_CREDENTIALS_H_ */
@@ -0,0 +1,100 @@
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_completion_queue.h"
22
+ #include "rb_grpc_imports.generated.h"
23
+
24
+ #include <ruby/thread.h>
25
+
26
+ #include <grpc/grpc.h>
27
+ #include <grpc/support/log.h>
28
+ #include <grpc/support/time.h>
29
+ #include "rb_grpc.h"
30
+
31
+ /* Used to allow grpc_completion_queue_next call to release the GIL */
32
+ typedef struct next_call_stack {
33
+ grpc_completion_queue* cq;
34
+ grpc_event event;
35
+ gpr_timespec timeout;
36
+ void* tag;
37
+ volatile int interrupted;
38
+ } next_call_stack;
39
+
40
+ /* Calls grpc_completion_queue_pluck without holding the ruby GIL */
41
+ static void* grpc_rb_completion_queue_pluck_no_gil(void* param) {
42
+ next_call_stack* const next_call = (next_call_stack*)param;
43
+ gpr_timespec increment = gpr_time_from_millis(20, GPR_TIMESPAN);
44
+ gpr_timespec deadline;
45
+ do {
46
+ deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), increment);
47
+ next_call->event = grpc_completion_queue_pluck(
48
+ next_call->cq, next_call->tag, deadline, NULL);
49
+ if (next_call->event.type != GRPC_QUEUE_TIMEOUT ||
50
+ gpr_time_cmp(deadline, next_call->timeout) > 0) {
51
+ break;
52
+ }
53
+ } while (!next_call->interrupted);
54
+ return NULL;
55
+ }
56
+
57
+ /* Helper function to free a completion queue. */
58
+ void grpc_rb_completion_queue_destroy(grpc_completion_queue* cq) {
59
+ /* Every function that adds an event to a queue also synchronously plucks
60
+ that event from the queue, and holds a reference to the Ruby object that
61
+ holds the queue, so we only get to this point if all of those functions
62
+ have completed, and the queue is empty */
63
+ grpc_completion_queue_shutdown(cq);
64
+ grpc_completion_queue_destroy(cq);
65
+ }
66
+
67
+ static void unblock_func(void* param) {
68
+ next_call_stack* const next_call = (next_call_stack*)param;
69
+ next_call->interrupted = 1;
70
+ }
71
+
72
+ /* Does the same thing as grpc_completion_queue_pluck, while properly releasing
73
+ the GVL and handling interrupts */
74
+ grpc_event rb_completion_queue_pluck(grpc_completion_queue* queue, void* tag,
75
+ gpr_timespec deadline, void* reserved) {
76
+ next_call_stack next_call;
77
+ MEMZERO(&next_call, next_call_stack, 1);
78
+ next_call.cq = queue;
79
+ next_call.timeout = deadline;
80
+ next_call.tag = tag;
81
+ next_call.event.type = GRPC_QUEUE_TIMEOUT;
82
+ (void)reserved;
83
+ /* Loop until we finish a pluck without an interruption. The internal
84
+ pluck function runs either until it is interrupted or it gets an
85
+ event, or time runs out.
86
+
87
+ The basic reason we need this relatively complicated construction is that
88
+ we need to re-acquire the GVL when an interrupt comes in, so that the ruby
89
+ interpreter can do what it needs to do with the interrupt. But we also need
90
+ to get back to plucking when the interrupt has been handled. */
91
+ do {
92
+ next_call.interrupted = 0;
93
+ rb_thread_call_without_gvl(grpc_rb_completion_queue_pluck_no_gil,
94
+ (void*)&next_call, unblock_func,
95
+ (void*)&next_call);
96
+ /* If an interrupt prevented pluck from returning useful information, then
97
+ any plucks that did complete must have timed out */
98
+ } while (next_call.interrupted && next_call.event.type == GRPC_QUEUE_TIMEOUT);
99
+ return next_call.event;
100
+ }
@@ -0,0 +1,36 @@
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_COMPLETION_QUEUE_H_
20
+ #define GRPC_RB_COMPLETION_QUEUE_H_
21
+
22
+ #include <ruby/ruby.h>
23
+
24
+ #include <grpc/grpc.h>
25
+
26
+ void grpc_rb_completion_queue_destroy(grpc_completion_queue* cq);
27
+
28
+ /**
29
+ * Makes the implementation of CompletionQueue#pluck available in other files
30
+ *
31
+ * This avoids having code that holds the GIL repeated at multiple sites.
32
+ */
33
+ grpc_event rb_completion_queue_pluck(grpc_completion_queue* queue, void* tag,
34
+ gpr_timespec deadline, void* reserved);
35
+
36
+ #endif /* GRPC_RB_COMPLETION_QUEUE_H_ */
@@ -0,0 +1,470 @@
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_byte_buffer.h"
22
+ #include "rb_compression_options.h"
23
+ #include "rb_grpc_imports.generated.h"
24
+
25
+ #include <grpc/compression.h>
26
+ #include <grpc/grpc.h>
27
+ #include <grpc/impl/codegen/compression_types.h>
28
+ #include <grpc/impl/codegen/grpc_types.h>
29
+ #include <grpc/support/alloc.h>
30
+ #include <grpc/support/log.h>
31
+ #include <grpc/support/string_util.h>
32
+ #include <string.h>
33
+
34
+ #include "rb_grpc.h"
35
+
36
+ static VALUE grpc_rb_cCompressionOptions = Qnil;
37
+
38
+ /* Ruby Ids for the names of valid compression levels. */
39
+ static VALUE id_compress_level_none = Qnil;
40
+ static VALUE id_compress_level_low = Qnil;
41
+ static VALUE id_compress_level_medium = Qnil;
42
+ static VALUE id_compress_level_high = Qnil;
43
+
44
+ /* grpc_rb_compression_options wraps a grpc_compression_options.
45
+ * It can be used to get the channel argument key-values for specific
46
+ * compression settings. */
47
+
48
+ /* Note that ruby objects of this type don't carry any state in other
49
+ * Ruby objects and don't have a mark for GC. */
50
+ typedef struct grpc_rb_compression_options {
51
+ /* The actual compression options that's being wrapped */
52
+ grpc_compression_options* wrapped;
53
+ } grpc_rb_compression_options;
54
+
55
+ static void grpc_rb_compression_options_free_internal(void* p) {
56
+ grpc_rb_compression_options* wrapper = NULL;
57
+ if (p == NULL) {
58
+ return;
59
+ };
60
+ wrapper = (grpc_rb_compression_options*)p;
61
+ if (wrapper->wrapped != NULL) {
62
+ gpr_free(wrapper->wrapped);
63
+ wrapper->wrapped = NULL;
64
+ }
65
+ xfree(p);
66
+ }
67
+
68
+ /* Destroys the compression options instances and free the
69
+ * wrapped grpc compression options. */
70
+ static void grpc_rb_compression_options_free(void* p) {
71
+ grpc_rb_compression_options_free_internal(p);
72
+ grpc_ruby_shutdown();
73
+ }
74
+
75
+ /* Ruby recognized data type for the CompressionOptions class. */
76
+ static rb_data_type_t grpc_rb_compression_options_data_type = {
77
+ "grpc_compression_options",
78
+ {NULL,
79
+ grpc_rb_compression_options_free,
80
+ GRPC_RB_MEMSIZE_UNAVAILABLE,
81
+ {NULL, NULL}},
82
+ NULL,
83
+ NULL,
84
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
85
+ RUBY_TYPED_FREE_IMMEDIATELY
86
+ #endif
87
+ };
88
+
89
+ /* Allocates CompressionOptions instances.
90
+ Allocate the wrapped grpc compression options and
91
+ initialize it here too. */
92
+ static VALUE grpc_rb_compression_options_alloc(VALUE cls) {
93
+ grpc_ruby_init();
94
+ grpc_rb_compression_options* wrapper = NULL;
95
+
96
+ wrapper = gpr_malloc(sizeof(grpc_rb_compression_options));
97
+ wrapper->wrapped = NULL;
98
+ wrapper->wrapped = gpr_malloc(sizeof(grpc_compression_options));
99
+ grpc_compression_options_init(wrapper->wrapped);
100
+
101
+ return TypedData_Wrap_Struct(cls, &grpc_rb_compression_options_data_type,
102
+ wrapper);
103
+ }
104
+
105
+ /* Disables a compression algorithm, given the GRPC core internal number of a
106
+ * compression algorithm. */
107
+ VALUE grpc_rb_compression_options_disable_compression_algorithm_internal(
108
+ VALUE self, VALUE algorithm_to_disable) {
109
+ grpc_compression_algorithm compression_algorithm = 0;
110
+ grpc_rb_compression_options* wrapper = NULL;
111
+
112
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
113
+ &grpc_rb_compression_options_data_type, wrapper);
114
+ compression_algorithm =
115
+ (grpc_compression_algorithm)NUM2INT(algorithm_to_disable);
116
+
117
+ grpc_compression_options_disable_algorithm(wrapper->wrapped,
118
+ compression_algorithm);
119
+
120
+ return Qnil;
121
+ }
122
+
123
+ /* Gets the compression internal enum value of a compression level given its
124
+ * name. */
125
+ grpc_compression_level grpc_rb_compression_options_level_name_to_value_internal(
126
+ VALUE level_name) {
127
+ Check_Type(level_name, T_SYMBOL);
128
+
129
+ /* Check the compression level of the name passed in, and see which macro
130
+ * from the GRPC core header files match. */
131
+ if (id_compress_level_none == SYM2ID(level_name)) {
132
+ return GRPC_COMPRESS_LEVEL_NONE;
133
+ } else if (id_compress_level_low == SYM2ID(level_name)) {
134
+ return GRPC_COMPRESS_LEVEL_LOW;
135
+ } else if (id_compress_level_medium == SYM2ID(level_name)) {
136
+ return GRPC_COMPRESS_LEVEL_MED;
137
+ } else if (id_compress_level_high == SYM2ID(level_name)) {
138
+ return GRPC_COMPRESS_LEVEL_HIGH;
139
+ }
140
+
141
+ rb_raise(rb_eArgError,
142
+ "Unrecognized compression level name."
143
+ "Valid compression level names are none, low, medium, and high.");
144
+
145
+ /* Dummy return statement. */
146
+ return GRPC_COMPRESS_LEVEL_NONE;
147
+ }
148
+
149
+ /* Sets the default compression level, given the name of a compression level.
150
+ * Throws an error if no algorithm matched. */
151
+ void grpc_rb_compression_options_set_default_level(
152
+ grpc_compression_options* options, VALUE new_level_name) {
153
+ options->default_level.level =
154
+ grpc_rb_compression_options_level_name_to_value_internal(new_level_name);
155
+ options->default_level.is_set = 1;
156
+ }
157
+
158
+ /* Gets the internal value of a compression algorithm suitable as the value
159
+ * in a GRPC core channel arguments hash.
160
+ * algorithm_value is an out parameter.
161
+ * Raises an error if the name of the algorithm passed in is invalid. */
162
+ void grpc_rb_compression_options_algorithm_name_to_value_internal(
163
+ grpc_compression_algorithm* algorithm_value, VALUE algorithm_name) {
164
+ grpc_slice name_slice;
165
+ VALUE algorithm_name_as_string = Qnil;
166
+
167
+ Check_Type(algorithm_name, T_SYMBOL);
168
+
169
+ /* Convert the algorithm symbol to a ruby string, so that we can get the
170
+ * correct C string out of it. */
171
+ algorithm_name_as_string = rb_funcall(algorithm_name, rb_intern("to_s"), 0);
172
+
173
+ name_slice =
174
+ grpc_slice_from_copied_buffer(RSTRING_PTR(algorithm_name_as_string),
175
+ RSTRING_LEN(algorithm_name_as_string));
176
+
177
+ /* Raise an error if the name isn't recognized as a compression algorithm by
178
+ * the algorithm parse function
179
+ * in GRPC core. */
180
+ if (!grpc_compression_algorithm_parse(name_slice, algorithm_value)) {
181
+ char* name_slice_str = grpc_slice_to_c_string(name_slice);
182
+ char* error_message_str = NULL;
183
+ VALUE error_message_ruby_str = Qnil;
184
+ GPR_ASSERT(gpr_asprintf(&error_message_str,
185
+ "Invalid compression algorithm name: %s",
186
+ name_slice_str) != -1);
187
+ gpr_free(name_slice_str);
188
+ error_message_ruby_str =
189
+ rb_str_new(error_message_str, strlen(error_message_str));
190
+ gpr_free(error_message_str);
191
+ rb_raise(rb_eNameError, "%s", StringValueCStr(error_message_ruby_str));
192
+ }
193
+
194
+ grpc_slice_unref(name_slice);
195
+ }
196
+
197
+ /* Indicates whether a given algorithm is enabled on this instance, given the
198
+ * readable algorithm name. */
199
+ VALUE grpc_rb_compression_options_is_algorithm_enabled(VALUE self,
200
+ VALUE algorithm_name) {
201
+ grpc_rb_compression_options* wrapper = NULL;
202
+ grpc_compression_algorithm internal_algorithm_value;
203
+
204
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
205
+ &grpc_rb_compression_options_data_type, wrapper);
206
+ grpc_rb_compression_options_algorithm_name_to_value_internal(
207
+ &internal_algorithm_value, algorithm_name);
208
+
209
+ if (grpc_compression_options_is_algorithm_enabled(wrapper->wrapped,
210
+ internal_algorithm_value)) {
211
+ return Qtrue;
212
+ }
213
+ return Qfalse;
214
+ }
215
+
216
+ /* Sets the default algorithm to the name of the algorithm passed in.
217
+ * Raises an error if the name is not a valid compression algorithm name. */
218
+ void grpc_rb_compression_options_set_default_algorithm(
219
+ grpc_compression_options* options, VALUE algorithm_name) {
220
+ grpc_rb_compression_options_algorithm_name_to_value_internal(
221
+ &options->default_algorithm.algorithm, algorithm_name);
222
+ options->default_algorithm.is_set = 1;
223
+ }
224
+
225
+ /* Disables an algorithm on the current instance, given the name of an
226
+ * algorithm.
227
+ * Fails if the algorithm name is invalid. */
228
+ void grpc_rb_compression_options_disable_algorithm(
229
+ grpc_compression_options* compression_options, VALUE algorithm_name) {
230
+ grpc_compression_algorithm internal_algorithm_value;
231
+
232
+ grpc_rb_compression_options_algorithm_name_to_value_internal(
233
+ &internal_algorithm_value, algorithm_name);
234
+ grpc_compression_options_disable_algorithm(compression_options,
235
+ internal_algorithm_value);
236
+ }
237
+
238
+ /* Provides a ruby hash of GRPC core channel argument key-values that
239
+ * correspond to the compression settings on this instance. */
240
+ VALUE grpc_rb_compression_options_to_hash(VALUE self) {
241
+ grpc_rb_compression_options* wrapper = NULL;
242
+ grpc_compression_options* compression_options = NULL;
243
+ VALUE channel_arg_hash = rb_hash_new();
244
+ VALUE key = Qnil;
245
+ VALUE value = Qnil;
246
+
247
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
248
+ &grpc_rb_compression_options_data_type, wrapper);
249
+ compression_options = wrapper->wrapped;
250
+
251
+ /* Add key-value pairs to the new Ruby hash. It can be used
252
+ * as GRPC core channel arguments. */
253
+ if (compression_options->default_level.is_set) {
254
+ key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL);
255
+ value = INT2NUM((int)compression_options->default_level.level);
256
+ rb_hash_aset(channel_arg_hash, key, value);
257
+ }
258
+
259
+ if (compression_options->default_algorithm.is_set) {
260
+ key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM);
261
+ value = INT2NUM((int)compression_options->default_algorithm.algorithm);
262
+ rb_hash_aset(channel_arg_hash, key, value);
263
+ }
264
+
265
+ key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET);
266
+ value = INT2NUM((int)compression_options->enabled_algorithms_bitset);
267
+ rb_hash_aset(channel_arg_hash, key, value);
268
+
269
+ return channel_arg_hash;
270
+ }
271
+
272
+ /* Converts an internal enum level value to a readable level name.
273
+ * Fails if the level value is invalid. */
274
+ VALUE grpc_rb_compression_options_level_value_to_name_internal(
275
+ grpc_compression_level compression_value) {
276
+ switch (compression_value) {
277
+ case GRPC_COMPRESS_LEVEL_NONE:
278
+ return ID2SYM(id_compress_level_none);
279
+ case GRPC_COMPRESS_LEVEL_LOW:
280
+ return ID2SYM(id_compress_level_low);
281
+ case GRPC_COMPRESS_LEVEL_MED:
282
+ return ID2SYM(id_compress_level_medium);
283
+ case GRPC_COMPRESS_LEVEL_HIGH:
284
+ return ID2SYM(id_compress_level_high);
285
+ default:
286
+ rb_raise(
287
+ rb_eArgError,
288
+ "Failed to convert compression level value to name for value: %d",
289
+ (int)compression_value);
290
+ /* return something to avoid compiler error about no return */
291
+ return Qnil;
292
+ }
293
+ }
294
+
295
+ /* Converts an algorithm internal enum value to a readable name.
296
+ * Fails if the enum value is invalid. */
297
+ VALUE grpc_rb_compression_options_algorithm_value_to_name_internal(
298
+ grpc_compression_algorithm internal_value) {
299
+ char* algorithm_name = NULL;
300
+
301
+ if (!grpc_compression_algorithm_name(internal_value, &algorithm_name)) {
302
+ rb_raise(rb_eArgError, "Failed to convert algorithm value to name");
303
+ }
304
+
305
+ return ID2SYM(rb_intern(algorithm_name));
306
+ }
307
+
308
+ /* Gets the readable name of the default algorithm if one has been set.
309
+ * Returns nil if no algorithm has been set. */
310
+ VALUE grpc_rb_compression_options_get_default_algorithm(VALUE self) {
311
+ grpc_compression_algorithm internal_value;
312
+ grpc_rb_compression_options* wrapper = NULL;
313
+
314
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
315
+ &grpc_rb_compression_options_data_type, wrapper);
316
+
317
+ if (wrapper->wrapped->default_algorithm.is_set) {
318
+ internal_value = wrapper->wrapped->default_algorithm.algorithm;
319
+ return grpc_rb_compression_options_algorithm_value_to_name_internal(
320
+ internal_value);
321
+ }
322
+
323
+ return Qnil;
324
+ }
325
+
326
+ /* Gets the internal value of the default compression level that is to be passed
327
+ * to the GRPC core as a channel argument value.
328
+ * A nil return value means that it hasn't been set. */
329
+ VALUE grpc_rb_compression_options_get_default_level(VALUE self) {
330
+ grpc_compression_level internal_value;
331
+ grpc_rb_compression_options* wrapper = NULL;
332
+
333
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
334
+ &grpc_rb_compression_options_data_type, wrapper);
335
+
336
+ if (wrapper->wrapped->default_level.is_set) {
337
+ internal_value = wrapper->wrapped->default_level.level;
338
+ return grpc_rb_compression_options_level_value_to_name_internal(
339
+ internal_value);
340
+ }
341
+
342
+ return Qnil;
343
+ }
344
+
345
+ /* Gets a list of the disabled algorithms as readable names.
346
+ * Returns an empty list if no algorithms have been disabled. */
347
+ VALUE grpc_rb_compression_options_get_disabled_algorithms(VALUE self) {
348
+ VALUE disabled_algorithms = rb_ary_new();
349
+ grpc_compression_algorithm internal_value;
350
+ grpc_rb_compression_options* wrapper = NULL;
351
+
352
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
353
+ &grpc_rb_compression_options_data_type, wrapper);
354
+
355
+ for (internal_value = GRPC_COMPRESS_NONE;
356
+ internal_value < GRPC_COMPRESS_ALGORITHMS_COUNT; internal_value++) {
357
+ if (!grpc_compression_options_is_algorithm_enabled(wrapper->wrapped,
358
+ internal_value)) {
359
+ rb_ary_push(disabled_algorithms,
360
+ grpc_rb_compression_options_algorithm_value_to_name_internal(
361
+ internal_value));
362
+ }
363
+ }
364
+ return disabled_algorithms;
365
+ }
366
+
367
+ /* Initializes the compression options wrapper.
368
+ * Takes an optional hash parameter.
369
+ *
370
+ * Example call-seq:
371
+ * options = CompressionOptions.new(
372
+ * default_level: :none,
373
+ * disabled_algorithms: [:gzip]
374
+ * )
375
+ * channel_arg hash = Hash.new[...]
376
+ * channel_arg_hash_with_compression_options = channel_arg_hash.merge(options)
377
+ */
378
+ VALUE grpc_rb_compression_options_init(int argc, VALUE* argv, VALUE self) {
379
+ grpc_rb_compression_options* wrapper = NULL;
380
+ VALUE default_algorithm = Qnil;
381
+ VALUE default_level = Qnil;
382
+ VALUE disabled_algorithms = Qnil;
383
+ VALUE algorithm_name = Qnil;
384
+ VALUE hash_arg = Qnil;
385
+
386
+ rb_scan_args(argc, argv, "01", &hash_arg);
387
+
388
+ /* Check if the hash parameter was passed, or if invalid arguments were
389
+ * passed. */
390
+ if (hash_arg == Qnil) {
391
+ return self;
392
+ } else if (TYPE(hash_arg) != T_HASH || argc > 1) {
393
+ rb_raise(rb_eArgError,
394
+ "Invalid arguments. Expecting optional hash parameter");
395
+ }
396
+
397
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
398
+ &grpc_rb_compression_options_data_type, wrapper);
399
+
400
+ /* Set the default algorithm if one was chosen. */
401
+ default_algorithm =
402
+ rb_hash_aref(hash_arg, ID2SYM(rb_intern("default_algorithm")));
403
+ if (default_algorithm != Qnil) {
404
+ grpc_rb_compression_options_set_default_algorithm(wrapper->wrapped,
405
+ default_algorithm);
406
+ }
407
+
408
+ /* Set the default level if one was chosen. */
409
+ default_level = rb_hash_aref(hash_arg, ID2SYM(rb_intern("default_level")));
410
+ if (default_level != Qnil) {
411
+ grpc_rb_compression_options_set_default_level(wrapper->wrapped,
412
+ default_level);
413
+ }
414
+
415
+ /* Set the disabled algorithms if any were chosen. */
416
+ disabled_algorithms =
417
+ rb_hash_aref(hash_arg, ID2SYM(rb_intern("disabled_algorithms")));
418
+ if (disabled_algorithms != Qnil) {
419
+ Check_Type(disabled_algorithms, T_ARRAY);
420
+
421
+ for (int i = 0; i < RARRAY_LEN(disabled_algorithms); i++) {
422
+ algorithm_name = rb_ary_entry(disabled_algorithms, i);
423
+ grpc_rb_compression_options_disable_algorithm(wrapper->wrapped,
424
+ algorithm_name);
425
+ }
426
+ }
427
+
428
+ return self;
429
+ }
430
+
431
+ void Init_grpc_compression_options() {
432
+ grpc_rb_cCompressionOptions = rb_define_class_under(
433
+ grpc_rb_mGrpcCore, "CompressionOptions", rb_cObject);
434
+
435
+ /* Allocates an object managed by the ruby runtime. */
436
+ rb_define_alloc_func(grpc_rb_cCompressionOptions,
437
+ grpc_rb_compression_options_alloc);
438
+
439
+ /* Initializes the ruby wrapper. #new method takes an optional hash argument.
440
+ */
441
+ rb_define_method(grpc_rb_cCompressionOptions, "initialize",
442
+ grpc_rb_compression_options_init, -1);
443
+
444
+ /* Methods for getting the default algorithm, default level, and disabled
445
+ * algorithms as readable names. */
446
+ rb_define_method(grpc_rb_cCompressionOptions, "default_algorithm",
447
+ grpc_rb_compression_options_get_default_algorithm, 0);
448
+ rb_define_method(grpc_rb_cCompressionOptions, "default_level",
449
+ grpc_rb_compression_options_get_default_level, 0);
450
+ rb_define_method(grpc_rb_cCompressionOptions, "disabled_algorithms",
451
+ grpc_rb_compression_options_get_disabled_algorithms, 0);
452
+
453
+ /* Determines whether or not an algorithm is enabled, given a readable
454
+ * algorithm name.*/
455
+ rb_define_method(grpc_rb_cCompressionOptions, "algorithm_enabled?",
456
+ grpc_rb_compression_options_is_algorithm_enabled, 1);
457
+
458
+ /* Provides a hash of the compression settings suitable
459
+ * for passing to server or channel args. */
460
+ rb_define_method(grpc_rb_cCompressionOptions, "to_hash",
461
+ grpc_rb_compression_options_to_hash, 0);
462
+ rb_define_alias(grpc_rb_cCompressionOptions, "to_channel_arg_hash",
463
+ "to_hash");
464
+
465
+ /* Ruby ids for the names of the different compression levels. */
466
+ id_compress_level_none = rb_intern("none");
467
+ id_compress_level_low = rb_intern("low");
468
+ id_compress_level_medium = rb_intern("medium");
469
+ id_compress_level_high = rb_intern("high");
470
+ }