blob: 26c0724a30a09ccfcb75a587c768ef8dd95025ba [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tiller0db1bef2015-02-09 13:47:39 -080036#include <grpc/support/cpu.h>
yangg9e21f722014-12-08 15:49:52 -080037#include <grpc/support/log.h>
Craig Tiller14a65f92015-02-09 13:13:14 -080038#include <grpc++/impl/service_type.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <grpc++/server.h>
Vijay Paie8a7e302015-08-24 10:52:33 -070040#include "src/cpp/server/thread_pool_interface.h"
41#include "src/cpp/server/fixed_size_thread_pool.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042
43namespace grpc {
44
Yang Gao5f4539f2015-03-06 16:11:16 -080045ServerBuilder::ServerBuilder()
David Garcia Quintasbeac88c2015-08-10 13:39:52 -070046 : max_message_size_(-1), generic_service_(nullptr), thread_pool_(nullptr) {
Craig Tiller71a0f9d2015-09-28 17:22:01 -070047 grpc_compression_options_init(&compression_options_);
David Garcia Quintasbeac88c2015-08-10 13:39:52 -070048}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049
Craig Tillerf9e6adf2015-05-06 11:45:59 -070050std::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 Tiller8c8d0aa2015-02-12 11:38:36 -080056void ServerBuilder::RegisterService(SynchronousService* service) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -070057 services_.emplace_back(new NamedService<RpcService>(service->service()));
Craig Tiller14a65f92015-02-09 13:13:14 -080058}
59
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080060void ServerBuilder::RegisterAsyncService(AsynchronousService* service) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -070061 async_services_.emplace_back(new NamedService<AsynchronousService>(service));
Craig Tiller822d2c72015-07-07 16:08:00 -070062}
63
Craig Tillerd6c98df2015-08-18 09:33:44 -070064void ServerBuilder::RegisterService(const grpc::string& addr,
65 SynchronousService* service) {
66 services_.emplace_back(
67 new NamedService<RpcService>(addr, service->service()));
Craig Tiller822d2c72015-07-07 16:08:00 -070068}
69
Craig Tillerd6c98df2015-08-18 09:33:44 -070070void ServerBuilder::RegisterAsyncService(const grpc::string& addr,
71 AsynchronousService* service) {
David Garcia Quintasbeac88c2015-08-10 13:39:52 -070072 async_services_.emplace_back(
73 new NamedService<AsynchronousService>(addr, service));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074}
75
Yang Gao49996492015-03-12 16:40:19 -070076void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) {
Yang Gao005eb882015-03-11 22:17:13 -070077 if (generic_service_) {
Yang Gao1c402332015-03-05 16:39:25 -080078 gpr_log(GPR_ERROR,
Yang Gao49996492015-03-12 16:40:19 -070079 "Adding multiple AsyncGenericService is unsupported for now. "
Yang Gao6baa9b62015-03-17 10:49:39 -070080 "Dropping the service %p",
81 service);
Yang Gao1c402332015-03-05 16:39:25 -080082 return;
83 }
Yang Gao005eb882015-03-11 22:17:13 -070084 generic_service_ = service;
Yang Gao1c402332015-03-05 16:39:25 -080085}
86
yang-ga23f17b2015-11-25 10:21:05 -080087void ServerBuilder::SetOption(std::unique_ptr<ServerBuilderOption> option) {
88 options_.push_back(std::move(option));
89}
90
Nicolas Noblecfd60732015-03-18 16:27:43 -070091void ServerBuilder::AddListeningPort(const grpc::string& addr,
92 std::shared_ptr<ServerCredentials> creds,
93 int* selected_port) {
Nicolas Noble30862032015-04-24 18:17:45 -070094 Port port = {addr, creds, selected_port};
95 ports_.push_back(port);
yangg9e21f722014-12-08 15:49:52 -080096}
97
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080098std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
Craig Tiller0db1bef2015-02-09 13:47:39 -080099 bool thread_pool_owned = false;
Craig Tiller40fcdaf2015-02-09 15:43:34 -0800100 if (!async_services_.empty() && !services_.empty()) {
101 gpr_log(GPR_ERROR, "Mixing async and sync services is unsupported for now");
102 return nullptr;
103 }
Craig Tiller42bc87c2015-02-23 08:50:19 -0800104 if (!thread_pool_ && !services_.empty()) {
Yang Gao6f4fb3b2015-06-03 12:56:19 -0700105 thread_pool_ = CreateDefaultThreadPool();
Craig Tiller0db1bef2015-02-09 13:47:39 -0800106 thread_pool_owned = true;
107 }
yang-ga23f17b2015-11-25 10:21:05 -0800108 ChannelArguments args;
109 for (auto option = options_.begin(); option != options_.end(); ++option) {
110 (*option)->UpdateArguments(&args);
111 }
112 if (max_message_size_ > 0) {
113 args.SetInt(GRPC_ARG_MAX_MESSAGE_LENGTH, max_message_size_);
114 }
115 args.SetInt(GRPC_COMPRESSION_ALGORITHM_STATE_ARG,
116 compression_options_.enabled_algorithms_bitset);
117 std::unique_ptr<Server> server(
118 new Server(thread_pool_, thread_pool_owned, max_message_size_, args));
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700119 for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) {
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200120 grpc_server_register_completion_queue(server->server_, (*cq)->cq(),
121 nullptr);
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700122 }
Vijay Pai82dd80a2015-03-24 10:36:08 -0700123 for (auto service = services_.begin(); service != services_.end();
124 service++) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -0700125 if (!server->RegisterService((*service)->host.get(), (*service)->service)) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800126 return nullptr;
127 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128 }
Craig Tillerd6c98df2015-08-18 09:33:44 -0700129 for (auto service = async_services_.begin(); service != async_services_.end();
130 service++) {
David Garcia Quintasbeac88c2015-08-10 13:39:52 -0700131 if (!server->RegisterAsyncService((*service)->host.get(),
132 (*service)->service)) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800133 return nullptr;
134 }
135 }
Yang Gao005eb882015-03-11 22:17:13 -0700136 if (generic_service_) {
Yang Gao49996492015-03-12 16:40:19 -0700137 server->RegisterAsyncGenericService(generic_service_);
Yang Gao1c402332015-03-05 16:39:25 -0800138 }
Vijay Pai82dd80a2015-03-24 10:36:08 -0700139 for (auto port = ports_.begin(); port != ports_.end(); port++) {
140 int r = server->AddListeningPort(port->addr, port->creds.get());
Craig Tiller42bc87c2015-02-23 08:50:19 -0800141 if (!r) return nullptr;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700142 if (port->selected_port != nullptr) {
143 *port->selected_port = r;
Craig Tiller0db1bef2015-02-09 13:47:39 -0800144 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800145 }
yang-g61e461e2015-09-03 13:08:59 -0700146 auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0];
147 if (!server->Start(cqs_data, cqs_.size())) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800148 return nullptr;
149 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800150 return server;
151}
152
Craig Tiller190d3602015-02-18 09:23:38 -0800153} // namespace grpc