grpc 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of grpc might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +1 -0
- data/.rubocop.yml +10 -0
- data/.rubocop_todo.yml +52 -0
- data/Gemfile +4 -0
- data/README.md +82 -0
- data/Rakefile +54 -0
- data/bin/apis/google/protobuf/empty.rb +44 -0
- data/bin/apis/pubsub_demo.rb +267 -0
- data/bin/apis/tech/pubsub/proto/pubsub.rb +174 -0
- data/bin/apis/tech/pubsub/proto/pubsub_services.rb +103 -0
- data/bin/interop/README.md +8 -0
- data/bin/interop/interop_client.rb +334 -0
- data/bin/interop/interop_server.rb +192 -0
- data/bin/interop/test/cpp/interop/empty.rb +44 -0
- data/bin/interop/test/cpp/interop/messages.rb +89 -0
- data/bin/interop/test/cpp/interop/test.rb +43 -0
- data/bin/interop/test/cpp/interop/test_services.rb +60 -0
- data/bin/math.proto +80 -0
- data/bin/math.rb +61 -0
- data/bin/math_client.rb +147 -0
- data/bin/math_server.rb +190 -0
- data/bin/math_services.rb +56 -0
- data/bin/noproto_client.rb +108 -0
- data/bin/noproto_server.rb +112 -0
- data/ext/grpc/extconf.rb +76 -0
- data/ext/grpc/rb_byte_buffer.c +241 -0
- data/ext/grpc/rb_byte_buffer.h +54 -0
- data/ext/grpc/rb_call.c +569 -0
- data/ext/grpc/rb_call.h +59 -0
- data/ext/grpc/rb_channel.c +264 -0
- data/ext/grpc/rb_channel.h +49 -0
- data/ext/grpc/rb_channel_args.c +154 -0
- data/ext/grpc/rb_channel_args.h +52 -0
- data/ext/grpc/rb_completion_queue.c +185 -0
- data/ext/grpc/rb_completion_queue.h +50 -0
- data/ext/grpc/rb_credentials.c +281 -0
- data/ext/grpc/rb_credentials.h +50 -0
- data/ext/grpc/rb_event.c +361 -0
- data/ext/grpc/rb_event.h +53 -0
- data/ext/grpc/rb_grpc.c +274 -0
- data/ext/grpc/rb_grpc.h +74 -0
- data/ext/grpc/rb_metadata.c +215 -0
- data/ext/grpc/rb_metadata.h +53 -0
- data/ext/grpc/rb_server.c +278 -0
- data/ext/grpc/rb_server.h +50 -0
- data/ext/grpc/rb_server_credentials.c +210 -0
- data/ext/grpc/rb_server_credentials.h +50 -0
- data/grpc.gemspec +41 -0
- data/lib/grpc.rb +39 -0
- data/lib/grpc/core/event.rb +44 -0
- data/lib/grpc/core/time_consts.rb +71 -0
- data/lib/grpc/errors.rb +61 -0
- data/lib/grpc/generic/active_call.rb +536 -0
- data/lib/grpc/generic/bidi_call.rb +221 -0
- data/lib/grpc/generic/client_stub.rb +413 -0
- data/lib/grpc/generic/rpc_desc.rb +150 -0
- data/lib/grpc/generic/rpc_server.rb +404 -0
- data/lib/grpc/generic/service.rb +235 -0
- data/lib/grpc/logconfig.rb +40 -0
- data/lib/grpc/version.rb +33 -0
- data/spec/alloc_spec.rb +44 -0
- data/spec/byte_buffer_spec.rb +67 -0
- data/spec/call_spec.rb +163 -0
- data/spec/channel_spec.rb +181 -0
- data/spec/client_server_spec.rb +372 -0
- data/spec/completion_queue_spec.rb +74 -0
- data/spec/credentials_spec.rb +71 -0
- data/spec/event_spec.rb +53 -0
- data/spec/generic/active_call_spec.rb +373 -0
- data/spec/generic/client_stub_spec.rb +519 -0
- data/spec/generic/rpc_desc_spec.rb +357 -0
- data/spec/generic/rpc_server_pool_spec.rb +139 -0
- data/spec/generic/rpc_server_spec.rb +404 -0
- data/spec/generic/service_spec.rb +342 -0
- data/spec/metadata_spec.rb +64 -0
- data/spec/server_credentials_spec.rb +69 -0
- data/spec/server_spec.rb +212 -0
- data/spec/spec_helper.rb +51 -0
- data/spec/testdata/README +1 -0
- data/spec/testdata/ca.pem +15 -0
- data/spec/testdata/server1.key +16 -0
- data/spec/testdata/server1.pem +16 -0
- data/spec/time_consts_spec.rb +89 -0
- metadata +353 -0
data/ext/grpc/rb_grpc.c
ADDED
@@ -0,0 +1,274 @@
|
|
1
|
+
/*
|
2
|
+
*
|
3
|
+
* Copyright 2015, 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_credentials.h"
|
50
|
+
#include "rb_server_credentials.h"
|
51
|
+
|
52
|
+
/* Define common vars and funcs declared in rb.h */
|
53
|
+
const RUBY_DATA_FUNC GC_NOT_MARKED = NULL;
|
54
|
+
const RUBY_DATA_FUNC GC_DONT_FREE = NULL;
|
55
|
+
|
56
|
+
VALUE rb_cTimeVal = Qnil;
|
57
|
+
|
58
|
+
/* Alloc func that blocks allocation of a given object by raising an
|
59
|
+
* exception. */
|
60
|
+
VALUE 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. */
|
68
|
+
VALUE 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. */
|
76
|
+
VALUE 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. */
|
84
|
+
static ID id_tv_sec;
|
85
|
+
static 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
|
+
*/
|
94
|
+
gpr_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)) {
|
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, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
|
110
|
+
rb_obj_classname(time), want);
|
111
|
+
}
|
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
|
+
}
|
137
|
+
t.tv_nsec = (time_t)(d * 1e9 + 0.5);
|
138
|
+
}
|
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:
|
149
|
+
rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr,
|
150
|
+
rb_obj_classname(time), want);
|
151
|
+
break;
|
152
|
+
}
|
153
|
+
return t;
|
154
|
+
}
|
155
|
+
|
156
|
+
void Init_grpc_status_codes() {
|
157
|
+
/* Constants representing the status codes or grpc_status_code in status.h */
|
158
|
+
VALUE rb_mStatusCodes =
|
159
|
+
rb_define_module_under(rb_mGrpcCore, "StatusCodes");
|
160
|
+
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
|
+
|
189
|
+
/* id_at is the constructor method of the ruby standard Time class. */
|
190
|
+
static ID id_at;
|
191
|
+
|
192
|
+
/* id_inspect is the inspect method found on various ruby objects. */
|
193
|
+
static ID id_inspect;
|
194
|
+
|
195
|
+
/* id_to_s is the to_s method found on various ruby objects. */
|
196
|
+
static ID id_to_s;
|
197
|
+
|
198
|
+
/* Converts `a wrapped time constant to a standard time. */
|
199
|
+
VALUE 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. */
|
207
|
+
VALUE 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. */
|
212
|
+
VALUE 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. */
|
217
|
+
void Init_grpc_time_consts() {
|
218
|
+
VALUE rb_mTimeConsts =
|
219
|
+
rb_define_module_under(rb_mGrpcCore, "TimeConsts");
|
220
|
+
rb_cTimeVal =
|
221
|
+
rb_define_class_under(rb_mGrpcCore, "TimeSpec", rb_cObject);
|
222
|
+
rb_define_const(rb_mTimeConsts, "ZERO",
|
223
|
+
Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE,
|
224
|
+
(void *)&gpr_time_0));
|
225
|
+
rb_define_const(rb_mTimeConsts, "INFINITE_FUTURE",
|
226
|
+
Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE,
|
227
|
+
(void *)&gpr_inf_future));
|
228
|
+
rb_define_const(rb_mTimeConsts, "INFINITE_PAST",
|
229
|
+
Data_Wrap_Struct(rb_cTimeVal, GC_NOT_MARKED, GC_DONT_FREE,
|
230
|
+
(void *)&gpr_inf_past));
|
231
|
+
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
|
+
|
241
|
+
void grpc_rb_shutdown(void *vm) { grpc_shutdown(); }
|
242
|
+
|
243
|
+
/* Initialize the GRPC module structs */
|
244
|
+
|
245
|
+
/* rb_sNewServerRpc is the struct that holds new server rpc details. */
|
246
|
+
VALUE rb_sNewServerRpc = Qnil;
|
247
|
+
/* rb_sStatus is the struct that holds status details. */
|
248
|
+
VALUE rb_sStatus = Qnil;
|
249
|
+
|
250
|
+
/* Initialize the GRPC module. */
|
251
|
+
VALUE rb_mGRPC = Qnil;
|
252
|
+
VALUE rb_mGrpcCore = Qnil;
|
253
|
+
|
254
|
+
void Init_grpc() {
|
255
|
+
grpc_init();
|
256
|
+
ruby_vm_at_exit(grpc_rb_shutdown);
|
257
|
+
rb_mGRPC = rb_define_module("GRPC");
|
258
|
+
rb_mGrpcCore = rb_define_module_under(rb_mGRPC, "Core");
|
259
|
+
rb_sNewServerRpc = rb_struct_define("NewServerRpc", "method", "host",
|
260
|
+
"deadline", "metadata", NULL);
|
261
|
+
rb_sStatus = rb_struct_define("Status", "code", "details", "metadata", NULL);
|
262
|
+
|
263
|
+
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();
|
274
|
+
}
|
data/ext/grpc/rb_grpc.h
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
/*
|
2
|
+
*
|
3
|
+
* Copyright 2015, 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
|
+
#ifndef GRPC_RB_H_
|
35
|
+
#define GRPC_RB_H_
|
36
|
+
|
37
|
+
#include <sys/time.h>
|
38
|
+
#include <ruby.h>
|
39
|
+
#include <grpc/support/time.h>
|
40
|
+
|
41
|
+
/* rb_mGrpcCore is the module containing the ruby wrapper GRPC classes. */
|
42
|
+
extern VALUE rb_mGrpcCore;
|
43
|
+
|
44
|
+
/* Class used to wrap timeval structs. */
|
45
|
+
extern VALUE rb_cTimeVal;
|
46
|
+
|
47
|
+
/* rb_sNewServerRpc is the struct that holds new server rpc details. */
|
48
|
+
extern VALUE rb_sNewServerRpc;
|
49
|
+
|
50
|
+
/* rb_sStruct is the struct that holds status details. */
|
51
|
+
extern VALUE rb_sStatus;
|
52
|
+
|
53
|
+
/* GC_NOT_MARKED is used in calls to Data_Wrap_Struct to indicate that the
|
54
|
+
wrapped struct does not need to participate in ruby gc. */
|
55
|
+
extern const RUBY_DATA_FUNC GC_NOT_MARKED;
|
56
|
+
|
57
|
+
/* GC_DONT_FREED is used in calls to Data_Wrap_Struct to indicate that the
|
58
|
+
wrapped struct should not be freed the wrapped ruby object is released by
|
59
|
+
the garbage collector. */
|
60
|
+
extern const RUBY_DATA_FUNC GC_DONT_FREE;
|
61
|
+
|
62
|
+
/* A ruby object alloc func that fails by raising an exception. */
|
63
|
+
VALUE grpc_rb_cannot_alloc(VALUE cls);
|
64
|
+
|
65
|
+
/* A ruby object init func that fails by raising an exception. */
|
66
|
+
VALUE grpc_rb_cannot_init(VALUE self);
|
67
|
+
|
68
|
+
/* A ruby object clone init func that fails by raising an exception. */
|
69
|
+
VALUE grpc_rb_cannot_init_copy(VALUE copy, VALUE self);
|
70
|
+
|
71
|
+
/* grpc_rb_time_timeval creates a gpr_timespec from a ruby time object. */
|
72
|
+
gpr_timespec grpc_rb_time_timeval(VALUE time, int interval);
|
73
|
+
|
74
|
+
#endif /* GRPC_RB_H_ */
|
@@ -0,0 +1,215 @@
|
|
1
|
+
/*
|
2
|
+
*
|
3
|
+
* Copyright 2015, 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_metadata.h"
|
35
|
+
|
36
|
+
#include <ruby.h>
|
37
|
+
#include <string.h>
|
38
|
+
|
39
|
+
#include <grpc/grpc.h>
|
40
|
+
#include "rb_grpc.h"
|
41
|
+
|
42
|
+
/* grpc_rb_metadata wraps a grpc_metadata. It provides a peer ruby object,
|
43
|
+
* 'mark' to minimize copying when a metadata is created from ruby. */
|
44
|
+
typedef struct grpc_rb_metadata {
|
45
|
+
/* Holder of ruby objects involved in constructing the metadata */
|
46
|
+
VALUE mark;
|
47
|
+
/* The actual metadata */
|
48
|
+
grpc_metadata *wrapped;
|
49
|
+
} grpc_rb_metadata;
|
50
|
+
|
51
|
+
/* Destroys Metadata instances. */
|
52
|
+
static void grpc_rb_metadata_free(void *p) {
|
53
|
+
if (p == NULL) {
|
54
|
+
return;
|
55
|
+
};
|
56
|
+
|
57
|
+
/* Because metadata is only created during a call to grpc_call_add_metadata,
|
58
|
+
* and the call takes ownership of the metadata, this does not free the
|
59
|
+
* wrapped struct, only the wrapper */
|
60
|
+
xfree(p);
|
61
|
+
}
|
62
|
+
|
63
|
+
/* Protects the mark object from GC */
|
64
|
+
static void grpc_rb_metadata_mark(void *p) {
|
65
|
+
grpc_rb_metadata *md = NULL;
|
66
|
+
if (p == NULL) {
|
67
|
+
return;
|
68
|
+
}
|
69
|
+
|
70
|
+
md = (grpc_rb_metadata *)p;
|
71
|
+
/* If it's not already cleaned up, mark the mark object */
|
72
|
+
if (md->mark != Qnil && BUILTIN_TYPE(md->mark) != T_NONE) {
|
73
|
+
rb_gc_mark(md->mark);
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
/* Allocates Metadata instances.
|
78
|
+
|
79
|
+
Provides safe default values for the Metadata fields. */
|
80
|
+
static VALUE grpc_rb_metadata_alloc(VALUE cls) {
|
81
|
+
grpc_rb_metadata *wrapper = ALLOC(grpc_rb_metadata);
|
82
|
+
wrapper->wrapped = NULL;
|
83
|
+
wrapper->mark = Qnil;
|
84
|
+
return Data_Wrap_Struct(cls, grpc_rb_metadata_mark, grpc_rb_metadata_free,
|
85
|
+
wrapper);
|
86
|
+
}
|
87
|
+
|
88
|
+
/* id_key and id_value are the names of the hidden ivars that preserve the
|
89
|
+
* original byte_buffer source string */
|
90
|
+
static ID id_key;
|
91
|
+
static ID id_value;
|
92
|
+
|
93
|
+
/* Initializes Metadata instances. */
|
94
|
+
static VALUE grpc_rb_metadata_init(VALUE self, VALUE key, VALUE value) {
|
95
|
+
grpc_rb_metadata *wrapper = NULL;
|
96
|
+
grpc_metadata *md = ALLOC(grpc_metadata);
|
97
|
+
|
98
|
+
/* Use direct pointers to the strings wrapped by the ruby object to avoid
|
99
|
+
* copying */
|
100
|
+
Data_Get_Struct(self, grpc_rb_metadata, wrapper);
|
101
|
+
wrapper->wrapped = md;
|
102
|
+
if (TYPE(key) == T_SYMBOL) {
|
103
|
+
md->key = (char *)rb_id2name(SYM2ID(key));
|
104
|
+
} else { /* StringValueCStr does all other type exclusions for us */
|
105
|
+
md->key = StringValueCStr(key);
|
106
|
+
}
|
107
|
+
md->value = RSTRING_PTR(value);
|
108
|
+
md->value_length = RSTRING_LEN(value);
|
109
|
+
|
110
|
+
/* Save references to the original values on the mark object so that the
|
111
|
+
* pointers used there are valid for the lifetime of the object. */
|
112
|
+
wrapper->mark = rb_class_new_instance(0, NULL, rb_cObject);
|
113
|
+
rb_ivar_set(wrapper->mark, id_key, key);
|
114
|
+
rb_ivar_set(wrapper->mark, id_value, value);
|
115
|
+
|
116
|
+
return self;
|
117
|
+
}
|
118
|
+
|
119
|
+
/* Clones Metadata instances.
|
120
|
+
|
121
|
+
Gives Metadata a consistent implementation of Ruby's object copy/dup
|
122
|
+
protocol. */
|
123
|
+
static VALUE grpc_rb_metadata_init_copy(VALUE copy, VALUE orig) {
|
124
|
+
grpc_rb_metadata *orig_md = NULL;
|
125
|
+
grpc_rb_metadata *copy_md = NULL;
|
126
|
+
|
127
|
+
if (copy == orig) {
|
128
|
+
return copy;
|
129
|
+
}
|
130
|
+
|
131
|
+
/* Raise an error if orig is not a metadata object or a subclass. */
|
132
|
+
if (TYPE(orig) != T_DATA ||
|
133
|
+
RDATA(orig)->dfree != (RUBY_DATA_FUNC)grpc_rb_metadata_free) {
|
134
|
+
rb_raise(rb_eTypeError, "not a %s", rb_obj_classname(rb_cMetadata));
|
135
|
+
}
|
136
|
+
|
137
|
+
Data_Get_Struct(orig, grpc_rb_metadata, orig_md);
|
138
|
+
Data_Get_Struct(copy, grpc_rb_metadata, copy_md);
|
139
|
+
|
140
|
+
/* use ruby's MEMCPY to make a byte-for-byte copy of the metadata wrapper
|
141
|
+
* object. */
|
142
|
+
MEMCPY(copy_md, orig_md, grpc_rb_metadata, 1);
|
143
|
+
return copy;
|
144
|
+
}
|
145
|
+
|
146
|
+
/* Gets the key from a metadata instance. */
|
147
|
+
static VALUE grpc_rb_metadata_key(VALUE self) {
|
148
|
+
VALUE key = Qnil;
|
149
|
+
grpc_rb_metadata *wrapper = NULL;
|
150
|
+
grpc_metadata *md = NULL;
|
151
|
+
|
152
|
+
Data_Get_Struct(self, grpc_rb_metadata, wrapper);
|
153
|
+
if (wrapper->mark != Qnil) {
|
154
|
+
key = rb_ivar_get(wrapper->mark, id_key);
|
155
|
+
if (key != Qnil) {
|
156
|
+
return key;
|
157
|
+
}
|
158
|
+
}
|
159
|
+
|
160
|
+
md = wrapper->wrapped;
|
161
|
+
if (md == NULL || md->key == NULL) {
|
162
|
+
return Qnil;
|
163
|
+
}
|
164
|
+
return rb_str_new2(md->key);
|
165
|
+
}
|
166
|
+
|
167
|
+
/* Gets the value from a metadata instance. */
|
168
|
+
static VALUE grpc_rb_metadata_value(VALUE self) {
|
169
|
+
VALUE val = Qnil;
|
170
|
+
grpc_rb_metadata *wrapper = NULL;
|
171
|
+
grpc_metadata *md = NULL;
|
172
|
+
|
173
|
+
Data_Get_Struct(self, grpc_rb_metadata, wrapper);
|
174
|
+
if (wrapper->mark != Qnil) {
|
175
|
+
val = rb_ivar_get(wrapper->mark, id_value);
|
176
|
+
if (val != Qnil) {
|
177
|
+
return val;
|
178
|
+
}
|
179
|
+
}
|
180
|
+
|
181
|
+
md = wrapper->wrapped;
|
182
|
+
if (md == NULL || md->value == NULL) {
|
183
|
+
return Qnil;
|
184
|
+
}
|
185
|
+
return rb_str_new2(md->value);
|
186
|
+
}
|
187
|
+
|
188
|
+
/* rb_cMetadata is the Metadata class whose instances proxy grpc_metadata. */
|
189
|
+
VALUE rb_cMetadata = Qnil;
|
190
|
+
void Init_grpc_metadata() {
|
191
|
+
rb_cMetadata =
|
192
|
+
rb_define_class_under(rb_mGrpcCore, "Metadata", rb_cObject);
|
193
|
+
|
194
|
+
/* Allocates an object managed by the ruby runtime */
|
195
|
+
rb_define_alloc_func(rb_cMetadata, grpc_rb_metadata_alloc);
|
196
|
+
|
197
|
+
/* Provides a ruby constructor and support for dup/clone. */
|
198
|
+
rb_define_method(rb_cMetadata, "initialize", grpc_rb_metadata_init, 2);
|
199
|
+
rb_define_method(rb_cMetadata, "initialize_copy", grpc_rb_metadata_init_copy,
|
200
|
+
1);
|
201
|
+
|
202
|
+
/* Provides accessors for the code and details. */
|
203
|
+
rb_define_method(rb_cMetadata, "key", grpc_rb_metadata_key, 0);
|
204
|
+
rb_define_method(rb_cMetadata, "value", grpc_rb_metadata_value, 0);
|
205
|
+
|
206
|
+
id_key = rb_intern("__key");
|
207
|
+
id_value = rb_intern("__value");
|
208
|
+
}
|
209
|
+
|
210
|
+
/* Gets the wrapped metadata from the ruby wrapper */
|
211
|
+
grpc_metadata *grpc_rb_get_wrapped_metadata(VALUE v) {
|
212
|
+
grpc_rb_metadata *wrapper = NULL;
|
213
|
+
Data_Get_Struct(v, grpc_rb_metadata, wrapper);
|
214
|
+
return wrapper->wrapped;
|
215
|
+
}
|