srs-control 0.2.2
Loading...
Searching...
No Matches
DataWriter.cpp
Go to the documentation of this file.
1#include "DataWriter.hpp"
9#include <algorithm>
10#include <asio/ip/udp.hpp>
11#include <asio/thread_pool.hpp>
12#include <magic_enum/magic_enum.hpp>
13#include <map>
14#include <memory>
15#include <optional>
16#include <ranges>
17#include <spdlog/spdlog.h>
18#include <string>
19#include <string_view>
20#include <system_error>
21#include <utility>
22#include <vector>
23
24#ifdef HAS_ROOT
26#endif
27
28namespace srs::sink
29{
30 namespace
31 {
32 auto convert_str_to_endpoint(asio::thread_pool& thread_pool, std::string_view ip_port)
33 -> std::optional<asio::ip::udp::endpoint>
34 {
35 const auto question_pos = ip_port.find('?');
36 const auto colon_pos = ip_port.find(':');
37 if (colon_pos == std::string::npos)
38 {
39 spdlog::critical("Ill format socket string {:?}. Please set it as \"ip:port\"", ip_port);
40 return {};
41 }
42 auto ip_string = ip_port.substr(0, colon_pos);
43 auto port_str =
44 (question_pos == std::string::npos) ? ip_port.substr(colon_pos + 1) : ip_port.substr(question_pos + 1);
45
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();
49 if (err_code)
50 {
51 spdlog::critical("Cannot query the ip address {:?} with port {:?}. Error code: {}",
52 ip_string,
53 port_str,
54 err_code.message());
55 return {};
56 }
57 return *iter;
58 }
59
60 auto check_root_dependency() -> bool
61 {
62#ifdef HAS_ROOT
63 return true;
64#else
65 spdlog::error("Cannot output to a root file. Please make sure the program is "
66 "built with the ROOT library.");
67 return false;
68#endif
69 }
70
71 } // namespace
72
74 : workflow_handler_{ processor }
75 {
76 }
77
78 Manager::~Manager() = default;
79
81 {
82 auto is_required = false;
83 do_for_each_sink([&is_required, dependee](std::string_view, auto& writer)
84 { is_required |= convert_option_has_dependency(dependee, writer.get_required_conversion()); });
85 return is_required;
86 }
87
89 {
90 constexpr auto conversions = magic_enum::enum_values<process::DataConvertOptions>();
91 auto convert_map =
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))
97 {
98 convert_map[process::DataConvertOptions::none] = true;
99 }
100 return convert_map;
101 }
102
104 {
105 if (frame_count_checker_ == nullptr)
106 {
107 frame_count_checker_ = std::make_unique<FrameCountChecker>(workflow_handler_->get_n_lines());
108 }
109 }
110
111 auto Manager::add_binary_file(const std::string& filename, process::DataConvertOptions prev_conversion) -> bool
112 {
113 return binary_files_
114 .try_emplace(filename,
115 std::make_unique<BinaryFile>(filename, prev_conversion, workflow_handler_->get_n_lines()))
116 .second;
117 }
118
119 auto Manager::add_udp_file(const std::string& filename, process::DataConvertOptions prev_conversion) -> bool
120 {
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())
124 {
125 return udp_files_
126 .try_emplace(filename,
127 std::make_unique<UDP>(app.get_io_context(),
128 std::move(endpoint.value()),
129 workflow_handler_->get_n_lines(),
130 prev_conversion))
131 .second;
132 }
133 return false;
134 }
135
136 auto Manager::add_root_file([[maybe_unused]] const std::string& filename,
137 [[maybe_unused]] process::DataConvertOptions prev_conversion) -> bool
138 {
139#ifdef HAS_ROOT
140 return root_files_
141 .try_emplace(
142 filename,
143 std::make_unique<RootFile>(filename.c_str(), prev_conversion, workflow_handler_->get_n_lines()))
144 .second;
145#else
146 return false;
147#endif
148 }
149
150 auto Manager::add_json_file(const std::string& filename, process::DataConvertOptions prev_conversion) -> bool
151 {
152 return json_files_
153 .try_emplace(filename, std::make_unique<Json>(filename, prev_conversion, workflow_handler_->get_n_lines()))
154 .second;
155 }
156
157 void Manager::set_output_filenames(const std::vector<std::string>& filenames)
158 {
159 for (const auto& filename : filenames)
160 {
161 if (filename.empty())
162 {
163 continue;
164 }
165 const auto [filetype, convert_mode] = get_filetype_from_filename(filename);
166
167 auto is_ok = false;
168
169 switch (filetype)
170 {
171 case no_output:
172 spdlog::error("Extension of the filename {:?} cannot be recognized!", filename);
173 continue;
174 break;
175 case bin:
176 is_ok = add_binary_file(filename, convert_mode);
177 break;
178 case udp:
179 is_ok = add_udp_file(filename, convert_mode);
180 break;
181 case root:
182 if (not check_root_dependency())
183 {
184 continue;
185 }
186 is_ok = add_root_file(filename, convert_mode);
187 break;
188 case json:
189 is_ok = add_json_file(filename, convert_mode);
190 break;
191 }
192
193 if (is_ok)
194 {
195 spdlog::info("Add the output source {:?}", filename);
196 }
197 else
198 {
199 spdlog::error("The filename {:?} has been already added!", filename);
200 }
201 }
202 }
203} // namespace srs::sink
DataConvertOptions
auto add_root_file(const std::string &filename, process::DataConvertOptions prev_conversion) -> bool
void enable_frame_count_checker()
std::map< std::string, std::unique_ptr< BinaryFile > > binary_files_
Definition Manager.hpp:72
std::unique_ptr< FrameCountChecker > frame_count_checker_
Definition Manager.hpp:73
std::map< std::string, std::unique_ptr< Json > > json_files_
Definition Manager.hpp:75
void set_output_filenames(const std::vector< std::string > &filenames)
auto add_binary_file(const std::string &filename, process::DataConvertOptions prev_conversion) -> bool
auto is_convert_required(process::DataConvertOptions dependee) const -> bool
Manager(workflow::AnalysisHandle *processor)
std::map< std::string, std::unique_ptr< UDP > > udp_files_
Definition Manager.hpp:74
auto add_json_file(const std::string &filename, process::DataConvertOptions prev_conversion) -> bool
workflow::AnalysisHandle * workflow_handler_
Definition Manager.hpp:79
void do_for_each_sink(SinkVisitor auto visitor)
Definition Manager.hpp:104
auto add_udp_file(const std::string &filename, process::DataConvertOptions prev_conversion) -> bool
auto generate_conversion_req_map() const -> std::map< process::DataConvertOptions, bool >
auto get_filetype_from_filename(std::string_view filename) -> std::tuple< DataWriterOption, process::DataConvertOptions >