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> |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 40 | #include "src/cpp/server/thread_pool.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() |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 45 | : generic_service_(nullptr), thread_pool_(nullptr) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 47 | void ServerBuilder::RegisterService(SynchronousService* service) { |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 48 | services_.push_back(service->service()); |
| 49 | } |
| 50 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 51 | void ServerBuilder::RegisterAsyncService(AsynchronousService* service) { |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 52 | async_services_.push_back(service); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 55 | void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) { |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 56 | if (generic_service_) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 57 | gpr_log(GPR_ERROR, |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 58 | "Adding multiple AsyncGenericService is unsupported for now. " |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 59 | "Dropping the service %p", |
| 60 | service); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 61 | return; |
| 62 | } |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 63 | generic_service_ = service; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 66 | void ServerBuilder::AddListeningPort(const grpc::string& addr, |
| 67 | std::shared_ptr<ServerCredentials> creds, |
| 68 | int* selected_port) { |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 69 | ports_.push_back(Port{addr, creds, selected_port}); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 72 | void ServerBuilder::SetThreadPool(ThreadPoolInterface* thread_pool) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 73 | thread_pool_ = thread_pool; |
| 74 | } |
| 75 | |
| 76 | std::unique_ptr<Server> ServerBuilder::BuildAndStart() { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 77 | bool thread_pool_owned = false; |
Craig Tiller | 40fcdaf | 2015-02-09 15:43:34 -0800 | [diff] [blame] | 78 | if (!async_services_.empty() && !services_.empty()) { |
| 79 | gpr_log(GPR_ERROR, "Mixing async and sync services is unsupported for now"); |
| 80 | return nullptr; |
| 81 | } |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 82 | if (!thread_pool_ && !services_.empty()) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 83 | int cores = gpr_cpu_num_cores(); |
| 84 | if (!cores) cores = 4; |
| 85 | thread_pool_ = new ThreadPool(cores); |
| 86 | thread_pool_owned = true; |
| 87 | } |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 88 | std::unique_ptr<Server> server(new Server(thread_pool_, thread_pool_owned)); |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame^] | 89 | for (auto service = services_.begin(); service != services_.end(); |
| 90 | service++) { |
| 91 | if (!server->RegisterService(*service)) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 92 | return nullptr; |
| 93 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 94 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame^] | 95 | for (auto service = async_services_.begin(); |
| 96 | service != async_services_.end(); service++) { |
| 97 | if (!server->RegisterAsyncService(*service)) { |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 98 | return nullptr; |
| 99 | } |
| 100 | } |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 101 | if (generic_service_) { |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 102 | server->RegisterAsyncGenericService(generic_service_); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 103 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame^] | 104 | for (auto port = ports_.begin(); port != ports_.end(); port++) { |
| 105 | int r = server->AddListeningPort(port->addr, port->creds.get()); |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 106 | if (!r) return nullptr; |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame^] | 107 | if (port->selected_port != nullptr) { |
| 108 | *port->selected_port = r; |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 109 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 111 | if (!server->Start()) { |
| 112 | return nullptr; |
| 113 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | return server; |
| 115 | } |
| 116 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 117 | } // namespace grpc |