blob: c5e115f3967de72fafe8a50060019de3f6f2cc1f [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 Gao005eb882015-03-11 22:17:13 -070045 : generic_service_(nullptr), thread_pool_(nullptr) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080047void ServerBuilder::RegisterService(SynchronousService* service) {
Craig Tiller14a65f92015-02-09 13:13:14 -080048 services_.push_back(service->service());
49}
50
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080051void ServerBuilder::RegisterAsyncService(AsynchronousService* service) {
Craig Tiller14a65f92015-02-09 13:13:14 -080052 async_services_.push_back(service);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053}
54
Yang Gao49996492015-03-12 16:40:19 -070055void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) {
Yang Gao005eb882015-03-11 22:17:13 -070056 if (generic_service_) {
Yang Gao1c402332015-03-05 16:39:25 -080057 gpr_log(GPR_ERROR,
Yang Gao49996492015-03-12 16:40:19 -070058 "Adding multiple AsyncGenericService is unsupported for now. "
Yang Gao6baa9b62015-03-17 10:49:39 -070059 "Dropping the service %p",
60 service);
Yang Gao1c402332015-03-05 16:39:25 -080061 return;
62 }
Yang Gao005eb882015-03-11 22:17:13 -070063 generic_service_ = service;
Yang Gao1c402332015-03-05 16:39:25 -080064}
65
Nicolas Noblecfd60732015-03-18 16:27:43 -070066void ServerBuilder::AddListeningPort(const grpc::string& addr,
67 std::shared_ptr<ServerCredentials> creds,
68 int* selected_port) {
Craig Tiller42bc87c2015-02-23 08:50:19 -080069 ports_.push_back(Port{addr, creds, selected_port});
yangg9e21f722014-12-08 15:49:52 -080070}
71
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080072void ServerBuilder::SetThreadPool(ThreadPoolInterface* thread_pool) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073 thread_pool_ = thread_pool;
74}
75
76std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
Craig Tiller0db1bef2015-02-09 13:47:39 -080077 bool thread_pool_owned = false;
Craig Tiller40fcdaf2015-02-09 15:43:34 -080078 if (!async_services_.empty() && !services_.empty()) {
79 gpr_log(GPR_ERROR, "Mixing async and sync services is unsupported for now");
80 return nullptr;
81 }
Craig Tiller42bc87c2015-02-23 08:50:19 -080082 if (!thread_pool_ && !services_.empty()) {
Craig Tiller0db1bef2015-02-09 13:47:39 -080083 int cores = gpr_cpu_num_cores();
84 if (!cores) cores = 4;
85 thread_pool_ = new ThreadPool(cores);
86 thread_pool_owned = true;
87 }
Craig Tiller42bc87c2015-02-23 08:50:19 -080088 std::unique_ptr<Server> server(new Server(thread_pool_, thread_pool_owned));
Vijay Pai82dd80a2015-03-24 10:36:08 -070089 for (auto service = services_.begin(); service != services_.end();
90 service++) {
91 if (!server->RegisterService(*service)) {
Craig Tiller0db1bef2015-02-09 13:47:39 -080092 return nullptr;
93 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080094 }
Vijay Pai82dd80a2015-03-24 10:36:08 -070095 for (auto service = async_services_.begin();
96 service != async_services_.end(); service++) {
97 if (!server->RegisterAsyncService(*service)) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080098 return nullptr;
99 }
100 }
Yang Gao005eb882015-03-11 22:17:13 -0700101 if (generic_service_) {
Yang Gao49996492015-03-12 16:40:19 -0700102 server->RegisterAsyncGenericService(generic_service_);
Yang Gao1c402332015-03-05 16:39:25 -0800103 }
Vijay Pai82dd80a2015-03-24 10:36:08 -0700104 for (auto port = ports_.begin(); port != ports_.end(); port++) {
105 int r = server->AddListeningPort(port->addr, port->creds.get());
Craig Tiller42bc87c2015-02-23 08:50:19 -0800106 if (!r) return nullptr;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700107 if (port->selected_port != nullptr) {
108 *port->selected_port = r;
Craig Tiller0db1bef2015-02-09 13:47:39 -0800109 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800111 if (!server->Start()) {
112 return nullptr;
113 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800114 return server;
115}
116
Craig Tiller190d3602015-02-18 09:23:38 -0800117} // namespace grpc