blob: ace86227277b666d659fd55ceee444479846beff [file] [log] [blame]
nnoble097ef9b2014-12-01 17:06:10 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
nnoble097ef9b2014-12-01 17:06:10 -08004 * 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>
Yuki Yugui Sonoda22887912015-04-16 20:57:17 +090037#include <ruby/ruby.h>
38#include <ruby/vm.h>
nnoble097ef9b2014-12-01 17:06:10 -080039#include <sys/time.h>
40
41#include <grpc/grpc.h>
42#include <grpc/support/time.h>
nnoble097ef9b2014-12-01 17:06:10 -080043#include "rb_call.h"
44#include "rb_channel.h"
45#include "rb_completion_queue.h"
nnoble097ef9b2014-12-01 17:06:10 -080046#include "rb_server.h"
nnoble0c475f02014-12-05 15:37:39 -080047#include "rb_credentials.h"
48#include "rb_server_credentials.h"
nnoble097ef9b2014-12-01 17:06:10 -080049
Yuki Yugui Sonoda3c88e5d2015-04-16 20:09:00 +090050static VALUE grpc_rb_cTimeVal = Qnil;
nnoble097ef9b2014-12-01 17:06:10 -080051
Yuki Yugui Sonodad441c2e2015-04-11 15:33:58 +090052static rb_data_type_t grpc_rb_timespec_data_type = {
53 "gpr_timespec",
murgatroid9987afb5d2015-07-16 16:01:02 -070054 {GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, GRPC_RB_MEMSIZE_UNAVAILABLE,
55 {NULL, NULL}},
Yuki Yugui Sonodad441c2e2015-04-11 15:33:58 +090056 NULL,
57 NULL,
58 RUBY_TYPED_FREE_IMMEDIATELY};
59
nnoble097ef9b2014-12-01 17:06:10 -080060/* Alloc func that blocks allocation of a given object by raising an
61 * exception. */
62VALUE grpc_rb_cannot_alloc(VALUE cls) {
63 rb_raise(rb_eTypeError,
64 "allocation of %s only allowed from the gRPC native layer",
65 rb_class2name(cls));
66 return Qnil;
67}
68
69/* Init func that fails by raising an exception. */
70VALUE grpc_rb_cannot_init(VALUE self) {
71 rb_raise(rb_eTypeError,
72 "initialization of %s only allowed from the gRPC native layer",
73 rb_obj_classname(self));
74 return Qnil;
75}
76
77/* Init/Clone func that fails by raising an exception. */
78VALUE grpc_rb_cannot_init_copy(VALUE copy, VALUE self) {
murgatroid9987afb5d2015-07-16 16:01:02 -070079 (void)self;
nnoble097ef9b2014-12-01 17:06:10 -080080 rb_raise(rb_eTypeError,
81 "initialization of %s only allowed from the gRPC native layer",
82 rb_obj_classname(copy));
83 return Qnil;
84}
85
86/* id_tv_{,u}sec are accessor methods on Ruby Time instances. */
87static ID id_tv_sec;
88static ID id_tv_nsec;
89
90/**
91 * grpc_rb_time_timeval creates a time_eval from a ruby time object.
92 *
93 * This func is copied from ruby source, MRI/source/time.c, which is published
94 * under the same license as the ruby.h, on which the entire extensions is
95 * based.
96 */
97gpr_timespec grpc_rb_time_timeval(VALUE time, int interval) {
98 gpr_timespec t;
99 gpr_timespec *time_const;
100 const char *tstr = interval ? "time interval" : "time";
101 const char *want = " want <secs from epoch>|<Time>|<GRPC::TimeConst.*>";
102
103 switch (TYPE(time)) {
nnoble097ef9b2014-12-01 17:06:10 -0800104 case T_DATA:
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900105 if (CLASS_OF(time) == grpc_rb_cTimeVal) {
Yuki Yugui Sonodad441c2e2015-04-11 15:33:58 +0900106 TypedData_Get_Struct(time, gpr_timespec, &grpc_rb_timespec_data_type,
107 time_const);
nnoble097ef9b2014-12-01 17:06:10 -0800108 t = *time_const;
109 } else if (CLASS_OF(time) == rb_cTime) {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800110 t.tv_sec = NUM2INT(rb_funcall(time, id_tv_sec, 0));
nnoble097ef9b2014-12-01 17:06:10 -0800111 t.tv_nsec = NUM2INT(rb_funcall(time, id_tv_nsec, 0));
112 } else {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800113 rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
114 rb_obj_classname(time), want);
nnoble097ef9b2014-12-01 17:06:10 -0800115 }
116 break;
117
118 case T_FIXNUM:
119 t.tv_sec = FIX2LONG(time);
120 if (interval && t.tv_sec < 0)
121 rb_raise(rb_eArgError, "%s must be positive", tstr);
122 t.tv_nsec = 0;
123 break;
124
125 case T_FLOAT:
mattnb9e15632015-02-28 23:45:58 +0900126 if (interval && RFLOAT_VALUE(time) < 0.0)
nnoble097ef9b2014-12-01 17:06:10 -0800127 rb_raise(rb_eArgError, "%s must be positive", tstr);
128 else {
129 double f, d;
130
mattnb9e15632015-02-28 23:45:58 +0900131 d = modf(RFLOAT_VALUE(time), &f);
nnoble097ef9b2014-12-01 17:06:10 -0800132 if (d < 0) {
133 d += 1;
134 f -= 1;
135 }
136 t.tv_sec = (time_t)f;
137 if (f != t.tv_sec) {
138 rb_raise(rb_eRangeError, "%f out of Time range",
mattnb9e15632015-02-28 23:45:58 +0900139 RFLOAT_VALUE(time));
nnoble097ef9b2014-12-01 17:06:10 -0800140 }
Craig Tillerb5dcec52015-01-13 11:13:42 -0800141 t.tv_nsec = (time_t)(d * 1e9 + 0.5);
nnoble097ef9b2014-12-01 17:06:10 -0800142 }
143 break;
144
145 case T_BIGNUM:
146 t.tv_sec = NUM2LONG(time);
147 if (interval && t.tv_sec < 0)
148 rb_raise(rb_eArgError, "%s must be positive", tstr);
149 t.tv_nsec = 0;
150 break;
151
152 default:
Craig Tillerb5dcec52015-01-13 11:13:42 -0800153 rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
154 rb_obj_classname(time), want);
nnoble097ef9b2014-12-01 17:06:10 -0800155 break;
156 }
157 return t;
158}
159
Yuki Yugui Sonodaf0eee5f2015-04-16 20:25:28 +0900160static void Init_grpc_status_codes() {
temiola58327912014-12-15 17:51:16 -0800161 /* Constants representing the status codes or grpc_status_code in status.h */
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900162 VALUE grpc_rb_mStatusCodes =
163 rb_define_module_under(grpc_rb_mGrpcCore, "StatusCodes");
164 rb_define_const(grpc_rb_mStatusCodes, "OK", INT2NUM(GRPC_STATUS_OK));
165 rb_define_const(grpc_rb_mStatusCodes, "CANCELLED",
166 INT2NUM(GRPC_STATUS_CANCELLED));
167 rb_define_const(grpc_rb_mStatusCodes, "UNKNOWN",
168 INT2NUM(GRPC_STATUS_UNKNOWN));
169 rb_define_const(grpc_rb_mStatusCodes, "INVALID_ARGUMENT",
temiola58327912014-12-15 17:51:16 -0800170 INT2NUM(GRPC_STATUS_INVALID_ARGUMENT));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900171 rb_define_const(grpc_rb_mStatusCodes, "DEADLINE_EXCEEDED",
temiola58327912014-12-15 17:51:16 -0800172 INT2NUM(GRPC_STATUS_DEADLINE_EXCEEDED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900173 rb_define_const(grpc_rb_mStatusCodes, "NOT_FOUND",
174 INT2NUM(GRPC_STATUS_NOT_FOUND));
175 rb_define_const(grpc_rb_mStatusCodes, "ALREADY_EXISTS",
temiola58327912014-12-15 17:51:16 -0800176 INT2NUM(GRPC_STATUS_ALREADY_EXISTS));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900177 rb_define_const(grpc_rb_mStatusCodes, "PERMISSION_DENIED",
temiola58327912014-12-15 17:51:16 -0800178 INT2NUM(GRPC_STATUS_PERMISSION_DENIED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900179 rb_define_const(grpc_rb_mStatusCodes, "UNAUTHENTICATED",
temiola58327912014-12-15 17:51:16 -0800180 INT2NUM(GRPC_STATUS_UNAUTHENTICATED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900181 rb_define_const(grpc_rb_mStatusCodes, "RESOURCE_EXHAUSTED",
temiola58327912014-12-15 17:51:16 -0800182 INT2NUM(GRPC_STATUS_RESOURCE_EXHAUSTED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900183 rb_define_const(grpc_rb_mStatusCodes, "FAILED_PRECONDITION",
temiola58327912014-12-15 17:51:16 -0800184 INT2NUM(GRPC_STATUS_FAILED_PRECONDITION));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900185 rb_define_const(grpc_rb_mStatusCodes, "ABORTED",
186 INT2NUM(GRPC_STATUS_ABORTED));
187 rb_define_const(grpc_rb_mStatusCodes, "OUT_OF_RANGE",
temiola58327912014-12-15 17:51:16 -0800188 INT2NUM(GRPC_STATUS_OUT_OF_RANGE));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900189 rb_define_const(grpc_rb_mStatusCodes, "UNIMPLEMENTED",
temiola58327912014-12-15 17:51:16 -0800190 INT2NUM(GRPC_STATUS_UNIMPLEMENTED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900191 rb_define_const(grpc_rb_mStatusCodes, "INTERNAL",
192 INT2NUM(GRPC_STATUS_INTERNAL));
193 rb_define_const(grpc_rb_mStatusCodes, "UNAVAILABLE",
temiola58327912014-12-15 17:51:16 -0800194 INT2NUM(GRPC_STATUS_UNAVAILABLE));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900195 rb_define_const(grpc_rb_mStatusCodes, "DATA_LOSS",
196 INT2NUM(GRPC_STATUS_DATA_LOSS));
temiola58327912014-12-15 17:51:16 -0800197}
198
nnoble097ef9b2014-12-01 17:06:10 -0800199/* id_at is the constructor method of the ruby standard Time class. */
200static ID id_at;
201
202/* id_inspect is the inspect method found on various ruby objects. */
203static ID id_inspect;
204
205/* id_to_s is the to_s method found on various ruby objects. */
206static ID id_to_s;
207
Tim Emiola98a32d32015-03-28 01:48:44 -0700208/* Converts a wrapped time constant to a standard time. */
Yuki Yugui Sonodaf0eee5f2015-04-16 20:25:28 +0900209static VALUE grpc_rb_time_val_to_time(VALUE self) {
nnoble097ef9b2014-12-01 17:06:10 -0800210 gpr_timespec *time_const = NULL;
Yuki Yugui Sonodad441c2e2015-04-11 15:33:58 +0900211 TypedData_Get_Struct(self, gpr_timespec, &grpc_rb_timespec_data_type,
212 time_const);
nnoble097ef9b2014-12-01 17:06:10 -0800213 return rb_funcall(rb_cTime, id_at, 2, INT2NUM(time_const->tv_sec),
214 INT2NUM(time_const->tv_nsec));
215}
216
217/* Invokes inspect on the ctime version of the time val. */
Yuki Yugui Sonodaf0eee5f2015-04-16 20:25:28 +0900218static VALUE grpc_rb_time_val_inspect(VALUE self) {
nnoble097ef9b2014-12-01 17:06:10 -0800219 return rb_funcall(grpc_rb_time_val_to_time(self), id_inspect, 0);
220}
221
222/* Invokes to_s on the ctime version of the time val. */
Yuki Yugui Sonodaf0eee5f2015-04-16 20:25:28 +0900223static VALUE grpc_rb_time_val_to_s(VALUE self) {
nnoble097ef9b2014-12-01 17:06:10 -0800224 return rb_funcall(grpc_rb_time_val_to_time(self), id_to_s, 0);
225}
226
227/* Adds a module with constants that map to gpr's static timeval structs. */
Yuki Yugui Sonodaf0eee5f2015-04-16 20:25:28 +0900228static void Init_grpc_time_consts() {
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900229 VALUE grpc_rb_mTimeConsts =
230 rb_define_module_under(grpc_rb_mGrpcCore, "TimeConsts");
231 grpc_rb_cTimeVal =
232 rb_define_class_under(grpc_rb_mGrpcCore, "TimeSpec", rb_cObject);
Yuki Yugui Sonodad441c2e2015-04-11 15:33:58 +0900233 rb_define_const(
234 grpc_rb_mTimeConsts, "ZERO",
235 TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type,
236 (void *)&gpr_time_0));
237 rb_define_const(
238 grpc_rb_mTimeConsts, "INFINITE_FUTURE",
239 TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type,
240 (void *)&gpr_inf_future));
241 rb_define_const(
242 grpc_rb_mTimeConsts, "INFINITE_PAST",
243 TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type,
244 (void *)&gpr_inf_past));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900245 rb_define_method(grpc_rb_cTimeVal, "to_time", grpc_rb_time_val_to_time, 0);
246 rb_define_method(grpc_rb_cTimeVal, "inspect", grpc_rb_time_val_inspect, 0);
247 rb_define_method(grpc_rb_cTimeVal, "to_s", grpc_rb_time_val_to_s, 0);
nnoble097ef9b2014-12-01 17:06:10 -0800248 id_at = rb_intern("at");
249 id_inspect = rb_intern("inspect");
250 id_to_s = rb_intern("to_s");
251 id_tv_sec = rb_intern("tv_sec");
252 id_tv_nsec = rb_intern("tv_nsec");
253}
254
murgatroid9987afb5d2015-07-16 16:01:02 -0700255static void grpc_rb_shutdown(ruby_vm_t *vm) {
256 (void)vm;
257 grpc_shutdown();
258}
nnoble097ef9b2014-12-01 17:06:10 -0800259
Tim Emiola409e6c82015-02-17 17:46:35 -0800260/* Initialize the GRPC module structs */
temiola21bb60c2014-12-18 10:58:22 -0800261
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900262/* grpc_rb_sNewServerRpc is the struct that holds new server rpc details. */
263VALUE grpc_rb_sNewServerRpc = Qnil;
264/* grpc_rb_sStatus is the struct that holds status details. */
265VALUE grpc_rb_sStatus = Qnil;
temiola21bb60c2014-12-18 10:58:22 -0800266
Tim Emiola409e6c82015-02-17 17:46:35 -0800267/* Initialize the GRPC module. */
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900268VALUE grpc_rb_mGRPC = Qnil;
269VALUE grpc_rb_mGrpcCore = Qnil;
temiola21bb60c2014-12-18 10:58:22 -0800270
Yuki Yugui Sonoda99eb9f92015-04-16 20:09:55 +0900271/* cached Symbols for members in Status struct */
272VALUE sym_code = Qundef;
273VALUE sym_details = Qundef;
274VALUE sym_metadata = Qundef;
275
nnoble097ef9b2014-12-01 17:06:10 -0800276void Init_grpc() {
277 grpc_init();
278 ruby_vm_at_exit(grpc_rb_shutdown);
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900279 grpc_rb_mGRPC = rb_define_module("GRPC");
280 grpc_rb_mGrpcCore = rb_define_module_under(grpc_rb_mGRPC, "Core");
281 grpc_rb_sNewServerRpc =
282 rb_struct_define("NewServerRpc", "method", "host",
283 "deadline", "metadata", "call", NULL);
284 grpc_rb_sStatus =
285 rb_struct_define("Status", "code", "details", "metadata", NULL);
Tim Emiola98a32d32015-03-28 01:48:44 -0700286 sym_code = ID2SYM(rb_intern("code"));
287 sym_details = ID2SYM(rb_intern("details"));
288 sym_metadata = ID2SYM(rb_intern("metadata"));
nnoble097ef9b2014-12-01 17:06:10 -0800289
Tim Emiola409e6c82015-02-17 17:46:35 -0800290 Init_grpc_channel();
291 Init_grpc_completion_queue();
292 Init_grpc_call();
293 Init_grpc_credentials();
Tim Emiola409e6c82015-02-17 17:46:35 -0800294 Init_grpc_server();
295 Init_grpc_server_credentials();
296 Init_grpc_status_codes();
297 Init_grpc_time_consts();
nnoble097ef9b2014-12-01 17:06:10 -0800298}