grpc 1.0.1-x86-linux → 1.2.0.pre1-x86-linux

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/etc/roots.pem +228 -213
  3. data/grpc_c.32.ruby +0 -0
  4. data/grpc_c.64.ruby +0 -0
  5. data/src/ruby/ext/grpc/extconf.rb +4 -2
  6. data/src/ruby/ext/grpc/rb_byte_buffer.c +15 -7
  7. data/src/ruby/ext/grpc/rb_byte_buffer.h +3 -0
  8. data/src/ruby/ext/grpc/rb_call.c +62 -51
  9. data/src/ruby/ext/grpc/rb_channel.c +22 -7
  10. data/src/ruby/ext/grpc/rb_compression_options.c +469 -0
  11. data/src/ruby/ext/grpc/rb_compression_options.h +44 -0
  12. data/src/ruby/ext/grpc/rb_grpc.c +3 -1
  13. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +232 -190
  14. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +362 -299
  15. data/src/ruby/ext/grpc/rb_server.c +24 -16
  16. data/src/ruby/lib/grpc/2.0/grpc_c.so +0 -0
  17. data/src/ruby/lib/grpc/2.1/grpc_c.so +0 -0
  18. data/src/ruby/lib/grpc/2.2/grpc_c.so +0 -0
  19. data/src/ruby/lib/grpc/2.3/grpc_c.so +0 -0
  20. data/src/ruby/lib/grpc/2.4/grpc_c.so +0 -0
  21. data/src/ruby/lib/grpc/errors.rb +154 -2
  22. data/src/ruby/lib/grpc/generic/active_call.rb +144 -63
  23. data/src/ruby/lib/grpc/generic/bidi_call.rb +18 -2
  24. data/src/ruby/lib/grpc/generic/client_stub.rb +8 -6
  25. data/src/ruby/lib/grpc/generic/rpc_desc.rb +39 -13
  26. data/src/ruby/lib/grpc/generic/rpc_server.rb +51 -24
  27. data/src/ruby/lib/grpc/generic/service.rb +3 -2
  28. data/src/ruby/lib/grpc/grpc_c.so +0 -0
  29. data/src/ruby/lib/grpc/version.rb +1 -1
  30. data/src/ruby/pb/grpc/health/checker.rb +3 -1
  31. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +7 -0
  32. data/src/ruby/pb/test/client.rb +307 -7
  33. data/src/ruby/pb/test/server.rb +26 -1
  34. data/src/ruby/spec/compression_options_spec.rb +164 -0
  35. data/src/ruby/spec/error_sanity_spec.rb +64 -0
  36. data/src/ruby/spec/generic/active_call_spec.rb +290 -12
  37. data/src/ruby/spec/generic/client_stub_spec.rb +91 -41
  38. data/src/ruby/spec/generic/rpc_desc_spec.rb +36 -16
  39. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +22 -28
  40. data/src/ruby/spec/generic/rpc_server_spec.rb +6 -6
  41. data/src/ruby/spec/pb/health/checker_spec.rb +27 -19
  42. data/src/ruby/spec/spec_helper.rb +2 -0
  43. metadata +20 -10
Binary file
Binary file
@@ -27,6 +27,7 @@
27
27
  # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
28
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
29
 
30
+ require 'etc'
30
31
  require 'mkmf'
31
32
 
32
33
  LIBDIR = RbConfig::CONFIG['libdir']
@@ -80,7 +81,9 @@ ENV['BUILDDIR'] = output_dir
80
81
 
81
82
  unless windows
82
83
  puts 'Building internal gRPC into ' + grpc_lib_dir
83
- system("make -j -C #{grpc_root} #{grpc_lib_dir}/libgrpc.a CONFIG=#{grpc_config}")
84
+ nproc = 4
85
+ nproc = Etc.nprocessors * 2 if Etc.respond_to? :nprocessors
86
+ system("make -j#{nproc} -C #{grpc_root} #{grpc_lib_dir}/libgrpc.a CONFIG=#{grpc_config}")
84
87
  exit 1 unless $? == 0
85
88
  end
86
89
 
@@ -102,7 +105,6 @@ $CFLAGS << ' -std=c99 '
102
105
  $CFLAGS << ' -Wall '
103
106
  $CFLAGS << ' -Wextra '
104
107
  $CFLAGS << ' -pedantic '
105
- $CFLAGS << ' -Werror '
106
108
  $CFLAGS << ' -Wno-format '
107
109
 
108
110
  output = File.join('grpc', 'grpc_c')
@@ -38,20 +38,20 @@
38
38
 
39
39
  #include <grpc/grpc.h>
40
40
  #include <grpc/byte_buffer_reader.h>
41
- #include <grpc/support/slice.h>
41
+ #include <grpc/slice.h>
42
42
  #include "rb_grpc.h"
43
43
 
44
44
  grpc_byte_buffer* grpc_rb_s_to_byte_buffer(char *string, size_t length) {
45
- gpr_slice slice = gpr_slice_from_copied_buffer(string, length);
45
+ grpc_slice slice = grpc_slice_from_copied_buffer(string, length);
46
46
  grpc_byte_buffer *buffer = grpc_raw_byte_buffer_create(&slice, 1);
47
- gpr_slice_unref(slice);
47
+ grpc_slice_unref(slice);
48
48
  return buffer;
49
49
  }
50
50
 
51
51
  VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer) {
52
52
  VALUE rb_string;
53
53
  grpc_byte_buffer_reader reader;
54
- gpr_slice next;
54
+ grpc_slice next;
55
55
  if (buffer == NULL) {
56
56
  return Qnil;
57
57
  }
@@ -61,9 +61,17 @@ VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer) {
61
61
  return Qnil;
62
62
  }
