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> |
David Garcia Quintas | 468e16d | 2016-08-31 13:29:40 +0200 | [diff] [blame] | 40 | #include <grpc/support/useful.h> |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 41 | |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 42 | #include "src/cpp/server/thread_pool_interface.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | |
| 44 | namespace grpc { |
| 45 | |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 46 | static std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>* |
| 47 | g_plugin_factory_list; |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 48 | static gpr_once once_init_plugin_list = GPR_ONCE_INIT; |
| 49 | |
| 50 | static void do_plugin_list_init(void) { |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 51 | g_plugin_factory_list = |
| 52 | new std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>(); |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 55 | ServerBuilder::ServerBuilder() |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 56 | : max_receive_message_size_(-1), |
| 57 | max_send_message_size_(-1), |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 58 | sync_server_settings_(SyncServerSettings()), |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 59 | generic_service_(nullptr) { |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 60 | gpr_once_init(&once_init_plugin_list, do_plugin_list_init); |
Vijay Pai | eac07c3 | 2016-06-10 01:36:53 -0700 | [diff] [blame] | 61 | for (auto it = g_plugin_factory_list->begin(); |
| 62 | it != g_plugin_factory_list->end(); it++) { |
Vijay Pai | b645a2d | 2016-06-09 18:39:06 -0700 | [diff] [blame] | 63 | auto& factory = *it; |
Vijay Pai | 15855f3 | 2016-06-10 12:25:32 -0700 | [diff] [blame] | 64 | plugins_.emplace_back(factory()); |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 65 | } |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 66 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 67 | // all compression algorithms enabled by default. |
| 68 | enabled_compression_algorithms_bitset_ = |
| 69 | (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; |
| 70 | memset(&maybe_default_compression_level_, 0, |
| 71 | sizeof(maybe_default_compression_level_)); |
| 72 | memset(&maybe_default_compression_algorithm_, 0, |
| 73 | sizeof(maybe_default_compression_algorithm_)); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 74 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | |
Sree Kuchibhotla | 1f5e262 | 2016-04-21 12:28:09 -0700 | [diff] [blame] | 76 | std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue( |
| 77 | bool is_frequently_polled) { |
| 78 | ServerCompletionQueue* cq = new ServerCompletionQueue(is_frequently_polled); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 79 | cqs_.push_back(cq); |
| 80 | return std::unique_ptr<ServerCompletionQueue>(cq); |
| 81 | } |
| 82 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 83 | ServerBuilder& ServerBuilder::RegisterService(Service* service) { |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 84 | services_.emplace_back(new NamedService(service)); |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 85 | return *this; |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 86 | } |
| 87 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 88 | ServerBuilder& ServerBuilder::RegisterService(const grpc::string& addr, |
| 89 | Service* service) { |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 90 | services_.emplace_back(new NamedService(addr, service)); |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 91 | return *this; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 92 | } |
| 93 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 94 | ServerBuilder& ServerBuilder::RegisterAsyncGenericService( |
| 95 | AsyncGenericService* service) { |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 96 | if (generic_service_) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 97 | gpr_log(GPR_ERROR, |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 98 | "Adding multiple AsyncGenericService is unsupported for now. " |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 99 | "Dropping the service %p", |
Sree Kuchibhotla | 4028d2c | 2016-09-21 10:45:33 -0700 | [diff] [blame] | 100 | (void*)service); |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 101 | } else { |
| 102 | generic_service_ = service; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 103 | } |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 104 | return *this; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 105 | } |
| 106 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 107 | ServerBuilder& ServerBuilder::SetOption( |
| 108 | std::unique_ptr<ServerBuilderOption> option) { |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 109 | options_.push_back(std::move(option)); |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 110 | return *this; |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 113 | ServerBuilder& ServerBuilder::SetSyncServerOption( |
| 114 | ServerBuilder::SyncServerOption option, int val) { |
| 115 | switch (option) { |
| 116 | case NUM_CQS: |
| 117 | sync_server_settings_.num_cqs = val; |
| 118 | break; |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 119 | case MIN_POLLERS: |
| 120 | sync_server_settings_.min_pollers = val; |
| 121 | break; |
| 122 | case MAX_POLLERS: |
| 123 | sync_server_settings_.max_pollers = val; |
| 124 | break; |
| 125 | case CQ_TIMEOUT_MSEC: |
| 126 | sync_server_settings_.cq_timeout_msec = val; |
| 127 | break; |
| 128 | } |
| 129 | return *this; |
| 130 | } |
| 131 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 132 | ServerBuilder& ServerBuilder::SetCompressionAlgorithmSupportStatus( |
| 133 | grpc_compression_algorithm algorithm, bool enabled) { |
| 134 | if (enabled) { |
| 135 | GPR_BITSET(&enabled_compression_algorithms_bitset_, algorithm); |
| 136 | } else { |
| 137 | GPR_BITCLEAR(&enabled_compression_algorithms_bitset_, algorithm); |
| 138 | } |
| 139 | return *this; |
| 140 | } |
| 141 | |
| 142 | ServerBuilder& ServerBuilder::SetDefaultCompressionLevel( |
| 143 | grpc_compression_level level) { |
| 144 | maybe_default_compression_level_.level = level; |
| 145 | return *this; |
| 146 | } |
| 147 | |
| 148 | ServerBuilder& ServerBuilder::SetDefaultCompressionAlgorithm( |
| 149 | grpc_compression_algorithm algorithm) { |
| 150 | maybe_default_compression_algorithm_.is_set = true; |
| 151 | maybe_default_compression_algorithm_.algorithm = algorithm; |
| 152 | return *this; |
| 153 | } |
| 154 | |
| 155 | ServerBuilder& ServerBuilder::AddListeningPort( |
| 156 | const grpc::string& addr, std::shared_ptr<ServerCredentials> creds, |
| 157 | int* selected_port) { |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 158 | Port port = {addr, creds, selected_port}; |
| 159 | ports_.push_back(port); |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 160 | return *this; |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 163 | std::unique_ptr<Server> ServerBuilder::BuildAndStart() { |
Sree Kuchibhotla | 2d08f5b | 2016-09-26 16:11:02 -0700 | [diff] [blame] | 164 | ChannelArguments args; |
| 165 | for (auto option = options_.begin(); option != options_.end(); ++option) { |
| 166 | (*option)->UpdateArguments(&args); |
| 167 | (*option)->UpdatePlugins(&plugins_); |
| 168 | } |
| 169 | |
| 170 | for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) { |
| 171 | (*plugin)->UpdateChannelArguments(&args); |
| 172 | } |
| 173 | |
| 174 | if (max_receive_message_size_ >= 0) { |
| 175 | args.SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, max_receive_message_size_); |
| 176 | } |
| 177 | |
| 178 | if (max_send_message_size_ >= 0) { |
| 179 | args.SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, max_send_message_size_); |
| 180 | } |
| 181 | |
| 182 | args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET, |
| 183 | enabled_compression_algorithms_bitset_); |
| 184 | if (maybe_default_compression_level_.is_set) { |
| 185 | args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL, |
| 186 | maybe_default_compression_level_.level); |
| 187 | } |
| 188 | if (maybe_default_compression_algorithm_.is_set) { |
| 189 | args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, |
| 190 | maybe_default_compression_algorithm_.algorithm); |
| 191 | } |
| 192 | |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 193 | // == Determine if the server has any syncrhonous methods == |
Craig Tiller | 8a7fe1a | 2016-05-20 11:17:20 -0700 | [diff] [blame] | 194 | bool has_sync_methods = false; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 195 | for (auto it = services_.begin(); it != services_.end(); ++it) { |
| 196 | if ((*it)->service->has_synchronous_methods()) { |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 197 | has_sync_methods = true; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (!has_sync_methods) { |
| 203 | for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) { |
| 204 | if ((*plugin)->has_sync_methods()) { |
Craig Tiller | 8a7fe1a | 2016-05-20 11:17:20 -0700 | [diff] [blame] | 205 | has_sync_methods = true; |
yang-g | bef0d87 | 2016-01-13 15:27:33 -0800 | [diff] [blame] | 206 | break; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 209 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 210 | |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 211 | // If this is a Sync server, i.e a server expositing sync API, then the server |
| 212 | // needs to create some completion queues to listen for incoming requests. |
| 213 | // 'sync_server_cqs' are those internal completion queues. |
| 214 | // |
| 215 | // This is different from the completion queues added to the server via |
| 216 | // ServerBuilder's AddCompletionQueue() method (those completion queues |
| 217 | // are in 'cqs_' member variable of ServerBuilder object) |
Sree Kuchibhotla | 4028d2c | 2016-09-21 10:45:33 -0700 | [diff] [blame] | 218 | std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>> |
Sree Kuchibhotla | 6135535 | 2016-10-20 11:17:22 -0700 | [diff] [blame^] | 219 | sync_server_cqs(std::make_shared< |
| 220 | std::vector<std::unique_ptr<ServerCompletionQueue>>>()); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 221 | |
| 222 | if (has_sync_methods) { |
Sree Kuchibhotla | a7a21d2 | 2016-09-28 12:38:01 -0700 | [diff] [blame] | 223 | // This is a Sync server |
| 224 | gpr_log(GPR_INFO, |
| 225 | "Synchronous server. Num CQs: %d, Min pollers: %d, Max Pollers: " |
| 226 | "%d, CQ timeout (msec): %d", |
| 227 | sync_server_settings_.num_cqs, sync_server_settings_.min_pollers, |
| 228 | sync_server_settings_.max_pollers, |
| 229 | sync_server_settings_.cq_timeout_msec); |
| 230 | |
| 231 | // Create completion queues to listen to incoming rpc requests |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 232 | for (int i = 0; i < sync_server_settings_.num_cqs; i++) { |
Sree Kuchibhotla | 4028d2c | 2016-09-21 10:45:33 -0700 | [diff] [blame] | 233 | sync_server_cqs->emplace_back(new ServerCompletionQueue()); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 237 | std::unique_ptr<Server> server(new Server( |
Sree Kuchibhotla | e4eb51f | 2016-10-18 11:51:28 -0700 | [diff] [blame] | 238 | max_receive_message_size_, &args, sync_server_cqs, |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 239 | sync_server_settings_.min_pollers, sync_server_settings_.max_pollers, |
| 240 | sync_server_settings_.cq_timeout_msec)); |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 241 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 242 | ServerInitializer* initializer = server->initializer(); |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 243 | |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 244 | // Register all the completion queues with the server. i.e |
| 245 | // 1. sync_server_cqs: internal completion queues created IF this is a sync |
| 246 | // server |
| 247 | // 2. cqs_: Completion queues added via AddCompletionQueue() call |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 248 | |
Sree Kuchibhotla | 8f7739b | 2016-10-13 15:12:55 -0700 | [diff] [blame] | 249 | // All sync cqs (if any) are frequently polled by ThreadManager |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 250 | int num_frequently_polled_cqs = sync_server_cqs->size(); |
| 251 | |
| 252 | for (auto it = sync_server_cqs->begin(); it != sync_server_cqs->end(); ++it) { |
Sree Kuchibhotla | 4028d2c | 2016-09-21 10:45:33 -0700 | [diff] [blame] | 253 | grpc_server_register_completion_queue(server->server_, (*it)->cq(), |
| 254 | nullptr); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // cqs_ contains the completion queue added by calling the ServerBuilder's |
| 258 | // AddCompletionQueue() API. Some of them may not be frequently polled (i.e by |
| 259 | // calling Next() or AsyncNext()) and hence are not safe to be used for |
| 260 | // listening to incoming channels. Such completion queues must be registered |
| 261 | // as non-listening queues |
| 262 | for (auto it = cqs_.begin(); it != cqs_.end(); ++it) { |
| 263 | if ((*it)->IsFrequentlyPolled()) { |
| 264 | grpc_server_register_completion_queue(server->server_, (*it)->cq(), |
Sree Kuchibhotla | 1f5e262 | 2016-04-21 12:28:09 -0700 | [diff] [blame] | 265 | nullptr); |
Craig Tiller | 20431a8 | 2016-05-20 10:02:07 -0700 | [diff] [blame] | 266 | num_frequently_polled_cqs++; |
Sree Kuchibhotla | 1f5e262 | 2016-04-21 12:28:09 -0700 | [diff] [blame] | 267 | } else { |
| 268 | grpc_server_register_non_listening_completion_queue(server->server_, |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 269 | (*it)->cq(), nullptr); |
Sree Kuchibhotla | 1f5e262 | 2016-04-21 12:28:09 -0700 | [diff] [blame] | 270 | } |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 271 | } |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 272 | |
| 273 | if (num_frequently_polled_cqs == 0) { |
| 274 | gpr_log(GPR_ERROR, |
Craig Tiller | aae6c2c | 2016-05-20 08:58:30 -0700 | [diff] [blame] | 275 | "At least one of the completion queues must be frequently polled"); |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 276 | return nullptr; |
| 277 | } |
| 278 | |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 279 | for (auto service = services_.begin(); service != services_.end(); |
| 280 | service++) { |
Craig Tiller | d9b6fcf | 2015-07-07 16:28:20 -0700 | [diff] [blame] | 281 | if (!server->RegisterService((*service)->host.get(), (*service)->service)) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 282 | return nullptr; |
| 283 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 284 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 285 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 286 | for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) { |
Vijay Pai | 15855f3 | 2016-06-10 12:25:32 -0700 | [diff] [blame] | 287 | (*plugin)->InitServer(initializer); |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 288 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 289 | |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 290 | if (generic_service_) { |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 291 | server->RegisterAsyncGenericService(generic_service_); |
yang-g | 1ac6f45 | 2016-01-15 11:46:40 -0800 | [diff] [blame] | 292 | } else { |
| 293 | for (auto it = services_.begin(); it != services_.end(); ++it) { |
| 294 | if ((*it)->service->has_generic_methods()) { |
| 295 | gpr_log(GPR_ERROR, |
| 296 | "Some methods were marked generic but there is no " |
| 297 | "generic service registered."); |
| 298 | return nullptr; |
| 299 | } |
| 300 | } |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 301 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 302 | |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 303 | for (auto port = ports_.begin(); port != ports_.end(); port++) { |
| 304 | int r = server->AddListeningPort(port->addr, port->creds.get()); |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 305 | if (!r) return nullptr; |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 306 | if (port->selected_port != nullptr) { |
| 307 | *port->selected_port = r; |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 308 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 309 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 310 | |
yang-g | 61e461e | 2015-09-03 13:08:59 -0700 | [diff] [blame] | 311 | auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0]; |
| 312 | if (!server->Start(cqs_data, cqs_.size())) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 313 | return nullptr; |
| 314 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 315 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 316 | for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) { |
Vijay Pai | 15855f3 | 2016-06-10 12:25:32 -0700 | [diff] [blame] | 317 | (*plugin)->Finish(initializer); |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 318 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 319 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 320 | return server; |
| 321 | } |
| 322 | |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 323 | void ServerBuilder::InternalAddPluginFactory( |
| 324 | std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()) { |
| 325 | gpr_once_init(&once_init_plugin_list, do_plugin_list_init); |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 326 | (*g_plugin_factory_list).push_back(CreatePlugin); |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 329 | } // namespace grpc |