Example to use various APIs of clients.
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
#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);
parser.
parse(config_file_path);
const std::shared_ptr<msgpack_rpc::logging::Logger> logger =
{
const int result = client.
call<
int>(
"add", 2, 3);
}
{
client.
call<
void>(
"print",
"Test message.");
}
{
try {
client.
call<
void>(
"throw");
logger, "Correctly thrown exception: {}", exception.what());
}
}
{
try {
client.
call<
void>(
"throw_int", 123);
logger, "Correctly thrown exception: {}", exception.what());
}
}
{
}
{
client.
async_call<
void>(
"print",
"Test message.");
}
{
try {
logger, "Correctly thrown exception: {}", exception.what());
}
}
{
client.
notify(
"print",
"Test message.");
}
client.
call<
void>(
"print",
"Client finishes.");
return 0;
}
void parse_command_line_arguments(int argc, char** argv,
std::string& config_file_path, std::string& config_name) {
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);
}
if (show_help) {
std::cout << cli << std::endl;
std::exit(0);
}
}
Definition of CallFuture class.
Class of future object to wait for asynchronous RPCs.
Result get_result()
Get the result of RPC.
Result get_result_within(std::chrono::nanoseconds timeout)
Get the result of RPC within a timeout.
Class of builders of clients.
ClientBuilder & connect_to_tcp(std::string_view host, std::uint16_t port_number)
Add a TCP address to connect to.
Client build()
Build a client.
ClientBuilder & connect_to(addresses::URI uri)
Add a URI to connect to.
CallFuture< std::decay_t< Result > > async_call(messages::MethodNameView method_name, const Parameters &... parameters)
Asynchronously call a method.
std::decay_t< Result > call(messages::MethodNameView method_name, const Parameters &... parameters)
Synchronously call a method.
void notify(messages::MethodNameView method_name, const Parameters &... parameters)
Notify to a method.
Class of exceptions specifying errors in servers.
Class to parse configuration.
const ClientConfig & client_config(std::string_view name) const
Get a configuration of client.
const LoggingConfig & logging_config(std::string_view name) const
Get a configuration of logging.
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 of Client class.
Definition of ClientBuilder class.
Definition of ClientConfig class.
Definition of ConfigParser class.
Definition of Logger class.
#define MSGPACK_RPC_CRITICAL(LOGGER_PTR,...)
Write a critical log.
#define MSGPACK_RPC_INFO(LOGGER_PTR,...)
Write a information log.
Definition of ServerException class.