ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
awaitState.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_common_libdaq
4  * @copyright 2022 ESO - European Southern Observatory
5  *
6  * @brief Contains declaration for the AwaitStateAsync operation
7  */
8 #ifndef OCM_DAQ_OP_AWAIT_STATE_HPP_
9 #define OCM_DAQ_OP_AWAIT_STATE_HPP_
10 #include "../config.hpp"
11 
12 #include <boost/asio/steady_timer.hpp>
13 #include <boost/signals2/connection.hpp>
14 #include <boost/thread/future.hpp>
15 #include <log4cplus/logger.h>
16 
17 #include "../status.hpp"
18 #include "../utility.hpp"
19 
20 
21 namespace daq::op {
22 
23 
24 /**
25  * Async operation to await Data Acquisition state
26  *
27  * Satisfies concept requirements for op::InitiateAbortableOperation and
28  * op::InitiateOperation.
29  *
30  * @ingroup daq_common_libdaq
31  */
33 public:
35  /**
36  * Constructs operation with the privided parameters.
37  *
38  * @param io_ctx ASIO context.
39  * @param status DAQ status to monitor/observe changes on.
40  * @param state Target state.
41  * @param timeout Operation timeout.
42  */
43  explicit AwaitStateAsync(boost::asio::io_context& io_ctx,
44  std::shared_ptr<ObservableStatus> status,
45  State state,
46  std::chrono::milliseconds timeout);
47 
48  /**
49  * Initiates operation that await state.
50  *
51  * @returns future with value being set when condition is satisifed or times out.
52  * Exception DaqOperationAborted is set when operation is aborted.
53  * ResultType::error is true if operation times out with await condition unsatisfied.
54  *
55  * @note Caller is responsible for keeping object alive until
56  * result is set.
57  */
58  [[nodiscard]] boost::future<ResultType> Initiate();
59 
60  /**
61  * Aborts the operation and completes the operation with DaqOperationAborted.
62  */
63  void Abort() noexcept;
64 
65 private:
66  bool IsConditionSatisfied() const;
67  boost::asio::io_context& m_io_ctx;
68  std::shared_ptr<ObservableStatus> m_status;
69  State m_target;
70  std::chrono::milliseconds m_timeout;
71  /**
72  * Indicates whether operation was aborted or not.
73  *
74  * If this is true it means that m_promise has already been fulfilled.
75  */
76  bool m_aborted;
77 
78  /**
79  * Promise for future returned from `Initiate()`
80  */
81  boost::promise<ResultType> m_promise;
82 
83  boost::asio::steady_timer m_timer;
84  boost::signals2::scoped_connection m_connection;
85  log4cplus::Logger m_logger;
86 };
87 
88 
89 } // namespace daq::op
90 #endif // #ifndef OCM_DAQ_OP_AWAIT_STATE_HPP_
daq::State
State
Observable states of the data acquisition process.
Definition: state.hpp:39
daq::op
Definition: abort.hpp:19
daq::ObservableStatus
Stores data acquisition status and allows subscription to status changes.
Definition: status.hpp:161
daq::op::AwaitStateAsync::Abort
void Abort() noexcept
Aborts the operation and completes the operation with DaqOperationAborted.
Definition: awaitState.cpp:92
daq::op::AwaitStateAsync::Initiate
boost::future< ResultType > Initiate()
Initiates operation that await state.
Definition: awaitState.cpp:32
daq::op::AwaitStateAsync
Async operation to await Data Acquisition state.
Definition: awaitState.hpp:32
daq::Result
Utility class that represents a result and an error.
Definition: utility.hpp:17
daq::op::AwaitStateAsync::AwaitStateAsync
AwaitStateAsync(boost::asio::io_context &io_ctx, std::shared_ptr< ObservableStatus > status, State state, std::chrono::milliseconds timeout)
Constructs operation with the privided parameters.
Definition: awaitState.cpp:18