blob: 5cc45cf743af7c1cc70ee986733fcc34c18c7c4d [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"
49#include "rb_status.h"
50
51/* Define common vars and funcs declared in rb.h */
52const RUBY_DATA_FUNC GC_NOT_MARKED = NULL;
53const RUBY_DATA_FUNC GC_DONT_FREE = NULL;
54
55VALUE rb_cTimeVal = Qnil;
56
57/* Alloc func that blocks allocation of a given object by raising an
58 * exception. */
59VALUE grpc_rb_cannot_alloc(VALUE cls) {
60 rb_raise(rb_eTypeError,
61 "allocation of %s only allowed from the gRPC native layer",
62 rb_class2name(cls));
63 return Qnil;
64}
65
66/* Init func that fails by raising an exception. */
67VALUE grpc_rb_cannot_init(VALUE self) {
68 rb_raise(rb_eTypeError,
69 "initialization of %s only allowed from the gRPC native layer",
70 rb_obj_classname(self));
71 return Qnil;
72}
73
74/* Init/Clone func that fails by raising an exception. */
75VALUE grpc_rb_cannot_init_copy(VALUE copy, VALUE self) {
76 rb_raise(rb_eTypeError,
77 "initialization of %s only allowed from the gRPC native layer",
78 rb_obj_classname(copy));
79 return Qnil;
80}
81
82/* id_tv_{,u}sec are accessor methods on Ruby Time instances. */
83static ID id_tv_sec;
84static ID id_tv_nsec;
85
86/**
87 * grpc_rb_time_timeval creates a time_eval from a ruby time object.
88 *
89 * This func is copied from ruby source, MRI/source/time.c, which is published
90 * under the same license as the ruby.h, on which the entire extensions is
91 * based.
92 */
93gpr_timespec grpc_rb_time_timeval(VALUE time, int interval) {
94 gpr_timespec t;
95 gpr_timespec *time_const;
96 const char *tstr = interval ? "time interval" : "time";
97 const char *want = " want <secs from epoch>|<Time>|<GRPC::TimeConst.*>";
98
99 switch (TYPE(time)) {
100
101 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) {
106 t.tv_sec = NUM2INT(rb_funcall(time, id_tv_sec, 0));
107 t.tv_nsec = NUM2INT(rb_funcall(time, id_tv_nsec, 0));
108 } else {
109 rb_raise(rb_eTypeError,
110 "bad input: (%s)->c_timeval, got <%s>,%s",
111 tstr, rb_obj_classname(time), want);
112 }
113 break;
114
115 case T_FIXNUM:
116 t.tv_sec = FIX2LONG(time);
117 if (interval && t.tv_sec < 0)
118 rb_raise(rb_eArgError, "%s must be positive", tstr);
119 t.tv_nsec = 0;
120 break;
121
122 case T_FLOAT:
123 if (interval && RFLOAT(time)->float_value < 0.0)
124 rb_raise(rb_eArgError, "%s must be positive", tstr);
125 else {
126 double f, d;
127
128 d = modf(RFLOAT(time)->float_value, &f);
129 if (d < 0) {
130 d += 1;
131 f -= 1;
132 }
133 t.tv_sec = (time_t)f;
134 if (f != t.tv_sec) {
135 rb_raise(rb_eRangeError, "%f out of Time range",
136 RFLOAT(time)->float_value);
137 }
138 t.tv_nsec = (time_t)(d*1e9+0.5);
139 }
140 break;
141
142 case T_BIGNUM:
143 t.tv_sec = NUM2LONG(time);
144 if (interval && t.tv_sec < 0)
145 rb_raise(rb_eArgError, "%s must be positive", tstr);
146 t.tv_nsec = 0;
147 break;
148
149 default:
150 rb_raise(rb_eTypeError,
151 "bad input: (%s)->c_timeval, got <%s>,%s",
152 tstr, rb_obj_classname(time), want);
153 break;
154 }
155 return t;
156}
157
158/* id_at is the constructor method of the ruby standard Time class. */
159static ID id_at;
160
161/* id_inspect is the inspect method found on various ruby objects. */
162static ID id_inspect;
163
164/* id_to_s is the to_s method found on various ruby objects. */
165static ID id_to_s;
166
167/* Converts `a wrapped time constant to a standard time. */
168VALUE grpc_rb_time_val_to_time(VALUE self) {
169 gpr_timespec *time_const = NULL;
170 Data_Get_Struct(self, gpr_timespec, time_const);
171 return rb_funcall(rb_cTime, id_at, 2, INT2NUM(time_const->tv_sec),
172 INT2NUM(time_const->tv_nsec));
173}
174
175/* Invokes inspect on the ctime version of the time val. */
176VALUE grpc_rb_time_val_inspect(VALUE self) {
177 return rb_funcall(grpc_rb_time_val_to_time(self), id_inspect, 0);
178}
179
180/* Invokes to_s on the ctime version of the time val. */
181VALUE grpc_rb_time_val_to_s(VALUE self) {
182 return rb_funcall(grpc_rb_time_val_to_time(self), id_to_s, 0);
183}
184
185/* Adds a module with constants that map to gpr's static timeval structs. */
186void Init_google_time_consts() {
187 VALUE rb_mTimeConsts = rb_define_module_under(rb_mGoogleRPC, "TimeConsts");
188 rb_cTimeVal = rb_define_class_under(rb_mGoogleRPC, "TimeSpec", rb_cObject);
189 rb_define_const(rb_mTimeConsts, "ZERO",
190 Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED,
191 GC_DONT_FREE, (void *)&gpr_time_0));
192 rb_define_const(rb_mTimeConsts, "INFINITE_FUTURE",
193 Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED,
194 GC_DONT_FREE, (void *)&gpr_inf_future));
195 rb_define_const(rb_mTimeConsts, "INFINITE_PAST",
196 Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED,
197 GC_DONT_FREE, (void *)&gpr_inf_past));
198 rb_define_method(rb_cTimeVal, "to_time", grpc_rb_time_val_to_time, 0);
199 rb_define_method(rb_cTimeVal, "inspect", grpc_rb_time_val_inspect, 0);
200 rb_define_method(rb_cTimeVal, "to_s", grpc_rb_time_val_to_s, 0);
201 id_at = rb_intern("at");
202 id_inspect = rb_intern("inspect");
203 id_to_s = rb_intern("to_s");
204 id_tv_sec = rb_intern("tv_sec");
205 id_tv_nsec = rb_intern("tv_nsec");
206}
207
208void grpc_rb_shutdown(void *vm) {
209 grpc_shutdown();
210}
211
212/* Initialize the Google RPC module. */
213VALUE rb_mGoogle = Qnil;
214VALUE rb_mGoogleRPC = Qnil;
215void Init_grpc() {
216 grpc_init();
217 ruby_vm_at_exit(grpc_rb_shutdown);
218 rb_mGoogle = rb_define_module("Google");
219 rb_mGoogleRPC = rb_define_module_under(rb_mGoogle, "RPC");
220
221 Init_google_rpc_byte_buffer();
222 Init_google_rpc_event();
223 Init_google_rpc_channel();
224 Init_google_rpc_completion_queue();
225 Init_google_rpc_call();
226 Init_google_rpc_metadata();
227 Init_google_rpc_server();
228 Init_google_rpc_status();
229 Init_google_time_consts();
230}