OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 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 |
| 36 #include <grpc/support/cpu.h> |
| 37 #include <grpc/support/log.h> |
| 38 #include <grpc++/impl/service_type.h> |
| 39 #include <grpc++/server.h> |
| 40 #include "src/cpp/server/thread_pool_interface.h" |
| 41 |
| 42 namespace grpc { |
| 43 |
| 44 ServerBuilder::ServerBuilder() |
| 45 : max_message_size_(-1), generic_service_(nullptr) { |
| 46 grpc_compression_options_init(&compression_options_); |
| 47 } |
| 48 |
| 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 |
| 55 void ServerBuilder::RegisterService(Service* service) { |
| 56 services_.emplace_back(new NamedService(service)); |
| 57 } |
| 58 |
| 59 void ServerBuilder::RegisterService(const grpc::string& addr, |
| 60 Service* service) { |
| 61 services_.emplace_back(new NamedService(addr, service)); |
| 62 } |
| 63 |
| 64 void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) { |
| 65 if (generic_service_) { |
| 66 gpr_log(GPR_ERROR, |
| 67 "Adding multiple AsyncGenericService is unsupported for now. " |
| 68 "Dropping the service %p", |
| 69 service); |
| 70 return; |
| 71 } |
| 72 generic_service_ = service; |
| 73 } |
| 74 |
| 75 void ServerBuilder::SetOption(std::unique_ptr<ServerBuilderOption> option) { |
| 76 options_.push_back(std::move(option)); |
| 77 } |
| 78 |
| 79 void ServerBuilder::AddListeningPort(const grpc::string& addr, |
| 80 std::shared_ptr<ServerCredentials> creds, |
| 81 int* selected_port) { |
| 82 Port port = {addr, creds, selected_port}; |
| 83 ports_.push_back(port); |
| 84 } |
| 85 |
| 86 std::unique_ptr<Server> ServerBuilder::BuildAndStart() { |
| 87 std::unique_ptr<ThreadPoolInterface> thread_pool; |
| 88 for (auto it = services_.begin(); it != services_.end(); ++it) { |
| 89 if ((*it)->service->has_synchronous_methods()) { |
| 90 if (thread_pool == nullptr) { |
| 91 thread_pool.reset(CreateDefaultThreadPool()); |
| 92 break; |
| 93 } |
| 94 } |
| 95 } |
| 96 ChannelArguments args; |
| 97 for (auto option = options_.begin(); option != options_.end(); ++option) { |
| 98 (*option)->UpdateArguments(&args); |
| 99 } |
| 100 if (max_message_size_ > 0) { |
| 101 args.SetInt(GRPC_ARG_MAX_MESSAGE_LENGTH, max_message_size_); |
| 102 } |
| 103 args.SetInt(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, |
| 104 compression_options_.enabled_algorithms_bitset); |
| 105 std::unique_ptr<Server> server( |
| 106 new Server(thread_pool.release(), true, max_message_size_, &args)); |
| 107 for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) { |
| 108 grpc_server_register_completion_queue(server->server_, (*cq)->cq(), |
| 109 nullptr); |
| 110 } |
| 111 for (auto service = services_.begin(); service != services_.end(); |
| 112 service++) { |
| 113 if (!server->RegisterService((*service)->host.get(), (*service)->service)) { |
| 114 return nullptr; |
| 115 } |
| 116 } |
| 117 if (generic_service_) { |
| 118 server->RegisterAsyncGenericService(generic_service_); |
| 119 } else { |
| 120 for (auto it = services_.begin(); it != services_.end(); ++it) { |
| 121 if ((*it)->service->has_generic_methods()) { |
| 122 gpr_log(GPR_ERROR, |
| 123 "Some methods were marked generic but there is no " |
| 124 "generic service registered."); |
| 125 return nullptr; |
| 126 } |
| 127 } |
| 128 } |
| 129 for (auto port = ports_.begin(); port != ports_.end(); port++) { |
| 130 int r = server->AddListeningPort(port->addr, port->creds.get()); |
| 131 if (!r) return nullptr; |
| 132 if (port->selected_port != nullptr) { |
| 133 *port->selected_port = r; |
| 134 } |
| 135 } |
| 136 auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0]; |
| 137 if (!server->Start(cqs_data, cqs_.size())) { |
| 138 return nullptr; |
| 139 } |
| 140 return server; |
| 141 } |
| 142 |
| 143 } // namespace grpc |
OLD | NEW |