blob: 5966e548b09ce7ea893c22d34804d99429f0fc7e [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());
122 break;
123 }
124 }
yang-ga23f17b2015-11-25 10:21:05 -0800125 }
126 if (max_message_size_ > 0) {
127 args.SetInt(GRPC_ARG_MAX_MESSAGE_LENGTH, max_message_size_);
128 }
David Garcia Quintas183ba022016-05-12 17:08:19 -0700129 args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET,
yang-ga23f17b2015-11-25 10:21:05 -0800130 compression_options_.enabled_algorithms_bitset);
131 std::unique_ptr<Server> server(
yang-geb62c942016-02-17 15:37:25 -0800132 new Server(thread_pool.release(), true, max_message_size_, &args));
Yuchen Zenga42ec212016-04-29 13:03:06 -0700133 ServerInitializer* initializer = server->initializer();
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700134
135 // If the server has atleast one sync methods, we know that this is a Sync
136 // server or a Hybrid server and the completion queue (server->cq_) would be
137 // frequently polled.
Craig Tiller8a7fe1a2016-05-20 11:17:20 -0700138 int num_frequently_polled_cqs = has_sync_methods ? 1 : 0;
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700139
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700140 for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) {
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700141 // A completion queue that is not polled frequently (by calling Next() or
142 // AsyncNext()) is not safe to use for listening to incoming channels.
143 // Register all such completion queues as non-listening completion queues
144 // with the GRPC core library.
145 if ((*cq)->IsFrequentlyPolled()) {
146 grpc_server_register_completion_queue(server->server_, (*cq)->cq(),
147 nullptr);
Craig Tiller20431a82016-05-20 10:02:07 -0700148 num_frequently_polled_cqs++;
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700149 } else {
150 grpc_server_register_non_listening_completion_queue(server->server_,
151 (*cq)->cq(), nullptr);
152 }
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700153 }
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700154
155 if (num_frequently_polled_cqs == 0) {
156 gpr_log(GPR_ERROR,
Craig Tilleraae6c2c2016-05-20 08:58:30 -0700157 "At least one of the completion queues must be frequently polled");
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700158 return nullptr;
159 }
160
Vijay Pai82dd80a2015-03-24 10:36:08 -0700161 for (auto service = services_.begin(); service != services_.end();
162 service++) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -0700163 if (!server->RegisterService((*service)->host.get(), (*service)->service)) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800164 return nullptr;
165 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800166 }
Yuchen Zenga42ec212016-04-29 13:03:06 -0700167 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
168 (*plugin).second->InitServer(initializer);
169 }
Yang Gao005eb882015-03-11 22:17:13 -0700170 if (generic_service_) {
Yang Gao49996492015-03-12 16:40:19 -0700171 server->RegisterAsyncGenericService(generic_service_);
yang-g1ac6f452016-01-15 11:46:40 -0800172 } else {
173 for (auto it = services_.begin(); it != services_.end(); ++it) {
174 if ((*it)->service->has_generic_methods()) {
175 gpr_log(GPR_ERROR,
176 "Some methods were marked generic but there is no "
177 "generic service registered.");
178 return nullptr;
179 }
180 }
Yang Gao1c402332015-03-05 16:39:25 -0800181 }
Vijay Pai82dd80a2015-03-24 10:36:08 -0700182 for (auto port = ports_.begin(); port != ports_.end(); port++) {
183 int r = server->AddListeningPort(port->addr, port->creds.get());
Craig Tiller42bc87c2015-02-23 08:50:19 -0800184 if (!r) return nullptr;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700185 if (port->selected_port != nullptr) {
186 *port->selected_port = r;
Craig Tiller0db1bef2015-02-09 13:47:39 -0800187 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188 }
yang-g61e461e2015-09-03 13:08:59 -0700189 auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0];
190 if (!server->Start(cqs_data, cqs_.size())) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800191 return nullptr;
192 }
Yuchen Zenga42ec212016-04-29 13:03:06 -0700193 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
194 (*plugin).second->Finish(initializer);
195 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800196 return server;
197}
198
Yuchen Zeng3b8f3352016-05-03 12:18:13 -0700199void ServerBuilder::InternalAddPluginFactory(
200 std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()) {
201 gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
Yuchen Zeng7d099a52016-05-06 13:21:36 -0700202 (*g_plugin_factory_list).push_back(CreatePlugin);
Yuchen Zeng3b8f3352016-05-03 12:18:13 -0700203}
204
Craig Tiller190d3602015-02-18 09:23:38 -0800205} // namespace grpc