SRS-control 0.1.4
Loading...
Searching...
No Matches
Application.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <boost/asio/executor_work_guard.hpp>
7#include <boost/asio/ip/udp.hpp>
8#include <boost/asio/signal_set.hpp>
9#include <boost/asio/strand.hpp>
10#include <boost/asio/thread_pool.hpp>
11#include <boost/system/detail/error_code.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 <thread>
21#include <utility>
22#include <vector>
23
24// #include <srs/devices/Fec.hpp>
25namespace srs
26{
27 namespace workflow
28 {
29 class Handler;
30 } // namespace workflow
31
32 namespace connection
33 {
34 class DataReader;
35 class DataSocket;
36 } // namespace connection
37
38 class App;
39
40 namespace internal
41 {
47 {
48 public:
52 explicit AppExitHelper(App* app)
53 : app_{ app }
54 {
55 }
56
57 AppExitHelper(const AppExitHelper&) = default;
64 ~AppExitHelper() noexcept;
65
66 private:
68 };
69
70 } // namespace internal
71
79
80 class App
81 {
82 public:
92 App();
93
94 App(const App&) = delete;
95 App(App&&) = delete;
96 App& operator=(const App&) = delete;
97 App& operator=(App&&) = delete;
98
121 ~App() noexcept;
122
126 void init();
127
129
133 void switch_on();
134
138 void switch_off();
139
148 void read_data(bool is_non_stop = true);
149
156 void start_workflow();
157
164 void wait_for_finish();
165
167
168 // setters:
172 void set_fec_data_receiv_port(int port_num) { config_.fec_data_receive_port = port_num; }
173
177 void set_print_mode(common::DataPrintMode mode);
178
182 void set_output_filenames(const std::vector<std::string>& filenames, std::size_t n_lines = 1);
183
187 void set_options(Config options) { config_ = std::move(options); }
188
189 void exit_and_switch_off();
190
191 // internal usage
192 // TODO: Refactor the code to not expose those methods for the internal usage
193
194 // getters:
195 [[nodiscard]] auto get_channel_address() const -> uint16_t { return channel_address_; }
196 [[nodiscard]] auto get_io_context() -> auto& { return io_context_; }
197 [[nodiscard]] auto get_fec_strand() -> auto& { return fec_strand_; }
198 [[nodiscard]] auto get_workflow_handler() const -> const auto& { return *workflow_handler_; };
199 [[nodiscard]] auto get_config() const -> const auto& { return config_; }
200 [[nodiscard]] auto get_config_ref() -> auto& { return config_; }
201
202 // called by ExitHelper
203 void action_after_destructor();
204
205 private:
206 using udp = asio::ip::udp;
207 using SwitchFutureType = std::expected<std::future<void>, boost::system::error_code>;
208 using SwitchFutureStatusType = std::expected<std::future_status, boost::system::error_code>;
209
212
213 // Destructors are called in the inverse order
214
218
221 asio::executor_work_guard<io_context_type::executor_type> io_work_guard_;
222
225 std::vector<udp::endpoint> remote_fec_endpoints_;
226
229 asio::signal_set signal_set_{ io_context_, SIGINT, SIGTERM };
230
233 asio::strand<io_context_type::executor_type> fec_strand_;
234
237 std::jthread working_thread_;
238
241 std::jthread workflow_thread_;
242
244
246
252
255 std::unique_ptr<workflow::Handler> workflow_handler_;
256
259 // std::shared_ptr<connection::DataReader> data_reader_;
260
261 std::shared_ptr<connection::DataSocket> data_socket_;
262
265 void add_remote_fec_endpoint(std::string_view remote_ip, int port_number);
266 static auto wait_for_switch_action(const SwitchFutureType& switch_future) -> SwitchFutureStatusType;
267
268 template <typename T>
269 auto switch_FECs(std::string_view connection_name) -> SwitchFutureType;
270 };
271
272} // 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_port(int port_num)
Set the local listen port number 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.
std::expected< std::future< void >, boost::system::error_code > SwitchFutureType
auto get_workflow_handler() const -> const auto &
std::jthread working_thread_
Main working thread.
std::expected< std::future_status, boost::system::error_code > SwitchFutureStatusType
std::unique_ptr< workflow::Handler > workflow_handler_
The handler to the analysis working flow.
auto get_channel_address() const -> uint16_t
asio::ip::udp udp
App & operator=(App &&)=delete
App()
Constructor of the App class.
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.
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()
std::shared_ptr< connection::DataSocket > data_socket_
Communication to the main input data stream.
static auto wait_for_switch_action(const SwitchFutureType &switch_future) -> SwitchFutureStatusType
SwitchFutureType switch_off_future_
Config config_
io_context_type io_context_
Asio io_context that manages the task scheduling and network IO.
std::jthread workflow_thread_
Main thread to run workflow.
auto get_config_ref() -> auto &
auto switch_FECs(std::string_view connection_name) -> SwitchFutureType
App(App &&)=delete
void wait_for_reading_finish()
void wait_for_workflow()
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.