aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/node/ext/channel.cc
blob: 9aed96bbf5ae430eebc1f76cd34f8f50e623764d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 *
 * Copyright 2015, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <vector>

#include "grpc/support/log.h"

#include <node.h>
#include <nan.h>
#include "grpc/grpc.h"
#include "grpc/grpc_security.h"
#include "call.h"
#include "channel.h"
#include "completion_queue_async_worker.h"
#include "credentials.h"
#include "timeval.h"

namespace grpc {
namespace node {

using v8::Array;
using v8::Exception;
using v8::Function;
using v8::FunctionTemplate;
using v8::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::Persistent;
using v8::String;
using v8::Value;

NanCallback *Channel::constructor;
Persistent<FunctionTemplate> Channel::fun_tpl;

Channel::Channel(grpc_channel *channel) : wrapped_channel(channel) {}

Channel::~Channel() {
  if (wrapped_channel != NULL) {
    grpc_channel_destroy(wrapped_channel);
  }
}

void Channel::Init(Handle<Object> exports) {
  NanScope();
  Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
  tpl->SetClassName(NanNew("Channel"));
  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  NanSetPrototypeTemplate(tpl, "close",
                          NanNew<FunctionTemplate>(Close)->GetFunction());
  NanSetPrototypeTemplate(tpl, "getTarget",
                          NanNew<FunctionTemplate>(GetTarget)->GetFunction());
  NanSetPrototypeTemplate(
      tpl, "getConnectivityState",
      NanNew<FunctionTemplate>(GetConnectivityState)->GetFunction());
  NanSetPrototypeTemplate(
      tpl, "watchConnectivityState",
      NanNew<FunctionTemplate>(WatchConnectivityState)->GetFunction());
  NanAssignPersistent(fun_tpl, tpl);
  Handle<Function> ctr = tpl->GetFunction();
  constructor = new NanCallback(ctr);
  exports->Set(NanNew("Channel"), ctr);
}

bool Channel::HasInstance(Handle<Value> val) {
  NanScope();
  return NanHasInstance(fun_tpl, val);
}

grpc_channel *Channel::GetWrappedChannel() { return this->wrapped_channel; }

NAN_METHOD(Channel::New) {
  NanScope();

  if (args.IsConstructCall()) {
    if (!args[0]->IsString()) {
      return NanThrowTypeError(
          "Channel expects a string, a credential and an object");
    }
    grpc_channel *wrapped_channel;
    // Owned by the Channel object
    NanUtf8String host(args[0]);
    grpc_credentials *creds;
    if (!Credentials::HasInstance(args[1])) {
      return NanThrowTypeError(
          "Channel's second argument must be a credential");
    }
    Credentials *creds_object = ObjectWrap::Unwrap<Credentials>(
        args[1]->ToObject());
    creds = creds_object->GetWrappedCredentials();
    grpc_channel_args *channel_args_ptr;
    if (args[2]->IsUndefined()) {
      channel_args_ptr = NULL;
      wrapped_channel = grpc_insecure_channel_create(*host, NULL, NULL);
    } else if (args[2]->IsObject()) {
      Handle<Object> args_hash(args[2]->ToObject()->Clone());
      Handle<Array> keys(args_hash->GetOwnPropertyNames());
      grpc_channel_args channel_args;
      channel_args.num_args = keys->Length();
      channel_args.args = reinterpret_cast<grpc_arg *>(
          calloc(channel_args.num_args, sizeof(grpc_arg)));
      /* These are used to keep all strings until then end of the block, then
         destroy them */
      std::vector<NanUtf8String *> key_strings(keys->Length());
      std::vector<NanUtf8String *> value_strings(keys->Length());
      for (unsigned int i = 0; i < channel_args.num_args; i++) {
        Handle<String> current_key(keys->Get(i)->ToString());
        Handle<Value> current_value(args_hash->Get(current_key));
        key_strings[i] = new NanUtf8String(current_key);
        channel_args.args[i].key = **key_strings[i];
        if (current_value->IsInt32()) {
          channel_args.args[i].type = GRPC_ARG_INTEGER;
          channel_args.args[i].value.integer = current_value->Int32Value();
        } else if (current_value->IsString()) {
          channel_args.args[i].type = GRPC_ARG_STRING;
          value_strings[i] = new NanUtf8String(current_value);
          channel_args.args[i].value.string = **value_strings[i];
        } else {
          free(channel_args.args);
          return NanThrowTypeError("Arg values must be strings");
        }
      }
      channel_args_ptr = &channel_args;
    } else {
      return NanThrowTypeError("Channel expects a string and an object");
    }
    if (creds == NULL) {
      wrapped_channel = grpc_insecure_channel_create(*host, channel_args_ptr,
                                                     NULL);
    } else {
      wrapped_channel =
          grpc_secure_channel_create(creds, *host, channel_args_ptr, NULL);
    }
    if (channel_args_ptr != NULL) {
      free(channel_args_ptr->args);
    }
    Channel *channel = new Channel(wrapped_channel);
    channel->Wrap(args.This());
    NanReturnValue(args.This());
  } else {
    const int argc = 3;
    Local<Value> argv[argc] = {args[0], args[1], args[2]};
    NanReturnValue(constructor->GetFunction()->NewInstance(argc, argv));
  }
}

NAN_METHOD(Channel::Close) {
  NanScope();
  if (!HasInstance(args.This())) {
    return NanThrowTypeError("close can only be called on Channel objects");
  }
  Channel *channel = ObjectWrap::Unwrap<Channel>(args.This());
  if (channel->wrapped_channel != NULL) {
    grpc_channel_destroy(channel->wrapped_channel);
    channel->wrapped_channel = NULL;
  }
  NanReturnUndefined();
}

NAN_METHOD(Channel::GetTarget) {
  NanScope();
  if (!HasInstance(args.This())) {
    return NanThrowTypeError("getTarget can only be called on Channel objects");
  }
  Channel *channel = ObjectWrap::Unwrap<Channel>(args.This());
  NanReturnValue(NanNew(grpc_channel_get_target(channel->wrapped_channel)));
}

NAN_METHOD(Channel::GetConnectivityState) {
  NanScope();
  if (!HasInstance(args.This())) {
    return NanThrowTypeError(
        "getConnectivityState can only be called on Channel objects");
  }
  Channel *channel = ObjectWrap::Unwrap<Channel>(args.This());
  int try_to_connect = (int)args[0]->Equals(NanTrue());
  NanReturnValue(grpc_channel_check_connectivity_state(channel->wrapped_channel,
                                                       try_to_connect));
}

NAN_METHOD(Channel::WatchConnectivityState) {
  NanScope();
  if (!HasInstance(args.This())) {
    return NanThrowTypeError(
        "watchConnectivityState can only be called on Channel objects");
  }
  if (!args[0]->IsUint32()) {
    return NanThrowTypeError(
        "watchConnectivityState's first argument must be a channel state");
  }
  if (!(args[1]->IsNumber() || args[1]->IsDate())) {
    return NanThrowTypeError(
        "watchConnectivityState's second argument must be a date or a number");
  }
  if (!args[2]->IsFunction()) {
    return NanThrowTypeError(
        "watchConnectivityState's third argument must be a callback");
  }
  grpc_connectivity_state last_state =
      static_cast<grpc_connectivity_state>(args[0]->Uint32Value());
  double deadline = args[1]->NumberValue();
  Handle<Function> callback_func = args[2].As<Function>();
  NanCallback *callback = new NanCallback(callback_func);
  Channel *channel = ObjectWrap::Unwrap<Channel>(args.This());
  unique_ptr<OpVec> ops(new OpVec());
  grpc_channel_watch_connectivity_state(
      channel->wrapped_channel, last_state, MillisecondsToTimespec(deadline),
      CompletionQueueAsyncWorker::GetQueue(),
      new struct tag(callback,
                     ops.release(),
                     shared_ptr<Resources>(nullptr)));
  CompletionQueueAsyncWorker::Next();
  NanReturnUndefined();
}

}  // namespace node
}  // namespace grpc