blob: 1871a3245287bf277d1ac625e1827d8f1375c294 [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"
Craig Tiller325a0592016-05-25 06:31:12 -070044#include "completion_queue_async_worker.h"
murgatroid99e5061512015-01-12 18:14:35 -080045#include "grpc/grpc.h"
46#include "grpc/grpc_security.h"
murgatroid9977659062015-02-11 09:26:25 -080047#include "grpc/support/log.h"
murgatroid99e5061512015-01-12 18:14:35 -080048#include "server_credentials.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080049#include "slice.h"
murgatroid99016bb502015-02-09 15:55:10 -080050#include "timeval.h"
murgatroid99e5061512015-01-12 18:14:35 -080051
52namespace grpc {
53namespace node {
54
murgatroid992b097832015-09-17 13:56:25 -070055using Nan::Callback;
56using Nan::EscapableHandleScope;
57using Nan::HandleScope;
58using Nan::Maybe;
59using Nan::MaybeLocal;
60using Nan::ObjectWrap;
61using Nan::Persistent;
62using Nan::Utf8String;
63
murgatroid99d4d67ad2015-02-09 10:43:21 -080064using std::unique_ptr;
murgatroid99e5061512015-01-12 18:14:35 -080065using v8::Array;
66using v8::Boolean;
murgatroid99016bb502015-02-09 15:55:10 -080067using v8::Date;
murgatroid99e5061512015-01-12 18:14:35 -080068using v8::Exception;
murgatroid999030c812016-09-16 13:25:08 -070069using v8::External;
murgatroid99e5061512015-01-12 18:14:35 -080070using v8::Function;
71using v8::FunctionTemplate;
murgatroid99e5061512015-01-12 18:14:35 -080072using v8::Local;
73using v8::Number;
74using v8::Object;
murgatroid99e5061512015-01-12 18:14:35 -080075using v8::String;
76using v8::Value;
77
murgatroid992b097832015-09-17 13:56:25 -070078Nan::Callback *Server::constructor;
murgatroid99e5061512015-01-12 18:14:35 -080079Persistent<FunctionTemplate> Server::fun_tpl;
80
murgatroid99d4d67ad2015-02-09 10:43:21 -080081class NewCallOp : public Op {
82 public:
83 NewCallOp() {
84 call = NULL;
85 grpc_call_details_init(&details);
86 grpc_metadata_array_init(&request_metadata);
87 }
88
89 ~NewCallOp() {
90 grpc_call_details_destroy(&details);
murgatroid99016bb502015-02-09 15:55:10 -080091 grpc_metadata_array_destroy(&request_metadata);
murgatroid99d4d67ad2015-02-09 10:43:21 -080092 }
93
murgatroid992b097832015-09-17 13:56:25 -070094 Local<Value> GetNodeValue() const {
95 Nan::EscapableHandleScope scope;
murgatroid99016bb502015-02-09 15:55:10 -080096 if (call == NULL) {
murgatroid992b097832015-09-17 13:56:25 -070097 return scope.Escape(Nan::Null());
murgatroid99d4d67ad2015-02-09 10:43:21 -080098 }
murgatroid992b097832015-09-17 13:56:25 -070099 Local<Object> obj = Nan::New<Object>();
100 Nan::Set(obj, Nan::New("call").ToLocalChecked(), Call::WrapStruct(call));
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800101 // TODO(murgatroid99): Use zero-copy string construction instead
murgatroid992b097832015-09-17 13:56:25 -0700102 Nan::Set(obj, Nan::New("method").ToLocalChecked(),
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800103 CopyStringFromSlice(details.method));
murgatroid992b097832015-09-17 13:56:25 -0700104 Nan::Set(obj, Nan::New("host").ToLocalChecked(),
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800105 CopyStringFromSlice(details.host));
murgatroid992b097832015-09-17 13:56:25 -0700106 Nan::Set(obj, Nan::New("deadline").ToLocalChecked(),
Craig Tiller325a0592016-05-25 06:31:12 -0700107 Nan::New<Date>(TimespecToMilliseconds(details.deadline))
108 .ToLocalChecked());
murgatroid992b097832015-09-17 13:56:25 -0700109 Nan::Set(obj, Nan::New("metadata").ToLocalChecked(),
110 ParseMetadata(&request_metadata));
111 return scope.Escape(obj);
murgatroid99d4d67ad2015-02-09 10:43:21 -0800112 }
113
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700114 bool ParseOp(Local<Value> value, grpc_op *out) { return true; }
115 bool IsFinalOp() { return false; }
116 void OnComplete(bool success) {}
murgatroid99d4d67ad2015-02-09 10:43:21 -0800117
118 grpc_call *call;
119 grpc_call_details details;
120 grpc_metadata_array request_metadata;
murgatroid99016bb502015-02-09 15:55:10 -0800121
122 protected:
Craig Tiller325a0592016-05-25 06:31:12 -0700123 std::string GetTypeString() const { return "new_call"; }
murgatroid99016bb502015-02-09 15:55:10 -0800124};
murgatroid99d4d67ad2015-02-09 10:43:21 -0800125
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700126class TryShutdownOp : public Op {
murgatroid9942cfaa92017-04-10 15:43:09 -0700127 public:
128 TryShutdownOp(Server *server, Local<Value> server_value) : server(server) {
129 server_persist.Reset(server_value);
130 }
131 Local<Value> GetNodeValue() const {
132 EscapableHandleScope scope;
133 return scope.Escape(Nan::New(server_persist));
134 }
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700135 bool ParseOp(Local<Value> value, grpc_op *out) { return true; }
136 bool IsFinalOp() { return false; }
murgatroid99017a3352017-04-11 14:34:04 -0700137 void OnComplete(bool success) {
138 if (success) {
139 server->DestroyWrappedServer();
140 }
murgatroid9942cfaa92017-04-10 15:43:09 -0700141 }
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700142
murgatroid9942cfaa92017-04-10 15:43:09 -0700143 protected:
144 std::string GetTypeString() const { return "try_shutdown"; }
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700145
murgatroid9942cfaa92017-04-10 15:43:09 -0700146 private:
147 Server *server;
148 Nan::Persistent<v8::Value, Nan::CopyablePersistentTraits<v8::Value>>
149 server_persist;
150};
151
murgatroid992b097832015-09-17 13:56:25 -0700152void Server::Init(Local<Object> exports) {
153 HandleScope scope;
154 Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
155 tpl->SetClassName(Nan::New("Server").ToLocalChecked());
murgatroid99e5061512015-01-12 18:14:35 -0800156 tpl->InstanceTemplate()->SetInternalFieldCount(1);
murgatroid992b097832015-09-17 13:56:25 -0700157 Nan::SetPrototypeMethod(tpl, "requestCall", RequestCall);
158 Nan::SetPrototypeMethod(tpl, "addHttp2Port", AddHttp2Port);
159 Nan::SetPrototypeMethod(tpl, "start", Start);
160 Nan::SetPrototypeMethod(tpl, "tryShutdown", TryShutdown);
161 Nan::SetPrototypeMethod(tpl, "forceShutdown", ForceShutdown);
162 fun_tpl.Reset(tpl);
163 Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked();
164 Nan::Set(exports, Nan::New("Server").ToLocalChecked(), ctr);
165 constructor = new Callback(ctr);
murgatroid99e5061512015-01-12 18:14:35 -0800166}
167
murgatroid992b097832015-09-17 13:56:25 -0700168bool Server::HasInstance(Local<Value> val) {
169 HandleScope scope;
170 return Nan::New(fun_tpl)->HasInstance(val);
murgatroid99e5061512015-01-12 18:14:35 -0800171}
172
murgatroid9942cfaa92017-04-10 15:43:09 -0700173void Server::DestroyWrappedServer() {
174 if (this->wrapped_server != NULL) {
175 grpc_server_destroy(this->wrapped_server);
176 this->wrapped_server = NULL;
177 }
178}
179
murgatroid99e5061512015-01-12 18:14:35 -0800180NAN_METHOD(Server::New) {
murgatroid99e5061512015-01-12 18:14:35 -0800181 /* If this is not a constructor call, make a constructor call and return
182 the result */
murgatroid992b097832015-09-17 13:56:25 -0700183 if (!info.IsConstructCall()) {
murgatroid99e5061512015-01-12 18:14:35 -0800184 const int argc = 1;
murgatroid992b097832015-09-17 13:56:25 -0700185 Local<Value> argv[argc] = {info[0]};
Craig Tiller325a0592016-05-25 06:31:12 -0700186 MaybeLocal<Object> maybe_instance =
murgatroid99c0a64cd2016-08-15 13:14:16 -0700187 Nan::NewInstance(constructor->GetFunction(), argc, argv);
murgatroid992b097832015-09-17 13:56:25 -0700188 if (maybe_instance.IsEmpty()) {
189 // There's probably a pending exception
190 return;
191 } else {
192 info.GetReturnValue().Set(maybe_instance.ToLocalChecked());
193 return;
194 }
murgatroid99e5061512015-01-12 18:14:35 -0800195 }
196 grpc_server *wrapped_server;
murgatroid999030c812016-09-16 13:25:08 -0700197 grpc_completion_queue *queue = GetCompletionQueue();
murgatroid9975a2bba2015-10-12 16:12:04 -0700198 grpc_channel_args *channel_args;
199 if (!ParseChannelArgs(info[0], &channel_args)) {
200 DeallocateChannelArgs(channel_args);
Craig Tiller325a0592016-05-25 06:31:12 -0700201 return Nan::ThrowTypeError(
202 "Server options must be an object with "
203 "string keys and integer or string values");
murgatroid99e5061512015-01-12 18:14:35 -0800204 }
murgatroid9975a2bba2015-10-12 16:12:04 -0700205 wrapped_server = grpc_server_create(channel_args, NULL);
206 DeallocateChannelArgs(channel_args);
Nicolas "Pixel" Noble8e6bab52015-08-07 01:40:49 +0200207 grpc_server_register_completion_queue(wrapped_server, queue, NULL);
murgatroid99e5061512015-01-12 18:14:35 -0800208 Server *server = new Server(wrapped_server);
murgatroid992b097832015-09-17 13:56:25 -0700209 server->Wrap(info.This());
210 info.GetReturnValue().Set(info.This());
murgatroid99e5061512015-01-12 18:14:35 -0800211}
212
213NAN_METHOD(Server::RequestCall) {
murgatroid992b097832015-09-17 13:56:25 -0700214 if (!HasInstance(info.This())) {
215 return Nan::ThrowTypeError("requestCall can only be called on a Server");
murgatroid99e5061512015-01-12 18:14:35 -0800216 }
murgatroid992b097832015-09-17 13:56:25 -0700217 Server *server = ObjectWrap::Unwrap<Server>(info.This());
murgatroid99016bb502015-02-09 15:55:10 -0800218 NewCallOp *op = new NewCallOp();
murgatroid99d3f9f9f2015-02-13 12:21:59 -0800219 unique_ptr<OpVec> ops(new OpVec());
murgatroid99016bb502015-02-09 15:55:10 -0800220 ops->push_back(unique_ptr<Op>(op));
murgatroid99d4d67ad2015-02-09 10:43:21 -0800221 grpc_call_error error = grpc_server_request_call(
murgatroid99016bb502015-02-09 15:55:10 -0800222 server->wrapped_server, &op->call, &op->details, &op->request_metadata,
Craig Tiller5b1c5f22017-04-19 09:52:18 -0700223 GetCompletionQueue(), GetCompletionQueue(),
224 new struct tag(new Callback(info[0].As<Function>()), ops.release(), NULL,
225 Nan::Null()));
murgatroid99d4d67ad2015-02-09 10:43:21 -0800226 if (error != GRPC_CALL_OK) {
murgatroid992b097832015-09-17 13:56:25 -0700227 return Nan::ThrowError(nanErrorWithCode("requestCall failed", error));
murgatroid99e5061512015-01-12 18:14:35 -0800228 }
murgatroid999030c812016-09-16 13:25:08 -0700229 CompletionQueueNext();
murgatroid99e5061512015-01-12 18:14:35 -0800230}
231
232NAN_METHOD(Server::AddHttp2Port) {
murgatroid992b097832015-09-17 13:56:25 -0700233 if (!HasInstance(info.This())) {
Craig Tiller325a0592016-05-25 06:31:12 -0700234 return Nan::ThrowTypeError("addHttp2Port can only be called on a Server");
murgatroid99e5061512015-01-12 18:14:35 -0800235 }
murgatroid992b097832015-09-17 13:56:25 -0700236 if (!info[0]->IsString()) {
237 return Nan::ThrowTypeError(
murgatroid991a7dcac2015-07-27 16:13:28 -0700238 "addHttp2Port's first argument must be a String");
murgatroid99da02a672015-03-02 17:28:02 -0800239 }
murgatroid992b097832015-09-17 13:56:25 -0700240 if (!ServerCredentials::HasInstance(info[1])) {
241 return Nan::ThrowTypeError(
murgatroid991a7dcac2015-07-27 16:13:28 -0700242 "addHttp2Port's second argument must be ServerCredentials");
murgatroid99e5061512015-01-12 18:14:35 -0800243 }
murgatroid992b097832015-09-17 13:56:25 -0700244 Server *server = ObjectWrap::Unwrap<Server>(info.This());
murgatroid991a7dcac2015-07-27 16:13:28 -0700245 ServerCredentials *creds_object = ObjectWrap::Unwrap<ServerCredentials>(
murgatroid992b097832015-09-17 13:56:25 -0700246 Nan::To<Object>(info[1]).ToLocalChecked());
murgatroid991a7dcac2015-07-27 16:13:28 -0700247 grpc_server_credentials *creds = creds_object->GetWrappedServerCredentials();
248 int port;
249 if (creds == NULL) {
Craig Tillerc5ae3eb2015-08-03 10:42:22 -0700250 port = grpc_server_add_insecure_http2_port(server->wrapped_server,
murgatroid992b097832015-09-17 13:56:25 -0700251 *Utf8String(info[0]));
murgatroid991a7dcac2015-07-27 16:13:28 -0700252 } else {
253 port = grpc_server_add_secure_http2_port(server->wrapped_server,
Craig Tiller325a0592016-05-25 06:31:12 -0700254 *Utf8String(info[0]), creds);
murgatroid991a7dcac2015-07-27 16:13:28 -0700255 }
murgatroid992b097832015-09-17 13:56:25 -0700256 info.GetReturnValue().Set(Nan::New<Number>(port));
murgatroid99e5061512015-01-12 18:14:35 -0800257}
258
259NAN_METHOD(Server::Start) {
murgatroid992b097832015-09-17 13:56:25 -0700260 Nan::HandleScope scope;
261 if (!HasInstance(info.This())) {
262 return Nan::ThrowTypeError("start can only be called on a Server");
murgatroid99e5061512015-01-12 18:14:35 -0800263 }
murgatroid992b097832015-09-17 13:56:25 -0700264 Server *server = ObjectWrap::Unwrap<Server>(info.This());
murgatroid99e5061512015-01-12 18:14:35 -0800265 grpc_server_start(server->wrapped_server);
murgatroid99e5061512015-01-12 18:14:35 -0800266}
267
murgatroid99cb951f62015-08-18 17:38:11 -0700268NAN_METHOD(Server::TryShutdown) {
murgatroid992b097832015-09-17 13:56:25 -0700269 Nan::HandleScope scope;
270 if (!HasInstance(info.This())) {
271 return Nan::ThrowTypeError("tryShutdown can only be called on a Server");
murgatroid99cb951f62015-08-18 17:38:11 -0700272 }
murgatroid992b097832015-09-17 13:56:25 -0700273 Server *server = ObjectWrap::Unwrap<Server>(info.This());
murgatroid99803392e2017-04-11 12:13:08 -0700274 if (server->wrapped_server == NULL) {
275 // Server is already shut down. Call callback immediately.
276 Nan::Callback callback(info[0].As<Function>());
277 callback.Call(0, {});
278 return;
279 }
murgatroid9942cfaa92017-04-10 15:43:09 -0700280 TryShutdownOp *op = new TryShutdownOp(server, info.This());
murgatroid99cb951f62015-08-18 17:38:11 -0700281 unique_ptr<OpVec> ops(new OpVec());
murgatroid9942cfaa92017-04-10 15:43:09 -0700282 ops->push_back(unique_ptr<Op>(op));
murgatroid99cb951f62015-08-18 17:38:11 -0700283 grpc_server_shutdown_and_notify(
murgatroid999030c812016-09-16 13:25:08 -0700284 server->wrapped_server, GetCompletionQueue(),
murgatroid992b097832015-09-17 13:56:25 -0700285 new struct tag(new Nan::Callback(info[0].As<Function>()), ops.release(),
murgatroid99130568e2017-04-04 13:43:49 -0700286 NULL, Nan::Null()));
murgatroid999030c812016-09-16 13:25:08 -0700287 CompletionQueueNext();
murgatroid99cd35c4c2015-05-28 13:39:25 -0700288}
289
murgatroid99cb951f62015-08-18 17:38:11 -0700290NAN_METHOD(Server::ForceShutdown) {
murgatroid992b097832015-09-17 13:56:25 -0700291 Nan::HandleScope scope;
292 if (!HasInstance(info.This())) {
293 return Nan::ThrowTypeError("forceShutdown can only be called on a Server");
murgatroid99e5061512015-01-12 18:14:35 -0800294 }
murgatroid992b097832015-09-17 13:56:25 -0700295 Server *server = ObjectWrap::Unwrap<Server>(info.This());
murgatroid99cd35c4c2015-05-28 13:39:25 -0700296 server->ShutdownServer();
murgatroid99e5061512015-01-12 18:14:35 -0800297}
298
299} // namespace node
Craig Tiller190d3602015-02-18 09:23:38 -0800300} // namespace grpc