blob: f138bf6dd5329b6f102b63b6c70804a430f6b676 [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",
54 {GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, GRPC_RB_MEMSIZE_UNAVAILABLE},
55 NULL,
56 NULL,
57 RUBY_TYPED_FREE_IMMEDIATELY};
58
nnoble097ef9b2014-12-01 17:06:10 -080059/* Alloc func that blocks allocation of a given object by raising an
60 * exception. */
61VALUE grpc_rb_cannot_alloc(VALUE cls) {
62 rb_raise(rb_eTypeError,
63 "allocation of %s only allowed from the gRPC native layer",
64 rb_class2name(cls));
65 return Qnil;
66}
67
68/* Init func that fails by raising an exception. */
69VALUE grpc_rb_cannot_init(VALUE self) {
70 rb_raise(rb_eTypeError,
71 "initialization of %s only allowed from the gRPC native layer",
72 rb_obj_classname(self));
73 return Qnil;
74}
75
76/* Init/Clone func that fails by raising an exception. */
77VALUE grpc_rb_cannot_init_copy(VALUE copy, VALUE self) {
78 rb_raise(rb_eTypeError,
79 "initialization of %s only allowed from the gRPC native layer",
80 rb_obj_classname(copy));
81 return Qnil;
82}
83
84/* id_tv_{,u}sec are accessor methods on Ruby Time instances. */
85static ID id_tv_sec;
86static ID id_tv_nsec;
87
88/**
89 * grpc_rb_time_timeval creates a time_eval from a ruby time object.
90 *
91 * This func is copied from ruby source, MRI/source/time.c, which is published
92 * under the same license as the ruby.h, on which the entire extensions is
93 * based.
94 */
95gpr_timespec grpc_rb_time_timeval(VALUE time, int interval) {
96 gpr_timespec t;
97 gpr_timespec *time_const;
98 const char *tstr = interval ? "time interval" : "time";
99 const char *want = " want <secs from epoch>|<Time>|<GRPC::TimeConst.*>";
100
Craig Tiller5a1e7fd2015-07-14 07:14:47 -0700101 t.clock_type = GPR_CLOCK_REALTIME;
nnoble097ef9b2014-12-01 17:06:10 -0800102 switch (TYPE(time)) {
nnoble097ef9b2014-12-01 17:06:10 -0800103 case T_DATA:
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900104 if (CLASS_OF(time) == grpc_rb_cTimeVal) {
Yuki Yugui Sonodad441c2e2015-04-11 15:33:58 +0900105 TypedData_Get_Struct(time, gpr_timespec, &grpc_rb_timespec_data_type,
106 time_const);
nnoble097ef9b2014-12-01 17:06:10 -0800107 t = *time_const;
108 } else if (CLASS_OF(time) == rb_cTime) {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800109 t.tv_sec = NUM2INT(rb_funcall(time, id_tv_sec, 0));
nnoble097ef9b2014-12-01 17:06:10 -0800110 t.tv_nsec = NUM2INT(rb_funcall(time, id_tv_nsec, 0));
111 } else {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800112 rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
113 rb_obj_classname(time), want);
nnoble097ef9b2014-12-01 17:06:10 -0800114 }
115 break;
116
117 case T_FIXNUM:
118 t.tv_sec = FIX2LONG(time);
119 if (interval && t.tv_sec < 0)
120 rb_raise(rb_eArgError, "%s must be positive", tstr);
121 t.tv_nsec = 0;
122 break;
123
124 case T_FLOAT:
mattnb9e15632015-02-28 23:45:58 +0900125 if (interval && RFLOAT_VALUE(time) < 0.0)
nnoble097ef9b2014-12-01 17:06:10 -0800126 rb_raise(rb_eArgError, "%s must be positive", tstr);
127 else {
128 double f, d;
129
mattnb9e15632015-02-28 23:45:58 +0900130 d = modf(RFLOAT_VALUE(time), &f);
nnoble097ef9b2014-12-01 17:06:10 -0800131 if (d < 0) {
132 d += 1;
133 f -= 1;
134 }
135 t.tv_sec = (time_t)f;
136 if (f != t.tv_sec) {
137 rb_raise(rb_eRangeError, "%f out of Time range",
mattnb9e15632015-02-28 23:45:58 +0900138 RFLOAT_VALUE(time));
nnoble097ef9b2014-12-01 17:06:10 -0800139 }
Craig Tillerb5dcec52015-01-13 11:13:42 -0800140 t.tv_nsec = (time_t)(d * 1e9 + 0.5);
nnoble097ef9b2014-12-01 17:06:10 -0800141 }
142 break;
143
144 case T_BIGNUM:
145 t.tv_sec = NUM2LONG(time);
146 if (interval && t.tv_sec < 0)
147 rb_raise(rb_eArgError, "%s must be positive", tstr);
148 t.tv_nsec = 0;
149 break;
150
151 default:
Craig Tillerb5dcec52015-01-13 11:13:42 -0800152 rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
153 rb_obj_classname(time), want);
nnoble097ef9b2014-12-01 17:06:10 -0800154 break;
155 }
156 return t;
157}
158
Yuki Yugui Sonodaf0eee5f2015-04-16 20:25:28 +0900159static void Init_grpc_status_codes() {
temiola58327912014-12-15 17:51:16 -0800160 /* Constants representing the status codes or grpc_status_code in status.h */
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900161 VALUE grpc_rb_mStatusCodes =
162 rb_define_module_under(grpc_rb_mGrpcCore, "StatusCodes");
163 rb_define_const(grpc_rb_mStatusCodes, "OK", INT2NUM(GRPC_STATUS_OK));
164 rb_define_const(grpc_rb_mStatusCodes, "CANCELLED",
165 INT2NUM(GRPC_STATUS_CANCELLED));
166 rb_define_const(grpc_rb_mStatusCodes, "UNKNOWN",
167 INT2NUM(GRPC_STATUS_UNKNOWN));
168 rb_define_const(grpc_rb_mStatusCodes, "INVALID_ARGUMENT",
temiola58327912014-12-15 17:51:16 -0800169 INT2NUM(GRPC_STATUS_INVALID_ARGUMENT));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900170 rb_define_const(grpc_rb_mStatusCodes, "DEADLINE_EXCEEDED",
temiola58327912014-12-15 17:51:16 -0800171 INT2NUM(GRPC_STATUS_DEADLINE_EXCEEDED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900172 rb_define_const(grpc_rb_mStatusCodes, "NOT_FOUND",
173 INT2NUM(GRPC_STATUS_NOT_FOUND));
174 rb_define_const(grpc_rb_mStatusCodes, "ALREADY_EXISTS",
temiola58327912014-12-15 17:51:16 -0800175 INT2NUM(GRPC_STATUS_ALREADY_EXISTS));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900176 rb_define_const(grpc_rb_mStatusCodes, "PERMISSION_DENIED",
temiola58327912014-12-15 17:51:16 -0800177 INT2NUM(GRPC_STATUS_PERMISSION_DENIED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900178 rb_define_const(grpc_rb_mStatusCodes, "UNAUTHENTICATED",
temiola58327912014-12-15 17:51:16 -0800179 INT2NUM(GRPC_STATUS_UNAUTHENTICATED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900180 rb_define_const(grpc_rb_mStatusCodes, "RESOURCE_EXHAUSTED",
temiola58327912014-12-15 17:51:16 -0800181 INT2NUM(GRPC_STATUS_RESOURCE_EXHAUSTED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900182 rb_define_const(grpc_rb_mStatusCodes, "FAILED_PRECONDITION",
temiola58327912014-12-15 17:51:16 -0800183 INT2NUM(GRPC_STATUS_FAILED_PRECONDITION));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900184 rb_define_const(grpc_rb_mStatusCodes, "ABORTED",
185 INT2NUM(GRPC_STATUS_ABORTED));
186 rb_define_const(grpc_rb_mStatusCodes, "OUT_OF_RANGE",
temiola58327912014-12-15 17:51:16 -0800187 INT2NUM(GRPC_STATUS_OUT_OF_RANGE));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900188 rb_define_const(grpc_rb_mStatusCodes, "UNIMPLEMENTED",
temiola58327912014-12-15 17:51:16 -0800189 INT2NUM(GRPC_STATUS_UNIMPLEMENTED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900190 rb_define_const(grpc_rb_mStatusCodes, "INTERNAL",
191 INT2NUM(GRPC_STATUS_INTERNAL));
192 rb_define_const(grpc_rb_mStatusCodes, "UNAVAILABLE",
temiola58327912014-12-15 17:51:16 -0800193 INT2NUM(GRPC_STATUS_UNAVAILABLE));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900194 rb_define_const(grpc_rb_mStatusCodes, "DATA_LOSS",
195 INT2NUM(GRPC_STATUS_DATA_LOSS));
temiola58327912014-12-15 17:51:16 -0800196}
197
nnoble097ef9b2014-12-01 17:06:10 -0800198/* id_at is the constructor method of the ruby standard Time class. */
199static ID id_at;
200
201/* id_inspect is the inspect method found on various ruby objects. */
202static ID id_inspect;
203
204/* id_to_s is the to_s method found on various ruby objects. */
205static ID id_to_s;
206
Tim Emiola98a32d32015-03-28 01:48:44 -0700207/* Converts a wrapped time constant to a standard time. */
Yuki Yugui Sonodaf0eee5f2015-04-16 20:25:28 +0900208static VALUE grpc_rb_time_val_to_time(VALUE self) {
nnoble097ef9b2014-12-01 17:06:10 -0800209 gpr_timespec *time_const = NULL;
Yuki Yugui Sonodad441c2e2015-04-11 15:33:58 +0900210 TypedData_Get_Struct(self, gpr_timespec, &grpc_rb_timespec_data_type,
211 time_const);
nnoble097ef9b2014-12-01 17:06:10 -0800212 return rb_funcall(rb_cTime, id_at, 2, INT2NUM(time_const->tv_sec),
213 INT2NUM(time_const->tv_nsec));
214}
215
216/* Invokes inspect on the ctime version of the time val. */
Yuki Yugui Sonodaf0eee5f2015-04-16 20:25:28 +0900217static VALUE grpc_rb_time_val_inspect(VALUE self) {
nnoble097ef9b2014-12-01 17:06:10 -0800218 return rb_funcall(grpc_rb_time_val_to_time(self), id_inspect, 0);
219}
220
221/* Invokes to_s on the ctime version of the time val. */
Yuki Yugui Sonodaf0eee5f2015-04-16 20:25:28 +0900222static VALUE grpc_rb_time_val_to_s(VALUE self) {
nnoble097ef9b2014-12-01 17:06:10 -0800223 return rb_funcall(grpc_rb_time_val_to_time(self), id_to_s, 0);
224}
225
Craig Tiller354398f2015-07-13 09:16:03 -0700226static gpr_timespec zero_realtime;
227static gpr_timespec inf_future_realtime;
228static gpr_timespec inf_past_realtime;
229
nnoble097ef9b2014-12-01 17:06:10 -0800230/* Adds a module with constants that map to gpr's static timeval structs. */
Yuki Yugui Sonodaf0eee5f2015-04-16 20:25:28 +0900231static void Init_grpc_time_consts() {
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900232 VALUE grpc_rb_mTimeConsts =
233 rb_define_module_under(grpc_rb_mGrpcCore, "TimeConsts");
234 grpc_rb_cTimeVal =
235 rb_define_class_under(grpc_rb_mGrpcCore, "TimeSpec", rb_cObject);
Craig Tiller354398f2015-07-13 09:16:03 -0700236 zero_realtime = gpr_time_0(GPR_CLOCK_REALTIME);
237 inf_future_realtime = gpr_inf_future(GPR_CLOCK_REALTIME);
238 inf_past_realtime = gpr_inf_past(GPR_CLOCK_REALTIME);
Yuki Yugui Sonodad441c2e2015-04-11 15:33:58 +0900239 rb_define_const(
240 grpc_rb_mTimeConsts, "ZERO",
241 TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type,
Craig Tiller354398f2015-07-13 09:16:03 -0700242 (void *)&zero_realtime));
Yuki Yugui Sonodad441c2e2015-04-11 15:33:58 +0900243 rb_define_const(
244 grpc_rb_mTimeConsts, "INFINITE_FUTURE",
245 TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type,
Craig Tiller354398f2015-07-13 09:16:03 -0700246 (void *)&inf_future_realtime));
Yuki Yugui Sonodad441c2e2015-04-11 15:33:58 +0900247 rb_define_const(
248 grpc_rb_mTimeConsts, "INFINITE_PAST",
249 TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type,
Craig Tiller354398f2015-07-13 09:16:03 -0700250 (void *)&inf_past_realtime));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900251 rb_define_method(grpc_rb_cTimeVal, "to_time", grpc_rb_time_val_to_time, 0);
252 rb_define_method(grpc_rb_cTimeVal, "inspect", grpc_rb_time_val_inspect, 0);
253 rb_define_method(grpc_rb_cTimeVal, "to_s", grpc_rb_time_val_to_s, 0);
nnoble097ef9b2014-12-01 17:06:10 -0800254 id_at = rb_intern("at");
255 id_inspect = rb_intern("inspect");
256 id_to_s = rb_intern("to_s");
257 id_tv_sec = rb_intern("tv_sec");
258 id_tv_nsec = rb_intern("tv_nsec");
259}
260
Yuki Yugui Sonodae8696fb2015-04-17 08:06:01 +0900261static void grpc_rb_shutdown(ruby_vm_t *vm) { grpc_shutdown(); }
nnoble097ef9b2014-12-01 17:06:10 -0800262
Tim Emiola409e6c82015-02-17 17:46:35 -0800263/* Initialize the GRPC module structs */
temiola21bb60c2014-12-18 10:58:22 -0800264
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900265/* grpc_rb_sNewServerRpc is the struct that holds new server rpc details. */
266VALUE grpc_rb_sNewServerRpc = Qnil;
267/* grpc_rb_sStatus is the struct that holds status details. */
268VALUE grpc_rb_sStatus = Qnil;
temiola21bb60c2014-12-18 10:58:22 -0800269
Tim Emiola409e6c82015-02-17 17:46:35 -0800270/* Initialize the GRPC module. */
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900271VALUE grpc_rb_mGRPC = Qnil;
272VALUE grpc_rb_mGrpcCore = Qnil;
temiola21bb60c2014-12-18 10:58:22 -0800273
Yuki Yugui Sonoda99eb9f92015-04-16 20:09:55 +0900274/* cached Symbols for members in Status struct */
275VALUE sym_code = Qundef;
276VALUE sym_details = Qundef;
277VALUE sym_metadata = Qundef;
278
nnoble097ef9b2014-12-01 17:06:10 -0800279void Init_grpc() {
280 grpc_init();
281 ruby_vm_at_exit(grpc_rb_shutdown);
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900282 grpc_rb_mGRPC = rb_define_module("GRPC");
283 grpc_rb_mGrpcCore = rb_define_module_under(grpc_rb_mGRPC, "Core");
284 grpc_rb_sNewServerRpc =
285 rb_struct_define("NewServerRpc", "method", "host",
286 "deadline", "metadata", "call", NULL);
287 grpc_rb_sStatus =
288 rb_struct_define("Status", "code", "details", "metadata", NULL);
Tim Emiola98a32d32015-03-28 01:48:44 -0700289 sym_code = ID2SYM(rb_intern("code"));
290 sym_details = ID2SYM(rb_intern("details"));
291 sym_metadata = ID2SYM(rb_intern("metadata"));
nnoble097ef9b2014-12-01 17:06:10 -0800292
Tim Emiola409e6c82015-02-17 17:46:35 -0800293 Init_grpc_channel();
294 Init_grpc_completion_queue();
295 Init_grpc_call();
296 Init_grpc_credentials();
Tim Emiola409e6c82015-02-17 17:46:35 -0800297 Init_grpc_server();
298 Init_grpc_server_credentials();
299 Init_grpc_status_codes();
300 Init_grpc_time_consts();
nnoble097ef9b2014-12-01 17:06:10 -0800301}