blob: a885a9f26845605d0ab368e6190a0e9323fda5ff [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
murgatroid99d4d67ad2015-02-09 10:43:21 -080034#include <memory>
35
murgatroid99e5061512015-01-12 18:14:35 -080036#include "server.h"
37
murgatroid99e5061512015-01-12 18:14:35 -080038#include <nan.h>
Craig Tiller325a0592016-05-25 06:31:12 -070039#include <node.h>
murgatroid99e5061512015-01-12 18:14:35 -080040
murgatroid99e5061512015-01-12 18:14:35 -080041#include <vector>
Craig Tiller325a0592016-05-25 06:31:12 -070042#include "call.h"
murgatroid999030c812016-09-16 13:25:08 -070043#include "completion_queue.h"
murgatroid99e5061512015-01-12 18:14:35 -080044#include "grpc/grpc.h"
45#include "grpc/grpc_security.h"
murgatroid9977659062015-02-11 09:26:25 -080046#include "grpc/support/log.h"
murgatroid99e5061512015-01-12 18:14:35 -080047#include "server_credentials.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080048#include "slice.h"
murgatroid99016bb502015-02-09 15:55:10 -080049#include "timeval.h"
murgatroid99e5061512015-01-12 18:14:35 -080050
51namespace grpc {
52namespace node {
53
murgatroid992b097832015-09-17 13:56:25 -070054using Nan::Callback;
55using Nan::EscapableHandleScope;
56using Nan::HandleScope;
57using Nan::Maybe;
58using Nan::MaybeLocal;
59using Nan::ObjectWrap;
60using Nan::Persistent;
61using Nan::Utf8String;
62
murgatroid99d4d67ad2015-02-09 10:43:21 -080063using std::unique_ptr;
murgatroid99e5061512015-01-12 18:14:35 -080064using v8::Array;
65using v8::Boolean;
murgatroid99016bb502015-02-09 15:55:10 -080066using v8::Date;
murgatroid99e5061512015-01-12 18:14:35 -080067using v8::Exception;
murgatroid999030c812016-09-16 13:25:08 -070068using v8::External;
murgatroid99e5061512015-01-12 18:14:35 -080069using v8::Function;
70using v8::FunctionTemplate;
murgatroid99e5061512015-01-12 18:14:35 -080071using v8::Local;
72using v8::Number;
73using v8::Object;
murgatroid99e5061512015-01-12 18:14:35 -080074using v8::String;
75using v8::Value;
76
murgatroid992b097832015-09-17 13:56:25 -070077Nan::Callback *Server::constructor;
murgatroid99e5061512015-01-12 18:14:35 -080078Persistent<FunctionTemplate> Server::fun_tpl;
79
murgatroid99f94f64f2017-04-24 13:35:21 -070080static Callback *shutdown_callback = NULL;
81
82class ServerShutdownOp : public Op {
83 public:
murgatroid99ea87dfb2017-04-25 12:51:40 -070084 ServerShutdownOp(grpc_server *server) : server(server) {}
murgatroid99f94f64f2017-04-24 13:35:21 -070085
murgatroid99ea87dfb2017-04-25 12:51:40 -070086 ~ServerShutdownOp() {}
murgatroid99f94f64f2017-04-24 13:35:21 -070087
murgatroid99ea87dfb2017-04-25 12:51:40 -070088 Local<Value> GetNodeValue() const { return Nan::Null(); }
murgatroid99f94f64f2017-04-24 13:35:21 -070089
murgatroid99ea87dfb2017-04-25 12:51:40 -070090 bool ParseOp(Local<Value> value, grpc_op *out) { return true; }
91 bool IsFinalOp() { return false; }
murgatroid99f94f64f2017-04-24 13:35:21 -070092 void OnComplete(bool success) {
93 /* Because cancel_all_calls was called, we assume that shutdown_and_notify
94 completes successfully */
95 grpc_server_destroy(server);
96 }
97
98 grpc_server *server;
99
100 protected:
101 std::string GetTypeString() const { return "shutdown"; }
102};
103
murgatroid99d4d67ad2015-02-09 10:43:21 -0800104class NewCallOp : public Op {
105 public:
106 NewCallOp() {
107 call = NULL;
108 grpc_call_details_init(&details);
109 grpc_metadata_array_init(&request_metadata);
110 }
111
112 ~NewCallOp() {
113 grpc_call_details_destroy(&details);
murgatroid99016bb502015-02-09 15:55:10 -0800114 grpc_metadata_array_destroy(&request_metadata);
murgatroid99d4d67ad2015-02-09 10:43:21 -0800115 }
116
murgatroid992b097832015-09-17 13:56:25 -0700117 Local<Value> GetNodeValue() const {
118 Nan::EscapableHandleScope scope;
murgatroid99016bb502015-02-09 15:55:10 -0800119 if (call == NULL) {
murgatroid992b097832015-09-17 13:56:25 -0700120 return scope.Escape(Nan::Null());
murgatroid99d4d67ad2015-02-09 10:43:21 -0800121 }
murgatroid992b097832015-09-17 13:56:25 -0700122 Local<Object> obj = Nan::New<Object>();
123 Nan::Set(obj, Nan::New("call").ToLocalChecked(), Call::WrapStruct(call));
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800124 // TODO(murgatroid99): Use zero-copy string construction instead
murgatroid992b097832015-09-17 13:56:25 -0700125 Nan::Set(obj, Nan::New("method").ToLocalChecked(),
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800126 CopyStringFromSlice(details.method));
murgatroid992b097832015-09-17 13:56:25 -0700127 Nan::Set(obj, Nan::New("host").ToLocalChecked(),
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800128 CopyStringFromSlice(details.host));
murgatroid992b097832015-09-17 13:56:25 -0700129 Nan::Set(obj, Nan::New("deadline").ToLocalChecked(),
Craig Tiller325a0592016-05-25 06:31:12 -0700130 Nan::New<Date>(TimespecToMilliseconds(details.deadline))
131 .ToLocalChecked());
murgatroid992b097832015-09-17 13:56:25 -0700132 Nan::Set(obj, Nan::New("metadata").ToLocalChecked(),
133 ParseMetadata(&request_metadata));
134 return scope.Escape(obj);
murgatroid99d4d67ad2015-02-09 10:43:21 -0800135 }
136
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700137 bool ParseOp(Local<Value> value, grpc_op *out) { return true; }
138 bool IsFinalOp() { return false; }
139 void OnComplete(bool success) {}
murgatroid99d4d67ad2015-02-09 10:43:21 -0800140
141 grpc_call *call;
142 grpc_call_details details;
143 grpc_metadata_array request_metadata;
murgatroid99016bb502015-02-09 15:55:10 -0800144
145 protected:
Craig Tiller325a0592016-05-25 06:31:12 -0700146 std::string GetTypeString() const { return "new_call"; }
murgatroid99016bb502015-02-09 15:55:10 -0800147};
murgatroid99d4d67ad2015-02-09 10:43:21 -0800148
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700149class TryShutdownOp : public Op {
murgatroid9942cfaa92017-04-10 15:43:09 -0700150 public:
151 TryShutdownOp(Server *server, Local<Value> server_value) : server(server) {
152 server_persist.Reset(server_value);
153 }
154 Local<Value> GetNodeValue() const {
155 EscapableHandleScope scope;
156 return scope.Escape(Nan::New(server_persist));
157 }
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700158 bool ParseOp(Local<Value> value, grpc_op *out) { return true; }
159 bool IsFinalOp() { return false; }
murgatroid99017a3352017-04-11 14:34:04 -0700160 void OnComplete(bool success) {
161 if (success) {
162 server->DestroyWrappedServer();
163 }
murgatroid9942cfaa92017-04-10 15:43:09 -0700164 }
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700165
murgatroid9942cfaa92017-04-10 15:43:09 -0700166 protected:
167 std::string GetTypeString() const { return "try_shutdown"; }
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700168
murgatroid9942cfaa92017-04-10 15:43:09 -0700169 private:
170 Server *server;
171 Nan::Persistent<v8::Value, Nan::CopyablePersistentTraits<v8::Value>>
172 server_persist;
173};
174
murgatroid99ea87dfb2017-04-25 12:51:40 -0700175Server::Server(grpc_server *server) : wrapped_server(server) {}
murgatroid99f94f64f2017-04-24 13:35:21 -0700176
murgatroid99ea87dfb2017-04-25 12:51:40 -0700177Server::~Server() { this->ShutdownServer(); }
murgatroid99f94f64f2017-04-24 13:35:21 -0700178
murgatroid992b097832015-09-17 13:56:25 -0700179void Server::Init(Local<Object> exports) {
180 HandleScope scope;
181 Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
182 tpl->SetClassName(Nan::New("Server").ToLocalChecked());
murgatroid99e5061512015-01-12 18:14:35 -0800183 tpl->InstanceTemplate()->SetInternalFieldCount(1);
murgatroid992b097832015-09-17 13:56:25 -0700184 Nan::SetPrototypeMethod(tpl, "requestCall", RequestCall);
185 Nan::SetPrototypeMethod(tpl, "addHttp2Port", AddHttp2Port);
186 Nan::SetPrototypeMethod(tpl, "start", Start);
187 Nan::SetPrototypeMethod(tpl, "tryShutdown", TryShutdown);
188 Nan::SetPrototypeMethod(tpl, "forceShutdown", ForceShutdown);
189 fun_tpl.Reset(tpl);
190 Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked();
191 Nan::Set(exports, Nan::New("Server").ToLocalChecked(), ctr);
192 constructor = new Callback(ctr);
murgatroid99e5061512015-01-12 18:14:35 -0800193}
194
murgatroid992b097832015-09-17 13:56:25 -0700195bool Server::HasInstance(Local<Value> val) {
196 HandleScope scope;
197 return Nan::New(fun_tpl)->HasInstance(val);
murgatroid99e5061512015-01-12 18:14:35 -0800198}
199
murgatroid9942cfaa92017-04-10 15:43:09 -0700200void Server::DestroyWrappedServer() {
201 if (this->wrapped_server != NULL) {
202 grpc_server_destroy(this->wrapped_server);
203 this->wrapped_server = NULL;
204 }
205}
206
murgatroid99f94f64f2017-04-24 13:35:21 -0700207NAN_METHOD(ServerShutdownCallback) {
208 if (!info[0]->IsNull()) {
209 return Nan::ThrowError("forceShutdown failed somehow");
210 }
211}
212
213void Server::ShutdownServer() {
214 Nan::HandleScope scope;
215 if (this->wrapped_server != NULL) {
216 if (shutdown_callback == NULL) {
murgatroid99ea87dfb2017-04-25 12:51:40 -0700217 Local<FunctionTemplate> callback_tpl =
murgatroid99f94f64f2017-04-24 13:35:21 -0700218 Nan::New<FunctionTemplate>(ServerShutdownCallback);
murgatroid99ea87dfb2017-04-25 12:51:40 -0700219 shutdown_callback =
220 new Callback(Nan::GetFunction(callback_tpl).ToLocalChecked());
murgatroid99f94f64f2017-04-24 13:35:21 -0700221 }
222
223 ServerShutdownOp *op = new ServerShutdownOp(this->wrapped_server);
224 unique_ptr<OpVec> ops(new OpVec());
225 ops->push_back(unique_ptr<Op>(op));
226
227 grpc_server_shutdown_and_notify(
228 this->wrapped_server, GetCompletionQueue(),
229 new struct tag(new Callback(**shutdown_callback), ops.release(), NULL,
230 Nan::Null()));
231 grpc_server_cancel_all_calls(this->wrapped_server);
232 CompletionQueueNext();
233 this->wrapped_server = NULL;
234 }
235}
236
murgatroid99e5061512015-01-12 18:14:35 -0800237NAN_METHOD(Server::New) {
murgatroid99e5061512015-01-12 18:14:35 -0800238 /* If this is not a constructor call, make a constructor call and return
239 the result */
murgatroid992b097832015-09-17 13:56:25 -0700240 if (!info.IsConstructCall()) {
murgatroid99e5061512015-01-12 18:14:35 -0800241 const int argc = 1;
murgatroid992b097832015-09-17 13:56:25 -0700242 Local<Value> argv[argc] = {info[0]};
Craig Tiller325a0592016-05-25 06:31:12 -0700243 MaybeLocal<Object> maybe_instance =
murgatroid99c0a64cd2016-08-15 13:14:16 -0700244 Nan::NewInstance(constructor->GetFunction(), argc, argv);
murgatroid992b097832015-09-17 13:56:25 -0700245 if (maybe_instance.IsEmpty()) {
246 // There's probably a pending exception
247 return;
248 } else {
249 info.GetReturnValue().Set(maybe_instance.ToLocalChecked());
250 return;
251 }
murgatroid99e5061512015-01-12 18:14:35 -0800252 }
253 grpc_server *wrapped_server;
murgatroid999030c812016-09-16 13:25:08 -0700254 grpc_completion_queue *queue = GetCompletionQueue();
murgatroid9975a2bba2015-10-12 16:12:04 -0700255 grpc_channel_args *channel_args;
256 if (!ParseChannelArgs(info[0], &channel_args)) {
257 DeallocateChannelArgs(channel_args);
Craig Tiller325a0592016-05-25 06:31:12 -0700258 return Nan::ThrowTypeError(
259 "Server options must be an object with "
260 "string keys and integer or string values");
murgatroid99e5061512015-01-12 18:14:35 -0800261 }
murgatroid9975a2bba2015-10-12 16:12:04 -0700262 wrapped_server = grpc_server_create(channel_args, NULL);
263 DeallocateChannelArgs(channel_args);
Nicolas "Pixel" Noble8e6bab52015-08-07 01:40:49 +0200264 grpc_server_register_completion_queue(wrapped_server, queue, NULL);
murgatroid99e5061512015-01-12 18:14:35 -0800265 Server *server = new Server(wrapped_server);
murgatroid992b097832015-09-17 13:56:25 -0700266 server->Wrap(info.This());
267 info.GetReturnValue().Set(info.This());
murgatroid99e5061512015-01-12 18:14:35 -0800268}
269
270NAN_METHOD(Server::RequestCall) {
murgatroid992b097832015-09-17 13:56:25 -0700271 if (!HasInstance(info.This())) {
272 return Nan::ThrowTypeError("requestCall can only be called on a Server");
murgatroid99e5061512015-01-12 18:14:35 -0800273 }
murgatroid992b097832015-09-17 13:56:25 -0700274 Server *server = ObjectWrap::Unwrap<Server>(info.This());
murgatroid99016bb502015-02-09 15:55:10 -0800275 NewCallOp *op = new NewCallOp();
murgatroid99d3f9f9f2015-02-13 12:21:59 -0800276 unique_ptr<OpVec> ops(new OpVec());
murgatroid99016bb502015-02-09 15:55:10 -0800277 ops->push_back(unique_ptr<Op>(op));
murgatroid99d4d67ad2015-02-09 10:43:21 -0800278 grpc_call_error error = grpc_server_request_call(
murgatroid99016bb502015-02-09 15:55:10 -0800279 server->wrapped_server, &op->call, &op->details, &op->request_metadata,
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700280 GetCompletionQueue(), GetCompletionQueue(),
281 new struct tag(new Callback(info[0].As<Function>()), ops.release(), NULL,
282 Nan::Null()));
murgatroid99d4d67ad2015-02-09 10:43:21 -0800283 if (error != GRPC_CALL_OK) {
murgatroid992b097832015-09-17 13:56:25 -0700284 return Nan::ThrowError(nanErrorWithCode("requestCall failed", error));
murgatroid99e5061512015-01-12 18:14:35 -0800285 }
murgatroid999030c812016-09-16 13:25:08 -0700286 CompletionQueueNext();
murgatroid99e5061512015-01-12 18:14:35 -0800287}
288
289NAN_METHOD(Server::AddHttp2Port) {
murgatroid992b097832015-09-17 13:56:25 -0700290 if (!HasInstance(info.This())) {
Craig Tiller325a0592016-05-25 06:31:12 -0700291 return Nan::ThrowTypeError("addHttp2Port can only be called on a Server");
murgatroid99e5061512015-01-12 18:14:35 -0800292 }
murgatroid992b097832015-09-17 13:56:25 -0700293 if (!info[0]->IsString()) {
294 return Nan::ThrowTypeError(
murgatroid991a7dcac2015-07-27 16:13:28 -0700295 "addHttp2Port's first argument must be a String");
murgatroid99da02a672015-03-02 17:28:02 -0800296 }
murgatroid992b097832015-09-17 13:56:25 -0700297 if (!ServerCredentials::HasInstance(info[1])) {
298 return Nan::ThrowTypeError(
murgatroid991a7dcac2015-07-27 16:13:28 -0700299 "addHttp2Port's second argument must be ServerCredentials");
murgatroid99e5061512015-01-12 18:14:35 -0800300 }
murgatroid992b097832015-09-17 13:56:25 -0700301 Server *server = ObjectWrap::Unwrap<Server>(info.This());
murgatroid991a7dcac2015-07-27 16:13:28 -0700302 ServerCredentials *creds_object = ObjectWrap::Unwrap<ServerCredentials>(
murgatroid992b097832015-09-17 13:56:25 -0700303 Nan::To<Object>(info[1]).ToLocalChecked());
murgatroid991a7dcac2015-07-27 16:13:28 -0700304 grpc_server_credentials *creds = creds_object->GetWrappedServerCredentials();
305 int port;
306 if (creds == NULL) {
Craig Tillerc5ae3eb2015-08-03 10:42:22 -0700307 port = grpc_server_add_insecure_http2_port(server->wrapped_server,
murgatroid992b097832015-09-17 13:56:25 -0700308 *Utf8String(info[0]));
murgatroid991a7dcac2015-07-27 16:13:28 -0700309 } else {
310 port = grpc_server_add_secure_http2_port(server->wrapped_server,
Craig Tiller325a0592016-05-25 06:31:12 -0700311 *Utf8String(info[0]), creds);
murgatroid991a7dcac2015-07-27 16:13:28 -0700312 }
murgatroid992b097832015-09-17 13:56:25 -0700313 info.GetReturnValue().Set(Nan::New<Number>(port));
murgatroid99e5061512015-01-12 18:14:35 -0800314}
315
316NAN_METHOD(Server::Start) {
murgatroid992b097832015-09-17 13:56:25 -0700317 Nan::HandleScope scope;
318 if (!HasInstance(info.This())) {
319 return Nan::ThrowTypeError("start can only be called on a Server");
murgatroid99e5061512015-01-12 18:14:35 -0800320 }
murgatroid992b097832015-09-17 13:56:25 -0700321 Server *server = ObjectWrap::Unwrap<Server>(info.This());
murgatroid99e5061512015-01-12 18:14:35 -0800322 grpc_server_start(server->wrapped_server);
murgatroid99e5061512015-01-12 18:14:35 -0800323}
324
murgatroid99cb951f62015-08-18 17:38:11 -0700325NAN_METHOD(Server::TryShutdown) {
murgatroid992b097832015-09-17 13:56:25 -0700326 Nan::HandleScope scope;
327 if (!HasInstance(info.This())) {
328 return Nan::ThrowTypeError("tryShutdown can only be called on a Server");
murgatroid99cb951f62015-08-18 17:38:11 -0700329 }
murgatroid992b097832015-09-17 13:56:25 -0700330 Server *server = ObjectWrap::Unwrap<Server>(info.This());
murgatroid99803392e2017-04-11 12:13:08 -0700331 if (server->wrapped_server == NULL) {
332 // Server is already shut down. Call callback immediately.
333 Nan::Callback callback(info[0].As<Function>());
334 callback.Call(0, {});
335 return;
336 }
murgatroid9942cfaa92017-04-10 15:43:09 -0700337 TryShutdownOp *op = new TryShutdownOp(server, info.This());
murgatroid99cb951f62015-08-18 17:38:11 -0700338 unique_ptr<OpVec> ops(new OpVec());
murgatroid9942cfaa92017-04-10 15:43:09 -0700339 ops->push_back(unique_ptr<Op>(op));
murgatroid99cb951f62015-08-18 17:38:11 -0700340 grpc_server_shutdown_and_notify(
murgatroid999030c812016-09-16 13:25:08 -0700341 server->wrapped_server, GetCompletionQueue(),
murgatroid992b097832015-09-17 13:56:25 -0700342 new struct tag(new Nan::Callback(info[0].As<Function>()), ops.release(),
murgatroid99130568e2017-04-04 13:43:49 -0700343 NULL, Nan::Null()));
murgatroid999030c812016-09-16 13:25:08 -0700344 CompletionQueueNext();
murgatroid99cd35c4c2015-05-28 13:39:25 -0700345}
346
murgatroid99cb951f62015-08-18 17:38:11 -0700347NAN_METHOD(Server::ForceShutdown) {
murgatroid992b097832015-09-17 13:56:25 -0700348 Nan::HandleScope scope;
349 if (!HasInstance(info.This())) {
350 return Nan::ThrowTypeError("forceShutdown can only be called on a Server");
murgatroid99e5061512015-01-12 18:14:35 -0800351 }
murgatroid992b097832015-09-17 13:56:25 -0700352 Server *server = ObjectWrap::Unwrap<Server>(info.This());
murgatroid99cd35c4c2015-05-28 13:39:25 -0700353 server->ShutdownServer();
murgatroid99e5061512015-01-12 18:14:35 -0800354}
355
356} // namespace node
Craig Tiller190d3602015-02-18 09:23:38 -0800357} // namespace grpc