nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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 "rb_grpc.h" |
| 35 | |
| 36 | #include <math.h> |
| 37 | #include <ruby.h> |
| 38 | #include <sys/time.h> |
| 39 | |
| 40 | #include <grpc/grpc.h> |
| 41 | #include <grpc/support/time.h> |
| 42 | #include "rb_byte_buffer.h" |
| 43 | #include "rb_call.h" |
| 44 | #include "rb_channel.h" |
| 45 | #include "rb_completion_queue.h" |
| 46 | #include "rb_event.h" |
| 47 | #include "rb_metadata.h" |
| 48 | #include "rb_server.h" |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 49 | #include "rb_credentials.h" |
| 50 | #include "rb_server_credentials.h" |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 51 | |
| 52 | /* Define common vars and funcs declared in rb.h */ |
| 53 | const RUBY_DATA_FUNC GC_NOT_MARKED = NULL; |
| 54 | const RUBY_DATA_FUNC GC_DONT_FREE = NULL; |
| 55 | |
| 56 | VALUE rb_cTimeVal = Qnil; |
| 57 | |
| 58 | /* Alloc func that blocks allocation of a given object by raising an |
| 59 | * exception. */ |
| 60 | VALUE grpc_rb_cannot_alloc(VALUE cls) { |
| 61 | rb_raise(rb_eTypeError, |
| 62 | "allocation of %s only allowed from the gRPC native layer", |
| 63 | rb_class2name(cls)); |
| 64 | return Qnil; |
| 65 | } |
| 66 | |
| 67 | /* Init func that fails by raising an exception. */ |
| 68 | VALUE grpc_rb_cannot_init(VALUE self) { |
| 69 | rb_raise(rb_eTypeError, |
| 70 | "initialization of %s only allowed from the gRPC native layer", |
| 71 | rb_obj_classname(self)); |
| 72 | return Qnil; |
| 73 | } |
| 74 | |
| 75 | /* Init/Clone func that fails by raising an exception. */ |
| 76 | VALUE grpc_rb_cannot_init_copy(VALUE copy, VALUE self) { |
| 77 | rb_raise(rb_eTypeError, |
| 78 | "initialization of %s only allowed from the gRPC native layer", |
| 79 | rb_obj_classname(copy)); |
| 80 | return Qnil; |
| 81 | } |
| 82 | |
| 83 | /* id_tv_{,u}sec are accessor methods on Ruby Time instances. */ |
| 84 | static ID id_tv_sec; |
| 85 | static ID id_tv_nsec; |
| 86 | |
| 87 | /** |
| 88 | * grpc_rb_time_timeval creates a time_eval from a ruby time object. |
| 89 | * |
| 90 | * This func is copied from ruby source, MRI/source/time.c, which is published |
| 91 | * under the same license as the ruby.h, on which the entire extensions is |
| 92 | * based. |
| 93 | */ |
| 94 | gpr_timespec grpc_rb_time_timeval(VALUE time, int interval) { |
| 95 | gpr_timespec t; |
| 96 | gpr_timespec *time_const; |
| 97 | const char *tstr = interval ? "time interval" : "time"; |
| 98 | const char *want = " want <secs from epoch>|<Time>|<GRPC::TimeConst.*>"; |
| 99 | |
| 100 | switch (TYPE(time)) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 101 | case T_DATA: |
| 102 | if (CLASS_OF(time) == rb_cTimeVal) { |
| 103 | Data_Get_Struct(time, gpr_timespec, time_const); |
| 104 | t = *time_const; |
| 105 | } else if (CLASS_OF(time) == rb_cTime) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 106 | t.tv_sec = NUM2INT(rb_funcall(time, id_tv_sec, 0)); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 107 | t.tv_nsec = NUM2INT(rb_funcall(time, id_tv_nsec, 0)); |
| 108 | } else { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 109 | rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr, |
| 110 | rb_obj_classname(time), want); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 111 | } |
| 112 | break; |
| 113 | |
| 114 | case T_FIXNUM: |
| 115 | t.tv_sec = FIX2LONG(time); |
| 116 | if (interval && t.tv_sec < 0) |
| 117 | rb_raise(rb_eArgError, "%s must be positive", tstr); |
| 118 | t.tv_nsec = 0; |
| 119 | break; |
| 120 | |
| 121 | case T_FLOAT: |
| 122 | if (interval && RFLOAT(time)->float_value < 0.0) |
| 123 | rb_raise(rb_eArgError, "%s must be positive", tstr); |
| 124 | else { |
| 125 | double f, d; |
| 126 | |
| 127 | d = modf(RFLOAT(time)->float_value, &f); |
| 128 | if (d < 0) { |
| 129 | d += 1; |
| 130 | f -= 1; |
| 131 | } |
| 132 | t.tv_sec = (time_t)f; |
| 133 | if (f != t.tv_sec) { |
| 134 | rb_raise(rb_eRangeError, "%f out of Time range", |
| 135 | RFLOAT(time)->float_value); |
| 136 | } |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 137 | t.tv_nsec = (time_t)(d * 1e9 + 0.5); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 138 | } |
| 139 | break; |
| 140 | |
| 141 | case T_BIGNUM: |
| 142 | t.tv_sec = NUM2LONG(time); |
| 143 | if (interval && t.tv_sec < 0) |
| 144 | rb_raise(rb_eArgError, "%s must be positive", tstr); |
| 145 | t.tv_nsec = 0; |
| 146 | break; |
| 147 | |
| 148 | default: |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 149 | rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr, |
| 150 | rb_obj_classname(time), want); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | return t; |
| 154 | } |
| 155 | |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame^] | 156 | void Init_grpc_status_codes() { |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 157 | /* Constants representing the status codes or grpc_status_code in status.h */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 158 | VALUE rb_mStatusCodes = |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame^] | 159 | rb_define_module_under(rb_mGrpcCore, "StatusCodes"); |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 160 | rb_define_const(rb_mStatusCodes, "OK", INT2NUM(GRPC_STATUS_OK)); |
| 161 | rb_define_const(rb_mStatusCodes, "CANCELLED", INT2NUM(GRPC_STATUS_CANCELLED)); |
| 162 | rb_define_const(rb_mStatusCodes, "UNKNOWN", INT2NUM(GRPC_STATUS_UNKNOWN)); |
| 163 | rb_define_const(rb_mStatusCodes, "INVALID_ARGUMENT", |
| 164 | INT2NUM(GRPC_STATUS_INVALID_ARGUMENT)); |
| 165 | rb_define_const(rb_mStatusCodes, "DEADLINE_EXCEEDED", |
| 166 | INT2NUM(GRPC_STATUS_DEADLINE_EXCEEDED)); |
| 167 | rb_define_const(rb_mStatusCodes, "NOT_FOUND", INT2NUM(GRPC_STATUS_NOT_FOUND)); |
| 168 | rb_define_const(rb_mStatusCodes, "ALREADY_EXISTS", |
| 169 | INT2NUM(GRPC_STATUS_ALREADY_EXISTS)); |
| 170 | rb_define_const(rb_mStatusCodes, "PERMISSION_DENIED", |
| 171 | INT2NUM(GRPC_STATUS_PERMISSION_DENIED)); |
| 172 | rb_define_const(rb_mStatusCodes, "UNAUTHENTICATED", |
| 173 | INT2NUM(GRPC_STATUS_UNAUTHENTICATED)); |
| 174 | rb_define_const(rb_mStatusCodes, "RESOURCE_EXHAUSTED", |
| 175 | INT2NUM(GRPC_STATUS_RESOURCE_EXHAUSTED)); |
| 176 | rb_define_const(rb_mStatusCodes, "FAILED_PRECONDITION", |
| 177 | INT2NUM(GRPC_STATUS_FAILED_PRECONDITION)); |
| 178 | rb_define_const(rb_mStatusCodes, "ABORTED", INT2NUM(GRPC_STATUS_ABORTED)); |
| 179 | rb_define_const(rb_mStatusCodes, "OUT_OF_RANGE", |
| 180 | INT2NUM(GRPC_STATUS_OUT_OF_RANGE)); |
| 181 | rb_define_const(rb_mStatusCodes, "UNIMPLEMENTED", |
| 182 | INT2NUM(GRPC_STATUS_UNIMPLEMENTED)); |
| 183 | rb_define_const(rb_mStatusCodes, "INTERNAL", INT2NUM(GRPC_STATUS_INTERNAL)); |
| 184 | rb_define_const(rb_mStatusCodes, "UNAVAILABLE", |
| 185 | INT2NUM(GRPC_STATUS_UNAVAILABLE)); |
| 186 | rb_define_const(rb_mStatusCodes, "DATA_LOSS", INT2NUM(GRPC_STATUS_DATA_LOSS)); |
| 187 | } |
| 188 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 189 | /* id_at is the constructor method of the ruby standard Time class. */ |
| 190 | static ID id_at; |
| 191 | |
| 192 | /* id_inspect is the inspect method found on various ruby objects. */ |
| 193 | static ID id_inspect; |
| 194 | |
| 195 | /* id_to_s is the to_s method found on various ruby objects. */ |
| 196 | static ID id_to_s; |
| 197 | |
| 198 | /* Converts `a wrapped time constant to a standard time. */ |
| 199 | VALUE grpc_rb_time_val_to_time(VALUE self) { |
| 200 | gpr_timespec *time_const = NULL; |
| 201 | Data_Get_Struct(self, gpr_timespec, time_const); |
| 202 | return rb_funcall(rb_cTime, id_at, 2, INT2NUM(time_const->tv_sec), |
| 203 | INT2NUM(time_const->tv_nsec)); |
| 204 | } |
| 205 | |
| 206 | /* Invokes inspect on the ctime version of the time val. */ |
| 207 | VALUE grpc_rb_time_val_inspect(VALUE self) { |
| 208 | return rb_funcall(grpc_rb_time_val_to_time(self), id_inspect, 0); |
| 209 | } |
| 210 | |
| 211 | /* Invokes to_s on the ctime version of the time val. */ |
| 212 | VALUE grpc_rb_time_val_to_s(VALUE self) { |
| 213 | return rb_funcall(grpc_rb_time_val_to_time(self), id_to_s, 0); |
| 214 | } |
| 215 | |
| 216 | /* Adds a module with constants that map to gpr's static timeval structs. */ |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame^] | 217 | void Init_grpc_time_consts() { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 218 | VALUE rb_mTimeConsts = |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame^] | 219 | rb_define_module_under(rb_mGrpcCore, "TimeConsts"); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 220 | rb_cTimeVal = |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame^] | 221 | rb_define_class_under(rb_mGrpcCore, "TimeSpec", rb_cObject); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 222 | rb_define_const(rb_mTimeConsts, "ZERO", |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 223 | Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE, |
| 224 | (void *)&gpr_time_0)); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 225 | rb_define_const(rb_mTimeConsts, "INFINITE_FUTURE", |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 226 | Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE, |
| 227 | (void *)&gpr_inf_future)); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 228 | rb_define_const(rb_mTimeConsts, "INFINITE_PAST", |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 229 | Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE, |
| 230 | (void *)&gpr_inf_past)); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 231 | rb_define_method(rb_cTimeVal, "to_time", grpc_rb_time_val_to_time, 0); |
| 232 | rb_define_method(rb_cTimeVal, "inspect", grpc_rb_time_val_inspect, 0); |
| 233 | rb_define_method(rb_cTimeVal, "to_s", grpc_rb_time_val_to_s, 0); |
| 234 | id_at = rb_intern("at"); |
| 235 | id_inspect = rb_intern("inspect"); |
| 236 | id_to_s = rb_intern("to_s"); |
| 237 | id_tv_sec = rb_intern("tv_sec"); |
| 238 | id_tv_nsec = rb_intern("tv_nsec"); |
| 239 | } |
| 240 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 241 | void grpc_rb_shutdown(void *vm) { grpc_shutdown(); } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 242 | |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame^] | 243 | /* Initialize the GRPC module structs */ |
temiola | 21bb60c | 2014-12-18 10:58:22 -0800 | [diff] [blame] | 244 | |
| 245 | /* rb_sNewServerRpc is the struct that holds new server rpc details. */ |
| 246 | VALUE rb_sNewServerRpc = Qnil; |
| 247 | /* rb_sStatus is the struct that holds status details. */ |
| 248 | VALUE rb_sStatus = Qnil; |
| 249 | |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame^] | 250 | /* Initialize the GRPC module. */ |
| 251 | VALUE rb_mGRPC = Qnil; |
| 252 | VALUE rb_mGrpcCore = Qnil; |
temiola | 21bb60c | 2014-12-18 10:58:22 -0800 | [diff] [blame] | 253 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 254 | void Init_grpc() { |
| 255 | grpc_init(); |
| 256 | ruby_vm_at_exit(grpc_rb_shutdown); |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame^] | 257 | rb_mGRPC = rb_define_module("GRPC"); |
| 258 | rb_mGrpcCore = rb_define_module_under(rb_mGRPC, "Core"); |
temiola | 21bb60c | 2014-12-18 10:58:22 -0800 | [diff] [blame] | 259 | rb_sNewServerRpc = rb_struct_define("NewServerRpc", "method", "host", |
| 260 | "deadline", "metadata", NULL); |
| 261 | rb_sStatus = rb_struct_define("Status", "code", "details", "metadata", NULL); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 262 | |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame^] | 263 | Init_grpc_byte_buffer(); |
| 264 | Init_grpc_event(); |
| 265 | Init_grpc_channel(); |
| 266 | Init_grpc_completion_queue(); |
| 267 | Init_grpc_call(); |
| 268 | Init_grpc_credentials(); |
| 269 | Init_grpc_metadata(); |
| 270 | Init_grpc_server(); |
| 271 | Init_grpc_server_credentials(); |
| 272 | Init_grpc_status_codes(); |
| 273 | Init_grpc_time_consts(); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 274 | } |