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 | |
| 36 | #include <node.h> |
| 37 | #include <nan.h> |
| 38 | #include "grpc/grpc.h" |
| 39 | #include "grpc/grpc_security.h" |
| 40 | #include "channel.h" |
| 41 | #include "credentials.h" |
| 42 | |
| 43 | namespace grpc { |
| 44 | namespace node { |
| 45 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 46 | using v8::Array; |
| 47 | using v8::Exception; |
| 48 | using v8::Function; |
| 49 | using v8::FunctionTemplate; |
| 50 | using v8::Handle; |
| 51 | using v8::HandleScope; |
| 52 | using v8::Integer; |
| 53 | using v8::Local; |
| 54 | using v8::Object; |
| 55 | using v8::Persistent; |
| 56 | using v8::String; |
| 57 | using v8::Value; |
| 58 | |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 59 | NanCallback *Channel::constructor; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 60 | Persistent<FunctionTemplate> Channel::fun_tpl; |
| 61 | |
| 62 | Channel::Channel(grpc_channel *channel, NanUtf8String *host) |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 63 | : wrapped_channel(channel), host(host) {} |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 64 | |
| 65 | Channel::~Channel() { |
| 66 | if (wrapped_channel != NULL) { |
| 67 | grpc_channel_destroy(wrapped_channel); |
| 68 | } |
| 69 | delete host; |
| 70 | } |
| 71 | |
| 72 | void Channel::Init(Handle<Object> exports) { |
| 73 | NanScope(); |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 74 | Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 75 | tpl->SetClassName(NanNew("Channel")); |
| 76 | tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 77 | NanSetPrototypeTemplate(tpl, "close", |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 78 | NanNew<FunctionTemplate>(Close)->GetFunction()); |
murgatroid99 | ea12b97 | 2015-07-24 10:43:27 -0700 | [diff] [blame] | 79 | NanSetPrototypeTemplate(tpl, "getTarget", |
| 80 | NanNew<FunctionTemplate>(GetTarget)->GetFunction()); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 81 | NanAssignPersistent(fun_tpl, tpl); |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 82 | Handle<Function> ctr = tpl->GetFunction(); |
| 83 | constructor = new NanCallback(ctr); |
| 84 | exports->Set(NanNew("Channel"), ctr); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | bool Channel::HasInstance(Handle<Value> val) { |
| 88 | NanScope(); |
| 89 | return NanHasInstance(fun_tpl, val); |
| 90 | } |
| 91 | |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 92 | grpc_channel *Channel::GetWrappedChannel() { return this->wrapped_channel; } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 93 | |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 94 | char *Channel::GetHost() { return **this->host; } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 95 | |
| 96 | NAN_METHOD(Channel::New) { |
| 97 | NanScope(); |
| 98 | |
| 99 | if (args.IsConstructCall()) { |
| 100 | if (!args[0]->IsString()) { |
| 101 | return NanThrowTypeError("Channel expects a string and an object"); |
| 102 | } |
| 103 | grpc_channel *wrapped_channel; |
| 104 | // Owned by the Channel object |
| 105 | NanUtf8String *host = new NanUtf8String(args[0]); |
murgatroid99 | 3061606 | 2015-02-25 10:38:34 -0800 | [diff] [blame] | 106 | NanUtf8String *host_override = NULL; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 107 | if (args[1]->IsUndefined()) { |
| 108 | wrapped_channel = grpc_channel_create(**host, NULL); |
| 109 | } else if (args[1]->IsObject()) { |
| 110 | grpc_credentials *creds = NULL; |
| 111 | Handle<Object> args_hash(args[1]->ToObject()->Clone()); |
murgatroid99 | 3061606 | 2015-02-25 10:38:34 -0800 | [diff] [blame] | 112 | if (args_hash->HasOwnProperty(NanNew(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG))) { |
| 113 | host_override = new NanUtf8String(args_hash->Get(NanNew(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG))); |
| 114 | } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 115 | if (args_hash->HasOwnProperty(NanNew("credentials"))) { |
| 116 | Handle<Value> creds_value = args_hash->Get(NanNew("credentials")); |
| 117 | if (!Credentials::HasInstance(creds_value)) { |
| 118 | return NanThrowTypeError( |
| 119 | "credentials arg must be a Credentials object"); |
| 120 | } |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 121 | Credentials *creds_object = |
| 122 | ObjectWrap::Unwrap<Credentials>(creds_value->ToObject()); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 123 | creds = creds_object->GetWrappedCredentials(); |
| 124 | args_hash->Delete(NanNew("credentials")); |
| 125 | } |
| 126 | Handle<Array> keys(args_hash->GetOwnPropertyNames()); |
| 127 | grpc_channel_args channel_args; |
| 128 | channel_args.num_args = keys->Length(); |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 129 | channel_args.args = reinterpret_cast<grpc_arg *>( |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 130 | calloc(channel_args.num_args, sizeof(grpc_arg))); |
| 131 | /* These are used to keep all strings until then end of the block, then |
| 132 | destroy them */ |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 133 | std::vector<NanUtf8String *> key_strings(keys->Length()); |
| 134 | std::vector<NanUtf8String *> value_strings(keys->Length()); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 135 | for (unsigned int i = 0; i < channel_args.num_args; i++) { |
| 136 | Handle<String> current_key(keys->Get(i)->ToString()); |
| 137 | Handle<Value> current_value(args_hash->Get(current_key)); |
| 138 | key_strings[i] = new NanUtf8String(current_key); |
| 139 | channel_args.args[i].key = **key_strings[i]; |
| 140 | if (current_value->IsInt32()) { |
| 141 | channel_args.args[i].type = GRPC_ARG_INTEGER; |
| 142 | channel_args.args[i].value.integer = current_value->Int32Value(); |
| 143 | } else if (current_value->IsString()) { |
| 144 | channel_args.args[i].type = GRPC_ARG_STRING; |
| 145 | value_strings[i] = new NanUtf8String(current_value); |
| 146 | channel_args.args[i].value.string = **value_strings[i]; |
| 147 | } else { |
| 148 | free(channel_args.args); |
| 149 | return NanThrowTypeError("Arg values must be strings"); |
| 150 | } |
| 151 | } |
| 152 | if (creds == NULL) { |
| 153 | wrapped_channel = grpc_channel_create(**host, &channel_args); |
| 154 | } else { |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 155 | wrapped_channel = |
| 156 | grpc_secure_channel_create(creds, **host, &channel_args); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 157 | } |
| 158 | free(channel_args.args); |
| 159 | } else { |
| 160 | return NanThrowTypeError("Channel expects a string and an object"); |
| 161 | } |
murgatroid99 | 3061606 | 2015-02-25 10:38:34 -0800 | [diff] [blame] | 162 | Channel *channel; |
| 163 | if (host_override == NULL) { |
| 164 | channel = new Channel(wrapped_channel, host); |
| 165 | } else { |
| 166 | channel = new Channel(wrapped_channel, host_override); |
| 167 | } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 168 | channel->Wrap(args.This()); |
| 169 | NanReturnValue(args.This()); |
| 170 | } else { |
| 171 | const int argc = 2; |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 172 | Local<Value> argv[argc] = {args[0], args[1]}; |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 173 | NanReturnValue(constructor->GetFunction()->NewInstance(argc, argv)); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | NAN_METHOD(Channel::Close) { |
| 178 | NanScope(); |
| 179 | if (!HasInstance(args.This())) { |
| 180 | return NanThrowTypeError("close can only be called on Channel objects"); |
| 181 | } |
| 182 | Channel *channel = ObjectWrap::Unwrap<Channel>(args.This()); |
| 183 | if (channel->wrapped_channel != NULL) { |
| 184 | grpc_channel_destroy(channel->wrapped_channel); |
| 185 | channel->wrapped_channel = NULL; |
| 186 | } |
| 187 | NanReturnUndefined(); |
| 188 | } |
| 189 | |
murgatroid99 | ea12b97 | 2015-07-24 10:43:27 -0700 | [diff] [blame] | 190 | NAN_METHOD(Channel::GetTarget) { |
| 191 | NanScope(); |
| 192 | if (!HasInstance(args.This())) { |
| 193 | return NanThrowTypeError("getTarget can only be called on Channel objects"); |
| 194 | } |
| 195 | Channel *channel = ObjectWrap::Unwrap<Channel>(args.This()); |
| 196 | NanReturnValue(NanNew(grpc_channel_get_target(channel->wrapped_channel))); |
| 197 | } |
| 198 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 199 | } // namespace node |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 200 | } // namespace grpc |