blob: c8e2bb5c4ae2fc9bd1b599810fc7a64039c4c5bc [file] [log] [blame]
nnoble097ef9b2014-12-01 17:06:10 -08001/*
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"
nnoble0c475f02014-12-05 15:37:39 -080049#include "rb_credentials.h"
50#include "rb_server_credentials.h"
nnoble097ef9b2014-12-01 17:06:10 -080051
52/* Define common vars and funcs declared in rb.h */
53const RUBY_DATA_FUNC GC_NOT_MARKED = NULL;
54const RUBY_DATA_FUNC GC_DONT_FREE = NULL;
55
56VALUE rb_cTimeVal = Qnil;
57
58/* Alloc func that blocks allocation of a given object by raising an
59 * exception. */
60VALUE 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. */
68VALUE 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. */
76VALUE 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. */
84static ID id_tv_sec;
85static 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 */
94gpr_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)) {
nnoble097ef9b2014-12-01 17:06:10 -0800101 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 Tillerb5dcec52015-01-13 11:13:42 -0800106 t.tv_sec = NUM2INT(rb_funcall(time, id_tv_sec, 0));
nnoble097ef9b2014-12-01 17:06:10 -0800107 t.tv_nsec = NUM2INT(rb_funcall(time, id_tv_nsec, 0));
108 } else {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800109 rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
110 rb_obj_classname(time), want);
nnoble097ef9b2014-12-01 17:06:10 -0800111 }
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 Tillerb5dcec52015-01-13 11:13:42 -0800137 t.tv_nsec = (time_t)(d * 1e9 + 0.5);
nnoble097ef9b2014-12-01 17:06:10 -0800138 }
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 Tillerb5dcec52015-01-13 11:13:42 -0800149 rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
150 rb_obj_classname(time), want);
nnoble097ef9b2014-12-01 17:06:10 -0800151 break;
152 }
153 return t;
154}
155
Tim Emiola409e6c82015-02-17 17:46:35 -0800156void Init_grpc_status_codes() {
temiola58327912014-12-15 17:51:16 -0800157 /* Constants representing the status codes or grpc_status_code in status.h */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800158 VALUE rb_mStatusCodes =
Tim Emiola409e6c82015-02-17 17:46:35 -0800159 rb_define_module_under(rb_mGrpcCore, "StatusCodes");
temiola58327912014-12-15 17:51:16 -0800160 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
nnoble097ef9b2014-12-01 17:06:10 -0800189/* id_at is the constructor method of the ruby standard Time class. */
190static ID id_at;
191
192/* id_inspect is the inspect method found on various ruby objects. */
193static ID id_inspect;
194
195/* id_to_s is the to_s method found on various ruby objects. */
196static ID id_to_s;
197
198/* Converts `a wrapped time constant to a standard time. */
199VALUE 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. */
207VALUE 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. */
212VALUE 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 Emiola409e6c82015-02-17 17:46:35 -0800217void Init_grpc_time_consts() {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800218 VALUE rb_mTimeConsts =
Tim Emiola409e6c82015-02-17 17:46:35 -0800219 rb_define_module_under(rb_mGrpcCore, "TimeConsts");
Craig Tillerb5dcec52015-01-13 11:13:42 -0800220 rb_cTimeVal =
Tim Emiola409e6c82015-02-17 17:46:35 -0800221 rb_define_class_under(rb_mGrpcCore, "TimeSpec", rb_cObject);
nnoble097ef9b2014-12-01 17:06:10 -0800222 rb_define_const(rb_mTimeConsts, "ZERO",
Craig Tillerb5dcec52015-01-13 11:13:42 -0800223 Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE,
224 (void *)&gpr_time_0));
nnoble097ef9b2014-12-01 17:06:10 -0800225 rb_define_const(rb_mTimeConsts, "INFINITE_FUTURE",
Craig Tillerb5dcec52015-01-13 11:13:42 -0800226 Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE,
227 (void *)&gpr_inf_future));
nnoble097ef9b2014-12-01 17:06:10 -0800228 rb_define_const(rb_mTimeConsts, "INFINITE_PAST",
Craig Tillerb5dcec52015-01-13 11:13:42 -0800229 Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE,
230 (void *)&gpr_inf_past));
nnoble097ef9b2014-12-01 17:06:10 -0800231 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 Tillerb5dcec52015-01-13 11:13:42 -0800241void grpc_rb_shutdown(void *vm) { grpc_shutdown(); }
nnoble097ef9b2014-12-01 17:06:10 -0800242
Tim Emiola409e6c82015-02-17 17:46:35 -0800243/* Initialize the GRPC module structs */
temiola21bb60c2014-12-18 10:58:22 -0800244
245/* rb_sNewServerRpc is the struct that holds new server rpc details. */
246VALUE rb_sNewServerRpc = Qnil;
247/* rb_sStatus is the struct that holds status details. */
248VALUE rb_sStatus = Qnil;
249
Tim Emiola409e6c82015-02-17 17:46:35 -0800250/* Initialize the GRPC module. */
251VALUE rb_mGRPC = Qnil;
252VALUE rb_mGrpcCore = Qnil;
temiola21bb60c2014-12-18 10:58:22 -0800253
nnoble097ef9b2014-12-01 17:06:10 -0800254void Init_grpc() {
255 grpc_init();
256 ruby_vm_at_exit(grpc_rb_shutdown);
Tim Emiola409e6c82015-02-17 17:46:35 -0800257 rb_mGRPC = rb_define_module("GRPC");
258 rb_mGrpcCore = rb_define_module_under(rb_mGRPC, "Core");
temiola21bb60c2014-12-18 10:58:22 -0800259 rb_sNewServerRpc = rb_struct_define("NewServerRpc", "method", "host",
260 "deadline", "metadata", NULL);
261 rb_sStatus = rb_struct_define("Status", "code", "details", "metadata", NULL);
nnoble097ef9b2014-12-01 17:06:10 -0800262
Tim Emiola409e6c82015-02-17 17:46:35 -0800263 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();
nnoble097ef9b2014-12-01 17:06:10 -0800274}