blob: e24f20d2f9dfa0dc5ab8df97e0177ac00da4d312 [file] [log] [blame]
Alexander Polcyn0dccf102016-06-27 13:11:07 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Alexander Polcyn0dccf102016-06-27 13:11:07 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Alexander Polcyn0dccf102016-06-27 13:11:07 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Alexander Polcyn0dccf102016-06-27 13:11:07 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Alexander Polcyn0dccf102016-06-27 13:11:07 -070016 *
17 */
18
19#include <ruby/ruby.h>
20
Craig Tiller7c70b6c2017-01-23 07:48:42 -080021#include "rb_byte_buffer.h"
Craig Tiller5b1c5f22017-04-19 09:52:18 -070022#include "rb_compression_options.h"
Alexander Polcyn0dccf102016-06-27 13:11:07 -070023#include "rb_grpc_imports.generated.h"
24
25#include <grpc/compression.h>
26#include <grpc/grpc.h>
Alexander Polcyn0dccf102016-06-27 13:11:07 -070027#include <grpc/impl/codegen/compression_types.h>
28#include <grpc/impl/codegen/grpc_types.h>
Craig Tiller5b1c5f22017-04-19 09:52:18 -070029#include <grpc/support/alloc.h>
Alexander Polcyn0dccf102016-06-27 13:11:07 -070030#include <string.h>
31
32#include "rb_grpc.h"
33
34static VALUE grpc_rb_cCompressionOptions = Qnil;
35
Alexander Polcyn6c4709e2016-07-11 13:14:07 -070036/* Ruby Ids for the names of valid compression levels. */
37static VALUE id_compress_level_none = Qnil;
38static VALUE id_compress_level_low = Qnil;
39static VALUE id_compress_level_medium = Qnil;
40static VALUE id_compress_level_high = Qnil;
41
Alexander Polcyn0dccf102016-06-27 13:11:07 -070042/* grpc_rb_compression_options wraps a grpc_compression_options.
Alexander Polcynd788b452016-07-06 13:58:09 -070043 * It can be used to get the channel argument key-values for specific
44 * compression settings. */
45
46/* Note that ruby objects of this type don't carry any state in other
Alexander Polcyn0dccf102016-06-27 13:11:07 -070047 * Ruby objects and don't have a mark for GC. */
48typedef struct grpc_rb_compression_options {
49 /* The actual compression options that's being wrapped */
Craig Tillerbaa14a92017-11-03 09:09:36 -070050 grpc_compression_options* wrapped;
Alexander Polcyn0dccf102016-06-27 13:11:07 -070051} grpc_rb_compression_options;
52
53/* Destroys the compression options instances and free the
54 * wrapped grpc compression options. */
Craig Tillerbaa14a92017-11-03 09:09:36 -070055static void grpc_rb_compression_options_free(void* p) {
56 grpc_rb_compression_options* wrapper = NULL;
Alexander Polcyn0dccf102016-06-27 13:11:07 -070057 if (p == NULL) {
58 return;
59 };
Craig Tillerbaa14a92017-11-03 09:09:36 -070060 wrapper = (grpc_rb_compression_options*)p;
Alexander Polcyn0dccf102016-06-27 13:11:07 -070061
62 if (wrapper->wrapped != NULL) {
63 gpr_free(wrapper->wrapped);
64 wrapper->wrapped = NULL;
65 }
66
67 xfree(p);
68}
69
70/* Ruby recognized data type for the CompressionOptions class. */
71static rb_data_type_t grpc_rb_compression_options_data_type = {
72 "grpc_compression_options",
73 {NULL,
74 grpc_rb_compression_options_free,
75 GRPC_RB_MEMSIZE_UNAVAILABLE,
76 {NULL, NULL}},
77 NULL,
78 NULL,
79#ifdef RUBY_TYPED_FREE_IMMEDIATELY
80 RUBY_TYPED_FREE_IMMEDIATELY
81#endif
82};
83
84/* Allocates CompressionOptions instances.
85 Allocate the wrapped grpc compression options and
86 initialize it here too. */
87static VALUE grpc_rb_compression_options_alloc(VALUE cls) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070088 grpc_rb_compression_options* wrapper = NULL;
Alexander Polcyn2a9b5d72017-04-14 12:10:55 -070089
90 grpc_ruby_once_init();
91
92 wrapper = gpr_malloc(sizeof(grpc_rb_compression_options));
Alexander Polcyn0dccf102016-06-27 13:11:07 -070093 wrapper->wrapped = NULL;
94 wrapper->wrapped = gpr_malloc(sizeof(grpc_compression_options));
95 grpc_compression_options_init(wrapper->wrapped);
96
97 return TypedData_Wrap_Struct(cls, &grpc_rb_compression_options_data_type,
98 wrapper);
99}
100
101/* Disables a compression algorithm, given the GRPC core internal number of a
102 * compression algorithm. */
103VALUE grpc_rb_compression_options_disable_compression_algorithm_internal(
104 VALUE self, VALUE algorithm_to_disable) {
105 grpc_compression_algorithm compression_algorithm = 0;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700106 grpc_rb_compression_options* wrapper = NULL;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700107
108 TypedData_Get_Struct(self, grpc_rb_compression_options,
109 &grpc_rb_compression_options_data_type, wrapper);
110 compression_algorithm =
111 (grpc_compression_algorithm)NUM2INT(algorithm_to_disable);
112
113 grpc_compression_options_disable_algorithm(wrapper->wrapped,
114 compression_algorithm);
115
116 return Qnil;
117}
118
Alexander Polcynd788b452016-07-06 13:58:09 -0700119/* Gets the compression internal enum value of a compression level given its
120 * name. */
121grpc_compression_level grpc_rb_compression_options_level_name_to_value_internal(
122 VALUE level_name) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700123 Check_Type(level_name, T_SYMBOL);
124
Alexander Polcynd788b452016-07-06 13:58:09 -0700125 /* Check the compression level of the name passed in, and see which macro
126 * from the GRPC core header files match. */
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700127 if (id_compress_level_none == SYM2ID(level_name)) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700128 return GRPC_COMPRESS_LEVEL_NONE;
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700129 } else if (id_compress_level_low == SYM2ID(level_name)) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700130 return GRPC_COMPRESS_LEVEL_LOW;
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700131 } else if (id_compress_level_medium == SYM2ID(level_name)) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700132 return GRPC_COMPRESS_LEVEL_MED;
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700133 } else if (id_compress_level_high == SYM2ID(level_name)) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700134 return GRPC_COMPRESS_LEVEL_HIGH;
Alexander Polcynd788b452016-07-06 13:58:09 -0700135 }
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700136
137 rb_raise(rb_eArgError,
138 "Unrecognized compression level name."
139 "Valid compression level names are none, low, medium, and high.");
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700140
Alex Polcyn7c55ab02016-07-11 23:14:32 -0700141 /* Dummy return statement. */
142 return GRPC_COMPRESS_LEVEL_NONE;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700143}
144
145/* Sets the default compression level, given the name of a compression level.
146 * Throws an error if no algorithm matched. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700147void grpc_rb_compression_options_set_default_level(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700148 grpc_compression_options* options, VALUE new_level_name) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700149 options->default_level.level =
150 grpc_rb_compression_options_level_name_to_value_internal(new_level_name);
151 options->default_level.is_set = 1;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700152}
153
154/* Gets the internal value of a compression algorithm suitable as the value
155 * in a GRPC core channel arguments hash.
Alexander Polcynd788b452016-07-06 13:58:09 -0700156 * algorithm_value is an out parameter.
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700157 * Raises an error if the name of the algorithm passed in is invalid. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700158void grpc_rb_compression_options_algorithm_name_to_value_internal(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700159 grpc_compression_algorithm* algorithm_value, VALUE algorithm_name) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800160 grpc_slice name_slice;
Alexander Polcynd788b452016-07-06 13:58:09 -0700161 VALUE algorithm_name_as_string = Qnil;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700162 char* tmp_str = NULL;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700163
Alexander Polcynd788b452016-07-06 13:58:09 -0700164 Check_Type(algorithm_name, T_SYMBOL);
165
166 /* Convert the algorithm symbol to a ruby string, so that we can get the
167 * correct C string out of it. */
168 algorithm_name_as_string = rb_funcall(algorithm_name, rb_intern("to_s"), 0);
169
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700170 name_slice =
171 grpc_slice_from_copied_buffer(RSTRING_PTR(algorithm_name_as_string),
172 RSTRING_LEN(algorithm_name_as_string));
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700173
174 /* Raise an error if the name isn't recognized as a compression algorithm by
175 * the algorithm parse function
176 * in GRPC core. */
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700177 if (!grpc_compression_algorithm_parse(name_slice, algorithm_value)) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800178 tmp_str = grpc_slice_to_c_string(name_slice);
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700179 rb_raise(rb_eNameError, "Invalid compression algorithm name: %s", tmp_str);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700180 }
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800181
182 grpc_slice_unref(name_slice);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700183}
184
Alexander Polcynd788b452016-07-06 13:58:09 -0700185/* Indicates whether a given algorithm is enabled on this instance, given the
186 * readable algorithm name. */
187VALUE grpc_rb_compression_options_is_algorithm_enabled(VALUE self,
188 VALUE algorithm_name) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700189 grpc_rb_compression_options* wrapper = NULL;
Alexander Polcynd788b452016-07-06 13:58:09 -0700190 grpc_compression_algorithm internal_algorithm_value;
191
192 TypedData_Get_Struct(self, grpc_rb_compression_options,
193 &grpc_rb_compression_options_data_type, wrapper);
194 grpc_rb_compression_options_algorithm_name_to_value_internal(
195 &internal_algorithm_value, algorithm_name);
196
197 if (grpc_compression_options_is_algorithm_enabled(wrapper->wrapped,
198 internal_algorithm_value)) {
199 return Qtrue;
200 }
201 return Qfalse;
202}
203
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700204/* Sets the default algorithm to the name of the algorithm passed in.
205 * Raises an error if the name is not a valid compression algorithm name. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700206void grpc_rb_compression_options_set_default_algorithm(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700207 grpc_compression_options* options, VALUE algorithm_name) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700208 grpc_rb_compression_options_algorithm_name_to_value_internal(
209 &options->default_algorithm.algorithm, algorithm_name);
210 options->default_algorithm.is_set = 1;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700211}
212
Alexander Polcynd788b452016-07-06 13:58:09 -0700213/* Disables an algorithm on the current instance, given the name of an
214 * algorithm.
215 * Fails if the algorithm name is invalid. */
216void grpc_rb_compression_options_disable_algorithm(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700217 grpc_compression_options* compression_options, VALUE algorithm_name) {
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700218 grpc_compression_algorithm internal_algorithm_value;
219
Alexander Polcynd788b452016-07-06 13:58:09 -0700220 grpc_rb_compression_options_algorithm_name_to_value_internal(
221 &internal_algorithm_value, algorithm_name);
222 grpc_compression_options_disable_algorithm(compression_options,
223 internal_algorithm_value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700224}
225
226/* Provides a ruby hash of GRPC core channel argument key-values that
227 * correspond to the compression settings on this instance. */
228VALUE grpc_rb_compression_options_to_hash(VALUE self) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700229 grpc_rb_compression_options* wrapper = NULL;
230 grpc_compression_options* compression_options = NULL;
Alexander Polcynd788b452016-07-06 13:58:09 -0700231 VALUE channel_arg_hash = rb_hash_new();
232 VALUE key = Qnil;
233 VALUE value = Qnil;
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700234
235 TypedData_Get_Struct(self, grpc_rb_compression_options,
236 &grpc_rb_compression_options_data_type, wrapper);
237 compression_options = wrapper->wrapped;
238
239 /* Add key-value pairs to the new Ruby hash. It can be used
240 * as GRPC core channel arguments. */
241 if (compression_options->default_level.is_set) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700242 key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL);
243 value = INT2NUM((int)compression_options->default_level.level);
244 rb_hash_aset(channel_arg_hash, key, value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700245 }
246
247 if (compression_options->default_algorithm.is_set) {
Alexander Polcynd788b452016-07-06 13:58:09 -0700248 key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM);
249 value = INT2NUM((int)compression_options->default_algorithm.algorithm);
250 rb_hash_aset(channel_arg_hash, key, value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700251 }
252
Alexander Polcynd788b452016-07-06 13:58:09 -0700253 key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET);
254 value = INT2NUM((int)compression_options->enabled_algorithms_bitset);
255 rb_hash_aset(channel_arg_hash, key, value);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700256
257 return channel_arg_hash;
258}
259
Alexander Polcynd788b452016-07-06 13:58:09 -0700260/* Converts an internal enum level value to a readable level name.
261 * Fails if the level value is invalid. */
262VALUE grpc_rb_compression_options_level_value_to_name_internal(
263 grpc_compression_level compression_value) {
264 switch (compression_value) {
265 case GRPC_COMPRESS_LEVEL_NONE:
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700266 return ID2SYM(id_compress_level_none);
Alexander Polcynd788b452016-07-06 13:58:09 -0700267 case GRPC_COMPRESS_LEVEL_LOW:
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700268 return ID2SYM(id_compress_level_low);
Alexander Polcynd788b452016-07-06 13:58:09 -0700269 case GRPC_COMPRESS_LEVEL_MED:
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700270 return ID2SYM(id_compress_level_medium);
Alexander Polcynd788b452016-07-06 13:58:09 -0700271 case GRPC_COMPRESS_LEVEL_HIGH:
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700272 return ID2SYM(id_compress_level_high);
Alexander Polcynd788b452016-07-06 13:58:09 -0700273 default:
274 rb_raise(
275 rb_eArgError,
276 "Failed to convert compression level value to name for value: %d",
277 (int)compression_value);
Vijay Paic21e94c2016-11-02 01:08:38 -0700278 /* return something to avoid compiler error about no return */
279 return Qnil;
Alexander Polcynd788b452016-07-06 13:58:09 -0700280 }
281}
282
Alexander Polcynd788b452016-07-06 13:58:09 -0700283/* Converts an algorithm internal enum value to a readable name.
284 * Fails if the enum value is invalid. */
285VALUE grpc_rb_compression_options_algorithm_value_to_name_internal(
286 grpc_compression_algorithm internal_value) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700287 char* algorithm_name = NULL;
Alexander Polcynd788b452016-07-06 13:58:09 -0700288
289 if (!grpc_compression_algorithm_name(internal_value, &algorithm_name)) {
290 rb_raise(rb_eArgError, "Failed to convert algorithm value to name");
291 }
292
293 return ID2SYM(rb_intern(algorithm_name));
294}
295
Alex Polcyn7c55ab02016-07-11 23:14:32 -0700296/* Gets the readable name of the default algorithm if one has been set.
297 * Returns nil if no algorithm has been set. */
298VALUE grpc_rb_compression_options_get_default_algorithm(VALUE self) {
299 grpc_compression_algorithm internal_value;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700300 grpc_rb_compression_options* wrapper = NULL;
Alexander Polcynd788b452016-07-06 13:58:09 -0700301
302 TypedData_Get_Struct(self, grpc_rb_compression_options,
303 &grpc_rb_compression_options_data_type, wrapper);
304
305 if (wrapper->wrapped->default_algorithm.is_set) {
Alex Polcyn7c55ab02016-07-11 23:14:32 -0700306 internal_value = wrapper->wrapped->default_algorithm.algorithm;
307 return grpc_rb_compression_options_algorithm_value_to_name_internal(
308 internal_value);
Alexander Polcynd788b452016-07-06 13:58:09 -0700309 }
310
311 return Qnil;
312}
313
Alexander Polcynd788b452016-07-06 13:58:09 -0700314/* Gets the internal value of the default compression level that is to be passed
315 * to the GRPC core as a channel argument value.
316 * A nil return value means that it hasn't been set. */
317VALUE grpc_rb_compression_options_get_default_level(VALUE self) {
318 grpc_compression_level internal_value;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700319 grpc_rb_compression_options* wrapper = NULL;
Alexander Polcynd788b452016-07-06 13:58:09 -0700320
Alex Polcyn7c55ab02016-07-11 23:14:32 -0700321 TypedData_Get_Struct(self, grpc_rb_compression_options,
322 &grpc_rb_compression_options_data_type, wrapper);
323
324 if (wrapper->wrapped->default_level.is_set) {
325 internal_value = wrapper->wrapped->default_level.level;
Alexander Polcynd788b452016-07-06 13:58:09 -0700326 return grpc_rb_compression_options_level_value_to_name_internal(
327 internal_value);
328 }
329
330 return Qnil;
331}
332
333/* Gets a list of the disabled algorithms as readable names.
Alex Polcyn7c55ab02016-07-11 23:14:32 -0700334 * Returns an empty list if no algorithms have been disabled. */
Alexander Polcynd788b452016-07-06 13:58:09 -0700335VALUE grpc_rb_compression_options_get_disabled_algorithms(VALUE self) {
336 VALUE disabled_algorithms = rb_ary_new();
337 grpc_compression_algorithm internal_value;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700338 grpc_rb_compression_options* wrapper = NULL;
Alexander Polcynd788b452016-07-06 13:58:09 -0700339
340 TypedData_Get_Struct(self, grpc_rb_compression_options,
341 &grpc_rb_compression_options_data_type, wrapper);
342
343 for (internal_value = GRPC_COMPRESS_NONE;
344 internal_value < GRPC_COMPRESS_ALGORITHMS_COUNT; internal_value++) {
345 if (!grpc_compression_options_is_algorithm_enabled(wrapper->wrapped,
346 internal_value)) {
347 rb_ary_push(disabled_algorithms,
348 grpc_rb_compression_options_algorithm_value_to_name_internal(
349 internal_value));
350 }
351 }
352 return disabled_algorithms;
353}
354
Alexander Polcynd788b452016-07-06 13:58:09 -0700355/* Initializes the compression options wrapper.
356 * Takes an optional hash parameter.
357 *
358 * Example call-seq:
359 * options = CompressionOptions.new(
360 * default_level: :none,
361 * disabled_algorithms: [:gzip]
362 * )
363 * channel_arg hash = Hash.new[...]
364 * channel_arg_hash_with_compression_options = channel_arg_hash.merge(options)
365 */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700366VALUE grpc_rb_compression_options_init(int argc, VALUE* argv, VALUE self) {
367 grpc_rb_compression_options* wrapper = NULL;
Alexander Polcynd788b452016-07-06 13:58:09 -0700368 VALUE default_algorithm = Qnil;
369 VALUE default_level = Qnil;
370 VALUE disabled_algorithms = Qnil;
371 VALUE algorithm_name = Qnil;
372 VALUE hash_arg = Qnil;
373
374 rb_scan_args(argc, argv, "01", &hash_arg);
375
376 /* Check if the hash parameter was passed, or if invalid arguments were
377 * passed. */
378 if (hash_arg == Qnil) {
379 return self;
380 } else if (TYPE(hash_arg) != T_HASH || argc > 1) {
381 rb_raise(rb_eArgError,
382 "Invalid arguments. Expecting optional hash parameter");
383 }
384
385 TypedData_Get_Struct(self, grpc_rb_compression_options,
386 &grpc_rb_compression_options_data_type, wrapper);
387
388 /* Set the default algorithm if one was chosen. */
389 default_algorithm =
390 rb_hash_aref(hash_arg, ID2SYM(rb_intern("default_algorithm")));
391 if (default_algorithm != Qnil) {
392 grpc_rb_compression_options_set_default_algorithm(wrapper->wrapped,
393 default_algorithm);
394 }
395
396 /* Set the default level if one was chosen. */
397 default_level = rb_hash_aref(hash_arg, ID2SYM(rb_intern("default_level")));
398 if (default_level != Qnil) {
399 grpc_rb_compression_options_set_default_level(wrapper->wrapped,
400 default_level);
401 }
402
403 /* Set the disabled algorithms if any were chosen. */
404 disabled_algorithms =
405 rb_hash_aref(hash_arg, ID2SYM(rb_intern("disabled_algorithms")));
406 if (disabled_algorithms != Qnil) {
407 Check_Type(disabled_algorithms, T_ARRAY);
408
409 for (int i = 0; i < RARRAY_LEN(disabled_algorithms); i++) {
410 algorithm_name = rb_ary_entry(disabled_algorithms, i);
411 grpc_rb_compression_options_disable_algorithm(wrapper->wrapped,
412 algorithm_name);
413 }
414 }
415
416 return self;
417}
418
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700419void Init_grpc_compression_options() {
420 grpc_rb_cCompressionOptions = rb_define_class_under(
421 grpc_rb_mGrpcCore, "CompressionOptions", rb_cObject);
422
423 /* Allocates an object managed by the ruby runtime. */
424 rb_define_alloc_func(grpc_rb_cCompressionOptions,
425 grpc_rb_compression_options_alloc);
426
Alexander Polcynd788b452016-07-06 13:58:09 -0700427 /* Initializes the ruby wrapper. #new method takes an optional hash argument.
428 */
429 rb_define_method(grpc_rb_cCompressionOptions, "initialize",
430 grpc_rb_compression_options_init, -1);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700431
Alexander Polcynd788b452016-07-06 13:58:09 -0700432 /* Methods for getting the default algorithm, default level, and disabled
433 * algorithms as readable names. */
434 rb_define_method(grpc_rb_cCompressionOptions, "default_algorithm",
435 grpc_rb_compression_options_get_default_algorithm, 0);
436 rb_define_method(grpc_rb_cCompressionOptions, "default_level",
437 grpc_rb_compression_options_get_default_level, 0);
438 rb_define_method(grpc_rb_cCompressionOptions, "disabled_algorithms",
439 grpc_rb_compression_options_get_disabled_algorithms, 0);
440
Alexander Polcynd788b452016-07-06 13:58:09 -0700441 /* Determines whether or not an algorithm is enabled, given a readable
442 * algorithm name.*/
Alex Polcyn7c55ab02016-07-11 23:14:32 -0700443 rb_define_method(grpc_rb_cCompressionOptions, "algorithm_enabled?",
Alexander Polcynd788b452016-07-06 13:58:09 -0700444 grpc_rb_compression_options_is_algorithm_enabled, 1);
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700445
Alexander Polcynd788b452016-07-06 13:58:09 -0700446 /* Provides a hash of the compression settings suitable
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700447 * for passing to server or channel args. */
448 rb_define_method(grpc_rb_cCompressionOptions, "to_hash",
449 grpc_rb_compression_options_to_hash, 0);
Alexander Polcynd788b452016-07-06 13:58:09 -0700450 rb_define_alias(grpc_rb_cCompressionOptions, "to_channel_arg_hash",
451 "to_hash");
452
Alexander Polcyn6c4709e2016-07-11 13:14:07 -0700453 /* Ruby ids for the names of the different compression levels. */
454 id_compress_level_none = rb_intern("none");
455 id_compress_level_low = rb_intern("low");
456 id_compress_level_medium = rb_intern("medium");
457 id_compress_level_high = rb_intern("high");
Alexander Polcyn0dccf102016-06-27 13:11:07 -0700458}