cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
server_builder.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 <cstdint>
23#include <memory>
24#include <optional>
25#include <string_view>
26#include <utility>
27
37
38namespace msgpack_rpc::servers {
39
44public:
51 ServerBuilder() : ServerBuilder(logging::Logger::create()) {}
52
60 explicit ServerBuilder(const std::shared_ptr<logging::Logger>& logger)
61 : ServerBuilder(config::ServerConfig(), logger) {}
62
69 explicit ServerBuilder(const config::ServerConfig& server_config,
70 const std::shared_ptr<logging::Logger>& logger =
72 : impl_(impl::create_default_builder_impl(server_config, logger)) {}
73
81 impl_->listen_to(std::move(uri));
82 return *this;
83 }
84
91 ServerBuilder& listen_to(std::string_view uri) {
92 impl_->listen_to(addresses::URI::parse(uri));
93 return *this;
94 }
95
104 std::string_view host, std::uint16_t port_number) {
106 return *this;
107 }
108
117 ServerBuilder& add_method(std::unique_ptr<methods::IMethod> method) {
118 impl_->add_method(std::move(method));
119 return *this;
120 }
121
135 template <typename Signature, typename Function>
136 ServerBuilder& add_method(messages::MethodName name, Function&& function) {
137 return add_method(
139 std::forward<Function>(function), impl_->logger()));
140 }
141
147 [[nodiscard]] Server build() { return Server{impl_->build()}; }
148
149private:
151 std::unique_ptr<impl::IServerBuilderImpl> impl_;
152};
153
154} // namespace msgpack_rpc::servers
Class of URIs (Uniform Resource Identifiers) to specify endpoints in this library.
Definition uri.h:38
static URI parse(std::string_view uri_string)
Parse a string to create a URI.
Definition uri.cpp:54
Class of configurations of servers.
static std::shared_ptr< Logger > create(const config::LoggingConfig &config=config::LoggingConfig())
Create a logger.
Definition logger.h:62
Class of method names.
Definition method_name.h:35
ServerBuilder(const std::shared_ptr< logging::Logger > &logger)
Constructor.
ServerBuilder & add_method(std::unique_ptr< methods::IMethod > method)
Add a method.
ServerBuilder(const config::ServerConfig &server_config, const std::shared_ptr< logging::Logger > &logger=logging::Logger::create())
Constructor.
ServerBuilder & add_method(messages::MethodName name, Function &&function)
Add a method implemented by a function object.
ServerBuilder & listen_to(std::string_view uri)
Add a URI to listen to.
ServerBuilder & listen_to_tcp(std::string_view host, std::uint16_t port_number)
Add a TCP address to listen to.
std::unique_ptr< impl::IServerBuilderImpl > impl_
Internal implementation.
ServerBuilder & listen_to(addresses::URI uri)
Add a URI to listen to.
Class of servers.
Definition server.h:37
Definition of FunctionalMethod class.
Definition of IMethod class.
Definition of IServerBuilderImpl class.
Definition of Logger class.
Definition of MethodName class.
constexpr std::string_view TCP_SCHEME
Scheme of TCP.
Definition schemes.h:27
Namespace of configurations.
Namespace of logging.
Definition i_log_sink.h:27
std::unique_ptr< FunctionalMethod< Signature, Function > > create_functional_method(messages::MethodName name, Function &&function, std::shared_ptr< logging::Logger > logger)
Create a method implemented by a function object.
Namespace of internal implementation.
Namespace of servers.
Definition of constants of schemes.
Definition of Server class.
Definition of ServerConfig class.
Definition of URI class.