63
63
  while (grpc_byte_buffer_reader_next(&reader, &next) != 0) {
64
- rb_str_cat(rb_string, (const char *) GPR_SLICE_START_PTR(next),
65
- GPR_SLICE_LENGTH(next));
66
- gpr_slice_unref(next);
64
+ rb_str_cat(rb_string, (const char *) GRPC_SLICE_START_PTR(next),
65
+ GRPC_SLICE_LENGTH(next));
66
+ grpc_slice_unref(next);
67
67
  }
68
+ grpc_byte_buffer_reader_destroy(&reader);
68
69
  return rb_string;
69
70
  }
71
+
72
+ VALUE grpc_rb_slice_to_ruby_string(grpc_slice slice) {
73
+ if (GRPC_SLICE_START_PTR(slice) == NULL) {
74
+ rb_raise(rb_eRuntimeError, "attempt to convert uninitialized grpc_slice to ruby string");
75
+ }
76
+ return rb_str_new((char*)GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice));
77
+ }
@@ -44,4 +44,7 @@ grpc_byte_buffer *grpc_rb_s_to_byte_buffer(char *string, size_t length);
44
44
  /* Converts a grpc_byte_buffer to a ruby string */
45
45
  VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer);
46
46
 
47
+ /* Converts a grpc_slice to a ruby string */
48
+ VALUE grpc_rb_slice_to_ruby_string(grpc_slice slice);
49
+
47
50
  #endif /* GRPC_RB_BYTE_BUFFER_H_ */
@@ -38,6 +38,7 @@
38
38
 
39
39
  #include <grpc/grpc.h>
40
40
  #include <grpc/support/alloc.h>
41
+ #include <grpc/impl/codegen/compression_types.h>
41
42
 
42
43
  #include "rb_byte_buffer.h"
43
44
  #include "rb_call_credentials.h"
