10 #include <fmt/format.h>
11 #include <fmt/ostream.h>
12 #include <log4cplus/loggingmacros.h>
19 std::shared_ptr<ObservableStatus> status,
21 std::chrono::milliseconds timeout,
22 log4cplus::Logger
const& logger)
24 , m_status(std::move(status))
34 LOG4CPLUS_DEBUG(m_logger,
35 fmt::format(
"AwaitStateAsync::Initate(): Awaiting state {} with timeout {}ms",
38 if (IsConditionSatisfied()) {
40 return boost::make_ready_future<ResultType>(
ResultType{
false, m_status->GetStatus()});
45 if (IsConditionSatisfied()) {
46 LOG4CPLUS_DEBUG(m_logger,
47 fmt::format(
"Await condition is satisfied by status change "
48 "-> setting promise: ",
51 m_promise.set_value(
ResultType{
false, m_status->GetStatus()});
53 }
catch (boost::promise_already_satisfied
const&) {
55 LOG4CPLUS_DEBUG(m_logger,
"Promise already satisfied");
60 m_timer.expires_after(m_timeout);
61 m_timer.async_wait([
this, logger=m_logger](boost::system::error_code
const&
error) {
64 LOG4CPLUS_DEBUG(logger,
"Timer cancelled: Doing nothing.");
69 LOG4CPLUS_INFO(logger,
"Operation aborted: Doing nothing.");
74 bool timeout = !IsConditionSatisfied();
77 fmt::format(
"AwaitStateAsync: {}: Timer expired waiting for target state '{}'. "
78 "Current state is '{}'",
81 m_status->GetState()));
82 m_promise.set_value(
ResultType{timeout, m_status->GetStatus()});
83 }
catch (boost::promise_already_satisfied
const&) {
85 LOG4CPLUS_DEBUG(m_logger,
"Promise already satisfied");
89 return m_promise.get_future();
94 LOG4CPLUS_INFO(m_logger,
"Aborting operation.");
96 fmt::format(
"AwaitStateAsync: {}: Operation aborted while waiting for target state {}. "
97 "Current state is {}",
100 m_status->GetState())));
104 m_connection.disconnect();
105 }
catch (boost::promise_already_satisfied
const&) {
109 bool AwaitStateAsync::IsConditionSatisfied()
const {
Contains declaration for the AwaitStateAsync operation.
Started operation was aborted.
Stores data acquisition status and allows subscription to status changes.
Contains error related declarations for DAQ.
bool IsSubsequentState(State state1, State state2) noexcept
Compares states and returns whether state1 occurs after state2.
State
Observable states of the data acquisition process.
Utility class that represents a result and an error.
boost::future< ResultType > Initiate()
Initiates operation that await state.
void Abort() noexcept
Aborts the operation and completes the operation with DaqOperationAborted.
AwaitStateAsync(boost::asio::io_context &io_ctx, std::shared_ptr< ObservableStatus > status, State state, std::chrono::milliseconds timeout, log4cplus::Logger const &logger)
Constructs operation with the privided parameters.