Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
3 : * http://soramitsu.co.jp
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #include "main/server_runner.hpp"
19 :
20 : #include <boost/format.hpp>
21 :
22 : const auto kPortBindError = "Cannot bind server to address %s";
23 :
24 : ServerRunner::ServerRunner(const std::string &address, bool reuse)
25 524 : : serverAddress_(address), reuse_(reuse) {}
26 :
27 : ServerRunner &ServerRunner::append(std::shared_ptr<grpc::Service> service) {
28 1519 : services_.push_back(service);
29 1519 : return *this;
30 : }
31 :
32 : iroha::expected::Result<int, std::string> ServerRunner::run() {
33 524 : grpc::ServerBuilder builder;
34 524 : int selected_port = 0;
35 :
36 524 : if (not reuse_) {
37 491 : builder.AddChannelArgument(GRPC_ARG_ALLOW_REUSEPORT, 0);
38 491 : }
39 :
40 524 : builder.AddListeningPort(
41 524 : serverAddress_, grpc::InsecureServerCredentials(), &selected_port);
42 :
43 2043 : for (auto &service : services_) {
44 1519 : builder.RegisterService(service.get());
45 : }
46 :
47 : // in order to bypass built-it limitation of gRPC message size
48 524 : builder.SetMaxReceiveMessageSize(INT_MAX);
49 524 : builder.SetMaxSendMessageSize(INT_MAX);
50 :
51 524 : serverInstance_ = builder.BuildAndStart();
52 524 : serverInstanceCV_.notify_one();
53 :
54 524 : if (selected_port == 0) {
55 1 : return iroha::expected::makeError(
56 1 : (boost::format(kPortBindError) % serverAddress_).str());
57 : }
58 :
59 523 : return iroha::expected::makeValue(selected_port);
60 524 : }
61 :
62 : void ServerRunner::waitForServersReady() {
63 29 : std::unique_lock<std::mutex> lock(waitForServer_);
64 29 : while (not serverInstance_) {
65 0 : serverInstanceCV_.wait(lock);
66 : }
67 29 : }
68 :
69 : void ServerRunner::shutdown() {
70 245 : if (serverInstance_) {
71 245 : serverInstance_->Shutdown();
72 245 : }
73 245 : }
|