OLD | NEW |
(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 <memory> |
| 35 |
| 36 #include "server.h" |
| 37 |
| 38 #include <node.h> |
| 39 #include <nan.h> |
| 40 |
| 41 #include <vector> |
| 42 #include "grpc/grpc.h" |
| 43 #include "grpc/grpc_security.h" |
| 44 #include "grpc/support/log.h" |
| 45 #include "call.h" |
| 46 #include "completion_queue_async_worker.h" |
| 47 #include "server_credentials.h" |
| 48 #include "timeval.h" |
| 49 |
| 50 namespace grpc { |
| 51 namespace node { |
| 52 |
| 53 using Nan::Callback; |
| 54 using Nan::EscapableHandleScope; |
| 55 using Nan::HandleScope; |
| 56 using Nan::Maybe; |
| 57 using Nan::MaybeLocal; |
| 58 using Nan::ObjectWrap; |
| 59 using Nan::Persistent; |
| 60 using Nan::Utf8String; |
| 61 |
| 62 using std::unique_ptr; |
| 63 using v8::Array; |
| 64 using v8::Boolean; |
| 65 using v8::Date; |
| 66 using v8::Exception; |
| 67 using v8::Function; |
| 68 using v8::FunctionTemplate; |
| 69 using v8::Local; |
| 70 using v8::Number; |
| 71 using v8::Object; |
| 72 using v8::String; |
| 73 using v8::Value; |
| 74 |
| 75 Nan::Callback *Server::constructor; |
| 76 Persistent<FunctionTemplate> Server::fun_tpl; |
| 77 |
| 78 class NewCallOp : public Op { |
| 79 public: |
| 80 NewCallOp() { |
| 81 call = NULL; |
| 82 grpc_call_details_init(&details); |
| 83 grpc_metadata_array_init(&request_metadata); |
| 84 } |
| 85 |
| 86 ~NewCallOp() { |
| 87 grpc_call_details_destroy(&details); |
| 88 grpc_metadata_array_destroy(&request_metadata); |
| 89 } |
| 90 |
| 91 Local<Value> GetNodeValue() const { |
| 92 Nan::EscapableHandleScope scope; |
| 93 if (call == NULL) { |
| 94 return scope.Escape(Nan::Null()); |
| 95 } |
| 96 Local<Object> obj = Nan::New<Object>(); |
| 97 Nan::Set(obj, Nan::New("call").ToLocalChecked(), Call::WrapStruct(call)); |
| 98 Nan::Set(obj, Nan::New("method").ToLocalChecked(), |
| 99 Nan::New(details.method).ToLocalChecked()); |
| 100 Nan::Set(obj, Nan::New("host").ToLocalChecked(), |
| 101 Nan::New(details.host).ToLocalChecked()); |
| 102 Nan::Set(obj, Nan::New("deadline").ToLocalChecked(), |
| 103 Nan::New<Date>( |
| 104 TimespecToMilliseconds(details.deadline)).ToLocalChecked()); |
| 105 Nan::Set(obj, Nan::New("metadata").ToLocalChecked(), |
| 106 ParseMetadata(&request_metadata)); |
| 107 return scope.Escape(obj); |
| 108 } |
| 109 |
| 110 bool ParseOp(Local<Value> value, grpc_op *out, |
| 111 shared_ptr<Resources> resources) { |
| 112 return true; |
| 113 } |
| 114 |
| 115 grpc_call *call; |
| 116 grpc_call_details details; |
| 117 grpc_metadata_array request_metadata; |
| 118 |
| 119 protected: |
| 120 std::string GetTypeString() const { |
| 121 return "new_call"; |
| 122 } |
| 123 }; |
| 124 |
| 125 Server::Server(grpc_server *server) : wrapped_server(server) { |
| 126 shutdown_queue = grpc_completion_queue_create(NULL); |
| 127 grpc_server_register_completion_queue(server, shutdown_queue, NULL); |
| 128 } |
| 129 |
| 130 Server::~Server() { |
| 131 this->ShutdownServer(); |
| 132 grpc_completion_queue_shutdown(this->shutdown_queue); |
| 133 grpc_server_destroy(this->wrapped_server); |
| 134 grpc_completion_queue_destroy(this->shutdown_queue); |
| 135 } |
| 136 |
| 137 void Server::Init(Local<Object> exports) { |
| 138 HandleScope scope; |
| 139 Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New); |
| 140 tpl->SetClassName(Nan::New("Server").ToLocalChecked()); |
| 141 tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 142 Nan::SetPrototypeMethod(tpl, "requestCall", RequestCall); |
| 143 Nan::SetPrototypeMethod(tpl, "addHttp2Port", AddHttp2Port); |
| 144 Nan::SetPrototypeMethod(tpl, "start", Start); |
| 145 Nan::SetPrototypeMethod(tpl, "tryShutdown", TryShutdown); |
| 146 Nan::SetPrototypeMethod(tpl, "forceShutdown", ForceShutdown); |
| 147 fun_tpl.Reset(tpl); |
| 148 Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked(); |
| 149 Nan::Set(exports, Nan::New("Server").ToLocalChecked(), ctr); |
| 150 constructor = new Callback(ctr); |
| 151 } |
| 152 |
| 153 bool Server::HasInstance(Local<Value> val) { |
| 154 HandleScope scope; |
| 155 return Nan::New(fun_tpl)->HasInstance(val); |
| 156 } |
| 157 |
| 158 void Server::ShutdownServer() { |
| 159 grpc_server_shutdown_and_notify(this->wrapped_server, |
| 160 this->shutdown_queue, |
| 161 NULL); |
| 162 grpc_server_cancel_all_calls(this->wrapped_server); |
| 163 grpc_completion_queue_pluck(this->shutdown_queue, NULL, |
| 164 gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 165 } |
| 166 |
| 167 NAN_METHOD(Server::New) { |
| 168 /* If this is not a constructor call, make a constructor call and return |
| 169 the result */ |
| 170 if (!info.IsConstructCall()) { |
| 171 const int argc = 1; |
| 172 Local<Value> argv[argc] = {info[0]}; |
| 173 MaybeLocal<Object> maybe_instance = constructor->GetFunction()->NewInstance( |
| 174 argc, argv); |
| 175 if (maybe_instance.IsEmpty()) { |
| 176 // There's probably a pending exception |
| 177 return; |
| 178 } else { |
| 179 info.GetReturnValue().Set(maybe_instance.ToLocalChecked()); |
| 180 return; |
| 181 } |
| 182 } |
| 183 grpc_server *wrapped_server; |
| 184 grpc_completion_queue *queue = CompletionQueueAsyncWorker::GetQueue(); |
| 185 grpc_channel_args *channel_args; |
| 186 if (!ParseChannelArgs(info[0], &channel_args)) { |
| 187 DeallocateChannelArgs(channel_args); |
| 188 return Nan::ThrowTypeError("Server options must be an object with " |
| 189 "string keys and integer or string values"); |
| 190 } |
| 191 wrapped_server = grpc_server_create(channel_args, NULL); |
| 192 DeallocateChannelArgs(channel_args); |
| 193 grpc_server_register_completion_queue(wrapped_server, queue, NULL); |
| 194 Server *server = new Server(wrapped_server); |
| 195 server->Wrap(info.This()); |
| 196 info.GetReturnValue().Set(info.This()); |
| 197 } |
| 198 |
| 199 NAN_METHOD(Server::RequestCall) { |
| 200 if (!HasInstance(info.This())) { |
| 201 return Nan::ThrowTypeError("requestCall can only be called on a Server"); |
| 202 } |
| 203 Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
| 204 NewCallOp *op = new NewCallOp(); |
| 205 unique_ptr<OpVec> ops(new OpVec()); |
| 206 ops->push_back(unique_ptr<Op>(op)); |
| 207 grpc_call_error error = grpc_server_request_call( |
| 208 server->wrapped_server, &op->call, &op->details, &op->request_metadata, |
| 209 CompletionQueueAsyncWorker::GetQueue(), |
| 210 CompletionQueueAsyncWorker::GetQueue(), |
| 211 new struct tag(new Callback(info[0].As<Function>()), ops.release(), |
| 212 shared_ptr<Resources>(nullptr))); |
| 213 if (error != GRPC_CALL_OK) { |
| 214 return Nan::ThrowError(nanErrorWithCode("requestCall failed", error)); |
| 215 } |
| 216 CompletionQueueAsyncWorker::Next(); |
| 217 } |
| 218 |
| 219 NAN_METHOD(Server::AddHttp2Port) { |
| 220 if (!HasInstance(info.This())) { |
| 221 return Nan::ThrowTypeError( |
| 222 "addHttp2Port can only be called on a Server"); |
| 223 } |
| 224 if (!info[0]->IsString()) { |
| 225 return Nan::ThrowTypeError( |
| 226 "addHttp2Port's first argument must be a String"); |
| 227 } |
| 228 if (!ServerCredentials::HasInstance(info[1])) { |
| 229 return Nan::ThrowTypeError( |
| 230 "addHttp2Port's second argument must be ServerCredentials"); |
| 231 } |
| 232 Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
| 233 ServerCredentials *creds_object = ObjectWrap::Unwrap<ServerCredentials>( |
| 234 Nan::To<Object>(info[1]).ToLocalChecked()); |
| 235 grpc_server_credentials *creds = creds_object->GetWrappedServerCredentials(); |
| 236 int port; |
| 237 if (creds == NULL) { |
| 238 port = grpc_server_add_insecure_http2_port(server->wrapped_server, |
| 239 *Utf8String(info[0])); |
| 240 } else { |
| 241 port = grpc_server_add_secure_http2_port(server->wrapped_server, |
| 242 *Utf8String(info[0]), |
| 243 creds); |
| 244 } |
| 245 info.GetReturnValue().Set(Nan::New<Number>(port)); |
| 246 } |
| 247 |
| 248 NAN_METHOD(Server::Start) { |
| 249 Nan::HandleScope scope; |
| 250 if (!HasInstance(info.This())) { |
| 251 return Nan::ThrowTypeError("start can only be called on a Server"); |
| 252 } |
| 253 Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
| 254 grpc_server_start(server->wrapped_server); |
| 255 } |
| 256 |
| 257 NAN_METHOD(Server::TryShutdown) { |
| 258 Nan::HandleScope scope; |
| 259 if (!HasInstance(info.This())) { |
| 260 return Nan::ThrowTypeError("tryShutdown can only be called on a Server"); |
| 261 } |
| 262 Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
| 263 unique_ptr<OpVec> ops(new OpVec()); |
| 264 grpc_server_shutdown_and_notify( |
| 265 server->wrapped_server, |
| 266 CompletionQueueAsyncWorker::GetQueue(), |
| 267 new struct tag(new Nan::Callback(info[0].As<Function>()), ops.release(), |
| 268 shared_ptr<Resources>(nullptr))); |
| 269 CompletionQueueAsyncWorker::Next(); |
| 270 } |
| 271 |
| 272 NAN_METHOD(Server::ForceShutdown) { |
| 273 Nan::HandleScope scope; |
| 274 if (!HasInstance(info.This())) { |
| 275 return Nan::ThrowTypeError("forceShutdown can only be called on a Server"); |
| 276 } |
| 277 Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
| 278 server->ShutdownServer(); |
| 279 } |
| 280 |
| 281 } // namespace node |
| 282 } // namespace grpc |
OLD | NEW |