Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <ruby/ruby.h> |
| 20 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 21 | #include "rb_byte_buffer.h" |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 22 | #include "rb_compression_options.h" |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 23 | #include "rb_grpc_imports.generated.h" |
| 24 | |
| 25 | #include <grpc/compression.h> |
| 26 | #include <grpc/grpc.h> |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 27 | #include <grpc/impl/codegen/compression_types.h> |
| 28 | #include <grpc/impl/codegen/grpc_types.h> |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 29 | #include <grpc/support/alloc.h> |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
| 32 | #include "rb_grpc.h" |
| 33 | |
| 34 | static VALUE grpc_rb_cCompressionOptions = Qnil; |
| 35 | |
Alexander Polcyn | 6c4709e | 2016-07-11 13:14:07 -0700 | [diff] [blame] | 36 | /* Ruby Ids for the names of valid compression levels. */ |
| 37 | static VALUE id_compress_level_none = Qnil; |
| 38 | static VALUE id_compress_level_low = Qnil; |
| 39 | static VALUE id_compress_level_medium = Qnil; |
| 40 | static VALUE id_compress_level_high = Qnil; |
| 41 | |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 42 | /* grpc_rb_compression_options wraps a grpc_compression_options. |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 43 | * 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 Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 47 | * Ruby objects and don't have a mark for GC. */ |
| 48 | typedef struct grpc_rb_compression_options { |
| 49 | /* The actual compression options that's being wrapped */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 50 | grpc_compression_options* wrapped; |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 51 | } grpc_rb_compression_options; |
| 52 | |
| 53 | /* Destroys the compression options instances and free the |
| 54 | * wrapped grpc compression options. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 55 | static void grpc_rb_compression_options_free(void* p) { |
| 56 | grpc_rb_compression_options* wrapper = NULL; |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 57 | if (p == NULL) { |
| 58 | return; |
| 59 | }; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 60 | wrapper = (grpc_rb_compression_options*)p; |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 61 | |
| 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. */ |
| 71 | static 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. */ |
| 87 | static VALUE grpc_rb_compression_options_alloc(VALUE cls) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 88 | grpc_rb_compression_options* wrapper = NULL; |
Alexander Polcyn | 2a9b5d7 | 2017-04-14 12:10:55 -0700 | [diff] [blame] | 89 | |
| 90 | grpc_ruby_once_init(); |
| 91 | |
| 92 | wrapper = gpr_malloc(sizeof(grpc_rb_compression_options)); |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 93 | 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. */ |
| 103 | VALUE grpc_rb_compression_options_disable_compression_algorithm_internal( |
| 104 | VALUE self, VALUE algorithm_to_disable) { |
| 105 | grpc_compression_algorithm compression_algorithm = 0; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 106 | grpc_rb_compression_options* wrapper = NULL; |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 107 | |
| 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 Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 119 | /* Gets the compression internal enum value of a compression level given its |
| 120 | * name. */ |
| 121 | grpc_compression_level grpc_rb_compression_options_level_name_to_value_internal( |
| 122 | VALUE level_name) { |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 123 | Check_Type(level_name, T_SYMBOL); |
| 124 | |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 125 | /* Check the compression level of the name passed in, and see which macro |
| 126 | * from the GRPC core header files match. */ |
Alexander Polcyn | 6c4709e | 2016-07-11 13:14:07 -0700 | [diff] [blame] | 127 | if (id_compress_level_none == SYM2ID(level_name)) { |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 128 | return GRPC_COMPRESS_LEVEL_NONE; |
Alexander Polcyn | 6c4709e | 2016-07-11 13:14:07 -0700 | [diff] [blame] | 129 | } else if (id_compress_level_low == SYM2ID(level_name)) { |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 130 | return GRPC_COMPRESS_LEVEL_LOW; |
Alexander Polcyn | 6c4709e | 2016-07-11 13:14:07 -0700 | [diff] [blame] | 131 | } else if (id_compress_level_medium == SYM2ID(level_name)) { |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 132 | return GRPC_COMPRESS_LEVEL_MED; |
Alexander Polcyn | 6c4709e | 2016-07-11 13:14:07 -0700 | [diff] [blame] | 133 | } else if (id_compress_level_high == SYM2ID(level_name)) { |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 134 | return GRPC_COMPRESS_LEVEL_HIGH; |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 135 | } |
Alexander Polcyn | 6c4709e | 2016-07-11 13:14:07 -0700 | [diff] [blame] | 136 | |
| 137 | rb_raise(rb_eArgError, |
| 138 | "Unrecognized compression level name." |
| 139 | "Valid compression level names are none, low, medium, and high."); |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 140 | |
Alex Polcyn | 7c55ab0 | 2016-07-11 23:14:32 -0700 | [diff] [blame] | 141 | /* Dummy return statement. */ |
| 142 | return GRPC_COMPRESS_LEVEL_NONE; |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* Sets the default compression level, given the name of a compression level. |
| 146 | * Throws an error if no algorithm matched. */ |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 147 | void grpc_rb_compression_options_set_default_level( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 148 | grpc_compression_options* options, VALUE new_level_name) { |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 149 | 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 Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* Gets the internal value of a compression algorithm suitable as the value |
| 155 | * in a GRPC core channel arguments hash. |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 156 | * algorithm_value is an out parameter. |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 157 | * Raises an error if the name of the algorithm passed in is invalid. */ |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 158 | void grpc_rb_compression_options_algorithm_name_to_value_internal( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 159 | grpc_compression_algorithm* algorithm_value, VALUE algorithm_name) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 160 | grpc_slice name_slice; |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 161 | VALUE algorithm_name_as_string = Qnil; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 162 | char* tmp_str = NULL; |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 163 | |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 164 | 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 Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 170 | name_slice = |
| 171 | grpc_slice_from_copied_buffer(RSTRING_PTR(algorithm_name_as_string), |
| 172 | RSTRING_LEN(algorithm_name_as_string)); |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 173 | |
| 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 Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 177 | if (!grpc_compression_algorithm_parse(name_slice, algorithm_value)) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 178 | tmp_str = grpc_slice_to_c_string(name_slice); |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 179 | rb_raise(rb_eNameError, "Invalid compression algorithm name: %s", tmp_str); |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 180 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 181 | |
| 182 | grpc_slice_unref(name_slice); |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 185 | /* Indicates whether a given algorithm is enabled on this instance, given the |
| 186 | * readable algorithm name. */ |
| 187 | VALUE grpc_rb_compression_options_is_algorithm_enabled(VALUE self, |
| 188 | VALUE algorithm_name) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 189 | grpc_rb_compression_options* wrapper = NULL; |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 190 | 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 Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 204 | /* 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 Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 206 | void grpc_rb_compression_options_set_default_algorithm( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 207 | grpc_compression_options* options, VALUE algorithm_name) { |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 208 | grpc_rb_compression_options_algorithm_name_to_value_internal( |
| 209 | &options->default_algorithm.algorithm, algorithm_name); |
| 210 | options->default_algorithm.is_set = 1; |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 213 | /* Disables an algorithm on the current instance, given the name of an |
| 214 | * algorithm. |
| 215 | * Fails if the algorithm name is invalid. */ |
| 216 | void grpc_rb_compression_options_disable_algorithm( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 217 | grpc_compression_options* compression_options, VALUE algorithm_name) { |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 218 | grpc_compression_algorithm internal_algorithm_value; |
| 219 | |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 220 | 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 Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* Provides a ruby hash of GRPC core channel argument key-values that |
| 227 | * correspond to the compression settings on this instance. */ |
| 228 | VALUE grpc_rb_compression_options_to_hash(VALUE self) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 229 | grpc_rb_compression_options* wrapper = NULL; |
| 230 | grpc_compression_options* compression_options = NULL; |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 231 | VALUE channel_arg_hash = rb_hash_new(); |
| 232 | VALUE key = Qnil; |
| 233 | VALUE value = Qnil; |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 234 | |
| 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 Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 242 | 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 Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | if (compression_options->default_algorithm.is_set) { |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 248 | 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 Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 253 | 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 Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 256 | |
| 257 | return channel_arg_hash; |
| 258 | } |
| 259 | |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 260 | /* Converts an internal enum level value to a readable level name. |
| 261 | * Fails if the level value is invalid. */ |
| 262 | VALUE 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 Polcyn | 6c4709e | 2016-07-11 13:14:07 -0700 | [diff] [blame] | 266 | return ID2SYM(id_compress_level_none); |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 267 | case GRPC_COMPRESS_LEVEL_LOW: |
Alexander Polcyn | 6c4709e | 2016-07-11 13:14:07 -0700 | [diff] [blame] | 268 | return ID2SYM(id_compress_level_low); |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 269 | case GRPC_COMPRESS_LEVEL_MED: |
Alexander Polcyn | 6c4709e | 2016-07-11 13:14:07 -0700 | [diff] [blame] | 270 | return ID2SYM(id_compress_level_medium); |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 271 | case GRPC_COMPRESS_LEVEL_HIGH: |
Alexander Polcyn | 6c4709e | 2016-07-11 13:14:07 -0700 | [diff] [blame] | 272 | return ID2SYM(id_compress_level_high); |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 273 | default: |
| 274 | rb_raise( |
| 275 | rb_eArgError, |
| 276 | "Failed to convert compression level value to name for value: %d", |
| 277 | (int)compression_value); |
Vijay Pai | c21e94c | 2016-11-02 01:08:38 -0700 | [diff] [blame] | 278 | /* return something to avoid compiler error about no return */ |
| 279 | return Qnil; |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 283 | /* Converts an algorithm internal enum value to a readable name. |
| 284 | * Fails if the enum value is invalid. */ |
| 285 | VALUE grpc_rb_compression_options_algorithm_value_to_name_internal( |
| 286 | grpc_compression_algorithm internal_value) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 287 | char* algorithm_name = NULL; |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 288 | |
| 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 Polcyn | 7c55ab0 | 2016-07-11 23:14:32 -0700 | [diff] [blame] | 296 | /* Gets the readable name of the default algorithm if one has been set. |
| 297 | * Returns nil if no algorithm has been set. */ |
| 298 | VALUE grpc_rb_compression_options_get_default_algorithm(VALUE self) { |
| 299 | grpc_compression_algorithm internal_value; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 300 | grpc_rb_compression_options* wrapper = NULL; |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 301 | |
| 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 Polcyn | 7c55ab0 | 2016-07-11 23:14:32 -0700 | [diff] [blame] | 306 | internal_value = wrapper->wrapped->default_algorithm.algorithm; |
| 307 | return grpc_rb_compression_options_algorithm_value_to_name_internal( |
| 308 | internal_value); |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | return Qnil; |
| 312 | } |
| 313 | |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 314 | /* 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. */ |
| 317 | VALUE grpc_rb_compression_options_get_default_level(VALUE self) { |
| 318 | grpc_compression_level internal_value; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 319 | grpc_rb_compression_options* wrapper = NULL; |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 320 | |
Alex Polcyn | 7c55ab0 | 2016-07-11 23:14:32 -0700 | [diff] [blame] | 321 | 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 Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 326 | 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 Polcyn | 7c55ab0 | 2016-07-11 23:14:32 -0700 | [diff] [blame] | 334 | * Returns an empty list if no algorithms have been disabled. */ |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 335 | VALUE grpc_rb_compression_options_get_disabled_algorithms(VALUE self) { |
| 336 | VALUE disabled_algorithms = rb_ary_new(); |
| 337 | grpc_compression_algorithm internal_value; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 338 | grpc_rb_compression_options* wrapper = NULL; |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 339 | |
| 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 Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 355 | /* 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 Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 366 | VALUE grpc_rb_compression_options_init(int argc, VALUE* argv, VALUE self) { |
| 367 | grpc_rb_compression_options* wrapper = NULL; |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 368 | 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 Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 419 | void 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 Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 427 | /* 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 Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 431 | |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 432 | /* 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 Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 441 | /* Determines whether or not an algorithm is enabled, given a readable |
| 442 | * algorithm name.*/ |
Alex Polcyn | 7c55ab0 | 2016-07-11 23:14:32 -0700 | [diff] [blame] | 443 | rb_define_method(grpc_rb_cCompressionOptions, "algorithm_enabled?", |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 444 | grpc_rb_compression_options_is_algorithm_enabled, 1); |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 445 | |
Alexander Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 446 | /* Provides a hash of the compression settings suitable |
Alexander Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 447 | * 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 Polcyn | d788b45 | 2016-07-06 13:58:09 -0700 | [diff] [blame] | 450 | rb_define_alias(grpc_rb_cCompressionOptions, "to_channel_arg_hash", |
| 451 | "to_hash"); |
| 452 | |
Alexander Polcyn | 6c4709e | 2016-07-11 13:14:07 -0700 | [diff] [blame] | 453 | /* 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 Polcyn | 0dccf10 | 2016-06-27 13:11:07 -0700 | [diff] [blame] | 458 | } |