@@ -120,8 +121,8 @@ static size_t md_ary_datasize(const void *p) {
120
121
  size_t i, datasize = sizeof(grpc_metadata_array);
121
122
  for (i = 0; i < ary->count; ++i) {
122
123
  const grpc_metadata *const md = &ary->metadata[i];
123
- datasize += strlen(md->key);
124
- datasize += md->value_length;
124
+ datasize += GRPC_SLICE_LENGTH(md->key);
125
+ datasize += GRPC_SLICE_LENGTH(md->value);
125
126
  }
126
127
  datasize += ary->capacity * sizeof(grpc_metadata);
127
128
  return datasize;
@@ -385,23 +386,23 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) {
385
386
  grpc_metadata_array *md_ary = NULL;
386
387
  long array_length;
387
388
  long i;
388
- char *key_str;
389
- size_t key_len;
390
- char *value_str;
391
- size_t value_len;
389
+ grpc_slice key_slice;
390
+ grpc_slice value_slice;
391
+ char* tmp_str;
392
392
 
393
393
  if (TYPE(key) == T_SYMBOL) {
394
- key_str = (char *)rb_id2name(SYM2ID(key));
395
- key_len = strlen(key_str);
396
- } else { /* StringValueCStr does all other type exclusions for us */
397
- key_str = StringValueCStr(key);
398
- key_len = RSTRING_LEN(key);
394
+ key_slice = grpc_slice_from_static_string(rb_id2name(SYM2ID(key)));
395
+ } else if (TYPE(key) == T_STRING) {
396
+ key_slice = grpc_slice_from_copied_buffer(RSTRING_PTR(key), RSTRING_LEN(key));
397
+ } else {
398
+ rb_raise(rb_eTypeError, "grpc_rb_md_ary_fill_hash_cb: bad type for key parameter");
399
399
  }
400
400
 
401
- if (!grpc_header_key_is_legal(key_str, key_len)) {
401
+ if (!grpc_header_key_is_legal(key_slice)) {
402
+ tmp_str = grpc_slice_to_c_string(key_slice);
402
403
  rb_raise(rb_eArgError,
403
404
  "'%s' is an invalid header key, must match [a-z0-9-_.]+",
404
- key_str);
405
+ tmp_str);
405
406
  return ST_STOP;
406
407
  }
407
408
 
@@ -413,33 +414,31 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) {
413
414
  array_length = RARRAY_LEN(val);
414
415
  /* If the value is an array, add capacity for each value in the array */
415
416
  for (i = 0; i < array_length; i++) {
416
- value_str = RSTRING_PTR(rb_ary_entry(val, i));
417
- value_len = RSTRING_LEN(rb_ary_entry(val, i));
418
- if (!grpc_is_binary_header(key_str, key_len) &&
419
- !grpc_header_nonbin_value_is_legal(value_str, value_len)) {
417
+ value_slice = grpc_slice_from_copied_buffer(RSTRING_PTR(rb_ary_entry(val, i)), RSTRING_LEN(rb_ary_entry(val, i)));
418
+ if (!grpc_is_binary_header(key_slice) &&
419
+ !grpc_header_nonbin_value_is_legal(value_slice)) {
420
420
  // The value has invalid characters
421
+ tmp_str = grpc_slice_to_c_string(value_slice);
421
422
  rb_raise(rb_eArgError,
422
- "Header value '%s' has invalid characters", value_str);
423
+ "Header value '%s' has invalid characters", tmp_str);
423
424
  return ST_STOP;
424
425
  }
425
- md_ary->metadata[md_ary->count].key = key_str;
426
- md_ary->metadata[md_ary->count].value = value_str;
427
- md_ary->metadata[md_ary->count].value_length = value_len;
426
+ md_ary->metadata[md_ary->count].key = key_slice;
427
+ md_ary->metadata[md_ary->count].value = value_slice;
428
428
  md_ary->count += 1;
429
429
  }
430
430
  } else if (TYPE(val) == T_STRING) {
431
- value_str = RSTRING_PTR(val);
432
- value_len = RSTRING_LEN(val);
433
- if (!grpc_is_binary_header(key_str, key_len) &&
434
- !grpc_header_nonbin_value_is_legal(value_str, value_len)) {
431
+ value_slice = grpc_slice_from_copied_buffer(RSTRING_PTR(val), RSTRING_LEN(val));
432
+ if (!grpc_is_binary_header(key_slice) &&
433
+ !grpc_header_nonbin_value_is_legal(value_slice)) {
435
434
  // The value has invalid characters
435
+ tmp_str = grpc_slice_to_c_string(value_slice);
436
436
  rb_raise(rb_eArgError,
437
- "Header value '%s' has invalid characters", value_str);
437
+ "Header value '%s' has invalid characters", tmp_str);
438
438
  return ST_STOP;
439
439
  }
440
- md_ary->metadata[md_ary->count].key = key_str;
441
- md_ary->metadata[md_ary->count].value = value_str;
442
- md_ary->metadata[md_ary->count].value_length = value_len;
440
+ md_ary->metadata[md_ary->count].key = key_slice;
441
+ md_ary->metadata[md_ary->count].value = value_slice;
443
442
  md_ary->count += 1;
444
443
  } else {
445
444
  rb_raise(rb_eArgError,
@@ -505,22 +504,19 @@ VALUE grpc_rb_md_ary_to_h(grpc_metadata_array *md_ary) {
505
504
  size_t i;
506
505
 
507
506
  for (i = 0; i < md_ary->count; i++) {
508
- key = rb_str_new2(md_ary->metadata[i].key);
507
+ key = grpc_rb_slice_to_ruby_string(md_ary->metadata[i].key);
509
508
  value = rb_hash_aref(result, key);
510
509
  if (value == Qnil) {
511
- value = rb_str_new(md_ary->metadata[i].value,
512
- md_ary->metadata[i].value_length);
510
+ value = grpc_rb_slice_to_ruby_string(md_ary->metadata[i].value);
513
511
  rb_hash_aset(result, key, value);
514
512
  } else if (TYPE(value) == T_ARRAY) {
515
513
  /* Add the string to the returned array */
516
- rb_ary_push(value, rb_str_new(md_ary->metadata[i].value,
517
- md_ary->metadata[i].value_length));
514
+ rb_ary_push(value, grpc_rb_slice_to_ruby_string(md_ary->metadata[i].value));
518
515
  } else {
519
516
  /* Add the current value with this key and the new one to an array */
520
517
  new_ary = rb_ary_new();
521
518
  rb_ary_push(new_ary, value);
522
- rb_ary_push(new_ary, rb_str_new(md_ary->metadata[i].value,
523
- md_ary->metadata[i].value_length));
519
+ rb_ary_push(new_ary, grpc_rb_slice_to_ruby_string(md_ary->metadata[i].value));
524
520
  rb_hash_aset(result, key, new_ary);
525
521
  }
526
522
  }
@@ -562,6 +558,7 @@ static int grpc_rb_call_check_op_keys_hash_cb(VALUE key, VALUE val,
562
558
  */
563
559
  static void grpc_rb_op_update_status_from_server(grpc_op *op,
564
560
  grpc_metadata_array *md_ary,
561
+ grpc_slice *send_status_details,
565
562
  VALUE status) {
566
563
  VALUE code = rb_struct_aref(status, sym_code);
567
564
  VALUE details = rb_struct_aref(status, sym_details);
@@ -578,8 +575,11 @@ static void grpc_rb_op_update_status_from_server(grpc_op *op,
578
575
  rb_obj_classname(code));
579
576
  return;
580
577
  }
578
+
579
+ *send_status_details = grpc_slice_from_copied_buffer(RSTRING_PTR(details), RSTRING_LEN(details));
580
+
581
581
  op->data.send_status_from_server.status = NUM2INT(code);
582
- op->data.send_status_from_server.status_details = StringValueCStr(details);
582
+ op->data.send_status_from_server.status_details = send_status_details;
583
583
  grpc_rb_md_ary_convert(metadata_hash, md_ary);
584
584
  op->data.send_status_from_server.trailing_metadata_count = md_ary->count;
585
585
  op->data.send_status_from_server.trailing_metadata = md_ary->metadata;
@@ -602,9 +602,9 @@ typedef struct run_batch_stack {
602
602
  grpc_metadata_array recv_trailing_metadata;
603
603
  int recv_cancelled;
604
604
  grpc_status_code recv_status;
605
- char *recv_status_details;
606
- size_t recv_status_details_capacity;
605
+ grpc_slice recv_status_details;
607
606
  unsigned write_flag;
607
+ grpc_slice send_status_details;
608
608
  } run_batch_stack;
609
609
 
610
610
  /* grpc_run_batch_stack_init ensures the run_batch_stack is properly
@@ -630,8 +630,12 @@ static void grpc_run_batch_stack_cleanup(run_batch_stack *st) {
630
630
  grpc_metadata_array_destroy(&st->recv_metadata);
631
631
  grpc_metadata_array_destroy(&st->recv_trailing_metadata);
632
632
 
633
- if (st->recv_status_details != NULL) {
634
- gpr_free(st->recv_status_details);
633
+ if (GRPC_SLICE_START_PTR(st->send_status_details) != NULL) {
634
+ grpc_slice_unref(st->send_status_details);
635
+ }
636
+
637
+ if (GRPC_SLICE_START_PTR(st->recv_status_details) != NULL) {
638
+ grpc_slice_unref(st->recv_status_details);
635
639
  }
636
640
 
637
641
  if (st->recv_message != NULL) {
@@ -640,7 +644,7 @@ static void grpc_run_batch_stack_cleanup(run_batch_stack *st) {
640
644
 
641
645
  for (i = 0; i < st->op_num; i++) {
642
646
  if (st->ops[i].op == GRPC_OP_SEND_MESSAGE) {
643
- grpc_byte_buffer_destroy(st->ops[i].data.send_message);
647
+ grpc_byte_buffer_destroy(st->ops[i].data.send_message.send_message);
644
648
  }
645
649
  }
646
650
  }
@@ -672,8 +676,9 @@ static void grpc_run_batch_stack_fill_ops(run_batch_stack *st, VALUE ops_hash) {
672
676
  st->send_metadata.metadata;
673
677
  break;
674
678
  case GRPC_OP_SEND_MESSAGE:
675
- st->ops[st->op_num].data.send_message = grpc_rb_s_to_byte_buffer(
676
- RSTRING_PTR(this_value), RSTRING_LEN(this_value));
679
+ st->ops[st->op_num].data.send_message.send_message =
680
+ grpc_rb_s_to_byte_buffer(RSTRING_PTR(this_value),
681
+ RSTRING_LEN(this_value));
677
682
  st->ops[st->op_num].flags = st->write_flag;
678
683
  break;
679
684
  case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
@@ -682,13 +687,14 @@ static void grpc_run_batch_stack_fill_ops(run_batch_stack *st, VALUE ops_hash) {
682
687
  /* N.B. later there is no need to explicitly delete the metadata keys
683
688
  * and values, they are references to data in ruby objects. */
684
689
  grpc_rb_op_update_status_from_server(
685
- &st->ops[st->op_num], &st->send_trailing_metadata, this_value);
690
+ &st->ops[st->op_num], &st->send_trailing_metadata, &st->send_status_details, this_value);
686
691
  break;
687
692
  case GRPC_OP_RECV_INITIAL_METADATA:
688
- st->ops[st->op_num].data.recv_initial_metadata = &st->recv_metadata;
693
+ st->ops[st->op_num].data.recv_initial_metadata.recv_initial_metadata =
694
+ &st->recv_metadata;
689
695
  break;
690
696
  case GRPC_OP_RECV_MESSAGE:
691
- st->ops[st->op_num].data.recv_message = &st->recv_message;
697
+ st->ops[st->op_num].data.recv_message.recv_message = &st->recv_message;
692
698
  break;
693
699
  case GRPC_OP_RECV_STATUS_ON_CLIENT:
694
700
  st->ops[st->op_num].data.recv_status_on_client.trailing_metadata =
@@ -697,8 +703,6 @@ static void grpc_run_batch_stack_fill_ops(run_batch_stack *st, VALUE ops_hash) {
697
703
  &st->recv_status;
698
704
  st->ops[st->op_num].data.recv_status_on_client.status_details =
699
705
  &st->recv_status_details;
700
- st->ops[st->op_num].data.recv_status_on_client.status_details_capacity =
701
- &st->recv_status_details_capacity;
702
706
  break;
703
707
  case GRPC_OP_RECV_CLOSE_ON_SERVER:
704
708
  st->ops[st->op_num].data.recv_close_on_server.cancelled =
@@ -746,9 +750,9 @@ static VALUE grpc_run_batch_stack_build_result(run_batch_stack *st) {
746
750
  rb_struct_aset(
747
751
  result, sym_status,
748
752
  rb_struct_new(grpc_rb_sStatus, UINT2NUM(st->recv_status),
749
- (st->recv_status_details == NULL
753
+ (GRPC_SLICE_START_PTR(st->recv_status_details) == NULL
750
754
  ? Qnil
751
- : rb_str_new2(st->recv_status_details)),
755
+ : grpc_rb_slice_to_ruby_string(st->recv_status_details)),
752
756
  grpc_rb_md_ary_to_h(&st->recv_trailing_metadata),
753
757
  NULL));
754
758
  break;
@@ -910,6 +914,12 @@ static void Init_grpc_op_codes() {
910
914
  UINT2NUM(GRPC_OP_RECV_CLOSE_ON_SERVER));
911
915
  }
912
916
 
917
+ static void Init_grpc_metadata_keys() {
918
+ VALUE grpc_rb_mMetadataKeys = rb_define_module_under(grpc_rb_mGrpcCore, "MetadataKeys");
919
+ rb_define_const(grpc_rb_mMetadataKeys, "COMPRESSION_REQUEST_ALGORITHM",
920
+ rb_str_new2(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY));
921
+ }
922
+
913
923
  void Init_grpc_call() {
914
924
  /* CallError inherits from Exception to signal that it is non-recoverable */
915
925
  grpc_rb_eCallError =
@@ -972,6 +982,7 @@ void Init_grpc_call() {
972
982
  Init_grpc_error_codes();
973
983
  Init_grpc_op_codes();
974
984
  Init_grpc_write_flags();
985
+ Init_grpc_metadata_keys();
975
986
  }
976
987
 
977
988
  /* Gets the call from the ruby object */
@@ -35,6 +35,7 @@
35
35
 
36
36
  #include "rb_grpc_imports.generated.h"
37
37
  #include "rb_channel.h"
38
+ #include "rb_byte_buffer.h"
38
39
 
39
40
  #include <grpc/grpc.h>
40
41
  #include <grpc/grpc_security.h>
@@ -252,10 +253,14 @@ static VALUE grpc_rb_channel_create_call(VALUE self, VALUE parent,
252
253
  grpc_channel *ch = NULL;
253
254
  grpc_completion_queue *cq = NULL;
254
255
  int flags = GRPC_PROPAGATE_DEFAULTS;
255
- char *method_chars = StringValueCStr(method);
256
- char *host_chars = NULL;
256
+ grpc_slice method_slice;
257
+ grpc_slice host_slice;
258
+ grpc_slice *host_slice_ptr = NULL;
259
+ char* tmp_str = NULL;
260
+
257
261
  if (host != Qnil) {
258
- host_chars = StringValueCStr(host);
262
+ host_slice = grpc_slice_from_copied_buffer(RSTRING_PTR(host), RSTRING_LEN(host));
263
+ host_slice_ptr = &host_slice;
259
264
  }
260
265
  if (mask != Qnil) {
261
266
  flags = NUM2UINT(mask);
@@ -272,15 +277,25 @@ static VALUE grpc_rb_channel_create_call(VALUE self, VALUE parent,
272
277
  return Qnil;
273
278
  }
274
279
 
275
- call = grpc_channel_create_call(ch, parent_call, flags, cq, method_chars,
276
- host_chars, grpc_rb_time_timeval(
280
+ method_slice = grpc_slice_from_copied_buffer(RSTRING_PTR(method), RSTRING_LEN(method));
281
+
282
+ call = grpc_channel_create_call(ch, parent_call, flags, cq, method_slice,
283
+ host_slice_ptr, grpc_rb_time_timeval(
277
284
  deadline,
278
285
  /* absolute time */ 0), NULL);
286
+
279
287
  if (call == NULL) {
288
+ tmp_str = grpc_slice_to_c_string(method_slice);
280
289
  rb_raise(rb_eRuntimeError, "cannot create call with method %s",
281
- method_chars);
290
+ tmp_str);
282
291
  return Qnil;
283
292
  }
293
+
294
+ grpc_slice_unref(method_slice);
295
+ if (host_slice_ptr != NULL) {
296
+ grpc_slice_unref(host_slice);
297
+ }
298
+
284
299
  res = grpc_rb_wrap_call(call, cq);
285
300
 
286
301
  /* Make this channel an instance attribute of the call so that it is not GCed
@@ -386,7 +401,7 @@ void Init_grpc_channel() {
386
401
  rb_define_const(grpc_rb_cChannel, "MAX_CONCURRENT_STREAMS",
387
402
  ID2SYM(rb_intern(GRPC_ARG_MAX_CONCURRENT_STREAMS)));
388
403
  rb_define_const(grpc_rb_cChannel, "MAX_MESSAGE_LENGTH",
389
- ID2SYM(rb_intern(GRPC_ARG_MAX_MESSAGE_LENGTH)));
404
+ ID2SYM(rb_intern(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH)));
390
405
  id_insecure_channel = rb_intern("this_channel_is_insecure");
391
406
  Init_grpc_propagate_masks();
392
407
  Init_grpc_connectivity_states();
@@ -0,0 +1,469 @@
1
+ /*
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_byte_buffer.h"
38
+ #include "rb_grpc_imports.generated.h"
39
+
40
+ #include <grpc/compression.h>
41
+ #include <grpc/grpc.h>
42
+ #include <grpc/support/alloc.h>
43
+ #include <grpc/impl/codegen/compression_types.h>
44
+ #include <grpc/impl/codegen/grpc_types.h>
45
+ #include <string.h>
46
+
47
+ #include "rb_grpc.h"
48
+
49
+ static VALUE grpc_rb_cCompressionOptions = Qnil;
50
+
51
+ /* Ruby Ids for the names of valid compression levels. */
52
+ static VALUE id_compress_level_none = Qnil;
53
+ static VALUE id_compress_level_low = Qnil;
54
+ static VALUE id_compress_level_medium = Qnil;
55
+ static VALUE id_compress_level_high = Qnil;
56
+
57
+ /* grpc_rb_compression_options wraps a grpc_compression_options.
58
+ * It can be used to get the channel argument key-values for specific
59
+ * compression settings. */
60
+
61
+ /* Note that ruby objects of this type don't carry any state in other
62
+ * Ruby objects and don't have a mark for GC. */
63
+ typedef struct grpc_rb_compression_options {
64
+ /* The actual compression options that's being wrapped */
65
+ grpc_compression_options *wrapped;
66
+ } grpc_rb_compression_options;
67
+
68
+ /* Destroys the compression options instances and free the
69
+ * wrapped grpc compression options. */
70
+ static void grpc_rb_compression_options_free(void *p) {
71
+ grpc_rb_compression_options *wrapper = NULL;
72
+ if (p == NULL) {
73
+ return;
74
+ };
75
+ wrapper = (grpc_rb_compression_options *)p;
76
+
77
+ if (wrapper->wrapped != NULL) {
78
+ gpr_free(wrapper->wrapped);
79
+ wrapper->wrapped = NULL;
80
+ }
81
+
82
+ xfree(p);
83
+ }
84
+
85
+ /* Ruby recognized data type for the CompressionOptions class. */
86
+ static rb_data_type_t grpc_rb_compression_options_data_type = {
87
+ "grpc_compression_options",
88
+ {NULL,
89
+ grpc_rb_compression_options_free,
90
+ GRPC_RB_MEMSIZE_UNAVAILABLE,
91
+ {NULL, NULL}},
92
+ NULL,
93
+ NULL,
94
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
95
+ RUBY_TYPED_FREE_IMMEDIATELY
96
+ #endif
97
+ };
98
+
99
+ /* Allocates CompressionOptions instances.
100
+ Allocate the wrapped grpc compression options and
101
+ initialize it here too. */
102
+ static VALUE grpc_rb_compression_options_alloc(VALUE cls) {
103
+ grpc_rb_compression_options *wrapper =
104
+ gpr_malloc(sizeof(grpc_rb_compression_options));
105
+ wrapper->wrapped = NULL;
106
+ wrapper->wrapped = gpr_malloc(sizeof(grpc_compression_options));
107
+ grpc_compression_options_init(wrapper->wrapped);
108
+
109
+ return TypedData_Wrap_Struct(cls, &grpc_rb_compression_options_data_type,
110
+ wrapper);
111
+ }
112
+
113
+ /* Disables a compression algorithm, given the GRPC core internal number of a
114
+ * compression algorithm. */
115
+ VALUE grpc_rb_compression_options_disable_compression_algorithm_internal(
116
+ VALUE self, VALUE algorithm_to_disable) {
117
+ grpc_compression_algorithm compression_algorithm = 0;
118
+ grpc_rb_compression_options *wrapper = NULL;
119
+
120
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
121
+ &grpc_rb_compression_options_data_type, wrapper);
122
+ compression_algorithm =
123
+ (grpc_compression_algorithm)NUM2INT(algorithm_to_disable);
124
+
125
+ grpc_compression_options_disable_algorithm(wrapper->wrapped,
126
+ compression_algorithm);
127
+
128
+ return Qnil;
129
+ }
130
+
131
+ /* Gets the compression internal enum value of a compression level given its
132
+ * name. */
133
+ grpc_compression_level grpc_rb_compression_options_level_name_to_value_internal(
134
+ VALUE level_name) {
135
+ Check_Type(level_name, T_SYMBOL);
136
+
137
+ /* Check the compression level of the name passed in, and see which macro
138
+ * from the GRPC core header files match. */
139
+ if (id_compress_level_none == SYM2ID(level_name)) {
140
+ return GRPC_COMPRESS_LEVEL_NONE;
141
+ } else if (id_compress_level_low == SYM2ID(level_name)) {
142
+ return GRPC_COMPRESS_LEVEL_LOW;
143
+ } else if (id_compress_level_medium == SYM2ID(level_name)) {
144
+ return GRPC_COMPRESS_LEVEL_MED;
145
+ } else if (id_compress_level_high == SYM2ID(level_name)) {
146
+ return GRPC_COMPRESS_LEVEL_HIGH;
147
+ }
148
+
149
+ rb_raise(rb_eArgError,
150
+ "Unrecognized compression level name."
151
+ "Valid compression level names are none, low, medium, and high.");
152
+
153
+ /* Dummy return statement. */
154
+ return GRPC_COMPRESS_LEVEL_NONE;
155
+ }
156
+
157
+ /* Sets the default compression level, given the name of a compression level.
158
+ * Throws an error if no algorithm matched. */
159
+ void grpc_rb_compression_options_set_default_level(
160
+ grpc_compression_options *options, VALUE new_level_name) {
161
+ options->default_level.level =
162
+ grpc_rb_compression_options_level_name_to_value_internal(new_level_name);
163
+ options->default_level.is_set = 1;
164
+ }
165
+
166
+ /* Gets the internal value of a compression algorithm suitable as the value
167
+ * in a GRPC core channel arguments hash.
168
+ * algorithm_value is an out parameter.
169
+ * Raises an error if the name of the algorithm passed in is invalid. */
170
+ void grpc_rb_compression_options_algorithm_name_to_value_internal(
171
+ grpc_compression_algorithm *algorithm_value, VALUE algorithm_name) {
172
+ grpc_slice name_slice;
173
+ VALUE algorithm_name_as_string = Qnil;
174
+ char *tmp_str = NULL;
175
+
176
+ Check_Type(algorithm_name, T_SYMBOL);
177
+
178
+ /* Convert the algorithm symbol to a ruby string, so that we can get the
179
+ * correct C string out of it. */
180
+ algorithm_name_as_string = rb_funcall(algorithm_name, rb_intern("to_s"), 0);
181
+
182
+ name_slice = grpc_slice_from_copied_buffer(RSTRING_PTR(algorithm_name_as_string), RSTRING_LEN(algorithm_name_as_string));
183
+
184
+ /* Raise an error if the name isn't recognized as a compression algorithm by
185
+ * the algorithm parse function
186
+ * in GRPC core. */
187
+ if(!grpc_compression_algorithm_parse(name_slice, algorithm_value)) {
188
+ tmp_str = grpc_slice_to_c_string(name_slice);
189
+ rb_raise(rb_eNameError, "Invalid compression algorithm name: %s",
190
+ tmp_str);
191
+ }
192
+
193
+ grpc_slice_unref(name_slice);
194
+ }
195
+
196
+ /* Indicates whether a given algorithm is enabled on this instance, given the
197
+ * readable algorithm name. */
198
+ VALUE grpc_rb_compression_options_is_algorithm_enabled(VALUE self,
199
+ VALUE algorithm_name) {
200
+ grpc_rb_compression_options *wrapper = NULL;
201
+ grpc_compression_algorithm internal_algorithm_value;
202
+
203
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
204
+ &grpc_rb_compression_options_data_type, wrapper);
205
+ grpc_rb_compression_options_algorithm_name_to_value_internal(
206
+ &internal_algorithm_value, algorithm_name);
207
+
208
+ if (grpc_compression_options_is_algorithm_enabled(wrapper->wrapped,
209
+ internal_algorithm_value)) {
210
+ return Qtrue;
211
+ }
212
+ return Qfalse;
213
+ }
214
+
215
+ /* Sets the default algorithm to the name of the algorithm passed in.
216
+ * Raises an error if the name is not a valid compression algorithm name. */
217
+ void grpc_rb_compression_options_set_default_algorithm(
218
+ grpc_compression_options *options, VALUE algorithm_name) {
219
+ grpc_rb_compression_options_algorithm_name_to_value_internal(
220
+ &options->default_algorithm.algorithm, algorithm_name);
221
+ options->default_algorithm.is_set = 1;
222
+ }
223
+
224
+ /* Disables an algorithm on the current instance, given the name of an
225
+ * algorithm.
226
+ * Fails if the algorithm name is invalid. */
227
+ void grpc_rb_compression_options_disable_algorithm(
228
+ grpc_compression_options *compression_options, VALUE algorithm_name) {
229
+ grpc_compression_algorithm internal_algorithm_value;
230
+
231
+ grpc_rb_compression_options_algorithm_name_to_value_internal(
232
+ &internal_algorithm_value, algorithm_name);
233
+ grpc_compression_options_disable_algorithm(compression_options,
234
+ internal_algorithm_value);
235
+ }
236
+
237
+ /* Provides a ruby hash of GRPC core channel argument key-values that
238
+ * correspond to the compression settings on this instance. */
239
+ VALUE grpc_rb_compression_options_to_hash(VALUE self) {
240
+ grpc_rb_compression_options *wrapper = NULL;
241
+ grpc_compression_options *compression_options = NULL;
242
+ VALUE channel_arg_hash = rb_hash_new();
243
+ VALUE key = Qnil;
244
+ VALUE value = Qnil;
245
+
246
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
247
+ &grpc_rb_compression_options_data_type, wrapper);
248
+ compression_options = wrapper->wrapped;
249
+
250
+ /* Add key-value pairs to the new Ruby hash. It can be used
251
+ * as GRPC core channel arguments. */
252
+ if (compression_options->default_level.is_set) {
253
+ key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL);
254
+ value = INT2NUM((int)compression_options->default_level.level);
255
+ rb_hash_aset(channel_arg_hash, key, value);
256
+ }
257
+
258
+ if (compression_options->default_algorithm.is_set) {
259
+ key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM);
260
+ value = INT2NUM((int)compression_options->default_algorithm.algorithm);
261
+ rb_hash_aset(channel_arg_hash, key, value);
262
+ }
263
+
264
+ key = rb_str_new2(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET);
265
+ value = INT2NUM((int)compression_options->enabled_algorithms_bitset);
266
+ rb_hash_aset(channel_arg_hash, key, value);
267
+
268
+ return channel_arg_hash;
269
+ }
270
+
271
+ /* Converts an internal enum level value to a readable level name.
272
+ * Fails if the level value is invalid. */
273
+ VALUE grpc_rb_compression_options_level_value_to_name_internal(
274
+ grpc_compression_level compression_value) {
275
+ switch (compression_value) {
276
+ case GRPC_COMPRESS_LEVEL_NONE:
277
+ return ID2SYM(id_compress_level_none);
278
+ case GRPC_COMPRESS_LEVEL_LOW:
279
+ return ID2SYM(id_compress_level_low);
280
+ case GRPC_COMPRESS_LEVEL_MED:
281
+ return ID2SYM(id_compress_level_medium);
282
+ case GRPC_COMPRESS_LEVEL_HIGH:
283
+ return ID2SYM(id_compress_level_high);
284
+ default:
285
+ rb_raise(
286
+ rb_eArgError,
287
+ "Failed to convert compression level value to name for value: %d",
288
+ (int)compression_value);
289
+ /* return something to avoid compiler error about no return */
290
+ return Qnil;
291
+ }
292
+ }
293
+
294
+ /* Converts an algorithm internal enum value to a readable name.
295
+ * Fails if the enum value is invalid. */
296
+ VALUE grpc_rb_compression_options_algorithm_value_to_name_internal(
297
+ grpc_compression_algorithm internal_value) {
298
+ char *algorithm_name = NULL;
299
+
300
+ if (!grpc_compression_algorithm_name(internal_value, &algorithm_name)) {
301
+ rb_raise(rb_eArgError, "Failed to convert algorithm value to name");
302
+ }
303
+
304
+ return ID2SYM(rb_intern(algorithm_name));
305
+ }
306
+
307
+ /* Gets the readable name of the default algorithm if one has been set.
308
+ * Returns nil if no algorithm has been set. */
309
+ VALUE grpc_rb_compression_options_get_default_algorithm(VALUE self) {
310
+ grpc_compression_algorithm internal_value;
311
+ grpc_rb_compression_options *wrapper = NULL;
312
+
313
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
314
+ &grpc_rb_compression_options_data_type, wrapper);
315
+
316
+ if (wrapper->wrapped->default_algorithm.is_set) {
317
+ internal_value = wrapper->wrapped->default_algorithm.algorithm;
318
+ return grpc_rb_compression_options_algorithm_value_to_name_internal(
319
+ internal_value);
320
+ }
321
+
322
+ return Qnil;
323
+ }
324
+
325
+ /* Gets the internal value of the default compression level that is to be passed
326
+ * to the GRPC core as a channel argument value.
327
+ * A nil return value means that it hasn't been set. */
328
+ VALUE grpc_rb_compression_options_get_default_level(VALUE self) {
329
+ grpc_compression_level internal_value;
330
+ grpc_rb_compression_options *wrapper = NULL;
331
+
332
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
333
+ &grpc_rb_compression_options_data_type, wrapper);
334
+
335
+ if (wrapper->wrapped->default_level.is_set) {
336
+ internal_value = wrapper->wrapped->default_level.level;
337
+ return grpc_rb_compression_options_level_value_to_name_internal(
338
+ internal_value);
339
+ }
340
+
341
+ return Qnil;
342
+ }
343
+
344
+ /* Gets a list of the disabled algorithms as readable names.
345
+ * Returns an empty list if no algorithms have been disabled. */
346
+ VALUE grpc_rb_compression_options_get_disabled_algorithms(VALUE self) {
347
+ VALUE disabled_algorithms = rb_ary_new();
348
+ grpc_compression_algorithm internal_value;
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
+ for (internal_value = GRPC_COMPRESS_NONE;
355
+ internal_value < GRPC_COMPRESS_ALGORITHMS_COUNT; internal_value++) {
356
+ if (!grpc_compression_options_is_algorithm_enabled(wrapper->wrapped,
357
+ internal_value)) {
358
+ rb_ary_push(disabled_algorithms,
359
+ grpc_rb_compression_options_algorithm_value_to_name_internal(
360
+ internal_value));
361
+ }
362
+ }
363
+ return disabled_algorithms;
364
+ }
365
+
366
+ /* Initializes the compression options wrapper.
367
+ * Takes an optional hash parameter.
368
+ *
369
+ * Example call-seq:
370
+ * options = CompressionOptions.new(
371
+ * default_level: :none,
372
+ * disabled_algorithms: [:gzip]
373
+ * )
374
+ * channel_arg hash = Hash.new[...]
375
+ * channel_arg_hash_with_compression_options = channel_arg_hash.merge(options)
376
+ */
377
+ VALUE grpc_rb_compression_options_init(int argc, VALUE *argv, VALUE self) {
378
+ grpc_rb_compression_options *wrapper = NULL;
379
+ VALUE default_algorithm = Qnil;
380
+ VALUE default_level = Qnil;
381
+ VALUE disabled_algorithms = Qnil;
382
+ VALUE algorithm_name = Qnil;
383
+ VALUE hash_arg = Qnil;
384
+
385
+ rb_scan_args(argc, argv, "01", &hash_arg);
386
+
387
+ /* Check if the hash parameter was passed, or if invalid arguments were
388
+ * passed. */
389
+ if (hash_arg == Qnil) {
390
+ return self;
391
+ } else if (TYPE(hash_arg) != T_HASH || argc > 1) {
392
+ rb_raise(rb_eArgError,
393
+ "Invalid arguments. Expecting optional hash parameter");
394
+ }
395
+
396
+ TypedData_Get_Struct(self, grpc_rb_compression_options,
397
+ &grpc_rb_compression_options_data_type, wrapper);
398
+
399
+ /* Set the default algorithm if one was chosen. */
400
+ default_algorithm =
401
+ rb_hash_aref(hash_arg, ID2SYM(rb_intern("default_algorithm")));
402
+ if (default_algorithm != Qnil) {
403
+ grpc_rb_compression_options_set_default_algorithm(wrapper->wrapped,
404
+ default_algorithm);
405
+ }
406
+
407
+ /* Set the default level if one was chosen. */
408
+ default_level = rb_hash_aref(hash_arg, ID2SYM(rb_intern("default_level")));
409
+ if (default_level != Qnil) {
410
+ grpc_rb_compression_options_set_default_level(wrapper->wrapped,
411
+ default_level);
412
+ }
413
+
414
+ /* Set the disabled algorithms if any were chosen. */
415
+ disabled_algorithms =
416
+ rb_hash_aref(hash_arg, ID2SYM(rb_intern("disabled_algorithms")));
417
+ if (disabled_algorithms != Qnil) {
418
+ Check_Type(disabled_algorithms, T_ARRAY);
419
+
420
+ for (int i = 0; i < RARRAY_LEN(disabled_algorithms); i++) {
421
+ algorithm_name = rb_ary_entry(disabled_algorithms, i);
422
+ grpc_rb_compression_options_disable_algorithm(wrapper->wrapped,
423
+ algorithm_name);
424
+ }
425
+ }
426
+
427
+ return self;
428
+ }
429
+
430
+ void Init_grpc_compression_options() {
431
+ grpc_rb_cCompressionOptions = rb_define_class_under(
432
+ grpc_rb_mGrpcCore, "CompressionOptions", rb_cObject);
433
+
434
+ /* Allocates an object managed by the ruby runtime. */
435
+ rb_define_alloc_func(grpc_rb_cCompressionOptions,
436
+ grpc_rb_compression_options_alloc);
437
+
438
+ /* Initializes the ruby wrapper. #new method takes an optional hash argument.
439
+ */
440
+ rb_define_method(grpc_rb_cCompressionOptions, "initialize",
441
+ grpc_rb_compression_options_init, -1);
442
+
443
+ /* Methods for getting the default algorithm, default level, and disabled
444
+ * algorithms as readable names. */
445
+ rb_define_method(grpc_rb_cCompressionOptions, "default_algorithm",
446
+ grpc_rb_compression_options_get_default_algorithm, 0);
447
+ rb_define_method(grpc_rb_cCompressionOptions, "default_level",
448
+ grpc_rb_compression_options_get_default_level, 0);
449
+ rb_define_method(grpc_rb_cCompressionOptions, "disabled_algorithms",
450
+ grpc_rb_compression_options_get_disabled_algorithms, 0);
451
+
452
+ /* Determines whether or not an algorithm is enabled, given a readable
453
+ * algorithm name.*/
454
+ rb_define_method(grpc_rb_cCompressionOptions, "algorithm_enabled?",
455
+ grpc_rb_compression_options_is_algorithm_enabled, 1);
456
+
457
+ /* Provides a hash of the compression settings suitable
458
+ * for passing to server or channel args. */
459
+ rb_define_method(grpc_rb_cCompressionOptions, "to_hash",
460
+ grpc_rb_compression_options_to_hash, 0);
461
+ rb_define_alias(grpc_rb_cCompressionOptions, "to_channel_arg_hash",
462
+ "to_hash");
463
+
464
+ /* Ruby ids for the names of the different compression levels. */
465
+ id_compress_level_none = rb_intern("none");
466
+ id_compress_level_low = rb_intern("low");
467
+ id_compress_level_medium = rb_intern("medium");
468
+ id_compress_level_high = rb_intern("high");
469
+ }