Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(664)

Side by Side Diff: third_party/grpc/src/node/ext/channel.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 Persistent<FunctionTemplate> Channel::fun_tpl;
73
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*>(
85 malloc(sizeof(grpc_channel_args)));
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
140 Channel::Channel(grpc_channel *channel) : wrapped_channel(channel) {}
141
142 Channel::~Channel() {
143 if (wrapped_channel != NULL) {
144 grpc_channel_destroy(wrapped_channel);
145 }
146 }
147
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());
152 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 Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked();
160 Nan::Set(exports, Nan::New("Channel").ToLocalChecked(), ctr);
161 constructor = new Callback(ctr);
162 }
163
164 bool Channel::HasInstance(Local<Value> val) {
165 HandleScope scope;
166 return Nan::New(fun_tpl)->HasInstance(val);
167 }
168
169 grpc_channel *Channel::GetWrappedChannel() { return this->wrapped_channel; }
170
171 NAN_METHOD(Channel::New) {
172 if (info.IsConstructCall()) {
173 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 Utf8String host(info[0]);
180 grpc_channel_credentials *creds;
181 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 Nan::To<Object>(info[1]).ToLocalChecked());
187 creds = creds_object->GetWrappedCredentials();
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");
193 }
194 if (creds == NULL) {
195 wrapped_channel = grpc_insecure_channel_create(*host, channel_args_ptr,
196 NULL);
197 } else {
198 wrapped_channel =
199 grpc_secure_channel_create(creds, *host, channel_args_ptr, NULL);
200 }
201 DeallocateChannelArgs(channel_args_ptr);
202 Channel *channel = new Channel(wrapped_channel);
203 channel->Wrap(info.This());
204 info.GetReturnValue().Set(info.This());
205 return;
206 } else {
207 const int argc = 3;
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 }
217 }
218 }
219
220 NAN_METHOD(Channel::Close) {
221 if (!HasInstance(info.This())) {
222 return Nan::ThrowTypeError("close can only be called on Channel objects");
223 }
224 Channel *channel = ObjectWrap::Unwrap<Channel>(info.This());
225 if (channel->wrapped_channel != NULL) {
226 grpc_channel_destroy(channel->wrapped_channel);
227 channel->wrapped_channel = NULL;
228 }
229 }
230
231 NAN_METHOD(Channel::GetTarget) {
232 if (!HasInstance(info.This())) {
233 return Nan::ThrowTypeError("getTarget can only be called on Channel objects" );
234 }
235 Channel *channel = ObjectWrap::Unwrap<Channel>(info.This());
236 info.GetReturnValue().Set(Nan::New(
237 grpc_channel_get_target(channel->wrapped_channel)).ToLocalChecked());
238 }
239
240 NAN_METHOD(Channel::GetConnectivityState) {
241 if (!HasInstance(info.This())) {
242 return Nan::ThrowTypeError(
243 "getConnectivityState can only be called on Channel objects");
244 }
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));
250 }
251
252 NAN_METHOD(Channel::WatchConnectivityState) {
253 if (!HasInstance(info.This())) {
254 return Nan::ThrowTypeError(
255 "watchConnectivityState can only be called on Channel objects");
256 }
257 if (!info[0]->IsUint32()) {
258 return Nan::ThrowTypeError(
259 "watchConnectivityState's first argument must be a channel state");
260 }
261 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 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 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());
276 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 shared_ptr<Resources>(nullptr)));
283 CompletionQueueAsyncWorker::Next();
284 }
285
286 } // namespace node
287 } // namespace grpc
OLDNEW
« no previous file with comments | « third_party/grpc/src/node/ext/channel.h ('k') | third_party/grpc/src/node/ext/channel_credentials.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698