SRS-control 0.1.4
Loading...
Searching...
No Matches
test_output.cpp
Go to the documentation of this file.
2#include "srs/SRSWorld.hpp"
6#include <CLI/CLI.hpp>
7#include <algorithm>
8#include <chrono>
9#include <cstddef>
10#include <cstdlib>
11#include <exception>
12#include <filesystem>
13#include <fmt/base.h>
14#include <fmt/format.h>
15#include <fmt/ranges.h>
16#include <format>
17#include <spdlog/common.h>
18#include <spdlog/spdlog.h>
19#include <string>
20#include <string_view>
21#include <thread>
22#include <utility>
23#include <vector>
24
25using std::exception;
26
30
33
34namespace
35{
36 auto get_file_size(std::string_view filename) -> std::size_t
37 {
38 namespace fs = std::filesystem;
39 auto file_path = fs::path{ filename };
40 if (fs::exists(file_path))
41 {
42 return std::filesystem::file_size(file_path);
43 }
44 spdlog::error("File with the name {:?} doesn't exist!", filename);
45 return 0;
46 }
47
48} // namespace
49
50constexpr auto DEFAULT_RUN_TIME_S = 5;
51constexpr auto DEFAULT_DELAY_TIME_US = 10000;
52// NOLINTNEXTLINE (bugprone-exception-escape)
53auto main(int argc, char** argv) -> int
54{
55 auto cli_args = CLI::App{ "SRS integration test" };
56
57 auto is_continuous_output = false;
58 const auto input_filename = std::string{ "test_data.bin" };
59 auto output_filenames = std::vector<std::string>{ "" };
60 try
61 {
62 auto delay_time = DEFAULT_DELAY_TIME_US;
63 auto n_output_split = 1;
64 auto run_time = DEFAULT_RUN_TIME_S;
65 auto spdlog_level = spdlog::level::info;
66 const auto home_dir = []() -> std::string
67 {
68 const auto* home_var = getenv("HOME");
69 if (home_var != nullptr)
70 {
71 return std::string{ home_var };
72 }
73 return {};
74 }();
75 auto json_filepath = home_dir.empty() ? "" : std::format("{}/.config/srs-control/config.json", getenv("HOME"));
76 argv = cli_args.ensure_utf8(argv);
77 auto app_config = srs::Config{};
78
79 cli_args
80 .add_option("-l, --log-level",
81 spdlog_level,
82 fmt::format("Set log level\nAvailable options: [{}]", fmt::join(SPDLOG_LOG_NAMES, ", ")))
83 ->transform(CLI::CheckedTransformer(spdlog_map, CLI::ignore_case).description(""))
84 ->default_str(get_enum_dashed_name(spdlog_level));
85 cli_args.add_flag("--cont", is_continuous_output, "Set the emulator continuous output");
86 cli_args.add_option("-o, --output-files", output_filenames, "Set output file (or socket) names")
87 ->capture_default_str()
88 ->expected(0, -1);
89 cli_args.add_option("-s, --split-output", n_output_split, "Splitting the output data into different files.")
90 ->capture_default_str();
91 cli_args
92 .add_option(
93 "-d, --delay-time", delay_time, "Set the delay time (us) when sending the data from the server.")
94 ->capture_default_str();
95 cli_args.add_option("-r, --run-time", run_time, "Set the run time when --cont is enabled.")
96 ->capture_default_str();
97 cli_args.add_option("-c, --config-file", json_filepath, "Set the path of the JSON config file")
98 ->capture_default_str();
99
100 cli_args.parse(argc, argv);
101
102 spdlog::set_level(spdlog_level);
103 auto world = srs::test::World{ "test_data.bin" };
104 world.set_continue_output(is_continuous_output);
105 world.set_delay_time_us(delay_time);
106
107 auto& app = world.get_app();
108
109 srs::config::set_config_from_json(app_config, json_filepath);
110 app.set_options(std::move(app_config));
111 app.get_config_ref().remote_fec_ips = std::vector{ std::string{ "127.0.0.1" } };
112 // auto output_file = std::vector<std::string>{ "test_output.bin" };
113 app.set_output_filenames(output_filenames, n_output_split);
114 world.init();
115 world.launch_server();
116
117 app.start_workflow();
118 app.read_data();
119 app.switch_on();
120
121 auto switch_off_thread = std::jthread(
122 [&app, &world, is_continuous_output, &run_time]()
123 {
124 if (is_continuous_output)
125 {
126 std::this_thread::sleep_for(std::chrono::seconds{ run_time });
127 }
128 else
129 {
130 world.get_server()->wait_for_data_sender();
131 }
132 app.exit_and_switch_off();
133 });
134
135 app.wait_for_workflow();
136 }
137 catch (const CLI::ParseError& e)
138 {
139 cli_args.exit(e);
140 return 0;
141 }
142 catch (const exception& ex)
143 {
144 spdlog::critical("exception occurred: {}", ex.what());
145 }
146 auto wrong_file_size = not is_continuous_output and
147 not std::ranges::all_of(output_filenames,
148 [&input_filename](std::string_view name) -> bool
149 {
150 using enum srs::DataWriterOption;
151 auto file_type =
152 std::get<0>(srs::get_filetype_from_filename(name));
153 if (file_type == no_output or file_type == udp)
154 {
155 return true;
156 }
157 if (name.ends_with(".bin"))
158 {
159 return get_file_size(name) == get_file_size(input_filename);
160 }
161 return get_file_size(name) != 0;
162 });
163 if (wrong_file_size)
164 {
165 spdlog::error("Output files don't have correct sizes!");
166 return 1;
167 }
168 spdlog::info("All output files have correct sizes!");
169
170 return 0;
171}
const auto spdlog_map
Definition main.cpp:32
constexpr auto SPDLOG_LOG_NAMES
Definition main.cpp:28
auto set_continue_output(bool is_continue)
Definition SRSWorld.hpp:24
asio::ip::udp udp
Definition main.cpp:9
constexpr auto get_enum_dashed_names()
constexpr auto get_enum_dashed_name()
void set_config_from_json(Config &app_config, std::string_view json_filename)
auto get_filetype_from_filename(std::string_view filename) -> std::tuple< DataWriterOption, process::DataConvertOptions >
Main configuration struct.
constexpr auto get_enum_dashed_names()
constexpr auto get_enum_dashed_name()
auto main(int argc, char **argv) -> int
constexpr auto get_enum_dash_map()
constexpr auto DEFAULT_DELAY_TIME_US
constexpr auto DEFAULT_RUN_TIME_S