blob: 41d12ed64fcf4168ae64c143a6d12cb8407a4333 [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>
37#include <ruby.h>
38#include <sys/time.h>
39
40#include <grpc/grpc.h>
41#include <grpc/support/time.h>
nnoble097ef9b2014-12-01 17:06:10 -080042#include "rb_call.h"
43#include "rb_channel.h"
44#include "rb_completion_queue.h"
nnoble097ef9b2014-12-01 17:06:10 -080045#include "rb_server.h"
nnoble0c475f02014-12-05 15:37:39 -080046#include "rb_credentials.h"
47#include "rb_server_credentials.h"
nnoble097ef9b2014-12-01 17:06:10 -080048
49/* Define common vars and funcs declared in rb.h */
50const RUBY_DATA_FUNC GC_NOT_MARKED = NULL;
51const RUBY_DATA_FUNC GC_DONT_FREE = NULL;
52
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +090053VALUE grpc_cTimeVal = Qnil;
nnoble097ef9b2014-12-01 17:06:10 -080054
55/* Alloc func that blocks allocation of a given object by raising an
56 * exception. */
57VALUE grpc_rb_cannot_alloc(VALUE cls) {
58 rb_raise(rb_eTypeError,
59 "allocation of %s only allowed from the gRPC native layer",
60 rb_class2name(cls));
61 return Qnil;
62}
63
64/* Init func that fails by raising an exception. */
65VALUE grpc_rb_cannot_init(VALUE self) {
66 rb_raise(rb_eTypeError,
67 "initialization of %s only allowed from the gRPC native layer",
68 rb_obj_classname(self));
69 return Qnil;
70}
71
72/* Init/Clone func that fails by raising an exception. */
73VALUE grpc_rb_cannot_init_copy(VALUE copy, VALUE self) {
74 rb_raise(rb_eTypeError,
75 "initialization of %s only allowed from the gRPC native layer",
76 rb_obj_classname(copy));
77 return Qnil;
78}
79
80/* id_tv_{,u}sec are accessor methods on Ruby Time instances. */
81static ID id_tv_sec;
82static ID id_tv_nsec;
83
84/**
85 * grpc_rb_time_timeval creates a time_eval from a ruby time object.
86 *
87 * This func is copied from ruby source, MRI/source/time.c, which is published
88 * under the same license as the ruby.h, on which the entire extensions is
89 * based.
90 */
91gpr_timespec grpc_rb_time_timeval(VALUE time, int interval) {
92 gpr_timespec t;
93 gpr_timespec *time_const;
94 const char *tstr = interval ? "time interval" : "time";
95 const char *want = " want <secs from epoch>|<Time>|<GRPC::TimeConst.*>";
96
97 switch (TYPE(time)) {
nnoble097ef9b2014-12-01 17:06:10 -080098 case T_DATA:
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +090099 if (CLASS_OF(time) == grpc_cTimeVal) {
nnoble097ef9b2014-12-01 17:06:10 -0800100 Data_Get_Struct(time, gpr_timespec, time_const);
101 t = *time_const;
102 } else if (CLASS_OF(time) == rb_cTime) {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800103 t.tv_sec = NUM2INT(rb_funcall(time, id_tv_sec, 0));
nnoble097ef9b2014-12-01 17:06:10 -0800104 t.tv_nsec = NUM2INT(rb_funcall(time, id_tv_nsec, 0));
105 } else {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800106 rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
107 rb_obj_classname(time), want);
nnoble097ef9b2014-12-01 17:06:10 -0800108 }
109 break;
110
111 case T_FIXNUM:
112 t.tv_sec = FIX2LONG(time);
113 if (interval && t.tv_sec < 0)
114 rb_raise(rb_eArgError, "%s must be positive", tstr);
115 t.tv_nsec = 0;
116 break;
117
118 case T_FLOAT:
mattnb9e15632015-02-28 23:45:58 +0900119 if (interval && RFLOAT_VALUE(time) < 0.0)
nnoble097ef9b2014-12-01 17:06:10 -0800120 rb_raise(rb_eArgError, "%s must be positive", tstr);
121 else {
122 double f, d;
123
mattnb9e15632015-02-28 23:45:58 +0900124 d = modf(RFLOAT_VALUE(time), &f);
nnoble097ef9b2014-12-01 17:06:10 -0800125 if (d < 0) {
126 d += 1;
127 f -= 1;
128 }
129 t.tv_sec = (time_t)f;
130 if (f != t.tv_sec) {
131 rb_raise(rb_eRangeError, "%f out of Time range",
mattnb9e15632015-02-28 23:45:58 +0900132 RFLOAT_VALUE(time));
nnoble097ef9b2014-12-01 17:06:10 -0800133 }
Craig Tillerb5dcec52015-01-13 11:13:42 -0800134 t.tv_nsec = (time_t)(d * 1e9 + 0.5);
nnoble097ef9b2014-12-01 17:06:10 -0800135 }
136 break;
137
138 case T_BIGNUM:
139 t.tv_sec = NUM2LONG(time);
140 if (interval && t.tv_sec < 0)
141 rb_raise(rb_eArgError, "%s must be positive", tstr);
142 t.tv_nsec = 0;
143 break;
144
145 default:
Craig Tillerb5dcec52015-01-13 11:13:42 -0800146 rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
147 rb_obj_classname(time), want);
nnoble097ef9b2014-12-01 17:06:10 -0800148 break;
149 }
150 return t;
151}
152
Tim Emiola409e6c82015-02-17 17:46:35 -0800153void Init_grpc_status_codes() {
temiola58327912014-12-15 17:51:16 -0800154 /* Constants representing the status codes or grpc_status_code in status.h */
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900155 VALUE grpc_mStatusCodes =
156 rb_define_module_under(grpc_mGrpcCore, "StatusCodes");
157 rb_define_const(grpc_mStatusCodes, "OK", INT2NUM(GRPC_STATUS_OK));
158 rb_define_const(grpc_mStatusCodes, "CANCELLED", INT2NUM(GRPC_STATUS_CANCELLED));
159 rb_define_const(grpc_mStatusCodes, "UNKNOWN", INT2NUM(GRPC_STATUS_UNKNOWN));
160 rb_define_const(grpc_mStatusCodes, "INVALID_ARGUMENT",
temiola58327912014-12-15 17:51:16 -0800161 INT2NUM(GRPC_STATUS_INVALID_ARGUMENT));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900162 rb_define_const(grpc_mStatusCodes, "DEADLINE_EXCEEDED",
temiola58327912014-12-15 17:51:16 -0800163 INT2NUM(GRPC_STATUS_DEADLINE_EXCEEDED));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900164 rb_define_const(grpc_mStatusCodes, "NOT_FOUND", INT2NUM(GRPC_STATUS_NOT_FOUND));
165 rb_define_const(grpc_mStatusCodes, "ALREADY_EXISTS",
temiola58327912014-12-15 17:51:16 -0800166 INT2NUM(GRPC_STATUS_ALREADY_EXISTS));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900167 rb_define_const(grpc_mStatusCodes, "PERMISSION_DENIED",
temiola58327912014-12-15 17:51:16 -0800168 INT2NUM(GRPC_STATUS_PERMISSION_DENIED));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900169 rb_define_const(grpc_mStatusCodes, "UNAUTHENTICATED",
temiola58327912014-12-15 17:51:16 -0800170 INT2NUM(GRPC_STATUS_UNAUTHENTICATED));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900171 rb_define_const(grpc_mStatusCodes, "RESOURCE_EXHAUSTED",
temiola58327912014-12-15 17:51:16 -0800172 INT2NUM(GRPC_STATUS_RESOURCE_EXHAUSTED));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900173 rb_define_const(grpc_mStatusCodes, "FAILED_PRECONDITION",
temiola58327912014-12-15 17:51:16 -0800174 INT2NUM(GRPC_STATUS_FAILED_PRECONDITION));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900175 rb_define_const(grpc_mStatusCodes, "ABORTED", INT2NUM(GRPC_STATUS_ABORTED));
176 rb_define_const(grpc_mStatusCodes, "OUT_OF_RANGE",
temiola58327912014-12-15 17:51:16 -0800177 INT2NUM(GRPC_STATUS_OUT_OF_RANGE));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900178 rb_define_const(grpc_mStatusCodes, "UNIMPLEMENTED",
temiola58327912014-12-15 17:51:16 -0800179 INT2NUM(GRPC_STATUS_UNIMPLEMENTED));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900180 rb_define_const(grpc_mStatusCodes, "INTERNAL", INT2NUM(GRPC_STATUS_INTERNAL));
181 rb_define_const(grpc_mStatusCodes, "UNAVAILABLE",
temiola58327912014-12-15 17:51:16 -0800182 INT2NUM(GRPC_STATUS_UNAVAILABLE));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900183 rb_define_const(grpc_mStatusCodes, "DATA_LOSS", INT2NUM(GRPC_STATUS_DATA_LOSS));
temiola58327912014-12-15 17:51:16 -0800184}
185
nnoble097ef9b2014-12-01 17:06:10 -0800186/* id_at is the constructor method of the ruby standard Time class. */
187static ID id_at;
188
189/* id_inspect is the inspect method found on various ruby objects. */
190static ID id_inspect;
191
192/* id_to_s is the to_s method found on various ruby objects. */
193static ID id_to_s;
194
Tim Emiola98a32d32015-03-28 01:48:44 -0700195/* Converts a wrapped time constant to a standard time. */
nnoble097ef9b2014-12-01 17:06:10 -0800196VALUE grpc_rb_time_val_to_time(VALUE self) {
197 gpr_timespec *time_const = NULL;
198 Data_Get_Struct(self, gpr_timespec, time_const);
199 return rb_funcall(rb_cTime, id_at, 2, INT2NUM(time_const->tv_sec),
200 INT2NUM(time_const->tv_nsec));
201}
202
203/* Invokes inspect on the ctime version of the time val. */
204VALUE grpc_rb_time_val_inspect(VALUE self) {
205 return rb_funcall(grpc_rb_time_val_to_time(self), id_inspect, 0);
206}
207
208/* Invokes to_s on the ctime version of the time val. */
209VALUE grpc_rb_time_val_to_s(VALUE self) {
210 return rb_funcall(grpc_rb_time_val_to_time(self), id_to_s, 0);
211}
212
213/* Adds a module with constants that map to gpr's static timeval structs. */
Tim Emiola409e6c82015-02-17 17:46:35 -0800214void Init_grpc_time_consts() {
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900215 VALUE grpc_mTimeConsts =
216 rb_define_module_under(grpc_mGrpcCore, "TimeConsts");
217 grpc_cTimeVal =
218 rb_define_class_under(grpc_mGrpcCore, "TimeSpec", rb_cObject);
219 rb_define_const(grpc_mTimeConsts, "ZERO",
220 Data_Wrap_Struct(grpc_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800221 (void *)&gpr_time_0));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900222 rb_define_const(grpc_mTimeConsts, "INFINITE_FUTURE",
223 Data_Wrap_Struct(grpc_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800224 (void *)&gpr_inf_future));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900225 rb_define_const(grpc_mTimeConsts, "INFINITE_PAST",
226 Data_Wrap_Struct(grpc_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800227 (void *)&gpr_inf_past));
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900228 rb_define_method(grpc_cTimeVal, "to_time", grpc_rb_time_val_to_time, 0);
229 rb_define_method(grpc_cTimeVal, "inspect", grpc_rb_time_val_inspect, 0);
230 rb_define_method(grpc_cTimeVal, "to_s", grpc_rb_time_val_to_s, 0);
nnoble097ef9b2014-12-01 17:06:10 -0800231 id_at = rb_intern("at");
232 id_inspect = rb_intern("inspect");
233 id_to_s = rb_intern("to_s");
234 id_tv_sec = rb_intern("tv_sec");
235 id_tv_nsec = rb_intern("tv_nsec");
236}
237
Craig Tillerb5dcec52015-01-13 11:13:42 -0800238void grpc_rb_shutdown(void *vm) { grpc_shutdown(); }
nnoble097ef9b2014-12-01 17:06:10 -0800239
Tim Emiola409e6c82015-02-17 17:46:35 -0800240/* Initialize the GRPC module structs */
temiola21bb60c2014-12-18 10:58:22 -0800241
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900242/* grpc_sNewServerRpc is the struct that holds new server rpc details. */
243VALUE grpc_sNewServerRpc = Qnil;
244/* grpc_sStatus is the struct that holds status details. */
245VALUE grpc_sStatus = Qnil;
temiola21bb60c2014-12-18 10:58:22 -0800246
Tim Emiola409e6c82015-02-17 17:46:35 -0800247/* Initialize the GRPC module. */
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900248VALUE grpc_mGRPC = Qnil;
249VALUE grpc_mGrpcCore = Qnil;
temiola21bb60c2014-12-18 10:58:22 -0800250
nnoble097ef9b2014-12-01 17:06:10 -0800251void Init_grpc() {
252 grpc_init();
253 ruby_vm_at_exit(grpc_rb_shutdown);
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900254 grpc_mGRPC = rb_define_module("GRPC");
255 grpc_mGrpcCore = rb_define_module_under(grpc_mGRPC, "Core");
256 grpc_sNewServerRpc = rb_struct_define("NewServerRpc", "method", "host",
Yuki Yugui Sonoda0f75ff52015-04-11 11:02:22 +0900257 "deadline", "metadata", "call", NULL);
Yuki Yugui Sonodad241bcc2015-04-10 19:47:27 +0900258 grpc_sStatus = rb_struct_define("Status", "code", "details", "metadata", NULL);
Tim Emiola98a32d32015-03-28 01:48:44 -0700259 sym_code = ID2SYM(rb_intern("code"));
260 sym_details = ID2SYM(rb_intern("details"));
261 sym_metadata = ID2SYM(rb_intern("metadata"));
nnoble097ef9b2014-12-01 17:06:10 -0800262
Tim Emiola409e6c82015-02-17 17:46:35 -0800263 Init_grpc_channel();
264 Init_grpc_completion_queue();
265 Init_grpc_call();
266 Init_grpc_credentials();
Tim Emiola409e6c82015-02-17 17:46:35 -0800267 Init_grpc_server();
268 Init_grpc_server_credentials();
269 Init_grpc_status_codes();
270 Init_grpc_time_consts();
nnoble097ef9b2014-12-01 17:06:10 -0800271}