cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
more_server.cpp

Example to use various APIs of servers.

/*
* Copyright 2024 MusicScience37 (Kenta Kabashima)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdlib>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
#include <fmt/ranges.h>
#include <lyra/lyra.hpp>
void parse_command_line_arguments(int argc, char** argv,
std::string& config_file_path, std::string& config_name);
int main(int argc, char** argv) {
std::string config_file_path;
std::string config_name;
parse_command_line_arguments(argc, argv, config_file_path, config_name);
// Parse configuration.
parser.parse(config_file_path);
// Create a logger from a configuration.
const std::shared_ptr<msgpack_rpc::logging::Logger> logger =
parser.logging_config(config_name));
// Create a server.
// Optionally specify configurations of the server and a logger.
parser.server_config(config_name), logger)
/* ****************************************************************
* Register methods.
******************************************************************/
// Register a method using a function object.
// Specify the signature of the method in the template parameter.
.add_method<int(int, int)>(
"add", [](int x, int y) { return x + y; })
// A method without results can be added.
.add_method<void(std::string)>("print",
[logger](const std::string& str) {
MSGPACK_RPC_INFO(logger, "message: {}", str);
})
// Register a method which throws an exception.
// Exceptions are thrown in clients.
.add_method<void()>("throw",
[]() -> void {
throw std::runtime_error("Example exception.");
})
// Register a method which throws an exception with a serializable
// object. Exceptions are thrown in clients.
.add_method<void(int)>("throw_int",
[](int val) -> void {
})
/* ****************************************************************
* Configure URIs of the server.
******************************************************************/
// Specify a URI of the server.
.listen_to("tcp://localhost:8246")
// Several URIs can be specified by calling multiple times.
.listen_to("tcp://localhost:8247")
// Specify a TCP port.
.listen_to_tcp("localhost", 8248) // NOLINT(*-magic-numbers)
// URIs can be also added in configuration files.
// build() creates a server and starts processing of the server.
.build();
// Get the server URIs.
const std::vector<msgpack_rpc::addresses::URI> server_uris =
MSGPACK_RPC_INFO(logger, "Server URIs: [{}]", fmt::join(server_uris, ", "));
// Run the server until this process receives SIGINT or SIGTERM.
// When this function returns, the server is automatically stopped.
server.run_until_signal();
// Server can be stopped using stop function.
server.stop();
return 0;
}
// Helper functions.
void parse_command_line_arguments(int argc, char** argv,
std::string& config_file_path, std::string& config_name) {
// This function parse command line arguments using lyra library.
config_file_path = "./examples/more/config.toml";
config_name = "example";
bool show_help = false;
const auto cli = lyra::cli()
.add_argument(lyra::opt(config_file_path, "file path")
.name("--config-file")
.name("-f")
.optional()
.help("Configuration file."))
.add_argument(lyra::opt(config_name, "name")
.name("--config-name")
.name("-n")
.optional()
.help("Configuration name."))
.add_argument(lyra::help(show_help));
const auto result = cli.parse({argc, argv});
if (!result) {
std::cerr << result.message() << "\n\n" << cli << std::endl;
std::exit(1); // NOLINT
}
if (show_help) {
std::cout << cli << std::endl;
std::exit(0); // NOLINT
}
}
Class to parse configuration.
const LoggingConfig & logging_config(std::string_view name) const
Get a configuration of logging.
const ServerConfig & server_config(std::string_view name) const
Get a configuration of server.
void parse(std::string_view file_path)
Parse a file.
static std::shared_ptr< Logger > create(const config::LoggingConfig &config=config::LoggingConfig())
Create a logger.
Definition logger.h:62
Class of exceptions in methods.
Class of builders of servers.
ServerBuilder & add_method(std::unique_ptr< methods::IMethod > method)
Add a method.
ServerBuilder & listen_to_tcp(std::string_view host, std::uint16_t port_number)
Add a TCP address to listen to.
ServerBuilder & listen_to(addresses::URI uri)
Add a URI to listen to.
Class of servers.
Definition server.h:37
void stop()
Stop processing of this server.
Definition server.h:56
void run_until_signal()
Run processing of this server until SIGINT or SIGTERM is received.
Definition server.h:63
std::vector< addresses::URI > local_endpoint_uris()
Get the URIs of the local endpoints in this server.
Definition server.h:70
Definition of ConfigParser class.
Definition of Logger class.
#define MSGPACK_RPC_INFO(LOGGER_PTR,...)
Write a information log.
Definition logger.h:198
Definition of MethodException class.
Definition of Server class.
Definition of ServerBuilder class.
Definition of URI class.