Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
yang-g | f48766d | 2016-01-15 11:26:31 -0800 | [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 | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 36 | #include <grpc/support/cpu.h> |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 37 | #include <grpc/support/log.h> |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 38 | #include <grpc++/impl/service_type.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc++/server.h> |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 40 | #include "src/cpp/server/thread_pool_interface.h" |
| 41 | #include "src/cpp/server/fixed_size_thread_pool.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | |
| 43 | namespace grpc { |
| 44 | |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 45 | ServerBuilder::ServerBuilder() |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 46 | : max_message_size_(-1), generic_service_(nullptr) { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 47 | grpc_compression_options_init(&compression_options_); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 48 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 50 | std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue() { |
| 51 | ServerCompletionQueue* cq = new ServerCompletionQueue(); |
| 52 | cqs_.push_back(cq); |
| 53 | return std::unique_ptr<ServerCompletionQueue>(cq); |
| 54 | } |
| 55 | |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 56 | void ServerBuilder::RegisterService(Service* service) { |
| 57 | services_.emplace_back(new NamedService(service)); |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 60 | void ServerBuilder::RegisterService(const grpc::string& addr, |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 61 | Service* service) { |
| 62 | services_.emplace_back(new NamedService(addr, service)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 65 | void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) { |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 66 | if (generic_service_) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 67 | gpr_log(GPR_ERROR, |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 68 | "Adding multiple AsyncGenericService is unsupported for now. " |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 69 | "Dropping the service %p", |
| 70 | service); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 71 | return; |
| 72 | } |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 73 | generic_service_ = service; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 74 | } |
| 75 | |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 76 | void ServerBuilder::SetOption(std::unique_ptr<ServerBuilderOption> option) { |
| 77 | options_.push_back(std::move(option)); |
| 78 | } |
| 79 | |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 80 | void ServerBuilder::AddListeningPort(const grpc::string& addr, |
| 81 | std::shared_ptr<ServerCredentials> creds, |
| 82 | int* selected_port) { |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 83 | Port port = {addr, creds, selected_port}; |
| 84 | ports_.push_back(port); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 87 | std::unique_ptr<Server> ServerBuilder::BuildAndStart() { |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 88 | std::unique_ptr<ThreadPoolInterface> thread_pool; |
| 89 | for (auto it = services_.begin(); it != services_.end(); ++it) { |
| 90 | if ((*it)->service->has_synchronous_methods()) { |
yang-g | bef0d87 | 2016-01-13 15:27:33 -0800 | [diff] [blame] | 91 | if (thread_pool == nullptr) { |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 92 | thread_pool.reset(CreateDefaultThreadPool()); |
yang-g | bef0d87 | 2016-01-13 15:27:33 -0800 | [diff] [blame] | 93 | break; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 96 | } |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 97 | ChannelArguments args; |
| 98 | for (auto option = options_.begin(); option != options_.end(); ++option) { |
| 99 | (*option)->UpdateArguments(&args); |
| 100 | } |
| 101 | if (max_message_size_ > 0) { |
| 102 | args.SetInt(GRPC_ARG_MAX_MESSAGE_LENGTH, max_message_size_); |
| 103 | } |
| 104 | args.SetInt(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, |
| 105 | compression_options_.enabled_algorithms_bitset); |
| 106 | std::unique_ptr<Server> server( |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 107 | new Server(thread_pool.release(), true, max_message_size_, args)); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 108 | for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) { |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 109 | grpc_server_register_completion_queue(server->server_, (*cq)->cq(), |
| 110 | nullptr); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 111 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 112 | for (auto service = services_.begin(); service != services_.end(); |
| 113 | service++) { |
Craig Tiller | d9b6fcf | 2015-07-07 16:28:20 -0700 | [diff] [blame] | 114 | if (!server->RegisterService((*service)->host.get(), (*service)->service)) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 115 | return nullptr; |
| 116 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | } |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 118 | if (generic_service_) { |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 119 | server->RegisterAsyncGenericService(generic_service_); |
yang-g | 1ac6f45 | 2016-01-15 11:46:40 -0800 | [diff] [blame^] | 120 | } else { |
| 121 | for (auto it = services_.begin(); it != services_.end(); ++it) { |
| 122 | if ((*it)->service->has_generic_methods()) { |
| 123 | gpr_log(GPR_ERROR, |
| 124 | "Some methods were marked generic but there is no " |
| 125 | "generic service registered."); |
| 126 | return nullptr; |
| 127 | } |
| 128 | } |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 129 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 130 | for (auto port = ports_.begin(); port != ports_.end(); port++) { |
| 131 | int r = server->AddListeningPort(port->addr, port->creds.get()); |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 132 | if (!r) return nullptr; |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 133 | if (port->selected_port != nullptr) { |
| 134 | *port->selected_port = r; |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 135 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | } |
yang-g | 61e461e | 2015-09-03 13:08:59 -0700 | [diff] [blame] | 137 | auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0]; |
| 138 | if (!server->Start(cqs_data, cqs_.size())) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 139 | return nullptr; |
| 140 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | return server; |
| 142 | } |
| 143 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 144 | } // namespace grpc |