cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
server_builder_impl.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <memory>
23#include <utility>
24#include <vector>
25
41
43
47class ServerBuilderImpl final : public IServerBuilderImpl {
48public:
57 ServerBuilderImpl(std::shared_ptr<executors::IAsyncExecutor> executor,
58 std::shared_ptr<logging::Logger> logger,
59 transport::BackendList backends, std::vector<addresses::URI> uris)
60 : executor_(std::move(executor)),
61 logger_(std::move(logger)),
62 backends_(std::move(backends)),
63 uris_(std::move(uris)),
64 processor_(methods::create_method_processor(logger_)) {}
65
68 std::shared_ptr<transport::IBackend> backend) override {
69 backends_.append(backend);
70 }
71
73 void listen_to(addresses::URI uri) override {
74 uris_.push_back(std::move(uri));
75 }
76
78 void add_method(std::unique_ptr<methods::IMethod> method) override {
79 processor_->append(std::move(method));
80 }
81
83 [[nodiscard]] std::unique_ptr<IServerImpl> build() override {
84 if (uris_.empty()) {
85 throw MsgpackRPCException(StatusCode::PRECONDITION_NOT_MET,
86 "No URI to listen to was given.");
87 }
88
89 std::vector<std::shared_ptr<transport::IAcceptor>> acceptors;
90 for (const auto& uri : uris_) {
91 const auto backend = backends_.find(uri.scheme());
92
93 const auto added_acceptors =
94 backend->create_acceptor_factory()->create(uri);
95 for (const auto& acceptor : added_acceptors) {
96 acceptors.push_back(acceptor);
97 }
98 }
99
100 if (acceptors.empty()) {
101 throw MsgpackRPCException(StatusCode::PRECONDITION_NOT_MET,
102 "All URI set to listen to was unusable.");
103 }
104
105 auto server = std::make_unique<ServerImpl>(
106 std::move(acceptors), std::move(processor_), executor_, logger_);
107 server->start();
108
109 return server;
110 }
111
113 [[nodiscard]] std::shared_ptr<logging::Logger> logger() override {
114 return logger_;
115 }
116
117private:
119 std::shared_ptr<executors::IAsyncExecutor> executor_;
120
122 std::shared_ptr<logging::Logger> logger_;
123
126
128 std::vector<addresses::URI> uris_{};
129
131 std::unique_ptr<methods::IMethodProcessor> processor_;
132};
133
134} // namespace msgpack_rpc::servers::impl
Definition of BackendList class.
Class of URIs (Uniform Resource Identifiers) to specify endpoints in this library.
Definition uri.h:38
Class of exceptions in cpp-msgpack-rpc library.
std::shared_ptr< executors::IAsyncExecutor > executor_
Executor.
void add_method(std::unique_ptr< methods::IMethod > method) override
Add a method.
ServerBuilderImpl(std::shared_ptr< executors::IAsyncExecutor > executor, std::shared_ptr< logging::Logger > logger, transport::BackendList backends, std::vector< addresses::URI > uris)
Constructor.
std::shared_ptr< logging::Logger > logger_
Logger.
std::unique_ptr< IServerImpl > build() override
Build a server.
void listen_to(addresses::URI uri) override
Add a URI to listen to.
void register_protocol(std::shared_ptr< transport::IBackend > backend) override
Register a protocol.
std::vector< addresses::URI > uris_
URIs to listen to.
std::shared_ptr< logging::Logger > logger() override
Get the logger in this builder.
std::unique_ptr< methods::IMethodProcessor > processor_
Processor of methods.
Class of lists of backends of protocols.
Definition of IAcceptor class.
Definition of IAcceptorFactory class.
Definition of IAsyncExecutor class.
Definition of IBackend class.
Definition of IMethod class.
Definition of IMethodProcessor class.
Definition of IServerBuilderImpl class.
Definition of IServerImpl class.
Definition of Logger class.
Definition of functions for processor of method calls.
Definition of MsgpackRPCException class.
Namespace of methods in MessagePack-RPC.
Namespace of internal implementation.
STL namespace.
Definition of ServerImpl class.
Definition of StatusCode enumeration.
Definition of URI class.