srs-control 0.2.2
Loading...
Searching...
No Matches
Application.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <asio/executor_work_guard.hpp>
7#include <asio/ip/udp.hpp>
8#include <asio/signal_set.hpp>
9#include <asio/steady_timer.hpp>
10#include <asio/strand.hpp>
11#include <asio/thread_pool.hpp>
12#include <csignal>
13#include <cstddef>
14#include <cstdint>
15#include <expected>
16#include <future>
17#include <memory>
18#include <string>
19#include <string_view>
20#include <system_error>
21#include <thread>
22#include <utility>
23#include <vector>
24
25namespace srs
26{
27 class AppReport;
28 namespace workflow
29 {
30 class AnalysisHandle;
31 } // namespace workflow
32
33 namespace connection
34 {
35 class DataReader;
36 class DataSocket;
37 } // namespace connection
38
39 class App;
40
41 namespace internal
42 {
48 {
49 public:
53 explicit AppExitHelper(App* app)
54 : app_{ app }
55 {
56 }
57
58 AppExitHelper(const AppExitHelper&) = default;
65 ~AppExitHelper() noexcept;
66
67 private:
69 };
70
71 } // namespace internal
72
80
81 class App
82 {
83 public:
94
95 App(const App&) = delete;
96 App(App&&) = delete;
97 App& operator=(const App&) = delete;
98 App& operator=(App&&) = delete;
99
122 ~App() noexcept;
123
127 void init();
128
130
134 void switch_on();
135
139 void switch_off();
140
149 void read_data(bool is_non_stop = true);
150
157 void start_workflow();
158
163
164 // setters:
168 void set_fec_data_receiv_ports(const std::vector<int>& port_nums)
169 {
170 config_.fec_data_receive_ports = port_nums;
171 }
172
176 void add_fec_data_receiv_port(int port_num) { config_.fec_data_receive_ports.push_back(port_num); }
177
181 void set_print_mode(common::DataPrintMode mode);
182
186 void set_output_filenames(const std::vector<std::string>& filenames, std::size_t n_lines = 1);
187
191 void set_options(Config options) { config_ = std::move(options); }
192
196 void set_runtime_sec(std::size_t runtime_sec) { runtime_sec_ = runtime_sec; }
197
198 void exit_and_switch_off();
199
200 // internal usage
201 // TODO: Refactor the code to not expose those methods for the internal usage
202
203 // getters:
204 [[nodiscard]] auto get_channel_address() const -> uint16_t { return channel_address_; }
205 [[nodiscard]] auto get_io_context() -> auto& { return io_context_; }
206 [[nodiscard]] auto get_fec_strand() -> auto& { return fec_strand_; }
207 [[nodiscard]] auto get_workflow_handler() const -> const auto& { return *workflow_handler_; };
208 [[nodiscard]] auto get_config() const -> const auto& { return config_; }
209 [[nodiscard]] auto get_config_ref() -> auto& { return config_; }
210 [[nodiscard]] auto get_report() -> AppReport& { return *report_; }
211
212 // called by ExitHelper
213 void action_after_destructor();
214
215 private:
216 using udp = asio::ip::udp;
217 using SwitchFutureType = std::expected<std::future<void>, std::error_code>;
218 using SwitchFutureStatusType = std::expected<std::future_status, std::error_code>;
219
222
223 // Destructors are called in the inverse order
224
225 std::unique_ptr<AppReport> report_;
226
230
234 std::size_t runtime_sec_{};
235
238 asio::executor_work_guard<io_context_type::executor_type> io_work_guard_;
239
242 std::vector<udp::endpoint> remote_fec_endpoints_;
243
246 asio::signal_set signal_set_{ io_context_, SIGINT, SIGTERM };
247
250 asio::steady_timer cancel_timer_{ io_context_ };
251
254 asio::strand<io_context_type::executor_type> fec_strand_;
255
258 std::jthread working_thread_;
259
262 std::jthread workflow_thread_;
263
265
267
273
276 std::unique_ptr<workflow::AnalysisHandle> workflow_handler_;
277
280 // std::shared_ptr<connection::DataReader> data_reader_;
281
282 std::vector<std::shared_ptr<connection::DataSocket>> data_sockets_;
283
284 void print_statistics() const;
285 void init_spdlog();
288 void set_cancel_method();
289 void add_remote_fec_endpoint(std::string_view remote_ip, int port_number);
290 static auto wait_for_switch_action(const SwitchFutureType& switch_future) -> SwitchFutureStatusType;
291
292 template <typename T>
293 auto switch_FECs(std::string_view connection_name) -> SwitchFutureType;
294
295 auto check_port_number_validity() -> bool;
296 };
297
298} // namespace srs
The primary interface class of SRS-Control.
asio::executor_work_guard< io_context_type::executor_type > io_work_guard_
Asio io_context work guard.
void set_fec_data_receiv_ports(const std::vector< int > &port_nums)
Set the local listen port numbers for the communications to FEC devices.
void init()
Initialization of internal members.
SwitchFutureType switch_on_future_
App & operator=(const App &)=delete
uint16_t channel_address_
void add_remote_fec_endpoint(std::string_view remote_ip, int port_number)
auto get_config() const -> const auto &
asio::signal_set signal_set_
User signal handler for interrupts.
std::vector< udp::endpoint > remote_fec_endpoints_
Remote endpoints of FEC devices.
auto get_workflow_handler() const -> const auto &
std::jthread working_thread_
Main working thread.
asio::steady_timer cancel_timer_
Cancelling timer.
auto get_channel_address() const -> uint16_t
void set_cancel_method()
std::vector< std::shared_ptr< connection::DataSocket > > data_sockets_
Communication to the main input data stream.
asio::ip::udp udp
App & operator=(App &&)=delete
auto get_report() -> AppReport &
void init_spdlog()
std::unique_ptr< AppReport > report_
std::unique_ptr< workflow::AnalysisHandle > workflow_handler_
The handler to the analysis working flow.
internal::AppExitHelper exit_helper_
Exit helper for App class. This is called after calling the destructor.
auto get_fec_strand() -> auto &
asio::strand< io_context_type::executor_type > fec_strand_
FEC communication strand for synchronous communications.
auto check_port_number_validity() -> bool
App(const App &)=delete
auto get_io_context() -> auto &
void set_options(Config options)
Set the configuration values.
void set_remote_fec_endpoints()
void configure_fec()
static auto wait_for_switch_action(const SwitchFutureType &switch_future) -> SwitchFutureStatusType
SwitchFutureType switch_off_future_
Config config_
void add_fec_data_receiv_port(int port_num)
Add a local listen port number for the communications to FEC devices.
io_context_type io_context_
Asio io_context that manages the task scheduling and network IO.
void print_statistics() const
std::jthread workflow_thread_
Main thread to run workflow.
void set_runtime_sec(std::size_t runtime_sec)
Set the runtime in seconds.
auto get_config_ref() -> auto &
App(Config config)
Constructor of the App class.
std::size_t runtime_sec_
Time duration (seconds) to stop the application. If value is zero, the application is in non-stop mod...
auto switch_FECs(std::string_view connection_name) -> SwitchFutureType
App(App &&)=delete
std::expected< std::future< void >, std::error_code > SwitchFutureType
void wait_for_reading_finish()
void wait_for_workflow()
Wait for the workflow process to finish.
std::expected< std::future_status, std::error_code > SwitchFutureStatusType
Socket class for reading main data stream.
An internal exit helper for App class.
AppExitHelper & operator=(const AppExitHelper &)=default
~AppExitHelper() noexcept
Destructor calling srs::App::end_of_work method.
AppExitHelper & operator=(AppExitHelper &&)=delete
AppExitHelper(const AppExitHelper &)=default
AppExitHelper(AppExitHelper &&)=delete
AppExitHelper(App *app)
Constructor with pointer to srs::App instance.
constexpr auto DEFAULT_CHANNEL_ADDRE
DataPrintMode
Print mode of the status line.
asio::thread_pool io_context_type
Main configuration struct.