blob: 02bb3a5ddffb05f680cdfbdf7167d8f3a42e80a3 [file] [log] [blame]
Alexander Polcyn0dccf102016-06-27 13:11:07 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <ruby/ruby.h>
35
36#include "rb_compression_options.h"
37#include "rb_grpc_imports.generated.h"
38
39#include <grpc/compression.h>
40#include <grpc/grpc.h>
41#include <grpc/impl/codegen/alloc.h>
42#include <grpc/impl/codegen/compression_types.h>
43#include <grpc/impl/codegen/grpc_types.h>
44#include <string.h>
45
46#include "rb_grpc.h"
47
48static VALUE grpc_rb_cCompressionOptions = Qnil;
49
Alexander Polcyn6c4709e2016-07-11 13:14:07 -070050/* Ruby Ids for the names of valid compression levels. */
51static VALUE id_compress_level_none = Qnil;
52static VALUE id_compress_level_low = Qnil;
53static VALUE id_compress_level_medium = Qnil;
54static VALUE id_compress_level_high = Qnil;
55
Alexander Polcyn0dccf102016-06-27 13:11:07 -070056/* grpc_rb_compression_options wraps a grpc_compression_options.
Alexander Polcynd788b452016-07-06 13:58:09 -070057 * It can be used to get the channel argument key-values for specific
58 * compression settings. */
59
60/* Note that ruby objects of this type don't carry any state in other
Alexander Polcyn0dccf102016-06-27 13:11:07 -070061 * Ruby objects and don't have a mark for GC. */
62typedef struct grpc_rb_compression_options {
63 /* The actual compression options that's being wrapped */
64 grpc_compression_options *wrapped;
65} grpc_rb_compression_options;
66
67/* Destroys the compression options instances and free the
68 * wrapped grpc compression options. */
69static void grpc_rb_compression_options_free(void *p) {
70 grpc_rb_compression_options *wrapper = NULL;
71 if (p == NULL) {
72 return;
73 };
74 wrapper = (grpc_rb_compression_options *)p;
75
76 if (wrapper->wrapped != NULL) {
77 gpr_free(wrapper->wrapped);
78 wrapper->wrapped = NULL;
79 }
80
81 xfree(p);
82}
83
84/* Ruby recognized data type for the CompressionOptions class. */
85static rb_data_type_t grpc_rb_compression_options_data_type = {
86 "grpc_compression_options",
87 {NULL,
88 grpc_rb_compression_options_free,
89 GRPC_RB_MEMSIZE_UNAVAILABLE,
90 {NULL, NULL}},
91 NULL,
92 NULL,
93#ifdef RUBY_TYPED_FREE_IMMEDIATELY
94 RUBY_TYPED_FREE_IMMEDIATELY
95#endif
96};
97
98/* Allocates CompressionOptions instances.
99 Allocate the wrapped grpc compression options and
100 initialize it here too. */
101static VALUE grpc_rb_compression_options_alloc(VALUE cls) {
102 grpc_rb_compression_options *wrapper =
103 gpr_malloc(sizeof(grpc_rb_compression_options));
104 wrapper->wrapped = NULL;
105 wrapper->wrapped = gpr_malloc(sizeof(grpc_compression_options));
106 grpc_compression_options_init(wrapper->wrapped);
107
108 return TypedData_Wrap_Struct(cls, &grpc_rb_compression_options_data_type,
109 wrapper);
110}
111
112/* Disables a compression algorithm, given the GRPC core internal number of a
113 * compression algorithm. */
114VALUE grpc_rb_compression_options_disable_compression_algorithm_internal(
115 VALUE self, VALUE algorithm_to_disable) {
116 grpc_compression_algorithm compression_algorithm = 0;
117 grpc_rb_compression_options *wrapper = NULL;
118
119 TypedData_Get_Struct(self, grpc_rb_compression_options,
120 &grpc_rb_compression_options_data_type, wrapper);
121 compression_algorithm =
122 (grpc_compression_algorithm)NUM2INT(algorithm_to_disable);
123
124 grpc_compression_options_disable_algorithm(wrapper->wrapped,
125 compression_algorithm);
126
127 return Qnil;
128}
129
Alexander Polcynd788b452016-07-06 13:58:09 -0700130/* Gets the compression internal enum value of a compression level given its
131 * name. */
132grpc_compression_level grpc_rb_compression_options_level_name_to_value_internal(
133 VALUE level_name) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700134 Check_Type(level_name, T_SYMBOL);
135
Alexander Polcynd788b452016-07-06 13:58:09 -0700136 /* Check the compression level of the name passed in, and see which macro
137 * from the GRPC core header files match. */
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700138 if (id_compress_level_none == SYM2ID(level_name)) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700139 return GRPC_COMPRESS_LEVEL_NONE;
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700140 } else if (id_compress_level_low == SYM2ID(level_name)) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700141 return GRPC_COMPRESS_LEVEL_LOW;
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700142 } else if (id_compress_level_medium == SYM2ID(level_name)) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700143 return GRPC_COMPRESS_LEVEL_MED;
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700144 } else if (id_compress_level_high == SYM2ID(level_name)) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700145 return GRPC_COMPRESS_LEVEL_HIGH;
Alexander Polcynd788b452016-07-06 13:58:09 -0700146 }
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700147
148 rb_raise(rb_eArgError,
149 "Unrecognized compression level name."
150 "Valid compression level names are none, low, medium, and high.");
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700151}
152
Alexander Polcynd788b452016-07-06 13:58:09 -0700153/* Wrapper over grpc_rb_compression_options_level_name_to_value available for
154 * use or testing.
155 * Raises an exception for unrecognized level names. */
156VALUE grpc_rb_compression_options_level_name_to_value(VALUE self,
157 VALUE level_name) {
Alex Polcynb72cc3d2016-07-10 19:08:54 -0700158 (void)self;
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700159 Check_Type(level_name, T_SYMBOL);
160
Alexander Polcynd788b452016-07-06 13:58:09 -0700161 return INT2NUM((int)grpc_rb_compression_options_level_name_to_value_internal(
162 level_name));
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700163}
164
165/* Sets the default compression level, given the name of a compression level.
166 * Throws an error if no algorithm matched. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700167void grpc_rb_compression_options_set_default_level(
168 grpc_compression_options *options, VALUE new_level_name) {
169 options->default_level.level =
170 grpc_rb_compression_options_level_name_to_value_internal(new_level_name);
171 options->default_level.is_set = 1;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700172}
173
174/* Gets the internal value of a compression algorithm suitable as the value
175 * in a GRPC core channel arguments hash.
Alexander Polcynd788b452016-07-06 13:58:09 -0700176 * algorithm_value is an out parameter.
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700177 * Raises an error if the name of the algorithm passed in is invalid. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700178void grpc_rb_compression_options_algorithm_name_to_value_internal(
179 grpc_compression_algorithm *algorithm_value, VALUE algorithm_name) {
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700180 char *name_str = NULL;
181 long name_len = 0;
Alexander Polcynd788b452016-07-06 13:58:09 -0700182 VALUE algorithm_name_as_string = Qnil;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700183
Alexander Polcynd788b452016-07-06 13:58:09 -0700184 Check_Type(algorithm_name, T_SYMBOL);
185
186 /* Convert the algorithm symbol to a ruby string, so that we can get the
187 * correct C string out of it. */
188 algorithm_name_as_string = rb_funcall(algorithm_name, rb_intern("to_s"), 0);
189
190 name_str = RSTRING_PTR(algorithm_name_as_string);
191 name_len = RSTRING_LEN(algorithm_name_as_string);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700192
193 /* Raise an error if the name isn't recognized as a compression algorithm by
194 * the algorithm parse function
195 * in GRPC core. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700196 if (!grpc_compression_algorithm_parse(name_str, name_len, algorithm_value)) {
197 rb_raise(rb_eNameError, "Invalid compression algorithm name: %s",
198 StringValueCStr(algorithm_name_as_string));
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700199 }
200}
201
Alexander Polcynd788b452016-07-06 13:58:09 -0700202/* Wrapper around algorithm_name_to_value_internal function available for use or
203 * testing. */
204VALUE grpc_rb_compression_options_algorithm_name_to_value(
205 VALUE self, VALUE algorithm_name) {
206 grpc_compression_algorithm algorithm_value;
Alex Polcynb72cc3d2016-07-10 19:08:54 -0700207 (void)self;
Alexander Polcynd788b452016-07-06 13:58:09 -0700208 grpc_rb_compression_options_algorithm_name_to_value_internal(&algorithm_value,
209 algorithm_name);
210
211 return INT2NUM((int)algorithm_value);
212}
213
214/* Indicates whether a given algorithm is enabled on this instance, given the
215 * readable algorithm name. */
216VALUE grpc_rb_compression_options_is_algorithm_enabled(VALUE self,
217 VALUE algorithm_name) {
218 grpc_rb_compression_options *wrapper = NULL;
219 grpc_compression_algorithm internal_algorithm_value;
220
221 TypedData_Get_Struct(self, grpc_rb_compression_options,
222 &grpc_rb_compression_options_data_type, wrapper);
223 grpc_rb_compression_options_algorithm_name_to_value_internal(
224 &internal_algorithm_value, algorithm_name);
225
226 if (grpc_compression_options_is_algorithm_enabled(wrapper->wrapped,
227 internal_algorithm_value)) {
228 return Qtrue;
229 }
230 return Qfalse;
231}
232
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700233/* Sets the default algorithm to the name of the algorithm passed in.
234 * Raises an error if the name is not a valid compression algorithm name. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700235void grpc_rb_compression_options_set_default_algorithm(
236 grpc_compression_options *options, VALUE algorithm_name) {
237 grpc_rb_compression_options_algorithm_name_to_value_internal(
238 &options->default_algorithm.algorithm, algorithm_name);
239 options->default_algorithm.is_set = 1;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700240}
241
Alexander Polcynd788b452016-07-06 13:58:09 -0700242/* Disables an algorithm on the current instance, given the name of an
243 * algorithm.
244 * Fails if the algorithm name is invalid. */
245void grpc_rb_compression_options_disable_algorithm(
246 grpc_compression_options *compression_options, VALUE algorithm_name) {
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700247 grpc_compression_algorithm internal_algorithm_value;
248
Alexander Polcynd788b452016-07-06 13:58:09 -0700249 grpc_rb_compression_options_algorithm_name_to_value_internal(
250 &internal_algorithm_value, algorithm_name);
251 grpc_compression_options_disable_algorithm(compression_options,
252 internal_algorithm_value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700253}
254
255/* Provides a ruby hash of GRPC core channel argument key-values that
256 * correspond to the compression settings on this instance. */
257VALUE grpc_rb_compression_options_to_hash(VALUE self) {
258 grpc_rb_compression_options *wrapper = NULL;
259 grpc_compression_options *compression_options = NULL;
Alexander Polcynd788b452016-07-06 13:58:09 -0700260 VALUE channel_arg_hash = rb_hash_new();
261 VALUE key = Qnil;
262 VALUE value = Qnil;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700263
264 TypedData_Get_Struct(self, grpc_rb_compression_options,
265 &grpc_rb_compression_options_data_type, wrapper);
266 compression_options = wrapper->wrapped;
267
268 /* Add key-value pairs to the new Ruby hash. It can be used
269 * as GRPC core channel arguments. */
270 if (compression_options->default_level.is_set) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700271 key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL);
272 value = INT2NUM((int)compression_options->default_level.level);
273 rb_hash_aset(channel_arg_hash, key, value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700274 }
275
276 if (compression_options->default_algorithm.is_set) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700277 key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM);
278 value = INT2NUM((int)compression_options->default_algorithm.algorithm);
279 rb_hash_aset(channel_arg_hash, key, value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700280 }
281
Alexander Polcynd788b452016-07-06 13:58:09 -0700282 key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET);
283 value = INT2NUM((int)compression_options->enabled_algorithms_bitset);
284 rb_hash_aset(channel_arg_hash, key, value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700285
286 return channel_arg_hash;
287}
288
Alexander Polcynd788b452016-07-06 13:58:09 -0700289/* Converts an internal enum level value to a readable level name.
290 * Fails if the level value is invalid. */
291VALUE grpc_rb_compression_options_level_value_to_name_internal(
292 grpc_compression_level compression_value) {
293 switch (compression_value) {
294 case GRPC_COMPRESS_LEVEL_NONE:
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700295 return ID2SYM(id_compress_level_none);
Alexander Polcynd788b452016-07-06 13:58:09 -0700296 case GRPC_COMPRESS_LEVEL_LOW:
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700297 return ID2SYM(id_compress_level_low);
Alexander Polcynd788b452016-07-06 13:58:09 -0700298 case GRPC_COMPRESS_LEVEL_MED:
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700299 return ID2SYM(id_compress_level_medium);
Alexander Polcynd788b452016-07-06 13:58:09 -0700300 case GRPC_COMPRESS_LEVEL_HIGH:
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700301 return ID2SYM(id_compress_level_high);
Alexander Polcynd788b452016-07-06 13:58:09 -0700302 default:
303 rb_raise(
304 rb_eArgError,
305 "Failed to convert compression level value to name for value: %d",
306 (int)compression_value);
307 }
308}
309
310/* Wrapper of internal method that makes it available for use and testing. */
311VALUE grpc_rb_compression_options_level_value_to_name(VALUE self,
312 VALUE compression_value) {
Alex Polcynb72cc3d2016-07-10 19:08:54 -0700313 (void)self;
Alexander Polcynd788b452016-07-06 13:58:09 -0700314 Check_Type(compression_value, T_FIXNUM);
315 return grpc_rb_compression_options_level_value_to_name_internal(
316 (grpc_compression_level)NUM2INT(compression_value));
317}
318
319/* Converts an algorithm internal enum value to a readable name.
320 * Fails if the enum value is invalid. */
321VALUE grpc_rb_compression_options_algorithm_value_to_name_internal(
322 grpc_compression_algorithm internal_value) {
323 char *algorithm_name = NULL;
324
325 if (!grpc_compression_algorithm_name(internal_value, &algorithm_name)) {
326 rb_raise(rb_eArgError, "Failed to convert algorithm value to name");
327 }
328
329 return ID2SYM(rb_intern(algorithm_name));
330}
331
332/* Wrapper of algorithm_to_name internal function available for ues and testing.
333 */
334VALUE grpc_rb_compression_options_algorithm_value_to_name(
335 VALUE self, VALUE algorithm_value) {
Alex Polcynb72cc3d2016-07-10 19:08:54 -0700336 grpc_compression_algorithm algorithm_internal_value =
Alexander Polcynd788b452016-07-06 13:58:09 -0700337 (grpc_compression_algorithm)NUM2INT(algorithm_value);
Alex Polcynb72cc3d2016-07-10 19:08:54 -0700338 (void)self;
Alexander Polcynd788b452016-07-06 13:58:09 -0700339
340 return grpc_rb_compression_options_algorithm_value_to_name_internal(
341 algorithm_internal_value);
342}
343
344/* Gets the internal value of the default compression level that is to be passed
345 * to the GRPC core as a channel argument value.
346 * A nil return value means that it hasn't been set. */
347VALUE grpc_rb_compression_options_get_default_algorithm_internal_value(
348 VALUE self) {
349 grpc_rb_compression_options *wrapper = NULL;
350
351 TypedData_Get_Struct(self, grpc_rb_compression_options,
352 &grpc_rb_compression_options_data_type, wrapper);
353
354 if (wrapper->wrapped->default_algorithm.is_set) {
355 return INT2NUM(wrapper->wrapped->default_algorithm.algorithm);
356 }
357 return Qnil;
358}
359
360/* Gets the readable name of the default algorithm if one has been set.
361 * Returns nil if no algorithm has been set. */
362VALUE grpc_rb_compression_options_get_default_algorithm(VALUE self) {
363 VALUE algorithm_value =
364 grpc_rb_compression_options_get_default_algorithm_internal_value(self);
365
366 if (RTEST(algorithm_value)) {
367 return grpc_rb_compression_options_algorithm_value_to_name(self,
368 algorithm_value);
369 }
370
371 return Qnil;
372}
373
374/* Gets the internal enum value of the default algorithm if one has been set.
375 * Returns nil if no default algorithm has been set. */
376VALUE grpc_rb_compression_options_get_default_level_internal_value(VALUE self) {
377 grpc_rb_compression_options *wrapper = NULL;
378
379 TypedData_Get_Struct(self, grpc_rb_compression_options,
380 &grpc_rb_compression_options_data_type, wrapper);
381
382 if (wrapper->wrapped->default_level.is_set) {
383 return INT2NUM((int)wrapper->wrapped->default_level.level);
384 }
385 return Qnil;
386}
387
388/* Gets the internal value of the default compression level that is to be passed
389 * to the GRPC core as a channel argument value.
390 * A nil return value means that it hasn't been set. */
391VALUE grpc_rb_compression_options_get_default_level(VALUE self) {
392 grpc_compression_level internal_value;
393 VALUE ruby_value =
394 grpc_rb_compression_options_get_default_level_internal_value(self);
395
396 if (RTEST(ruby_value)) {
397 internal_value = (grpc_compression_level)NUM2INT(ruby_value);
398 return grpc_rb_compression_options_level_value_to_name_internal(
399 internal_value);
400 }
401
402 return Qnil;
403}
404
405/* Gets a list of the disabled algorithms as readable names.
406 * Returns an empty list of no algorithms have been disabled. */
407VALUE grpc_rb_compression_options_get_disabled_algorithms(VALUE self) {
408 VALUE disabled_algorithms = rb_ary_new();
409 grpc_compression_algorithm internal_value;
410 grpc_rb_compression_options *wrapper = NULL;
411
412 TypedData_Get_Struct(self, grpc_rb_compression_options,
413 &grpc_rb_compression_options_data_type, wrapper);
414
415 for (internal_value = GRPC_COMPRESS_NONE;
416 internal_value < GRPC_COMPRESS_ALGORITHMS_COUNT; internal_value++) {
417 if (!grpc_compression_options_is_algorithm_enabled(wrapper->wrapped,
418 internal_value)) {
419 rb_ary_push(disabled_algorithms,
420 grpc_rb_compression_options_algorithm_value_to_name_internal(
421 internal_value));
422 }
423 }
424 return disabled_algorithms;
425}
426
427/* Provides a bitset as a ruby number that is suitable to pass to
428 * the GRPC core as a channel argument to enable compression algorithms. */
429VALUE grpc_rb_compression_options_get_enabled_algorithms_bitset(VALUE self) {
430 grpc_rb_compression_options *wrapper = NULL;
431
432 TypedData_Get_Struct(self, grpc_rb_compression_options,
433 &grpc_rb_compression_options_data_type, wrapper);
434 return INT2NUM((int)wrapper->wrapped->enabled_algorithms_bitset);
435}
436
437/* Initializes the compression options wrapper.
438 * Takes an optional hash parameter.
439 *
440 * Example call-seq:
441 * options = CompressionOptions.new(
442 * default_level: :none,
443 * disabled_algorithms: [:gzip]
444 * )
445 * channel_arg hash = Hash.new[...]
446 * channel_arg_hash_with_compression_options = channel_arg_hash.merge(options)
447 */
448VALUE grpc_rb_compression_options_init(int argc, VALUE *argv, VALUE self) {
449 grpc_rb_compression_options *wrapper = NULL;
450 VALUE default_algorithm = Qnil;
451 VALUE default_level = Qnil;
452 VALUE disabled_algorithms = Qnil;
453 VALUE algorithm_name = Qnil;
454 VALUE hash_arg = Qnil;
455
456 rb_scan_args(argc, argv, "01", &hash_arg);
457
458 /* Check if the hash parameter was passed, or if invalid arguments were
459 * passed. */
460 if (hash_arg == Qnil) {
461 return self;
462 } else if (TYPE(hash_arg) != T_HASH || argc > 1) {
463 rb_raise(rb_eArgError,
464 "Invalid arguments. Expecting optional hash parameter");
465 }
466
467 TypedData_Get_Struct(self, grpc_rb_compression_options,
468 &grpc_rb_compression_options_data_type, wrapper);
469
470 /* Set the default algorithm if one was chosen. */
471 default_algorithm =
472 rb_hash_aref(hash_arg, ID2SYM(rb_intern("default_algorithm")));
473 if (default_algorithm != Qnil) {
474 grpc_rb_compression_options_set_default_algorithm(wrapper->wrapped,
475 default_algorithm);
476 }
477
478 /* Set the default level if one was chosen. */
479 default_level = rb_hash_aref(hash_arg, ID2SYM(rb_intern("default_level")));
480 if (default_level != Qnil) {
481 grpc_rb_compression_options_set_default_level(wrapper->wrapped,
482 default_level);
483 }
484
485 /* Set the disabled algorithms if any were chosen. */
486 disabled_algorithms =
487 rb_hash_aref(hash_arg, ID2SYM(rb_intern("disabled_algorithms")));
488 if (disabled_algorithms != Qnil) {
489 Check_Type(disabled_algorithms, T_ARRAY);
490
491 for (int i = 0; i < RARRAY_LEN(disabled_algorithms); i++) {
492 algorithm_name = rb_ary_entry(disabled_algorithms, i);
493 grpc_rb_compression_options_disable_algorithm(wrapper->wrapped,
494 algorithm_name);
495 }
496 }
497
498 return self;
499}
500
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700501void Init_grpc_compression_options() {
502 grpc_rb_cCompressionOptions = rb_define_class_under(
503 grpc_rb_mGrpcCore, "CompressionOptions", rb_cObject);
504
505 /* Allocates an object managed by the ruby runtime. */
506 rb_define_alloc_func(grpc_rb_cCompressionOptions,
507 grpc_rb_compression_options_alloc);
508
Alexander Polcynd788b452016-07-06 13:58:09 -0700509 /* Initializes the ruby wrapper. #new method takes an optional hash argument.
510 */
511 rb_define_method(grpc_rb_cCompressionOptions, "initialize",
512 grpc_rb_compression_options_init, -1);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700513
Alexander Polcynd788b452016-07-06 13:58:09 -0700514 /* Gets the bitset of enabled algorithms. */
515 rb_define_method(grpc_rb_cCompressionOptions, "enabled_algorithms_bitset",
516 grpc_rb_compression_options_get_enabled_algorithms_bitset,
517 0);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700518
Alexander Polcynd788b452016-07-06 13:58:09 -0700519 /* Methods for getting the default algorithm, default level, and disabled
520 * algorithms as readable names. */
521 rb_define_method(grpc_rb_cCompressionOptions, "default_algorithm",
522 grpc_rb_compression_options_get_default_algorithm, 0);
523 rb_define_method(grpc_rb_cCompressionOptions, "default_level",
524 grpc_rb_compression_options_get_default_level, 0);
525 rb_define_method(grpc_rb_cCompressionOptions, "disabled_algorithms",
526 grpc_rb_compression_options_get_disabled_algorithms, 0);
527
528 /* Methods for getting the internal enum default algorithm and level enum
529 * values of an instance. */
530 rb_define_method(
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700531 grpc_rb_cCompressionOptions, "default_algorithm_internal_value",
Alexander Polcynd788b452016-07-06 13:58:09 -0700532 grpc_rb_compression_options_get_default_algorithm_internal_value, 0);
533 rb_define_method(grpc_rb_cCompressionOptions, "default_level_internal_value",
534 grpc_rb_compression_options_get_default_level_internal_value,
535 0);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700536
Alexander Polcynd788b452016-07-06 13:58:09 -0700537 /* Determines whether or not an algorithm is enabled, given a readable
538 * algorithm name.*/
539 rb_define_method(grpc_rb_cCompressionOptions, "is_algorithm_enabled",
540 grpc_rb_compression_options_is_algorithm_enabled, 1);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700541
Alexander Polcynd788b452016-07-06 13:58:09 -0700542 /* Methods for converting to and from algorithm enum values and their readable
543 * names. */
544 rb_define_singleton_method(
545 grpc_rb_cCompressionOptions, "algorithm_name_to_value",
546 grpc_rb_compression_options_algorithm_name_to_value, 1);
547 rb_define_singleton_method(
548 grpc_rb_cCompressionOptions, "algorithm_value_to_name",
549 grpc_rb_compression_options_algorithm_value_to_name, 1);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700550
Alexander Polcynd788b452016-07-06 13:58:09 -0700551 /* Methods for converting to and from compression level enum values and their
552 * readable names. */
553 rb_define_singleton_method(grpc_rb_cCompressionOptions, "level_name_to_value",
554 grpc_rb_compression_options_level_name_to_value,
555 1);
556 rb_define_singleton_method(grpc_rb_cCompressionOptions, "level_value_to_name",
557 grpc_rb_compression_options_level_value_to_name,
558 1);
559
560 /* Provides a hash of the compression settings suitable
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700561 * for passing to server or channel args. */
562 rb_define_method(grpc_rb_cCompressionOptions, "to_hash",
563 grpc_rb_compression_options_to_hash, 0);
Alexander Polcynd788b452016-07-06 13:58:09 -0700564 rb_define_alias(grpc_rb_cCompressionOptions, "to_channel_arg_hash",
565 "to_hash");
566
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700567 /* Ruby ids for the names of the different compression levels. */
568 id_compress_level_none = rb_intern("none");
569 id_compress_level_low = rb_intern("low");
570 id_compress_level_medium = rb_intern("medium");
571 id_compress_level_high = rb_intern("high");
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700572}