Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, 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> |
vjpai | a2e1bb1 | 2015-07-15 16:49:49 -0700 | [diff] [blame] | 40 | #include <grpc++/thread_pool_interface.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | |
| 42 | namespace grpc { |
| 43 | |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 44 | ServerBuilder::ServerBuilder() |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 45 | : max_message_size_(-1), generic_service_(nullptr), thread_pool_(nullptr) { |
| 46 | grpc_compression_options_init(&compression_options_); |
| 47 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 49 | std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue() { |
| 50 | ServerCompletionQueue* cq = new ServerCompletionQueue(); |
| 51 | cqs_.push_back(cq); |
| 52 | return std::unique_ptr<ServerCompletionQueue>(cq); |
| 53 | } |
| 54 | |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 55 | ServerBuilder& ServerBuilder::RegisterService(SynchronousService* service) { |
Craig Tiller | d9b6fcf | 2015-07-07 16:28:20 -0700 | [diff] [blame] | 56 | services_.emplace_back(new NamedService<RpcService>(service->service())); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 57 | return *this; |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 58 | } |
| 59 | |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 60 | ServerBuilder& ServerBuilder::RegisterAsyncService( |
| 61 | AsynchronousService* service) { |
Craig Tiller | d9b6fcf | 2015-07-07 16:28:20 -0700 | [diff] [blame] | 62 | async_services_.emplace_back(new NamedService<AsynchronousService>(service)); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 63 | return *this; |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 64 | } |
| 65 | |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 66 | ServerBuilder& ServerBuilder::RegisterService( |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 67 | const grpc::string& addr, SynchronousService* service) { |
Craig Tiller | d9b6fcf | 2015-07-07 16:28:20 -0700 | [diff] [blame] | 68 | services_.emplace_back(new NamedService<RpcService>(addr, service->service())); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 69 | return *this; |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 70 | } |
| 71 | |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 72 | ServerBuilder& ServerBuilder::RegisterAsyncService( |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 73 | const grpc::string& addr, AsynchronousService* service) { |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 74 | async_services_.emplace_back( |
| 75 | new NamedService<AsynchronousService>(addr, service)); |
| 76 | return *this; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | } |
| 78 | |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 79 | ServerBuilder& ServerBuilder::RegisterAsyncGenericService( |
| 80 | AsyncGenericService* service) { |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 81 | if (generic_service_) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 82 | gpr_log(GPR_ERROR, |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 83 | "Adding multiple AsyncGenericService is unsupported for now. " |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 84 | "Dropping the service %p", |
| 85 | service); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 86 | } else { |
| 87 | generic_service_ = service; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 88 | } |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 89 | return *this; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 90 | } |
| 91 | |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 92 | ServerBuilder& ServerBuilder::SetMaxMessageSize(int max_message_size) { |
| 93 | max_message_size_ = max_message_size; |
| 94 | return *this; |
| 95 | } |
| 96 | |
| 97 | ServerBuilder& ServerBuilder::AddListeningPort(const grpc::string& addr, |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 98 | std::shared_ptr<ServerCredentials> creds, |
| 99 | int* selected_port) { |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 100 | Port port = {addr, creds, selected_port}; |
| 101 | ports_.push_back(port); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 102 | return *this; |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 103 | } |
| 104 | |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 105 | ServerBuilder& ServerBuilder::SetThreadPool(ThreadPoolInterface* thread_pool) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | thread_pool_ = thread_pool; |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 107 | return *this; |
| 108 | } |
| 109 | |
| 110 | ServerBuilder& ServerBuilder::SetCompressionOptions( |
| 111 | const grpc_compression_options& options) { |
| 112 | compression_options_ = options; |
| 113 | return *this; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | std::unique_ptr<Server> ServerBuilder::BuildAndStart() { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 117 | bool thread_pool_owned = false; |
Craig Tiller | 40fcdaf | 2015-02-09 15:43:34 -0800 | [diff] [blame] | 118 | if (!async_services_.empty() && !services_.empty()) { |
| 119 | gpr_log(GPR_ERROR, "Mixing async and sync services is unsupported for now"); |
| 120 | return nullptr; |
| 121 | } |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 122 | if (!thread_pool_ && !services_.empty()) { |
Yang Gao | 6f4fb3b | 2015-06-03 12:56:19 -0700 | [diff] [blame] | 123 | thread_pool_ = CreateDefaultThreadPool(); |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 124 | thread_pool_owned = true; |
| 125 | } |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 126 | std::unique_ptr<Server> server(new Server(thread_pool_, thread_pool_owned, |
| 127 | max_message_size_, |
| 128 | compression_options_)); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 129 | for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) { |
| 130 | grpc_server_register_completion_queue(server->server_, (*cq)->cq()); |
| 131 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 132 | for (auto service = services_.begin(); service != services_.end(); |
| 133 | service++) { |
Craig Tiller | d9b6fcf | 2015-07-07 16:28:20 -0700 | [diff] [blame] | 134 | if (!server->RegisterService((*service)->host.get(), (*service)->service)) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 135 | return nullptr; |
| 136 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 137 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 138 | for (auto service = async_services_.begin(); |
| 139 | service != async_services_.end(); service++) { |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 140 | if (!server->RegisterAsyncService((*service)->host.get(), |
| 141 | (*service)->service)) { |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 142 | return nullptr; |
| 143 | } |
| 144 | } |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 145 | if (generic_service_) { |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 146 | server->RegisterAsyncGenericService(generic_service_); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 147 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 148 | for (auto port = ports_.begin(); port != ports_.end(); port++) { |
| 149 | int r = server->AddListeningPort(port->addr, port->creds.get()); |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 150 | if (!r) return nullptr; |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 151 | if (port->selected_port != nullptr) { |
| 152 | *port->selected_port = r; |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 153 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 155 | if (!server->Start()) { |
| 156 | return nullptr; |
| 157 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 158 | return server; |
| 159 | } |
| 160 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 161 | } // namespace grpc |