blob: 2d3be4c7d07940ca3e92f3484b4a8201f72f0e32 [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
50/* grpc_rb_compression_options wraps a grpc_compression_options.
Alexander Polcynd788b452016-07-06 13:58:09 -070051 * It can be used to get the channel argument key-values for specific
52 * compression settings. */
53
54/* Note that ruby objects of this type don't carry any state in other
Alexander Polcyn0dccf102016-06-27 13:11:07 -070055 * Ruby objects and don't have a mark for GC. */
56typedef struct grpc_rb_compression_options {
57 /* The actual compression options that's being wrapped */
58 grpc_compression_options *wrapped;
59} grpc_rb_compression_options;
60
61/* Destroys the compression options instances and free the
62 * wrapped grpc compression options. */
63static void grpc_rb_compression_options_free(void *p) {
64 grpc_rb_compression_options *wrapper = NULL;
65 if (p == NULL) {
66 return;
67 };
68 wrapper = (grpc_rb_compression_options *)p;
69
70 if (wrapper->wrapped != NULL) {
71 gpr_free(wrapper->wrapped);
72 wrapper->wrapped = NULL;
73 }
74
75 xfree(p);
76}
77
78/* Ruby recognized data type for the CompressionOptions class. */
79static rb_data_type_t grpc_rb_compression_options_data_type = {
80 "grpc_compression_options",
81 {NULL,
82 grpc_rb_compression_options_free,
83 GRPC_RB_MEMSIZE_UNAVAILABLE,
84 {NULL, NULL}},
85 NULL,
86 NULL,
87#ifdef RUBY_TYPED_FREE_IMMEDIATELY
88 RUBY_TYPED_FREE_IMMEDIATELY
89#endif
90};
91
92/* Allocates CompressionOptions instances.
93 Allocate the wrapped grpc compression options and
94 initialize it here too. */
95static VALUE grpc_rb_compression_options_alloc(VALUE cls) {
96 grpc_rb_compression_options *wrapper =
97 gpr_malloc(sizeof(grpc_rb_compression_options));
98 wrapper->wrapped = NULL;
99 wrapper->wrapped = gpr_malloc(sizeof(grpc_compression_options));
100 grpc_compression_options_init(wrapper->wrapped);
101
102 return TypedData_Wrap_Struct(cls, &grpc_rb_compression_options_data_type,
103 wrapper);
104}
105
106/* Disables a compression algorithm, given the GRPC core internal number of a
107 * compression algorithm. */
108VALUE grpc_rb_compression_options_disable_compression_algorithm_internal(
109 VALUE self, VALUE algorithm_to_disable) {
110 grpc_compression_algorithm compression_algorithm = 0;
111 grpc_rb_compression_options *wrapper = NULL;
112
113 TypedData_Get_Struct(self, grpc_rb_compression_options,
114 &grpc_rb_compression_options_data_type, wrapper);
115 compression_algorithm =
116 (grpc_compression_algorithm)NUM2INT(algorithm_to_disable);
117
118 grpc_compression_options_disable_algorithm(wrapper->wrapped,
119 compression_algorithm);
120
121 return Qnil;
122}
123
124/* Provides a bitset as a ruby number that is suitable to pass to
125 * the GRPC core as a channel argument to enable compression algorithms. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700126/* Gets the compression internal enum value of a compression level given its
127 * name. */
128grpc_compression_level grpc_rb_compression_options_level_name_to_value_internal(
129 VALUE level_name) {
130 VALUE none_symbol = Qnil;
131 VALUE low_symbol = Qnil;
132 VALUE medium_symbol = Qnil;
133 VALUE high_symbol = Qnil;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700134
Alexander Polcynd788b452016-07-06 13:58:09 -0700135 Check_Type(level_name, T_SYMBOL);
136
137 /* Ruby symbols that correspond to names of valid compression levels */
138 none_symbol =
139 rb_const_get(grpc_rb_cCompressionOptions, rb_intern("COMPRESS_NONE_SYM"));
140 low_symbol =
141 rb_const_get(grpc_rb_cCompressionOptions, rb_intern("COMPRESS_LOW_SYM"));
142 medium_symbol = rb_const_get(grpc_rb_cCompressionOptions,
143 rb_intern("COMPRESS_MEDIUM_SYM"));
144 high_symbol =
145 rb_const_get(grpc_rb_cCompressionOptions, rb_intern("COMPRESS_HIGH_SYM"));
146
147 /* Check the compression level of the name passed in, and see which macro
148 * from the GRPC core header files match. */
149 if (RTEST(rb_funcall(level_name, rb_intern("=="), 1, none_symbol)) != 0) {
150 return GRPC_COMPRESS_LEVEL_NONE;
151 } else if (RTEST(rb_funcall(level_name, rb_intern("=="), 1, low_symbol)) !=
152 0) {
153 return GRPC_COMPRESS_LEVEL_LOW;
154 } else if (RTEST(rb_funcall(level_name, rb_intern("=="), 1, medium_symbol)) !=
155 0) {
156 return GRPC_COMPRESS_LEVEL_MED;
157 } else if (RTEST(rb_funcall(level_name, rb_intern("=="), 1, high_symbol)) !=
158 0) {
159 return GRPC_COMPRESS_LEVEL_HIGH;
160 } else {
161 rb_raise(rb_eArgError,
162 "Unrecognized compression level name."
163 "Valid compression level names are none, low, medium, and high.");
164 }
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700165}
166
Alexander Polcynd788b452016-07-06 13:58:09 -0700167/* Wrapper over grpc_rb_compression_options_level_name_to_value available for
168 * use or testing.
169 * Raises an exception for unrecognized level names. */
170VALUE grpc_rb_compression_options_level_name_to_value(VALUE self,
171 VALUE level_name) {
172 return INT2NUM((int)grpc_rb_compression_options_level_name_to_value_internal(
173 level_name));
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700174}
175
176/* Sets the default compression level, given the name of a compression level.
177 * Throws an error if no algorithm matched. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700178void grpc_rb_compression_options_set_default_level(
179 grpc_compression_options *options, VALUE new_level_name) {
180 options->default_level.level =
181 grpc_rb_compression_options_level_name_to_value_internal(new_level_name);
182 options->default_level.is_set = 1;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700183}
184
185/* Gets the internal value of a compression algorithm suitable as the value
186 * in a GRPC core channel arguments hash.
Alexander Polcynd788b452016-07-06 13:58:09 -0700187 * algorithm_value is an out parameter.
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700188 * Raises an error if the name of the algorithm passed in is invalid. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700189void grpc_rb_compression_options_algorithm_name_to_value_internal(
190 grpc_compression_algorithm *algorithm_value, VALUE algorithm_name) {
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700191 char *name_str = NULL;
192 long name_len = 0;
Alexander Polcynd788b452016-07-06 13:58:09 -0700193 VALUE algorithm_name_as_string = Qnil;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700194
Alexander Polcynd788b452016-07-06 13:58:09 -0700195 Check_Type(algorithm_name, T_SYMBOL);
196
197 /* Convert the algorithm symbol to a ruby string, so that we can get the
198 * correct C string out of it. */
199 algorithm_name_as_string = rb_funcall(algorithm_name, rb_intern("to_s"), 0);
200
201 name_str = RSTRING_PTR(algorithm_name_as_string);
202 name_len = RSTRING_LEN(algorithm_name_as_string);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700203
204 /* Raise an error if the name isn't recognized as a compression algorithm by
205 * the algorithm parse function
206 * in GRPC core. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700207 if (!grpc_compression_algorithm_parse(name_str, name_len, algorithm_value)) {
208 rb_raise(rb_eNameError, "Invalid compression algorithm name: %s",
209 StringValueCStr(algorithm_name_as_string));
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700210 }
211}
212
Alexander Polcynd788b452016-07-06 13:58:09 -0700213/* Wrapper around algorithm_name_to_value_internal function available for use or
214 * testing. */
215VALUE grpc_rb_compression_options_algorithm_name_to_value(
216 VALUE self, VALUE algorithm_name) {
217 grpc_compression_algorithm algorithm_value;
218 grpc_rb_compression_options_algorithm_name_to_value_internal(&algorithm_value,
219 algorithm_name);
220
221 return INT2NUM((int)algorithm_value);
222}
223
224/* Indicates whether a given algorithm is enabled on this instance, given the
225 * readable algorithm name. */
226VALUE grpc_rb_compression_options_is_algorithm_enabled(VALUE self,
227 VALUE algorithm_name) {
228 grpc_rb_compression_options *wrapper = NULL;
229 grpc_compression_algorithm internal_algorithm_value;
230
231 TypedData_Get_Struct(self, grpc_rb_compression_options,
232 &grpc_rb_compression_options_data_type, wrapper);
233 grpc_rb_compression_options_algorithm_name_to_value_internal(
234 &internal_algorithm_value, algorithm_name);
235
236 if (grpc_compression_options_is_algorithm_enabled(wrapper->wrapped,
237 internal_algorithm_value)) {
238 return Qtrue;
239 }
240 return Qfalse;
241}
242
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700243/* Sets the default algorithm to the name of the algorithm passed in.
244 * Raises an error if the name is not a valid compression algorithm name. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700245void grpc_rb_compression_options_set_default_algorithm(
246 grpc_compression_options *options, VALUE algorithm_name) {
247 grpc_rb_compression_options_algorithm_name_to_value_internal(
248 &options->default_algorithm.algorithm, algorithm_name);
249 options->default_algorithm.is_set = 1;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700250}
251
Alexander Polcynd788b452016-07-06 13:58:09 -0700252/* Disables an algorithm on the current instance, given the name of an
253 * algorithm.
254 * Fails if the algorithm name is invalid. */
255void grpc_rb_compression_options_disable_algorithm(
256 grpc_compression_options *compression_options, VALUE algorithm_name) {
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700257 grpc_compression_algorithm internal_algorithm_value;
258
Alexander Polcynd788b452016-07-06 13:58:09 -0700259 grpc_rb_compression_options_algorithm_name_to_value_internal(
260 &internal_algorithm_value, algorithm_name);
261 grpc_compression_options_disable_algorithm(compression_options,
262 internal_algorithm_value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700263}
264
265/* Provides a ruby hash of GRPC core channel argument key-values that
266 * correspond to the compression settings on this instance. */
267VALUE grpc_rb_compression_options_to_hash(VALUE self) {
268 grpc_rb_compression_options *wrapper = NULL;
269 grpc_compression_options *compression_options = NULL;
Alexander Polcynd788b452016-07-06 13:58:09 -0700270 VALUE channel_arg_hash = rb_hash_new();
271 VALUE key = Qnil;
272 VALUE value = Qnil;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700273
274 TypedData_Get_Struct(self, grpc_rb_compression_options,
275 &grpc_rb_compression_options_data_type, wrapper);
276 compression_options = wrapper->wrapped;
277
278 /* Add key-value pairs to the new Ruby hash. It can be used
279 * as GRPC core channel arguments. */
280 if (compression_options->default_level.is_set) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700281 key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL);
282 value = INT2NUM((int)compression_options->default_level.level);
283 rb_hash_aset(channel_arg_hash, key, value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700284 }
285
286 if (compression_options->default_algorithm.is_set) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700287 key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM);
288 value = INT2NUM((int)compression_options->default_algorithm.algorithm);
289 rb_hash_aset(channel_arg_hash, key, value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700290 }
291
Alexander Polcynd788b452016-07-06 13:58:09 -0700292 key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET);
293 value = INT2NUM((int)compression_options->enabled_algorithms_bitset);
294 rb_hash_aset(channel_arg_hash, key, value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700295
296 return channel_arg_hash;
297}
298
Alexander Polcynd788b452016-07-06 13:58:09 -0700299/* Converts an internal enum level value to a readable level name.
300 * Fails if the level value is invalid. */
301VALUE grpc_rb_compression_options_level_value_to_name_internal(
302 grpc_compression_level compression_value) {
303 switch (compression_value) {
304 case GRPC_COMPRESS_LEVEL_NONE:
305 return rb_const_get(grpc_rb_cCompressionOptions,
306 rb_intern("COMPRESS_NONE_SYM"));
307 case GRPC_COMPRESS_LEVEL_LOW:
308 return rb_const_get(grpc_rb_cCompressionOptions,
309 rb_intern("COMPRESS_LOW_SYM"));
310 case GRPC_COMPRESS_LEVEL_MED:
311 return rb_const_get(grpc_rb_cCompressionOptions,
312 rb_intern("COMPRESS_MEDIUM_SYM"));
313 case GRPC_COMPRESS_LEVEL_HIGH:
314 return rb_const_get(grpc_rb_cCompressionOptions,
315 rb_intern("COMPRESS_HIGH_SYM"));
316 default:
317 rb_raise(
318 rb_eArgError,
319 "Failed to convert compression level value to name for value: %d",
320 (int)compression_value);
321 }
322}
323
324/* Wrapper of internal method that makes it available for use and testing. */
325VALUE grpc_rb_compression_options_level_value_to_name(VALUE self,
326 VALUE compression_value) {
327 Check_Type(compression_value, T_FIXNUM);
328 return grpc_rb_compression_options_level_value_to_name_internal(
329 (grpc_compression_level)NUM2INT(compression_value));
330}
331
332/* Converts an algorithm internal enum value to a readable name.
333 * Fails if the enum value is invalid. */
334VALUE grpc_rb_compression_options_algorithm_value_to_name_internal(
335 grpc_compression_algorithm internal_value) {
336 char *algorithm_name = NULL;
337
338 if (!grpc_compression_algorithm_name(internal_value, &algorithm_name)) {
339 rb_raise(rb_eArgError, "Failed to convert algorithm value to name");
340 }
341
342 return ID2SYM(rb_intern(algorithm_name));
343}
344
345/* Wrapper of algorithm_to_name internal function available for ues and testing.
346 */
347VALUE grpc_rb_compression_options_algorithm_value_to_name(
348 VALUE self, VALUE algorithm_value) {
349 grpc_compression_algorithm algorithm_internal_value;
350 algorithm_internal_value =
351 (grpc_compression_algorithm)NUM2INT(algorithm_value);
352
353 return grpc_rb_compression_options_algorithm_value_to_name_internal(
354 algorithm_internal_value);
355}
356
357/* Gets the internal value of the default compression level that is to be passed
358 * to the GRPC core as a channel argument value.
359 * A nil return value means that it hasn't been set. */
360VALUE grpc_rb_compression_options_get_default_algorithm_internal_value(
361 VALUE self) {
362 grpc_rb_compression_options *wrapper = NULL;
363
364 TypedData_Get_Struct(self, grpc_rb_compression_options,
365 &grpc_rb_compression_options_data_type, wrapper);
366
367 if (wrapper->wrapped->default_algorithm.is_set) {
368 return INT2NUM(wrapper->wrapped->default_algorithm.algorithm);
369 }
370 return Qnil;
371}
372
373/* Gets the readable name of the default algorithm if one has been set.
374 * Returns nil if no algorithm has been set. */
375VALUE grpc_rb_compression_options_get_default_algorithm(VALUE self) {
376 VALUE algorithm_value =
377 grpc_rb_compression_options_get_default_algorithm_internal_value(self);
378
379 if (RTEST(algorithm_value)) {
380 return grpc_rb_compression_options_algorithm_value_to_name(self,
381 algorithm_value);
382 }
383
384 return Qnil;
385}
386
387/* Gets the internal enum value of the default algorithm if one has been set.
388 * Returns nil if no default algorithm has been set. */
389VALUE grpc_rb_compression_options_get_default_level_internal_value(VALUE self) {
390 grpc_rb_compression_options *wrapper = NULL;
391
392 TypedData_Get_Struct(self, grpc_rb_compression_options,
393 &grpc_rb_compression_options_data_type, wrapper);
394
395 if (wrapper->wrapped->default_level.is_set) {
396 return INT2NUM((int)wrapper->wrapped->default_level.level);
397 }
398 return Qnil;
399}
400
401/* Gets the internal value of the default compression level that is to be passed
402 * to the GRPC core as a channel argument value.
403 * A nil return value means that it hasn't been set. */
404VALUE grpc_rb_compression_options_get_default_level(VALUE self) {
405 grpc_compression_level internal_value;
406 VALUE ruby_value =
407 grpc_rb_compression_options_get_default_level_internal_value(self);
408
409 if (RTEST(ruby_value)) {
410 internal_value = (grpc_compression_level)NUM2INT(ruby_value);
411 return grpc_rb_compression_options_level_value_to_name_internal(
412 internal_value);
413 }
414
415 return Qnil;
416}
417
418/* Gets a list of the disabled algorithms as readable names.
419 * Returns an empty list of no algorithms have been disabled. */
420VALUE grpc_rb_compression_options_get_disabled_algorithms(VALUE self) {
421 VALUE disabled_algorithms = rb_ary_new();
422 grpc_compression_algorithm internal_value;
423 grpc_rb_compression_options *wrapper = NULL;
424
425 TypedData_Get_Struct(self, grpc_rb_compression_options,
426 &grpc_rb_compression_options_data_type, wrapper);
427
428 for (internal_value = GRPC_COMPRESS_NONE;
429 internal_value < GRPC_COMPRESS_ALGORITHMS_COUNT; internal_value++) {
430 if (!grpc_compression_options_is_algorithm_enabled(wrapper->wrapped,
431 internal_value)) {
432 rb_ary_push(disabled_algorithms,
433 grpc_rb_compression_options_algorithm_value_to_name_internal(
434 internal_value));
435 }
436 }
437 return disabled_algorithms;
438}
439
440/* Provides a bitset as a ruby number that is suitable to pass to
441 * the GRPC core as a channel argument to enable compression algorithms. */
442VALUE grpc_rb_compression_options_get_enabled_algorithms_bitset(VALUE self) {
443 grpc_rb_compression_options *wrapper = NULL;
444
445 TypedData_Get_Struct(self, grpc_rb_compression_options,
446 &grpc_rb_compression_options_data_type, wrapper);
447 return INT2NUM((int)wrapper->wrapped->enabled_algorithms_bitset);
448}
449
450/* Initializes the compression options wrapper.
451 * Takes an optional hash parameter.
452 *
453 * Example call-seq:
454 * options = CompressionOptions.new(
455 * default_level: :none,
456 * disabled_algorithms: [:gzip]
457 * )
458 * channel_arg hash = Hash.new[...]
459 * channel_arg_hash_with_compression_options = channel_arg_hash.merge(options)
460 */
461VALUE grpc_rb_compression_options_init(int argc, VALUE *argv, VALUE self) {
462 grpc_rb_compression_options *wrapper = NULL;
463 VALUE default_algorithm = Qnil;
464 VALUE default_level = Qnil;
465 VALUE disabled_algorithms = Qnil;
466 VALUE algorithm_name = Qnil;
467 VALUE hash_arg = Qnil;
468
469 rb_scan_args(argc, argv, "01", &hash_arg);
470
471 /* Check if the hash parameter was passed, or if invalid arguments were
472 * passed. */
473 if (hash_arg == Qnil) {
474 return self;
475 } else if (TYPE(hash_arg) != T_HASH || argc > 1) {
476 rb_raise(rb_eArgError,
477 "Invalid arguments. Expecting optional hash parameter");
478 }
479
480 TypedData_Get_Struct(self, grpc_rb_compression_options,
481 &grpc_rb_compression_options_data_type, wrapper);
482
483 /* Set the default algorithm if one was chosen. */
484 default_algorithm =
485 rb_hash_aref(hash_arg, ID2SYM(rb_intern("default_algorithm")));
486 if (default_algorithm != Qnil) {
487 grpc_rb_compression_options_set_default_algorithm(wrapper->wrapped,
488 default_algorithm);
489 }
490
491 /* Set the default level if one was chosen. */
492 default_level = rb_hash_aref(hash_arg, ID2SYM(rb_intern("default_level")));
493 if (default_level != Qnil) {
494 grpc_rb_compression_options_set_default_level(wrapper->wrapped,
495 default_level);
496 }
497
498 /* Set the disabled algorithms if any were chosen. */
499 disabled_algorithms =
500 rb_hash_aref(hash_arg, ID2SYM(rb_intern("disabled_algorithms")));
501 if (disabled_algorithms != Qnil) {
502 Check_Type(disabled_algorithms, T_ARRAY);
503
504 for (int i = 0; i < RARRAY_LEN(disabled_algorithms); i++) {
505 algorithm_name = rb_ary_entry(disabled_algorithms, i);
506 grpc_rb_compression_options_disable_algorithm(wrapper->wrapped,
507 algorithm_name);
508 }
509 }
510
511 return self;
512}
513
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700514void Init_grpc_compression_options() {
515 grpc_rb_cCompressionOptions = rb_define_class_under(
516 grpc_rb_mGrpcCore, "CompressionOptions", rb_cObject);
517
518 /* Allocates an object managed by the ruby runtime. */
519 rb_define_alloc_func(grpc_rb_cCompressionOptions,
520 grpc_rb_compression_options_alloc);
521
Alexander Polcynd788b452016-07-06 13:58:09 -0700522 /* Initializes the ruby wrapper. #new method takes an optional hash argument.
523 */
524 rb_define_method(grpc_rb_cCompressionOptions, "initialize",
525 grpc_rb_compression_options_init, -1);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700526
Alexander Polcynd788b452016-07-06 13:58:09 -0700527 /* Gets the bitset of enabled algorithms. */
528 rb_define_method(grpc_rb_cCompressionOptions, "enabled_algorithms_bitset",
529 grpc_rb_compression_options_get_enabled_algorithms_bitset,
530 0);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700531
Alexander Polcynd788b452016-07-06 13:58:09 -0700532 /* Methods for getting the default algorithm, default level, and disabled
533 * algorithms as readable names. */
534 rb_define_method(grpc_rb_cCompressionOptions, "default_algorithm",
535 grpc_rb_compression_options_get_default_algorithm, 0);
536 rb_define_method(grpc_rb_cCompressionOptions, "default_level",
537 grpc_rb_compression_options_get_default_level, 0);
538 rb_define_method(grpc_rb_cCompressionOptions, "disabled_algorithms",
539 grpc_rb_compression_options_get_disabled_algorithms, 0);
540
541 /* Methods for getting the internal enum default algorithm and level enum
542 * values of an instance. */
543 rb_define_method(
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700544 grpc_rb_cCompressionOptions, "default_algorithm_internal_value",
Alexander Polcynd788b452016-07-06 13:58:09 -0700545 grpc_rb_compression_options_get_default_algorithm_internal_value, 0);
546 rb_define_method(grpc_rb_cCompressionOptions, "default_level_internal_value",
547 grpc_rb_compression_options_get_default_level_internal_value,
548 0);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700549
Alexander Polcynd788b452016-07-06 13:58:09 -0700550 /* Determines whether or not an algorithm is enabled, given a readable
551 * algorithm name.*/
552 rb_define_method(grpc_rb_cCompressionOptions, "is_algorithm_enabled",
553 grpc_rb_compression_options_is_algorithm_enabled, 1);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700554
Alexander Polcynd788b452016-07-06 13:58:09 -0700555 /* Methods for converting to and from algorithm enum values and their readable
556 * names. */
557 rb_define_singleton_method(
558 grpc_rb_cCompressionOptions, "algorithm_name_to_value",
559 grpc_rb_compression_options_algorithm_name_to_value, 1);
560 rb_define_singleton_method(
561 grpc_rb_cCompressionOptions, "algorithm_value_to_name",
562 grpc_rb_compression_options_algorithm_value_to_name, 1);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700563
Alexander Polcynd788b452016-07-06 13:58:09 -0700564 /* Methods for converting to and from compression level enum values and their
565 * readable names. */
566 rb_define_singleton_method(grpc_rb_cCompressionOptions, "level_name_to_value",
567 grpc_rb_compression_options_level_name_to_value,
568 1);
569 rb_define_singleton_method(grpc_rb_cCompressionOptions, "level_value_to_name",
570 grpc_rb_compression_options_level_value_to_name,
571 1);
572
573 /* Provides a hash of the compression settings suitable
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700574 * for passing to server or channel args. */
575 rb_define_method(grpc_rb_cCompressionOptions, "to_hash",
576 grpc_rb_compression_options_to_hash, 0);
Alexander Polcynd788b452016-07-06 13:58:09 -0700577 rb_define_alias(grpc_rb_cCompressionOptions, "to_channel_arg_hash",
578 "to_hash");
579
580 /* Ruby symbols for the names of the different compression levels. */
581 rb_define_const(grpc_rb_cCompressionOptions, "COMPRESS_NONE_SYM",
582 ID2SYM(rb_intern("none")));
583 rb_define_const(grpc_rb_cCompressionOptions, "COMPRESS_LOW_SYM",
584 ID2SYM(rb_intern("low")));
585 rb_define_const(grpc_rb_cCompressionOptions, "COMPRESS_MEDIUM_SYM",
586 ID2SYM(rb_intern("medium")));
587 rb_define_const(grpc_rb_cCompressionOptions, "COMPRESS_HIGH_SYM",
588 ID2SYM(rb_intern("high")));
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700589}