blob: 86c78f05ff91a2d51e9ad62c677abff39cc01d6e [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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
34#include <grpc++/server_builder.h>
35
Craig Tiller0db1bef2015-02-09 13:47:39 -080036#include <grpc/support/cpu.h>
yangg9e21f722014-12-08 15:49:52 -080037#include <grpc/support/log.h>
Craig Tiller14a65f92015-02-09 13:13:14 -080038#include <grpc++/impl/service_type.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <grpc++/server.h>
Craig Tiller0db1bef2015-02-09 13:47:39 -080040#include "src/cpp/server/thread_pool.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041
42namespace grpc {
43
Yang Gao5f4539f2015-03-06 16:11:16 -080044ServerBuilder::ServerBuilder()
Yang Gao3921c562015-04-30 16:07:06 -070045 : max_message_size_(-1), generic_service_(nullptr), thread_pool_(nullptr) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
Craig Tillerf9e6adf2015-05-06 11:45:59 -070047std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue() {
48 ServerCompletionQueue* cq = new ServerCompletionQueue();
49 cqs_.push_back(cq);
50 return std::unique_ptr<ServerCompletionQueue>(cq);
51}
52
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080053void ServerBuilder::RegisterService(SynchronousService* service) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -070054 services_.emplace_back(new NamedService<RpcService>(service->service()));
Craig Tiller14a65f92015-02-09 13:13:14 -080055}
56
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080057void ServerBuilder::RegisterAsyncService(AsynchronousService* service) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -070058 async_services_.emplace_back(new NamedService<AsynchronousService>(service));
Craig Tiller822d2c72015-07-07 16:08:00 -070059}
60
61void ServerBuilder::RegisterService(
62 const grpc::string& addr, SynchronousService* service) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -070063 services_.emplace_back(new NamedService<RpcService>(addr, service->service()));
Craig Tiller822d2c72015-07-07 16:08:00 -070064}
65
66void ServerBuilder::RegisterAsyncService(
67 const grpc::string& addr, AsynchronousService* service) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -070068 async_services_.emplace_back(new NamedService<AsynchronousService>(addr, service));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069}
70
Yang Gao49996492015-03-12 16:40:19 -070071void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) {
Yang Gao005eb882015-03-11 22:17:13 -070072 if (generic_service_) {
Yang Gao1c402332015-03-05 16:39:25 -080073 gpr_log(GPR_ERROR,
Yang Gao49996492015-03-12 16:40:19 -070074 "Adding multiple AsyncGenericService is unsupported for now. "
Yang Gao6baa9b62015-03-17 10:49:39 -070075 "Dropping the service %p",
76 service);
Yang Gao1c402332015-03-05 16:39:25 -080077 return;
78 }
Yang Gao005eb882015-03-11 22:17:13 -070079 generic_service_ = service;
Yang Gao1c402332015-03-05 16:39:25 -080080}
81
Nicolas Noblecfd60732015-03-18 16:27:43 -070082void ServerBuilder::AddListeningPort(const grpc::string& addr,
83 std::shared_ptr<ServerCredentials> creds,
84 int* selected_port) {
Nicolas Noble30862032015-04-24 18:17:45 -070085 Port port = {addr, creds, selected_port};
86 ports_.push_back(port);
yangg9e21f722014-12-08 15:49:52 -080087}
88
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080089void ServerBuilder::SetThreadPool(ThreadPoolInterface* thread_pool) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090 thread_pool_ = thread_pool;
91}
92
93std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
Craig Tiller0db1bef2015-02-09 13:47:39 -080094 bool thread_pool_owned = false;
Craig Tiller40fcdaf2015-02-09 15:43:34 -080095 if (!async_services_.empty() && !services_.empty()) {
96 gpr_log(GPR_ERROR, "Mixing async and sync services is unsupported for now");
97 return nullptr;
98 }
Craig Tiller42bc87c2015-02-23 08:50:19 -080099 if (!thread_pool_ && !services_.empty()) {
Yang Gao6f4fb3b2015-06-03 12:56:19 -0700100 thread_pool_ = CreateDefaultThreadPool();
Craig Tiller0db1bef2015-02-09 13:47:39 -0800101 thread_pool_owned = true;
102 }
Yang Gao3921c562015-04-30 16:07:06 -0700103 std::unique_ptr<Server> server(
104 new Server(thread_pool_, thread_pool_owned, max_message_size_));
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700105 for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) {
106 grpc_server_register_completion_queue(server->server_, (*cq)->cq());
107 }
Vijay Pai82dd80a2015-03-24 10:36:08 -0700108 for (auto service = services_.begin(); service != services_.end();
109 service++) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -0700110 if (!server->RegisterService((*service)->host.get(), (*service)->service)) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800111 return nullptr;
112 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113 }
Vijay Pai82dd80a2015-03-24 10:36:08 -0700114 for (auto service = async_services_.begin();
115 service != async_services_.end(); service++) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -0700116 if (!server->RegisterAsyncService((*service)->host.get(), (*service)->service)) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800117 return nullptr;
118 }
119 }
Yang Gao005eb882015-03-11 22:17:13 -0700120 if (generic_service_) {
Yang Gao49996492015-03-12 16:40:19 -0700121 server->RegisterAsyncGenericService(generic_service_);
Yang Gao1c402332015-03-05 16:39:25 -0800122 }
Vijay Pai82dd80a2015-03-24 10:36:08 -0700123 for (auto port = ports_.begin(); port != ports_.end(); port++) {
124 int r = server->AddListeningPort(port->addr, port->creds.get());
Craig Tiller42bc87c2015-02-23 08:50:19 -0800125 if (!r) return nullptr;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700126 if (port->selected_port != nullptr) {
127 *port->selected_port = r;
Craig Tiller0db1bef2015-02-09 13:47:39 -0800128 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800129 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800130 if (!server->Start()) {
131 return nullptr;
132 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133 return server;
134}
135
Craig Tiller190d3602015-02-18 09:23:38 -0800136} // namespace grpc