Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Sree Kuchibhotla | 0190712 | 2016-04-21 15:09:13 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 36 | #include <grpc++/impl/service_type.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <grpc++/server.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 38 | #include <grpc/support/cpu.h> |
| 39 | #include <grpc/support/log.h> |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 40 | #include "src/cpp/server/thread_pool_interface.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | |
| 42 | namespace grpc { |
| 43 | |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 44 | static std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>* |
| 45 | g_plugin_factory_list; |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 46 | static gpr_once once_init_plugin_list = GPR_ONCE_INIT; |
| 47 | |
| 48 | static void do_plugin_list_init(void) { |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 49 | g_plugin_factory_list = |
| 50 | new std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>(); |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 53 | ServerBuilder::ServerBuilder() |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 54 | : max_message_size_(-1), generic_service_(nullptr) { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 55 | grpc_compression_options_init(&compression_options_); |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 56 | gpr_once_init(&once_init_plugin_list, do_plugin_list_init); |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 57 | for (auto factory : (*g_plugin_factory_list)) { |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 58 | std::unique_ptr<ServerBuilderPlugin> plugin = factory(); |
| 59 | plugins_[plugin->name()] = std::move(plugin); |
| 60 | } |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 61 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | |
Sree Kuchibhotla | 1f5e262 | 2016-04-21 12:28:09 -0700 | [diff] [blame] | 63 | std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue( |
| 64 | bool is_frequently_polled) { |
| 65 | ServerCompletionQueue* cq = new ServerCompletionQueue(is_frequently_polled); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 66 | cqs_.push_back(cq); |
| 67 | return std::unique_ptr<ServerCompletionQueue>(cq); |
| 68 | } |
| 69 | |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 70 | void ServerBuilder::RegisterService(Service* service) { |
| 71 | services_.emplace_back(new NamedService(service)); |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 74 | void ServerBuilder::RegisterService(const grpc::string& addr, |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 75 | Service* service) { |
| 76 | services_.emplace_back(new NamedService(addr, service)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 79 | void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) { |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 80 | if (generic_service_) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 81 | gpr_log(GPR_ERROR, |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 82 | "Adding multiple AsyncGenericService is unsupported for now. " |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 83 | "Dropping the service %p", |
| 84 | service); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 85 | return; |
| 86 | } |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 87 | generic_service_ = service; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 88 | } |
| 89 | |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 90 | void ServerBuilder::SetOption(std::unique_ptr<ServerBuilderOption> option) { |
| 91 | options_.push_back(std::move(option)); |
| 92 | } |
| 93 | |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 94 | void ServerBuilder::AddListeningPort(const grpc::string& addr, |
| 95 | std::shared_ptr<ServerCredentials> creds, |
| 96 | int* selected_port) { |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 97 | Port port = {addr, creds, selected_port}; |
| 98 | ports_.push_back(port); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | std::unique_ptr<Server> ServerBuilder::BuildAndStart() { |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 102 | std::unique_ptr<ThreadPoolInterface> thread_pool; |
Craig Tiller | 8a7fe1a | 2016-05-20 11:17:20 -0700 | [diff] [blame^] | 103 | bool has_sync_methods = false; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 104 | for (auto it = services_.begin(); it != services_.end(); ++it) { |
| 105 | if ((*it)->service->has_synchronous_methods()) { |
yang-g | bef0d87 | 2016-01-13 15:27:33 -0800 | [diff] [blame] | 106 | if (thread_pool == nullptr) { |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 107 | thread_pool.reset(CreateDefaultThreadPool()); |
Craig Tiller | 8a7fe1a | 2016-05-20 11:17:20 -0700 | [diff] [blame^] | 108 | has_sync_methods = true; |
yang-g | bef0d87 | 2016-01-13 15:27:33 -0800 | [diff] [blame] | 109 | break; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 112 | } |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 113 | ChannelArguments args; |
| 114 | for (auto option = options_.begin(); option != options_.end(); ++option) { |
| 115 | (*option)->UpdateArguments(&args); |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 116 | (*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-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 125 | } |
| 126 | if (max_message_size_ > 0) { |
| 127 | args.SetInt(GRPC_ARG_MAX_MESSAGE_LENGTH, max_message_size_); |
| 128 | } |
David Garcia Quintas | 183ba02 | 2016-05-12 17:08:19 -0700 | [diff] [blame] | 129 | args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET, |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 130 | compression_options_.enabled_algorithms_bitset); |
| 131 | std::unique_ptr<Server> server( |
yang-g | eb62c94 | 2016-02-17 15:37:25 -0800 | [diff] [blame] | 132 | new Server(thread_pool.release(), true, max_message_size_, &args)); |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 133 | ServerInitializer* initializer = server->initializer(); |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 134 | |
| 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 Tiller | 8a7fe1a | 2016-05-20 11:17:20 -0700 | [diff] [blame^] | 138 | int num_frequently_polled_cqs = has_sync_methods ? 1 : 0; |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 139 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 140 | for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) { |
Sree Kuchibhotla | 1f5e262 | 2016-04-21 12:28:09 -0700 | [diff] [blame] | 141 | // 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 Tiller | 20431a8 | 2016-05-20 10:02:07 -0700 | [diff] [blame] | 148 | num_frequently_polled_cqs++; |
Sree Kuchibhotla | 1f5e262 | 2016-04-21 12:28:09 -0700 | [diff] [blame] | 149 | } else { |
| 150 | grpc_server_register_non_listening_completion_queue(server->server_, |
| 151 | (*cq)->cq(), nullptr); |
| 152 | } |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 153 | } |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 154 | |
| 155 | if (num_frequently_polled_cqs == 0) { |
| 156 | gpr_log(GPR_ERROR, |
Craig Tiller | aae6c2c | 2016-05-20 08:58:30 -0700 | [diff] [blame] | 157 | "At least one of the completion queues must be frequently polled"); |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 158 | return nullptr; |
| 159 | } |
| 160 | |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 161 | for (auto service = services_.begin(); service != services_.end(); |
| 162 | service++) { |
Craig Tiller | d9b6fcf | 2015-07-07 16:28:20 -0700 | [diff] [blame] | 163 | if (!server->RegisterService((*service)->host.get(), (*service)->service)) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 164 | return nullptr; |
| 165 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | } |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 167 | for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) { |
| 168 | (*plugin).second->InitServer(initializer); |
| 169 | } |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 170 | if (generic_service_) { |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 171 | server->RegisterAsyncGenericService(generic_service_); |
yang-g | 1ac6f45 | 2016-01-15 11:46:40 -0800 | [diff] [blame] | 172 | } 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 Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 181 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 182 | for (auto port = ports_.begin(); port != ports_.end(); port++) { |
| 183 | int r = server->AddListeningPort(port->addr, port->creds.get()); |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 184 | if (!r) return nullptr; |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 185 | if (port->selected_port != nullptr) { |
| 186 | *port->selected_port = r; |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 187 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 188 | } |
yang-g | 61e461e | 2015-09-03 13:08:59 -0700 | [diff] [blame] | 189 | auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0]; |
| 190 | if (!server->Start(cqs_data, cqs_.size())) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 191 | return nullptr; |
| 192 | } |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 193 | for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) { |
| 194 | (*plugin).second->Finish(initializer); |
| 195 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 196 | return server; |
| 197 | } |
| 198 | |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 199 | void ServerBuilder::InternalAddPluginFactory( |
| 200 | std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()) { |
| 201 | gpr_once_init(&once_init_plugin_list, do_plugin_list_init); |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 202 | (*g_plugin_factory_list).push_back(CreatePlugin); |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 205 | } // namespace grpc |