blob: 9c54a05c45d4bdefdaffa43d74d2197122d53222 [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#include "rb_status.h"
52
53/* Define common vars and funcs declared in rb.h */
54const RUBY_DATA_FUNC GC_NOT_MARKED = NULL;
55const RUBY_DATA_FUNC GC_DONT_FREE = NULL;
56
57VALUE rb_cTimeVal = Qnil;
58
59/* 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
101 switch (TYPE(time)) {
102
103 case T_DATA:
104 if (CLASS_OF(time) == rb_cTimeVal) {
105 Data_Get_Struct(time, gpr_timespec, time_const);
106 t = *time_const;
107 } else if (CLASS_OF(time) == rb_cTime) {
108 t.tv_sec = NUM2INT(rb_funcall(time, id_tv_sec, 0));
109 t.tv_nsec = NUM2INT(rb_funcall(time, id_tv_nsec, 0));
110 } else {
111 rb_raise(rb_eTypeError,
112 "bad input: (%s)->c_timeval, got <%s>,%s",
113 tstr, rb_obj_classname(time), want);
114 }
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:
125 if (interval && RFLOAT(time)->float_value < 0.0)
126 rb_raise(rb_eArgError, "%s must be positive", tstr);
127 else {
128 double f, d;
129
130 d = modf(RFLOAT(time)->float_value, &f);
131 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",
138 RFLOAT(time)->float_value);
139 }
140 t.tv_nsec = (time_t)(d*1e9+0.5);
141 }
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:
152 rb_raise(rb_eTypeError,
153 "bad input: (%s)->c_timeval, got <%s>,%s",
154 tstr, rb_obj_classname(time), want);
155 break;
156 }
157 return t;
158}
159
160/* id_at is the constructor method of the ruby standard Time class. */
161static ID id_at;
162
163/* id_inspect is the inspect method found on various ruby objects. */
164static ID id_inspect;
165
166/* id_to_s is the to_s method found on various ruby objects. */
167static ID id_to_s;
168
169/* Converts `a wrapped time constant to a standard time. */
170VALUE grpc_rb_time_val_to_time(VALUE self) {
171 gpr_timespec *time_const = NULL;
172 Data_Get_Struct(self, gpr_timespec, time_const);
173 return rb_funcall(rb_cTime, id_at, 2, INT2NUM(time_const->tv_sec),
174 INT2NUM(time_const->tv_nsec));
175}
176
177/* Invokes inspect on the ctime version of the time val. */
178VALUE grpc_rb_time_val_inspect(VALUE self) {
179 return rb_funcall(grpc_rb_time_val_to_time(self), id_inspect, 0);
180}
181
182/* Invokes to_s on the ctime version of the time val. */
183VALUE grpc_rb_time_val_to_s(VALUE self) {
184 return rb_funcall(grpc_rb_time_val_to_time(self), id_to_s, 0);
185}
186
187/* Adds a module with constants that map to gpr's static timeval structs. */
188void Init_google_time_consts() {
nnoble0c475f02014-12-05 15:37:39 -0800189 VALUE rb_mTimeConsts = rb_define_module_under(rb_mGoogleRpcCore,
190 "TimeConsts");
191 rb_cTimeVal = rb_define_class_under(rb_mGoogleRpcCore, "TimeSpec",
192 rb_cObject);
nnoble097ef9b2014-12-01 17:06:10 -0800193 rb_define_const(rb_mTimeConsts, "ZERO",
194 Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED,
195 GC_DONT_FREE, (void *)&gpr_time_0));
196 rb_define_const(rb_mTimeConsts, "INFINITE_FUTURE",
197 Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED,
198 GC_DONT_FREE, (void *)&gpr_inf_future));
199 rb_define_const(rb_mTimeConsts, "INFINITE_PAST",
200 Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED,
201 GC_DONT_FREE, (void *)&gpr_inf_past));
202 rb_define_method(rb_cTimeVal, "to_time", grpc_rb_time_val_to_time, 0);
203 rb_define_method(rb_cTimeVal, "inspect", grpc_rb_time_val_inspect, 0);
204 rb_define_method(rb_cTimeVal, "to_s", grpc_rb_time_val_to_s, 0);
205 id_at = rb_intern("at");
206 id_inspect = rb_intern("inspect");
207 id_to_s = rb_intern("to_s");
208 id_tv_sec = rb_intern("tv_sec");
209 id_tv_nsec = rb_intern("tv_nsec");
210}
211
212void grpc_rb_shutdown(void *vm) {
213 grpc_shutdown();
214}
215
216/* Initialize the Google RPC module. */
217VALUE rb_mGoogle = Qnil;
218VALUE rb_mGoogleRPC = Qnil;
nnoble0c475f02014-12-05 15:37:39 -0800219VALUE rb_mGoogleRpcCore = Qnil;
nnoble097ef9b2014-12-01 17:06:10 -0800220void Init_grpc() {
221 grpc_init();
222 ruby_vm_at_exit(grpc_rb_shutdown);
223 rb_mGoogle = rb_define_module("Google");
224 rb_mGoogleRPC = rb_define_module_under(rb_mGoogle, "RPC");
nnoble0c475f02014-12-05 15:37:39 -0800225 rb_mGoogleRpcCore = rb_define_module_under(rb_mGoogleRPC, "Core");
nnoble097ef9b2014-12-01 17:06:10 -0800226
227 Init_google_rpc_byte_buffer();
228 Init_google_rpc_event();
229 Init_google_rpc_channel();
230 Init_google_rpc_completion_queue();
231 Init_google_rpc_call();
nnoble0c475f02014-12-05 15:37:39 -0800232 Init_google_rpc_credentials();
nnoble097ef9b2014-12-01 17:06:10 -0800233 Init_google_rpc_metadata();
234 Init_google_rpc_server();
nnoble0c475f02014-12-05 15:37:39 -0800235 Init_google_rpc_server_credentials();
nnoble097ef9b2014-12-01 17:06:10 -0800236 Init_google_rpc_status();
237 Init_google_time_consts();
238}