blob: e721a9b3e7793c5aaf1a784268535ebadb92682e [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
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +090049VALUE grpc_rb_cTimeVal = Qnil;
nnoble097ef9b2014-12-01 17:06:10 -080050
51/* Alloc func that blocks allocation of a given object by raising an
52 * exception. */
53VALUE grpc_rb_cannot_alloc(VALUE cls) {
54 rb_raise(rb_eTypeError,
55 "allocation of %s only allowed from the gRPC native layer",
56 rb_class2name(cls));
57 return Qnil;
58}
59
60/* Init func that fails by raising an exception. */
61VALUE grpc_rb_cannot_init(VALUE self) {
62 rb_raise(rb_eTypeError,
63 "initialization of %s only allowed from the gRPC native layer",
64 rb_obj_classname(self));
65 return Qnil;
66}
67
68/* Init/Clone func that fails by raising an exception. */
69VALUE grpc_rb_cannot_init_copy(VALUE copy, VALUE self) {
70 rb_raise(rb_eTypeError,
71 "initialization of %s only allowed from the gRPC native layer",
72 rb_obj_classname(copy));
73 return Qnil;
74}
75
76/* id_tv_{,u}sec are accessor methods on Ruby Time instances. */
77static ID id_tv_sec;
78static ID id_tv_nsec;
79
80/**
81 * grpc_rb_time_timeval creates a time_eval from a ruby time object.
82 *
83 * This func is copied from ruby source, MRI/source/time.c, which is published
84 * under the same license as the ruby.h, on which the entire extensions is
85 * based.
86 */
87gpr_timespec grpc_rb_time_timeval(VALUE time, int interval) {
88 gpr_timespec t;
89 gpr_timespec *time_const;
90 const char *tstr = interval ? "time interval" : "time";
91 const char *want = " want <secs from epoch>|<Time>|<GRPC::TimeConst.*>";
92
93 switch (TYPE(time)) {
nnoble097ef9b2014-12-01 17:06:10 -080094 case T_DATA:
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +090095 if (CLASS_OF(time) == grpc_rb_cTimeVal) {
nnoble097ef9b2014-12-01 17:06:10 -080096 Data_Get_Struct(time, gpr_timespec, time_const);
97 t = *time_const;
98 } else if (CLASS_OF(time) == rb_cTime) {
Craig Tillerb5dcec52015-01-13 11:13:42 -080099 t.tv_sec = NUM2INT(rb_funcall(time, id_tv_sec, 0));
nnoble097ef9b2014-12-01 17:06:10 -0800100 t.tv_nsec = NUM2INT(rb_funcall(time, id_tv_nsec, 0));
101 } else {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800102 rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
103 rb_obj_classname(time), want);
nnoble097ef9b2014-12-01 17:06:10 -0800104 }
105 break;
106
107 case T_FIXNUM:
108 t.tv_sec = FIX2LONG(time);
109 if (interval && t.tv_sec < 0)
110 rb_raise(rb_eArgError, "%s must be positive", tstr);
111 t.tv_nsec = 0;
112 break;
113
114 case T_FLOAT:
mattnb9e15632015-02-28 23:45:58 +0900115 if (interval && RFLOAT_VALUE(time) < 0.0)
nnoble097ef9b2014-12-01 17:06:10 -0800116 rb_raise(rb_eArgError, "%s must be positive", tstr);
117 else {
118 double f, d;
119
mattnb9e15632015-02-28 23:45:58 +0900120 d = modf(RFLOAT_VALUE(time), &f);
nnoble097ef9b2014-12-01 17:06:10 -0800121 if (d < 0) {
122 d += 1;
123 f -= 1;
124 }
125 t.tv_sec = (time_t)f;
126 if (f != t.tv_sec) {
127 rb_raise(rb_eRangeError, "%f out of Time range",
mattnb9e15632015-02-28 23:45:58 +0900128 RFLOAT_VALUE(time));
nnoble097ef9b2014-12-01 17:06:10 -0800129 }
Craig Tillerb5dcec52015-01-13 11:13:42 -0800130 t.tv_nsec = (time_t)(d * 1e9 + 0.5);
nnoble097ef9b2014-12-01 17:06:10 -0800131 }
132 break;
133
134 case T_BIGNUM:
135 t.tv_sec = NUM2LONG(time);
136 if (interval && t.tv_sec < 0)
137 rb_raise(rb_eArgError, "%s must be positive", tstr);
138 t.tv_nsec = 0;
139 break;
140
141 default:
Craig Tillerb5dcec52015-01-13 11:13:42 -0800142 rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
143 rb_obj_classname(time), want);
nnoble097ef9b2014-12-01 17:06:10 -0800144 break;
145 }
146 return t;
147}
148
Tim Emiola409e6c82015-02-17 17:46:35 -0800149void Init_grpc_status_codes() {
temiola58327912014-12-15 17:51:16 -0800150 /* Constants representing the status codes or grpc_status_code in status.h */
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900151 VALUE grpc_rb_mStatusCodes =
152 rb_define_module_under(grpc_rb_mGrpcCore, "StatusCodes");
153 rb_define_const(grpc_rb_mStatusCodes, "OK", INT2NUM(GRPC_STATUS_OK));
154 rb_define_const(grpc_rb_mStatusCodes, "CANCELLED",
155 INT2NUM(GRPC_STATUS_CANCELLED));
156 rb_define_const(grpc_rb_mStatusCodes, "UNKNOWN",
157 INT2NUM(GRPC_STATUS_UNKNOWN));
158 rb_define_const(grpc_rb_mStatusCodes, "INVALID_ARGUMENT",
temiola58327912014-12-15 17:51:16 -0800159 INT2NUM(GRPC_STATUS_INVALID_ARGUMENT));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900160 rb_define_const(grpc_rb_mStatusCodes, "DEADLINE_EXCEEDED",
temiola58327912014-12-15 17:51:16 -0800161 INT2NUM(GRPC_STATUS_DEADLINE_EXCEEDED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900162 rb_define_const(grpc_rb_mStatusCodes, "NOT_FOUND",
163 INT2NUM(GRPC_STATUS_NOT_FOUND));
164 rb_define_const(grpc_rb_mStatusCodes, "ALREADY_EXISTS",
temiola58327912014-12-15 17:51:16 -0800165 INT2NUM(GRPC_STATUS_ALREADY_EXISTS));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900166 rb_define_const(grpc_rb_mStatusCodes, "PERMISSION_DENIED",
temiola58327912014-12-15 17:51:16 -0800167 INT2NUM(GRPC_STATUS_PERMISSION_DENIED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900168 rb_define_const(grpc_rb_mStatusCodes, "UNAUTHENTICATED",
temiola58327912014-12-15 17:51:16 -0800169 INT2NUM(GRPC_STATUS_UNAUTHENTICATED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900170 rb_define_const(grpc_rb_mStatusCodes, "RESOURCE_EXHAUSTED",
temiola58327912014-12-15 17:51:16 -0800171 INT2NUM(GRPC_STATUS_RESOURCE_EXHAUSTED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900172 rb_define_const(grpc_rb_mStatusCodes, "FAILED_PRECONDITION",
temiola58327912014-12-15 17:51:16 -0800173 INT2NUM(GRPC_STATUS_FAILED_PRECONDITION));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900174 rb_define_const(grpc_rb_mStatusCodes, "ABORTED",
175 INT2NUM(GRPC_STATUS_ABORTED));
176 rb_define_const(grpc_rb_mStatusCodes, "OUT_OF_RANGE",
temiola58327912014-12-15 17:51:16 -0800177 INT2NUM(GRPC_STATUS_OUT_OF_RANGE));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900178 rb_define_const(grpc_rb_mStatusCodes, "UNIMPLEMENTED",
temiola58327912014-12-15 17:51:16 -0800179 INT2NUM(GRPC_STATUS_UNIMPLEMENTED));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900180 rb_define_const(grpc_rb_mStatusCodes, "INTERNAL",
181 INT2NUM(GRPC_STATUS_INTERNAL));
182 rb_define_const(grpc_rb_mStatusCodes, "UNAVAILABLE",
temiola58327912014-12-15 17:51:16 -0800183 INT2NUM(GRPC_STATUS_UNAVAILABLE));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900184 rb_define_const(grpc_rb_mStatusCodes, "DATA_LOSS",
185 INT2NUM(GRPC_STATUS_DATA_LOSS));
temiola58327912014-12-15 17:51:16 -0800186}
187
nnoble097ef9b2014-12-01 17:06:10 -0800188/* id_at is the constructor method of the ruby standard Time class. */
189static ID id_at;
190
191/* id_inspect is the inspect method found on various ruby objects. */
192static ID id_inspect;
193
194/* id_to_s is the to_s method found on various ruby objects. */
195static ID id_to_s;
196
Tim Emiola98a32d32015-03-28 01:48:44 -0700197/* Converts a wrapped time constant to a standard time. */
nnoble097ef9b2014-12-01 17:06:10 -0800198VALUE grpc_rb_time_val_to_time(VALUE self) {
199 gpr_timespec *time_const = NULL;
200 Data_Get_Struct(self, gpr_timespec, time_const);
201 return rb_funcall(rb_cTime, id_at, 2, INT2NUM(time_const->tv_sec),
202 INT2NUM(time_const->tv_nsec));
203}
204
205/* Invokes inspect on the ctime version of the time val. */
206VALUE grpc_rb_time_val_inspect(VALUE self) {
207 return rb_funcall(grpc_rb_time_val_to_time(self), id_inspect, 0);
208}
209
210/* Invokes to_s on the ctime version of the time val. */
211VALUE grpc_rb_time_val_to_s(VALUE self) {
212 return rb_funcall(grpc_rb_time_val_to_time(self), id_to_s, 0);
213}
214
215/* Adds a module with constants that map to gpr's static timeval structs. */
Tim Emiola409e6c82015-02-17 17:46:35 -0800216void Init_grpc_time_consts() {
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900217 VALUE grpc_rb_mTimeConsts =
218 rb_define_module_under(grpc_rb_mGrpcCore, "TimeConsts");
219 grpc_rb_cTimeVal =
220 rb_define_class_under(grpc_rb_mGrpcCore, "TimeSpec", rb_cObject);
221 rb_define_const(grpc_rb_mTimeConsts, "ZERO",
222 Data_Wrap_Struct(grpc_rb_cTimeVal,
Yuki Yugui Sonoda76801d22015-04-11 14:20:27 +0900223 GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800224 (void *)&gpr_time_0));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900225 rb_define_const(grpc_rb_mTimeConsts, "INFINITE_FUTURE",
226 Data_Wrap_Struct(grpc_rb_cTimeVal,
Yuki Yugui Sonoda76801d22015-04-11 14:20:27 +0900227 GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800228 (void *)&gpr_inf_future));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900229 rb_define_const(grpc_rb_mTimeConsts, "INFINITE_PAST",
230 Data_Wrap_Struct(grpc_rb_cTimeVal,
Yuki Yugui Sonoda76801d22015-04-11 14:20:27 +0900231 GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800232 (void *)&gpr_inf_past));
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900233 rb_define_method(grpc_rb_cTimeVal, "to_time", grpc_rb_time_val_to_time, 0);
234 rb_define_method(grpc_rb_cTimeVal, "inspect", grpc_rb_time_val_inspect, 0);
235 rb_define_method(grpc_rb_cTimeVal, "to_s", grpc_rb_time_val_to_s, 0);
nnoble097ef9b2014-12-01 17:06:10 -0800236 id_at = rb_intern("at");
237 id_inspect = rb_intern("inspect");
238 id_to_s = rb_intern("to_s");
239 id_tv_sec = rb_intern("tv_sec");
240 id_tv_nsec = rb_intern("tv_nsec");
241}
242
Craig Tillerb5dcec52015-01-13 11:13:42 -0800243void grpc_rb_shutdown(void *vm) { grpc_shutdown(); }
nnoble097ef9b2014-12-01 17:06:10 -0800244
Tim Emiola409e6c82015-02-17 17:46:35 -0800245/* Initialize the GRPC module structs */
temiola21bb60c2014-12-18 10:58:22 -0800246
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900247/* grpc_rb_sNewServerRpc is the struct that holds new server rpc details. */
248VALUE grpc_rb_sNewServerRpc = Qnil;
249/* grpc_rb_sStatus is the struct that holds status details. */
250VALUE grpc_rb_sStatus = Qnil;
temiola21bb60c2014-12-18 10:58:22 -0800251
Tim Emiola409e6c82015-02-17 17:46:35 -0800252/* Initialize the GRPC module. */
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900253VALUE grpc_rb_mGRPC = Qnil;
254VALUE grpc_rb_mGrpcCore = Qnil;
temiola21bb60c2014-12-18 10:58:22 -0800255
nnoble097ef9b2014-12-01 17:06:10 -0800256void Init_grpc() {
257 grpc_init();
258 ruby_vm_at_exit(grpc_rb_shutdown);
Yuki Yugui Sonodaa7d369e2015-04-11 11:48:36 +0900259 grpc_rb_mGRPC = rb_define_module("GRPC");
260 grpc_rb_mGrpcCore = rb_define_module_under(grpc_rb_mGRPC, "Core");
261 grpc_rb_sNewServerRpc =
262 rb_struct_define("NewServerRpc", "method", "host",
263 "deadline", "metadata", "call", NULL);
264 grpc_rb_sStatus =
265 rb_struct_define("Status", "code", "details", "metadata", NULL);
Tim Emiola98a32d32015-03-28 01:48:44 -0700266 sym_code = ID2SYM(rb_intern("code"));
267 sym_details = ID2SYM(rb_intern("details"));
268 sym_metadata = ID2SYM(rb_intern("metadata"));
nnoble097ef9b2014-12-01 17:06:10 -0800269
Tim Emiola409e6c82015-02-17 17:46:35 -0800270 Init_grpc_channel();
271 Init_grpc_completion_queue();
272 Init_grpc_call();
273 Init_grpc_credentials();
Tim Emiola409e6c82015-02-17 17:46:35 -0800274 Init_grpc_server();
275 Init_grpc_server_credentials();
276 Init_grpc_status_codes();
277 Init_grpc_time_consts();
nnoble097ef9b2014-12-01 17:06:10 -0800278}