blob: 083509e797c9539c206b8ee46128d2fa228900fd [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 Gao005eb882015-03-11 22:17:13 -070055void ServerBuilder::RegisterGenericService(GenericService* service) {
56 if (generic_service_) {
Yang Gao1c402332015-03-05 16:39:25 -080057 gpr_log(GPR_ERROR,
Yang Gao005eb882015-03-11 22:17:13 -070058 "Adding multiple GenericService is unsupported for now. "
Yang Gao1c402332015-03-05 16:39:25 -080059 "Dropping the service %p", service);
60 return;
61 }
Yang Gao005eb882015-03-11 22:17:13 -070062 generic_service_ = service;
Yang Gao1c402332015-03-05 16:39:25 -080063}
64
Craig Tiller42bc87c2015-02-23 08:50:19 -080065void ServerBuilder::AddPort(const grpc::string& addr,
66 std::shared_ptr<ServerCredentials> creds,
67 int* selected_port) {
68 ports_.push_back(Port{addr, creds, selected_port});
yangg9e21f722014-12-08 15:49:52 -080069}
70
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080071void ServerBuilder::SetThreadPool(ThreadPoolInterface* thread_pool) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072 thread_pool_ = thread_pool;
73}
74
75std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
Craig Tiller0db1bef2015-02-09 13:47:39 -080076 bool thread_pool_owned = false;
Craig Tiller40fcdaf2015-02-09 15:43:34 -080077 if (!async_services_.empty() && !services_.empty()) {
78 gpr_log(GPR_ERROR, "Mixing async and sync services is unsupported for now");
79 return nullptr;
80 }
Craig Tiller42bc87c2015-02-23 08:50:19 -080081 if (!thread_pool_ && !services_.empty()) {
Craig Tiller0db1bef2015-02-09 13:47:39 -080082 int cores = gpr_cpu_num_cores();
83 if (!cores) cores = 4;
84 thread_pool_ = new ThreadPool(cores);
85 thread_pool_owned = true;
86 }
Craig Tiller42bc87c2015-02-23 08:50:19 -080087 std::unique_ptr<Server> server(new Server(thread_pool_, thread_pool_owned));
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080088 for (auto* service : services_) {
Craig Tiller0db1bef2015-02-09 13:47:39 -080089 if (!server->RegisterService(service)) {
90 return nullptr;
91 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092 }
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080093 for (auto* service : async_services_) {
94 if (!server->RegisterAsyncService(service)) {
95 return nullptr;
96 }
97 }
Yang Gao005eb882015-03-11 22:17:13 -070098 if (generic_service_) {
99 server->RegisterGenericService(generic_service_);
Yang Gao1c402332015-03-05 16:39:25 -0800100 }
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800101 for (auto& port : ports_) {
Craig Tiller42bc87c2015-02-23 08:50:19 -0800102 int r = server->AddPort(port.addr, port.creds.get());
103 if (!r) return nullptr;
104 if (port.selected_port != nullptr) {
105 *port.selected_port = r;
Craig Tiller0db1bef2015-02-09 13:47:39 -0800106 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800107 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800108 if (!server->Start()) {
109 return nullptr;
110 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111 return server;
112}
113
Craig Tiller190d3602015-02-18 09:23:38 -0800114} // namespace grpc