Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, 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 : #include "src/cpp/server/fixed_size_thread_pool.h"
42 :
43 : namespace grpc {
44 :
45 186 : ServerBuilder::ServerBuilder()
46 186 : : max_message_size_(-1), generic_service_(nullptr), thread_pool_(nullptr) {
47 186 : grpc_compression_options_init(&compression_options_);
48 186 : }
49 :
50 39 : std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue() {
51 39 : ServerCompletionQueue* cq = new ServerCompletionQueue();
52 39 : cqs_.push_back(cq);
53 39 : return std::unique_ptr<ServerCompletionQueue>(cq);
54 : }
55 :
56 237 : void ServerBuilder::RegisterService(SynchronousService* service) {
57 237 : services_.emplace_back(new NamedService<RpcService>(service->service()));
58 237 : }
59 :
60 55 : void ServerBuilder::RegisterAsyncService(AsynchronousService* service) {
61 55 : async_services_.emplace_back(new NamedService<AsynchronousService>(service));
62 55 : }
63 :
64 82 : void ServerBuilder::RegisterService(const grpc::string& addr,
65 : SynchronousService* service) {
66 : services_.emplace_back(
67 82 : new NamedService<RpcService>(addr, service->service()));
68 82 : }
69 :
70 0 : void ServerBuilder::RegisterAsyncService(const grpc::string& addr,
71 : AsynchronousService* service) {
72 : async_services_.emplace_back(
73 0 : new NamedService<AsynchronousService>(addr, service));
74 0 : }
75 :
76 6 : void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) {
77 6 : if (generic_service_) {
78 : gpr_log(GPR_ERROR,
79 : "Adding multiple AsyncGenericService is unsupported for now. "
80 : "Dropping the service %p",
81 3 : service);
82 9 : return;
83 : }
84 3 : generic_service_ = service;
85 : }
86 :
87 0 : void ServerBuilder::SetOption(std::unique_ptr<ServerBuilderOption> option) {
88 0 : options_.push_back(std::move(option));
89 0 : }
90 :
91 186 : void ServerBuilder::AddListeningPort(const grpc::string& addr,
92 : std::shared_ptr<ServerCredentials> creds,
93 : int* selected_port) {
94 186 : Port port = {addr, creds, selected_port};
95 186 : ports_.push_back(port);
96 186 : }
97 :
98 186 : std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
99 186 : bool thread_pool_owned = false;
100 186 : if (!async_services_.empty() && !services_.empty()) {
101 26 : gpr_log(GPR_ERROR, "Mixing async and sync services is unsupported for now");
102 26 : return nullptr;
103 : }
104 160 : if (!thread_pool_ && !services_.empty()) {
105 128 : thread_pool_ = CreateDefaultThreadPool();
106 128 : thread_pool_owned = true;
107 : }
108 160 : ChannelArguments args;
109 160 : for (auto option = options_.begin(); option != options_.end(); ++option) {
110 0 : (*option)->UpdateArguments(&args);
111 : }
112 160 : if (max_message_size_ > 0) {
113 83 : args.SetInt(GRPC_ARG_MAX_MESSAGE_LENGTH, max_message_size_);
114 : }
115 : args.SetInt(GRPC_COMPRESSION_ALGORITHM_STATE_ARG,
116 160 : compression_options_.enabled_algorithms_bitset);
117 : std::unique_ptr<Server> server(
118 320 : new Server(thread_pool_, thread_pool_owned, max_message_size_, args));
119 199 : for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) {
120 78 : grpc_server_register_completion_queue(server->server_, (*cq)->cq(),
121 78 : nullptr);
122 : }
123 453 : for (auto service = services_.begin(); service != services_.end();
124 : service++) {
125 293 : if (!server->RegisterService((*service)->host.get(), (*service)->service)) {
126 0 : return nullptr;
127 : }
128 : }
129 189 : for (auto service = async_services_.begin(); service != async_services_.end();
130 : service++) {
131 58 : if (!server->RegisterAsyncService((*service)->host.get(),
132 58 : (*service)->service)) {
133 0 : return nullptr;
134 : }
135 : }
136 160 : if (generic_service_) {
137 3 : server->RegisterAsyncGenericService(generic_service_);
138 : }
139 320 : for (auto port = ports_.begin(); port != ports_.end(); port++) {
140 160 : int r = server->AddListeningPort(port->addr, port->creds.get());
141 160 : if (!r) return nullptr;
142 160 : if (port->selected_port != nullptr) {
143 0 : *port->selected_port = r;
144 : }
145 : }
146 160 : auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0];
147 160 : if (!server->Start(cqs_data, cqs_.size())) {
148 0 : return nullptr;
149 : }
150 320 : return server;
151 : }
152 :
153 : } // namespace grpc
|