blob: 5bc58b9b324ddb1bf3026129c95e44d0450c75f7 [file] [log] [blame]
murgatroid99749666e2015-01-12 18:25:58 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
murgatroid99749666e2015-01-12 18:25:58 -08004 * 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
murgatroid99e5061512015-01-12 18:14:35 -080034#include <vector>
35
murgatroid99c7f4d4f2015-07-28 15:18:57 -070036#include "grpc/support/log.h"
37
murgatroid99e5061512015-01-12 18:14:35 -080038#include <node.h>
39#include <nan.h>
40#include "grpc/grpc.h"
41#include "grpc/grpc_security.h"
murgatroid99c7f4d4f2015-07-28 15:18:57 -070042#include "call.h"
murgatroid99e5061512015-01-12 18:14:35 -080043#include "channel.h"
murgatroid999030c812016-09-16 13:25:08 -070044#include "completion_queue.h"
murgatroid99c7f4d4f2015-07-28 15:18:57 -070045#include "completion_queue_async_worker.h"
murgatroid995f709ca2015-09-30 14:22:54 -070046#include "channel_credentials.h"
murgatroid99c7f4d4f2015-07-28 15:18:57 -070047#include "timeval.h"
murgatroid99e5061512015-01-12 18:14:35 -080048
49namespace grpc {
50namespace node {
51
murgatroid992b097832015-09-17 13:56:25 -070052using Nan::Callback;
53using Nan::EscapableHandleScope;
54using Nan::HandleScope;
55using Nan::Maybe;
56using Nan::MaybeLocal;
57using Nan::ObjectWrap;
58using Nan::Persistent;
59using Nan::Utf8String;
60
murgatroid99e5061512015-01-12 18:14:35 -080061using v8::Array;
62using v8::Exception;
63using v8::Function;
64using v8::FunctionTemplate;
murgatroid99e5061512015-01-12 18:14:35 -080065using v8::Integer;
66using v8::Local;
murgatroid99c7f4d4f2015-07-28 15:18:57 -070067using v8::Number;
murgatroid99e5061512015-01-12 18:14:35 -080068using v8::Object;
murgatroid99e5061512015-01-12 18:14:35 -080069using v8::String;
70using v8::Value;
71
murgatroid992b097832015-09-17 13:56:25 -070072Callback *Channel::constructor;
murgatroid99e5061512015-01-12 18:14:35 -080073Persistent<FunctionTemplate> Channel::fun_tpl;
74
murgatroid9975a2bba2015-10-12 16:12:04 -070075bool ParseChannelArgs(Local<Value> args_val,
76 grpc_channel_args **channel_args_ptr) {
77 if (args_val->IsUndefined() || args_val->IsNull()) {
78 *channel_args_ptr = NULL;
79 return true;
80 }
81 if (!args_val->IsObject()) {
82 *channel_args_ptr = NULL;
83 return false;
84 }
85 grpc_channel_args *channel_args = reinterpret_cast<grpc_channel_args*>(
murgatroid9932366072015-11-09 11:04:07 -080086 malloc(sizeof(grpc_channel_args)));
murgatroid9975a2bba2015-10-12 16:12:04 -070087 *channel_args_ptr = channel_args;
88 Local<Object> args_hash = Nan::To<Object>(args_val).ToLocalChecked();
89 Local<Array> keys = Nan::GetOwnPropertyNames(args_hash).ToLocalChecked();
90 channel_args->num_args = keys->Length();
91 channel_args->args = reinterpret_cast<grpc_arg *>(
92 calloc(channel_args->num_args, sizeof(grpc_arg)));
93 for (unsigned int i = 0; i < channel_args->num_args; i++) {
94 Local<Value> key = Nan::Get(keys, i).ToLocalChecked();
95 Utf8String key_str(key);
96 if (*key_str == NULL) {
97 // Key string onversion failed
98 return false;
99 }
100 Local<Value> value = Nan::Get(args_hash, key).ToLocalChecked();
101 if (value->IsInt32()) {
102 channel_args->args[i].type = GRPC_ARG_INTEGER;
103 channel_args->args[i].value.integer = Nan::To<int32_t>(value).FromJust();
104 } else if (value->IsString()) {
105 Utf8String val_str(value);
106 channel_args->args[i].type = GRPC_ARG_STRING;
107 channel_args->args[i].value.string = reinterpret_cast<char*>(
108 calloc(val_str.length() + 1,sizeof(char)));
109 memcpy(channel_args->args[i].value.string,
110 *val_str, val_str.length() + 1);
111 } else {
112 // The value does not match either of the accepted types
113 return false;
114 }
115 channel_args->args[i].key = reinterpret_cast<char*>(
116 calloc(key_str.length() + 1, sizeof(char)));
117 memcpy(channel_args->args[i].key, *key_str, key_str.length() + 1);
118 }
119 return true;
120}
121
122void DeallocateChannelArgs(grpc_channel_args *channel_args) {
123 if (channel_args == NULL) {
124 return;
125 }
126 for (size_t i = 0; i < channel_args->num_args; i++) {
127 if (channel_args->args[i].key == NULL) {
128 /* NULL key implies that this argument and all subsequent arguments failed
129 * to parse */
130 break;
131 }
132 free(channel_args->args[i].key);
133 if (channel_args->args[i].type == GRPC_ARG_STRING) {
134 free(channel_args->args[i].value.string);
135 }
136 }
137 free(channel_args->args);
138 free(channel_args);
139}
140
murgatroid993f507d02015-08-03 15:17:53 -0700141Channel::Channel(grpc_channel *channel) : wrapped_channel(channel) {}
murgatroid99e5061512015-01-12 18:14:35 -0800142
143Channel::~Channel() {
murgatroid999030c812016-09-16 13:25:08 -0700144 gpr_log(GPR_DEBUG, "Destroying channel");
murgatroid99e5061512015-01-12 18:14:35 -0800145 if (wrapped_channel != NULL) {
146 grpc_channel_destroy(wrapped_channel);
147 }
murgatroid99e5061512015-01-12 18:14:35 -0800148}
149
murgatroid992b097832015-09-17 13:56:25 -0700150void Channel::Init(Local<Object> exports) {
151 Nan::HandleScope scope;
152 Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
153 tpl->SetClassName(Nan::New("Channel").ToLocalChecked());
murgatroid99e5061512015-01-12 18:14:35 -0800154 tpl->InstanceTemplate()->SetInternalFieldCount(1);
murgatroid992b097832015-09-17 13:56:25 -0700155 Nan::SetPrototypeMethod(tpl, "close", Close);
156 Nan::SetPrototypeMethod(tpl, "getTarget", GetTarget);
157 Nan::SetPrototypeMethod(tpl, "getConnectivityState", GetConnectivityState);
158 Nan::SetPrototypeMethod(tpl, "watchConnectivityState",
159 WatchConnectivityState);
160 fun_tpl.Reset(tpl);
161 Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked();
162 Nan::Set(exports, Nan::New("Channel").ToLocalChecked(), ctr);
163 constructor = new Callback(ctr);
murgatroid99e5061512015-01-12 18:14:35 -0800164}
165
murgatroid992b097832015-09-17 13:56:25 -0700166bool Channel::HasInstance(Local<Value> val) {
167 HandleScope scope;
168 return Nan::New(fun_tpl)->HasInstance(val);
murgatroid99e5061512015-01-12 18:14:35 -0800169}
170
Craig Tillere8e304e2015-01-13 14:41:29 -0800171grpc_channel *Channel::GetWrappedChannel() { return this->wrapped_channel; }
murgatroid99e5061512015-01-12 18:14:35 -0800172
murgatroid99e5061512015-01-12 18:14:35 -0800173NAN_METHOD(Channel::New) {
murgatroid992b097832015-09-17 13:56:25 -0700174 if (info.IsConstructCall()) {
175 if (!info[0]->IsString()) {
176 return Nan::ThrowTypeError(
murgatroid99893690f2015-07-27 14:56:40 -0700177 "Channel expects a string, a credential and an object");
murgatroid99e5061512015-01-12 18:14:35 -0800178 }
179 grpc_channel *wrapped_channel;
180 // Owned by the Channel object
murgatroid992b097832015-09-17 13:56:25 -0700181 Utf8String host(info[0]);
murgatroid997c0dbf82015-10-20 16:10:20 -0700182 grpc_channel_credentials *creds;
murgatroid995f709ca2015-09-30 14:22:54 -0700183 if (!ChannelCredentials::HasInstance(info[1])) {
murgatroid992b097832015-09-17 13:56:25 -0700184 return Nan::ThrowTypeError(
murgatroid995f709ca2015-09-30 14:22:54 -0700185 "Channel's second argument must be a ChannelCredentials");
murgatroid99893690f2015-07-27 14:56:40 -0700186 }
murgatroid995f709ca2015-09-30 14:22:54 -0700187 ChannelCredentials *creds_object = ObjectWrap::Unwrap<ChannelCredentials>(
murgatroid992b097832015-09-17 13:56:25 -0700188 Nan::To<Object>(info[1]).ToLocalChecked());
murgatroid99893690f2015-07-27 14:56:40 -0700189 creds = creds_object->GetWrappedCredentials();
murgatroid9975a2bba2015-10-12 16:12:04 -0700190 grpc_channel_args *channel_args_ptr = NULL;
191 if (!ParseChannelArgs(info[2], &channel_args_ptr)) {
192 DeallocateChannelArgs(channel_args_ptr);
193 return Nan::ThrowTypeError("Channel options must be an object with "
194 "string keys and integer or string values");
murgatroid99e5061512015-01-12 18:14:35 -0800195 }
murgatroid99893690f2015-07-27 14:56:40 -0700196 if (creds == NULL) {
Nicolas "Pixel" Noble720903a2015-08-12 20:07:54 +0200197 wrapped_channel = grpc_insecure_channel_create(*host, channel_args_ptr,
198 NULL);
murgatroid99893690f2015-07-27 14:56:40 -0700199 } else {
200 wrapped_channel =
Julien Boeufc5b570f2015-08-25 17:47:55 -0700201 grpc_secure_channel_create(creds, *host, channel_args_ptr, NULL);
murgatroid99893690f2015-07-27 14:56:40 -0700202 }
murgatroid9975a2bba2015-10-12 16:12:04 -0700203 DeallocateChannelArgs(channel_args_ptr);
murgatroid993f507d02015-08-03 15:17:53 -0700204 Channel *channel = new Channel(wrapped_channel);
murgatroid992b097832015-09-17 13:56:25 -0700205 channel->Wrap(info.This());
206 info.GetReturnValue().Set(info.This());
207 return;
murgatroid99e5061512015-01-12 18:14:35 -0800208 } else {
murgatroid99893690f2015-07-27 14:56:40 -0700209 const int argc = 3;
murgatroid992b097832015-09-17 13:56:25 -0700210 Local<Value> argv[argc] = {info[0], info[1], info[2]};
murgatroid99c0a64cd2016-08-15 13:14:16 -0700211 MaybeLocal<Object> maybe_instance = Nan::NewInstance(
212 constructor->GetFunction(), argc, argv);
murgatroid992b097832015-09-17 13:56:25 -0700213 if (maybe_instance.IsEmpty()) {
214 // There's probably a pending exception
215 return;
216 } else {
217 info.GetReturnValue().Set(maybe_instance.ToLocalChecked());
218 }
murgatroid99e5061512015-01-12 18:14:35 -0800219 }
220}
221
222NAN_METHOD(Channel::Close) {
murgatroid992b097832015-09-17 13:56:25 -0700223 if (!HasInstance(info.This())) {
224 return Nan::ThrowTypeError("close can only be called on Channel objects");
murgatroid99e5061512015-01-12 18:14:35 -0800225 }
murgatroid992b097832015-09-17 13:56:25 -0700226 Channel *channel = ObjectWrap::Unwrap<Channel>(info.This());
murgatroid99e5061512015-01-12 18:14:35 -0800227 if (channel->wrapped_channel != NULL) {
228 grpc_channel_destroy(channel->wrapped_channel);
229 channel->wrapped_channel = NULL;
230 }
murgatroid99e5061512015-01-12 18:14:35 -0800231}
232
murgatroid99ea12b972015-07-24 10:43:27 -0700233NAN_METHOD(Channel::GetTarget) {
murgatroid992b097832015-09-17 13:56:25 -0700234 if (!HasInstance(info.This())) {
235 return Nan::ThrowTypeError("getTarget can only be called on Channel objects");
murgatroid99ea12b972015-07-24 10:43:27 -0700236 }
murgatroid992b097832015-09-17 13:56:25 -0700237 Channel *channel = ObjectWrap::Unwrap<Channel>(info.This());
238 info.GetReturnValue().Set(Nan::New(
239 grpc_channel_get_target(channel->wrapped_channel)).ToLocalChecked());
murgatroid99ea12b972015-07-24 10:43:27 -0700240}
241
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700242NAN_METHOD(Channel::GetConnectivityState) {
murgatroid992b097832015-09-17 13:56:25 -0700243 if (!HasInstance(info.This())) {
244 return Nan::ThrowTypeError(
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700245 "getConnectivityState can only be called on Channel objects");
246 }
murgatroid992b097832015-09-17 13:56:25 -0700247 Channel *channel = ObjectWrap::Unwrap<Channel>(info.This());
248 int try_to_connect = (int)info[0]->Equals(Nan::True());
249 info.GetReturnValue().Set(
250 grpc_channel_check_connectivity_state(channel->wrapped_channel,
251 try_to_connect));
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700252}
253
254NAN_METHOD(Channel::WatchConnectivityState) {
murgatroid992b097832015-09-17 13:56:25 -0700255 if (!HasInstance(info.This())) {
256 return Nan::ThrowTypeError(
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700257 "watchConnectivityState can only be called on Channel objects");
258 }
murgatroid992b097832015-09-17 13:56:25 -0700259 if (!info[0]->IsUint32()) {
260 return Nan::ThrowTypeError(
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700261 "watchConnectivityState's first argument must be a channel state");
262 }
murgatroid992b097832015-09-17 13:56:25 -0700263 if (!(info[1]->IsNumber() || info[1]->IsDate())) {
264 return Nan::ThrowTypeError(
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700265 "watchConnectivityState's second argument must be a date or a number");
266 }
murgatroid992b097832015-09-17 13:56:25 -0700267 if (!info[2]->IsFunction()) {
268 return Nan::ThrowTypeError(
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700269 "watchConnectivityState's third argument must be a callback");
270 }
271 grpc_connectivity_state last_state =
murgatroid992b097832015-09-17 13:56:25 -0700272 static_cast<grpc_connectivity_state>(
273 Nan::To<uint32_t>(info[0]).FromJust());
274 double deadline = Nan::To<double>(info[1]).FromJust();
275 Local<Function> callback_func = info[2].As<Function>();
276 Nan::Callback *callback = new Callback(callback_func);
277 Channel *channel = ObjectWrap::Unwrap<Channel>(info.This());
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700278 unique_ptr<OpVec> ops(new OpVec());
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700279 grpc_channel_watch_connectivity_state(
murgatroid9937135462015-08-13 11:24:34 -0700280 channel->wrapped_channel, last_state, MillisecondsToTimespec(deadline),
murgatroid999030c812016-09-16 13:25:08 -0700281 GetCompletionQueue(),
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700282 new struct tag(callback,
283 ops.release(),
murgatroid999030c812016-09-16 13:25:08 -0700284 shared_ptr<Resources>(nullptr), NULL));
285 CompletionQueueNext();
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700286}
287
murgatroid99e5061512015-01-12 18:14:35 -0800288} // namespace node
Craig Tiller190d3602015-02-18 09:23:38 -0800289} // namespace grpc