10#include <asio/ip/udp.hpp>
11#include <asio/thread_pool.hpp>
12#include <magic_enum/magic_enum.hpp>
17#include <spdlog/spdlog.h>
20#include <system_error>
32 auto convert_str_to_endpoint(asio::thread_pool& thread_pool, std::string_view ip_port)
33 -> std::optional<asio::ip::udp::endpoint>
35 const auto question_pos = ip_port.find(
'?');
36 const auto colon_pos = ip_port.find(
':');
37 if (colon_pos == std::string::npos)
39 spdlog::critical(
"Ill format socket string {:?}. Please set it as \"ip:port\"", ip_port);
42 auto ip_string = ip_port.substr(0, colon_pos);
44 (question_pos == std::string::npos) ? ip_port.substr(colon_pos + 1) : ip_port.substr(question_pos + 1);
46 auto err_code = std::error_code{};
47 auto resolver = asio::ip::udp::resolver{ thread_pool };
48 auto iter = resolver.resolve(std::string{ ip_string }, std::string{ port_str }, err_code).begin();
51 spdlog::critical(
"Cannot query the ip address {:?} with port {:?}. Error code: {}",
60 auto check_root_dependency() ->
bool
65 spdlog::error(
"Cannot output to a root file. Please make sure the program is "
66 "built with the ROOT library.");
74 : workflow_handler_{ processor }
78 Manager::~Manager() =
default;
82 auto is_required =
false;
83 do_for_each_sink([&is_required, dependee](std::string_view,
auto& writer)
90 constexpr auto conversions = magic_enum::enum_values<process::DataConvertOptions>();
92 std::views::transform(conversions,
93 [
this](
const auto conversion) -> std::pair<process::DataConvertOptions, bool>
94 {
return std::pair{ conversion, is_convert_required(conversion) }; }) |
95 std::ranges::to<std::map<process::DataConvertOptions, bool>>();
96 if (not std::ranges::contains(convert_map | std::views::values,
true))
103 void Manager::enable_frame_count_checker()
105 if (frame_count_checker_ ==
nullptr)
107 frame_count_checker_ = std::make_unique<FrameCountChecker>(workflow_handler_->get_n_lines());
114 .try_emplace(filename,
115 std::make_unique<BinaryFile>(filename, prev_conversion, workflow_handler_->get_n_lines()))
121 auto& app = workflow_handler_->get_app_ref();
122 auto endpoint = convert_str_to_endpoint(app.get_io_context(), filename);
123 if (endpoint.has_value())
126 .try_emplace(filename,
127 std::make_unique<UDP>(app.get_io_context(),
128 std::move(endpoint.value()),
129 workflow_handler_->get_n_lines(),
136 auto Manager::add_root_file([[maybe_unused]]
const std::string& filename,
143 std::make_unique<RootFile>(filename.c_str(), prev_conversion, workflow_handler_->get_n_lines()))
153 .try_emplace(filename, std::make_unique<Json>(filename, prev_conversion, workflow_handler_->get_n_lines()))
157 void Manager::set_output_filenames(
const std::vector<std::string>& filenames)
159 for (
const auto& filename : filenames)
161 if (filename.empty())
172 spdlog::error(
"Extension of the filename {:?} cannot be recognized!", filename);
176 is_ok = add_binary_file(filename, convert_mode);
179 is_ok = add_udp_file(filename, convert_mode);
182 if (not check_root_dependency())
186 is_ok = add_root_file(filename, convert_mode);
189 is_ok = add_json_file(filename, convert_mode);
195 spdlog::info(
"Add the output source {:?}", filename);
199 spdlog::error(
"The filename {:?} has been already added!", filename);
Manager(workflow::AnalysisHandle *processor)
constexpr auto convert_option_has_dependency(DataConvertOptions dependee, DataConvertOptions depender) -> bool
auto get_filetype_from_filename(std::string_view filename) -> std::tuple< DataWriterOption, process::DataConvertOptions >