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 159 : ServerBuilder::ServerBuilder()
46 159 : : max_message_size_(-1), generic_service_(nullptr), thread_pool_(nullptr) {
47 159 : grpc_compression_options_init(&compression_options_);
48 159 : }
49 :
50 43 : std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue() {
51 43 : ServerCompletionQueue* cq = new ServerCompletionQueue();
52 43 : cqs_.push_back(cq);
53 43 : return std::unique_ptr<ServerCompletionQueue>(cq);
54 : }
55 :
56 208 : void ServerBuilder::RegisterService(SynchronousService* service) {
57 208 : services_.emplace_back(new NamedService<RpcService>(service->service()));
58 208 : }
59 :
60 30 : void ServerBuilder::RegisterAsyncService(AsynchronousService* service) {
61 30 : async_services_.emplace_back(new NamedService<AsynchronousService>(service));
62 30 : }
63 :
64 81 : void ServerBuilder::RegisterService(const grpc::string& addr,
65 : SynchronousService* service) {
66 : services_.emplace_back(
67 81 : new NamedService<RpcService>(addr, service->service()));
68 81 : }
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 3 : void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) {
77 3 : if (generic_service_) {
78 : gpr_log(GPR_ERROR,
79 : "Adding multiple AsyncGenericService is unsupported for now. "
80 : "Dropping the service %p",
81 0 : service);
82 3 : return;
83 : }
84 3 : generic_service_ = service;
85 : }
86 :
87 159 : void ServerBuilder::AddListeningPort(const grpc::string& addr,
88 : std::shared_ptr<ServerCredentials> creds,
89 : int* selected_port) {
90 159 : Port port = {addr, creds, selected_port};
91 159 : ports_.push_back(port);
92 159 : }
93 :
94 159 : std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
95 159 : bool thread_pool_owned = false;
96 159 : if (!async_services_.empty() && !services_.empty()) {
97 0 : gpr_log(GPR_ERROR, "Mixing async and sync services is unsupported for now");
98 0 : return nullptr;
99 : }
100 159 : if (!thread_pool_ && !services_.empty()) {
101 126 : thread_pool_ = CreateDefaultThreadPool();
102 126 : thread_pool_owned = true;
103 : }
104 : std::unique_ptr<Server> server(new Server(thread_pool_, thread_pool_owned,
105 : max_message_size_,
106 159 : compression_options_));
107 202 : for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) {
108 86 : grpc_server_register_completion_queue(server->server_, (*cq)->cq(),
109 86 : nullptr);
110 : }
111 448 : for (auto service = services_.begin(); service != services_.end();
112 : service++) {
113 289 : if (!server->RegisterService((*service)->host.get(), (*service)->service)) {
114 0 : return nullptr;
115 : }
116 : }
117 189 : for (auto service = async_services_.begin(); service != async_services_.end();
118 : service++) {
119 60 : if (!server->RegisterAsyncService((*service)->host.get(),
120 60 : (*service)->service)) {
121 0 : return nullptr;
122 : }
123 : }
124 159 : if (generic_service_) {
125 3 : server->RegisterAsyncGenericService(generic_service_);
126 : }
127 318 : for (auto port = ports_.begin(); port != ports_.end(); port++) {
128 159 : int r = server->AddListeningPort(port->addr, port->creds.get());
129 159 : if (!r) return nullptr;
130 159 : if (port->selected_port != nullptr) {
131 0 : *port->selected_port = r;
132 : }
133 : }
134 159 : auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0];
135 159 : if (!server->Start(cqs_data, cqs_.size())) {
136 0 : return nullptr;
137 : }
138 159 : return server;
139 : }
140 :
141 : } // namespace grpc
|