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