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)
23 , m_status(std::move(status))
28 , m_logger(log4cplus::Logger::getInstance(
"daq.op.awaitstate")) {
33 LOG4CPLUS_DEBUG(m_logger,
34 fmt::format(
"AwaitStateAsync::Initate(): Awaiting state {} with timeout {}ms",
37 if (IsConditionSatisfied()) {
39 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](boost::system::error_code
const&
error) {
64 LOG4CPLUS_DEBUG(m_logger,
"Timer cancelled: Doing nothing.");
69 LOG4CPLUS_INFO(m_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 {