blob: 54feac398253b2569de3f17a51d3ae36acdf1c27 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Sree Kuchibhotla01907122016-04-21 15:09:13 -07003 * Copyright 2015-2016, 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 Tiller14a65f92015-02-09 13:13:14 -080036#include <grpc++/impl/service_type.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#include <grpc++/server.h>
Craig Tillerf40df232016-03-25 13:38:14 -070038#include <grpc/support/cpu.h>
39#include <grpc/support/log.h>
Vijay Paie8a7e302015-08-24 10:52:33 -070040#include "src/cpp/server/thread_pool_interface.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041
42namespace grpc {
43
Yuchen Zeng7d099a52016-05-06 13:21:36 -070044static std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>*
45 g_plugin_factory_list;
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070046static gpr_once once_init_plugin_list = GPR_ONCE_INIT;
47
48static void do_plugin_list_init(void) {
Yuchen Zeng7d099a52016-05-06 13:21:36 -070049 g_plugin_factory_list =
50 new std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>();
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070051}
52
Yang Gao5f4539f2015-03-06 16:11:16 -080053ServerBuilder::ServerBuilder()
Craig Tiller15f383c2016-01-07 12:45:32 -080054 : max_message_size_(-1), generic_service_(nullptr) {
Craig Tiller71a0f9d2015-09-28 17:22:01 -070055 grpc_compression_options_init(&compression_options_);
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070056 gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
Yuchen Zeng7d099a52016-05-06 13:21:36 -070057 for (auto factory : (*g_plugin_factory_list)) {
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070058 std::unique_ptr<ServerBuilderPlugin> plugin = factory();
59 plugins_[plugin->name()] = std::move(plugin);
60 }
David Garcia Quintasbeac88c2015-08-10 13:39:52 -070061}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -070063std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue(
64 bool is_frequently_polled) {
65 ServerCompletionQueue* cq = new ServerCompletionQueue(is_frequently_polled);
Craig Tillerf9e6adf2015-05-06 11:45:59 -070066 cqs_.push_back(cq);
67 return std::unique_ptr<ServerCompletionQueue>(cq);
68}
69
Craig Tiller15f383c2016-01-07 12:45:32 -080070void ServerBuilder::RegisterService(Service* service) {
71 services_.emplace_back(new NamedService(service));
Craig Tiller822d2c72015-07-07 16:08:00 -070072}
73
Craig Tillerd6c98df2015-08-18 09:33:44 -070074void ServerBuilder::RegisterService(const grpc::string& addr,
Craig Tiller15f383c2016-01-07 12:45:32 -080075 Service* service) {
76 services_.emplace_back(new NamedService(addr, service));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080077}
78
Yang Gao49996492015-03-12 16:40:19 -070079void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) {
Yang Gao005eb882015-03-11 22:17:13 -070080 if (generic_service_) {
Yang Gao1c402332015-03-05 16:39:25 -080081 gpr_log(GPR_ERROR,
Yang Gao49996492015-03-12 16:40:19 -070082 "Adding multiple AsyncGenericService is unsupported for now. "
Yang Gao6baa9b62015-03-17 10:49:39 -070083 "Dropping the service %p",
84 service);
Yang Gao1c402332015-03-05 16:39:25 -080085 return;
86 }
Yang Gao005eb882015-03-11 22:17:13 -070087 generic_service_ = service;
Yang Gao1c402332015-03-05 16:39:25 -080088}
89
yang-ga23f17b2015-11-25 10:21:05 -080090void ServerBuilder::SetOption(std::unique_ptr<ServerBuilderOption> option) {
91 options_.push_back(std::move(option));
92}
93
Nicolas Noblecfd60732015-03-18 16:27:43 -070094void ServerBuilder::AddListeningPort(const grpc::string& addr,
95 std::shared_ptr<ServerCredentials> creds,
96 int* selected_port) {
Nicolas Noble30862032015-04-24 18:17:45 -070097 Port port = {addr, creds, selected_port};
98 ports_.push_back(port);
yangg9e21f722014-12-08 15:49:52 -080099}
100
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
Craig Tiller15f383c2016-01-07 12:45:32 -0800102 std::unique_ptr<ThreadPoolInterface> thread_pool;
Craig Tiller8a7fe1a2016-05-20 11:17:20 -0700103 bool has_sync_methods = false;
Craig Tiller15f383c2016-01-07 12:45:32 -0800104 for (auto it = services_.begin(); it != services_.end(); ++it) {
105 if ((*it)->service->has_synchronous_methods()) {
yang-gbef0d872016-01-13 15:27:33 -0800106 if (thread_pool == nullptr) {
Craig Tiller15f383c2016-01-07 12:45:32 -0800107 thread_pool.reset(CreateDefaultThreadPool());
Craig Tiller8a7fe1a2016-05-20 11:17:20 -0700108 has_sync_methods = true;
yang-gbef0d872016-01-13 15:27:33 -0800109 break;
Craig Tiller15f383c2016-01-07 12:45:32 -0800110 }
111 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800112 }
yang-ga23f17b2015-11-25 10:21:05 -0800113 ChannelArguments args;
114 for (auto option = options_.begin(); option != options_.end(); ++option) {
115 (*option)->UpdateArguments(&args);
Yuchen Zenga42ec212016-04-29 13:03:06 -0700116 (*option)->UpdatePlugins(&plugins_);
117 }
118 if (thread_pool == nullptr) {
119 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
120 if ((*plugin).second->has_sync_methods()) {
121 thread_pool.reset(CreateDefaultThreadPool());
Craig Tillerfa96d862016-05-21 12:39:56 -0700122 has_sync_methods = true;
Yuchen Zenga42ec212016-04-29 13:03:06 -0700123 break;
124 }
125 }
yang-ga23f17b2015-11-25 10:21:05 -0800126 }
127 if (max_message_size_ > 0) {
128 args.SetInt(GRPC_ARG_MAX_MESSAGE_LENGTH, max_message_size_);
129 }
David Garcia Quintas183ba022016-05-12 17:08:19 -0700130 args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET,
yang-ga23f17b2015-11-25 10:21:05 -0800131 compression_options_.enabled_algorithms_bitset);
132 std::unique_ptr<Server> server(
yang-geb62c942016-02-17 15:37:25 -0800133 new Server(thread_pool.release(), true, max_message_size_, &args));
Yuchen Zenga42ec212016-04-29 13:03:06 -0700134 ServerInitializer* initializer = server->initializer();
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700135
136 // If the server has atleast one sync methods, we know that this is a Sync
137 // server or a Hybrid server and the completion queue (server->cq_) would be
138 // frequently polled.
Craig Tiller8a7fe1a2016-05-20 11:17:20 -0700139 int num_frequently_polled_cqs = has_sync_methods ? 1 : 0;
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700140
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700141 for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) {
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700142 // A completion queue that is not polled frequently (by calling Next() or
143 // AsyncNext()) is not safe to use for listening to incoming channels.
144 // Register all such completion queues as non-listening completion queues
145 // with the GRPC core library.
146 if ((*cq)->IsFrequentlyPolled()) {
147 grpc_server_register_completion_queue(server->server_, (*cq)->cq(),
148 nullptr);
Craig Tiller20431a82016-05-20 10:02:07 -0700149 num_frequently_polled_cqs++;
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700150 } else {
151 grpc_server_register_non_listening_completion_queue(server->server_,
152 (*cq)->cq(), nullptr);
153 }
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700154 }
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700155
156 if (num_frequently_polled_cqs == 0) {
157 gpr_log(GPR_ERROR,
Craig Tilleraae6c2c2016-05-20 08:58:30 -0700158 "At least one of the completion queues must be frequently polled");
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700159 return nullptr;
160 }
161
Vijay Pai82dd80a2015-03-24 10:36:08 -0700162 for (auto service = services_.begin(); service != services_.end();
163 service++) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -0700164 if (!server->RegisterService((*service)->host.get(), (*service)->service)) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800165 return nullptr;
166 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800167 }
Yuchen Zenga42ec212016-04-29 13:03:06 -0700168 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
169 (*plugin).second->InitServer(initializer);
170 }
Yang Gao005eb882015-03-11 22:17:13 -0700171 if (generic_service_) {
Yang Gao49996492015-03-12 16:40:19 -0700172 server->RegisterAsyncGenericService(generic_service_);
yang-g1ac6f452016-01-15 11:46:40 -0800173 } else {
174 for (auto it = services_.begin(); it != services_.end(); ++it) {
175 if ((*it)->service->has_generic_methods()) {
176 gpr_log(GPR_ERROR,
177 "Some methods were marked generic but there is no "
178 "generic service registered.");
179 return nullptr;
180 }
181 }
Yang Gao1c402332015-03-05 16:39:25 -0800182 }
Vijay Pai82dd80a2015-03-24 10:36:08 -0700183 for (auto port = ports_.begin(); port != ports_.end(); port++) {
184 int r = server->AddListeningPort(port->addr, port->creds.get());
Craig Tiller42bc87c2015-02-23 08:50:19 -0800185 if (!r) return nullptr;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700186 if (port->selected_port != nullptr) {
187 *port->selected_port = r;
Craig Tiller0db1bef2015-02-09 13:47:39 -0800188 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800189 }
yang-g61e461e2015-09-03 13:08:59 -0700190 auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0];
191 if (!server->Start(cqs_data, cqs_.size())) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800192 return nullptr;
193 }
Yuchen Zenga42ec212016-04-29 13:03:06 -0700194 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
195 (*plugin).second->Finish(initializer);
196 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800197 return server;
198}
199
Yuchen Zeng3b8f3352016-05-03 12:18:13 -0700200void ServerBuilder::InternalAddPluginFactory(
201 std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()) {
202 gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
Yuchen Zeng7d099a52016-05-06 13:21:36 -0700203 (*g_plugin_factory_list).push_back(CreatePlugin);
Yuchen Zeng3b8f3352016-05-03 12:18:13 -0700204}
205
Craig Tiller190d3602015-02-18 09:23:38 -0800206} // namespace grpc