Line data Source code
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 <vector>
35 :
36 : #include "grpc/support/log.h"
37 :
38 : #include <node.h>
39 : #include <nan.h>
40 : #include "grpc/grpc.h"
41 : #include "grpc/grpc_security.h"
42 : #include "call.h"
43 : #include "channel.h"
44 : #include "completion_queue_async_worker.h"
45 : #include "channel_credentials.h"
46 : #include "timeval.h"
47 :
48 : namespace grpc {
49 : namespace node {
50 :
51 : using Nan::Callback;
52 : using Nan::EscapableHandleScope;
53 : using Nan::HandleScope;
54 : using Nan::Maybe;
55 : using Nan::MaybeLocal;
56 : using Nan::ObjectWrap;
57 : using Nan::Persistent;
58 : using Nan::Utf8String;
59 :
60 : using v8::Array;
61 : using v8::Exception;
62 : using v8::Function;
63 : using v8::FunctionTemplate;
64 : using v8::Integer;
65 : using v8::Local;
66 : using v8::Number;
67 : using v8::Object;
68 : using v8::String;
69 : using v8::Value;
70 :
71 : Callback *Channel::constructor;
72 1 : Persistent<FunctionTemplate> Channel::fun_tpl;
73 :
74 104 : bool ParseChannelArgs(Local<Value> args_val,
75 : grpc_channel_args **channel_args_ptr) {
76 269 : if (args_val->IsUndefined() || args_val->IsNull()) {
77 43 : *channel_args_ptr = NULL;
78 43 : return true;
79 : }
80 61 : if (!args_val->IsObject()) {
81 1 : *channel_args_ptr = NULL;
82 1 : return false;
83 : }
84 : grpc_channel_args *channel_args = reinterpret_cast<grpc_channel_args*>(
85 60 : malloc(sizeof(grpc_channel_args)));
86 60 : *channel_args_ptr = channel_args;
87 60 : Local<Object> args_hash = Nan::To<Object>(args_val).ToLocalChecked();
88 60 : Local<Array> keys = Nan::GetOwnPropertyNames(args_hash).ToLocalChecked();
89 60 : channel_args->num_args = keys->Length();
90 : channel_args->args = reinterpret_cast<grpc_arg *>(
91 60 : calloc(channel_args->num_args, sizeof(grpc_arg)));
92 284 : for (unsigned int i = 0; i < channel_args->num_args; i++) {
93 86 : Local<Value> key = Nan::Get(keys, i).ToLocalChecked();
94 : Utf8String key_str(key);
95 86 : if (*key_str == NULL) {
96 : // Key string onversion failed
97 : return false;
98 : }
99 86 : Local<Value> value = Nan::Get(args_hash, key).ToLocalChecked();
100 86 : if (value->IsInt32()) {
101 2 : channel_args->args[i].type = GRPC_ARG_INTEGER;
102 2 : channel_args->args[i].value.integer = Nan::To<int32_t>(value).FromJust();
103 168 : } else if (value->IsString()) {
104 : Utf8String val_str(value);
105 80 : channel_args->args[i].type = GRPC_ARG_STRING;
106 : channel_args->args[i].value.string = reinterpret_cast<char*>(
107 80 : calloc(val_str.length() + 1,sizeof(char)));
108 : memcpy(channel_args->args[i].value.string,
109 80 : *val_str, val_str.length() + 1);
110 : } else {
111 : // The value does not match either of the accepted types
112 : return false;
113 : }
114 82 : channel_args->args[i].key = reinterpret_cast<char*>(
115 82 : calloc(key_str.length() + 1, sizeof(char)));
116 82 : memcpy(channel_args->args[i].key, *key_str, key_str.length() + 1);
117 : }
118 : return true;
119 : }
120 :
121 104 : void DeallocateChannelArgs(grpc_channel_args *channel_args) {
122 104 : if (channel_args == NULL) {
123 104 : return;
124 : }
125 82 : for (size_t i = 0; i < channel_args->num_args; i++) {
126 86 : if (channel_args->args[i].key == NULL) {
127 : /* NULL key implies that this argument and all subsequent arguments failed
128 : * to parse */
129 : break;
130 : }
131 82 : free(channel_args->args[i].key);
132 82 : if (channel_args->args[i].type == GRPC_ARG_STRING) {
133 80 : free(channel_args->args[i].value.string);
134 : }
135 : }
136 60 : free(channel_args->args);
137 60 : free(channel_args);
138 : }
139 :
140 116 : Channel::Channel(grpc_channel *channel) : wrapped_channel(channel) {}
141 :
142 45 : Channel::~Channel() {
143 15 : if (wrapped_channel != NULL) {
144 11 : grpc_channel_destroy(wrapped_channel);
145 : }
146 30 : }
147 :
148 1 : void Channel::Init(Local<Object> exports) {
149 : Nan::HandleScope scope;
150 1 : Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
151 1 : tpl->SetClassName(Nan::New("Channel").ToLocalChecked());
152 2 : tpl->InstanceTemplate()->SetInternalFieldCount(1);
153 : Nan::SetPrototypeMethod(tpl, "close", Close);
154 : Nan::SetPrototypeMethod(tpl, "getTarget", GetTarget);
155 : Nan::SetPrototypeMethod(tpl, "getConnectivityState", GetConnectivityState);
156 : Nan::SetPrototypeMethod(tpl, "watchConnectivityState",
157 : WatchConnectivityState);
158 : fun_tpl.Reset(tpl);
159 1 : Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked();
160 1 : Nan::Set(exports, Nan::New("Channel").ToLocalChecked(), ctr);
161 1 : constructor = new Callback(ctr);
162 1 : }
163 :
164 159 : bool Channel::HasInstance(Local<Value> val) {
165 : HandleScope scope;
166 477 : return Nan::New(fun_tpl)->HasInstance(val);
167 : }
168 :
169 222 : grpc_channel *Channel::GetWrappedChannel() { return this->wrapped_channel; }
170 :
171 66 : NAN_METHOD(Channel::New) {
172 132 : if (info.IsConstructCall()) {
173 195 : if (!info[0]->IsString()) {
174 : return Nan::ThrowTypeError(
175 : "Channel expects a string, a credential and an object");
176 : }
177 : grpc_channel *wrapped_channel;
178 : // Owned by the Channel object
179 63 : Utf8String host(info[0]);
180 : grpc_channel_credentials *creds;
181 126 : if (!ChannelCredentials::HasInstance(info[1])) {
182 : return Nan::ThrowTypeError(
183 : "Channel's second argument must be a ChannelCredentials");
184 : }
185 : ChannelCredentials *creds_object = ObjectWrap::Unwrap<ChannelCredentials>(
186 122 : Nan::To<Object>(info[1]).ToLocalChecked());
187 61 : creds = creds_object->GetWrappedCredentials();
188 61 : grpc_channel_args *channel_args_ptr = NULL;
189 122 : if (!ParseChannelArgs(info[2], &channel_args_ptr)) {
190 3 : DeallocateChannelArgs(channel_args_ptr);
191 : return Nan::ThrowTypeError("Channel options must be an object with "
192 : "string keys and integer or string values");
193 : }
194 58 : if (creds == NULL) {
195 40 : wrapped_channel = grpc_insecure_channel_create(*host, channel_args_ptr,
196 80 : NULL);
197 : } else {
198 : wrapped_channel =
199 18 : grpc_secure_channel_create(creds, *host, channel_args_ptr, NULL);
200 : }
201 58 : DeallocateChannelArgs(channel_args_ptr);
202 58 : Channel *channel = new Channel(wrapped_channel);
203 116 : channel->Wrap(info.This());
204 174 : info.GetReturnValue().Set(info.This());
205 : return;
206 : } else {
207 1 : const int argc = 3;
208 4 : Local<Value> argv[argc] = {info[0], info[1], info[2]};
209 1 : MaybeLocal<Object> maybe_instance = constructor->GetFunction()->NewInstance(
210 2 : argc, argv);
211 1 : if (maybe_instance.IsEmpty()) {
212 : // There's probably a pending exception
213 0 : return;
214 : } else {
215 2 : info.GetReturnValue().Set(maybe_instance.ToLocalChecked());
216 : }
217 : }
218 : }
219 :
220 7 : NAN_METHOD(Channel::Close) {
221 14 : if (!HasInstance(info.This())) {
222 7 : return Nan::ThrowTypeError("close can only be called on Channel objects");
223 : }
224 14 : Channel *channel = ObjectWrap::Unwrap<Channel>(info.This());
225 7 : if (channel->wrapped_channel != NULL) {
226 6 : grpc_channel_destroy(channel->wrapped_channel);
227 6 : channel->wrapped_channel = NULL;
228 : }
229 : }
230 :
231 2 : NAN_METHOD(Channel::GetTarget) {
232 4 : if (!HasInstance(info.This())) {
233 2 : return Nan::ThrowTypeError("getTarget can only be called on Channel objects");
234 : }
235 4 : Channel *channel = ObjectWrap::Unwrap<Channel>(info.This());
236 : info.GetReturnValue().Set(Nan::New(
237 6 : grpc_channel_get_target(channel->wrapped_channel)).ToLocalChecked());
238 : }
239 :
240 20 : NAN_METHOD(Channel::GetConnectivityState) {
241 40 : if (!HasInstance(info.This())) {
242 : return Nan::ThrowTypeError(
243 20 : "getConnectivityState can only be called on Channel objects");
244 : }
245 40 : Channel *channel = ObjectWrap::Unwrap<Channel>(info.This());
246 60 : int try_to_connect = (int)info[0]->Equals(Nan::True());
247 : info.GetReturnValue().Set(
248 : grpc_channel_check_connectivity_state(channel->wrapped_channel,
249 40 : try_to_connect));
250 : }
251 :
252 12 : NAN_METHOD(Channel::WatchConnectivityState) {
253 24 : if (!HasInstance(info.This())) {
254 : return Nan::ThrowTypeError(
255 0 : "watchConnectivityState can only be called on Channel objects");
256 : }
257 24 : if (!info[0]->IsUint32()) {
258 : return Nan::ThrowTypeError(
259 : "watchConnectivityState's first argument must be a channel state");
260 : }
261 30 : if (!(info[1]->IsNumber() || info[1]->IsDate())) {
262 : return Nan::ThrowTypeError(
263 : "watchConnectivityState's second argument must be a date or a number");
264 : }
265 24 : if (!info[2]->IsFunction()) {
266 : return Nan::ThrowTypeError(
267 : "watchConnectivityState's third argument must be a callback");
268 : }
269 : grpc_connectivity_state last_state =
270 : static_cast<grpc_connectivity_state>(
271 24 : Nan::To<uint32_t>(info[0]).FromJust());
272 24 : double deadline = Nan::To<double>(info[1]).FromJust();
273 36 : Local<Function> callback_func = info[2].As<Function>();
274 12 : Nan::Callback *callback = new Callback(callback_func);
275 24 : Channel *channel = ObjectWrap::Unwrap<Channel>(info.This());
276 12 : unique_ptr<OpVec> ops(new OpVec());
277 : grpc_channel_watch_connectivity_state(
278 : channel->wrapped_channel, last_state, MillisecondsToTimespec(deadline),
279 : CompletionQueueAsyncWorker::GetQueue(),
280 : new struct tag(callback,
281 : ops.release(),
282 36 : shared_ptr<Resources>(nullptr)));
283 12 : CompletionQueueAsyncWorker::Next();
284 : }
285 :
286 : } // namespace node
287 3 : } // namespace grpc
|