ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
asyncOpParams.hpp
Go to the documentation of this file.
1 #ifndef DAQ_OP_ASYNC_OP_PARAMS_HPP_
2 #define DAQ_OP_ASYNC_OP_PARAMS_HPP_
3 
4 #include <boost/asio/io_context.hpp>
5 #include <log4cplus/logger.h>
6 
7 #include <rad/ioExecutor.hpp>
8 
9 #include "../status.hpp"
10 #include "../eventLog.hpp"
11 #include "../pendingReplies.hpp"
12 #include "../source.hpp"
13 #include "../state.hpp"
14 #include "../utility.hpp"
15 
16 namespace daq::op {
17 
18 struct AlertState {
19  std::vector<Alert> set;
20  std::vector<Alert> cleared;
21 
22  void Set(Alert alert) {
23  SetAlert(set, std::move(alert));
24  }
25  void Clear(AlertId id) {
26  auto alert = MakeAlert(std::move(id), "");
27  auto it = std::find(cleared.begin(), cleared.end(), alert);
28  if (it != cleared.end()) {
29  // Replace
30  *it = std::move(alert);
31  } else {
32  // Or add
33  cleared.emplace_back(std::move(alert));
34  }
35  }
36 };
37 
38 /**
39  * Merge alerts.
40  *
41  * It only set/clears if timestamp is newer.
42  */
43 inline void MergeAlerts(ObservableStatus& dest, AlertState& src) {
44  auto const& active = dest.GetAlerts();
45  // Order should not be important
46  for (auto& a : src.set) {
47  auto it = std::find(active.begin(), active.end(), a);
48  // Only set alert if new or newer
49  if (it == active.end() || a.timestamp > it->timestamp) {
50  dest.SetAlert(std::move(a));
51  }
52  }
53  for (auto& a : src.cleared) {
54  auto it = std::find(active.begin(), active.end(), a);
55  // Only clear alert if timestamp of `a` is newer
56  if (it != active.end() && a.timestamp > it->timestamp) {
57  dest.ClearAlert(a.id);
58  }
59  }
60 }
61 
62 /**
63  * Parameters required for each async operation.
64  *
65  * Caller is responsible for keeping objects alive for the duration of the operation.
66  */
67 struct AsyncOpParams {
69  ObservableEventLog& event_log_arg,
71  rad::IoExecutor& executor_arg,
72  log4cplus::Logger& logger_arg,
73  std::string const& id_arg,
74  PendingReplies& pending_replies_arg,
75  std::vector<Source<PrimSource>>& prim_sources_arg,
76  std::vector<Source<MetaSource>>& meta_sources_arg)
77  : status(status_arg)
78  , event_log(event_log_arg)
79  , alerts(alerts)
80  , executor(executor_arg)
81  , logger(logger_arg)
82  , id(id_arg)
83  , pending_replies(pending_replies_arg)
84  , prim_sources(prim_sources_arg)
85  , meta_sources(meta_sources_arg) {
86  }
87  AsyncOpParams(AsyncOpParams const&) = default;
89 
90  /**
91  * Async operations should not modify state directly
92  * DaqController does that. The only allowed operation is to set/clear alerts.
93  */
96  /**
97  * Alerts to be merged only after completion of async operation.
98  */
101  log4cplus::Logger& logger;
102  std::string const& id;
104  std::vector<Source<PrimSource>>& prim_sources; ///< Note: Consider vector immutable!
105  std::vector<Source<MetaSource>>& meta_sources; ///< Note: Consider vector immutable!a
106 };
107 
108 /**
109  * Await specific parameters that is not provided with AsyncOpParams.
110  *
111  * @important The clients provided must be configured with a reply timeout that takes the wait
112  * timeout into account, otherwise MAL will time out before reply is sent.
113  */
115  using Duration = std::chrono::milliseconds;
116  AwaitOpParams(AsyncOpParams common_arg, AwaitOpParams::Duration wait_interval_arg) noexcept
117  : common(common_arg), wait_interval(wait_interval_arg) {
118  }
119  AwaitOpParams(AwaitOpParams const&) = default;
121 
123 
124  /**
125  * Total amount of time to wait for condition to be fulfilled.
126  */
128 };
129 
130 } // namespace daq::op
131 
132 #endif // #ifndef DAQ_OP_ASYNC_OP_PARAMS_HPP_
daq::op::AsyncOpParams::prim_sources
std::vector< Source< PrimSource > > & prim_sources
Note: Consider vector immutable!
Definition: asyncOpParams.hpp:104
daq::op::AsyncOpParams::logger
log4cplus::Logger & logger
Definition: asyncOpParams.hpp:101
daq::op::AsyncOpParams::pending_replies
PendingReplies & pending_replies
Definition: asyncOpParams.hpp:103
daq::ObservableStatus::SetAlert
void SetAlert(Alert alert)
Set alert.
Definition: status.cpp:218
daq::op::AsyncOpParams::AsyncOpParams
AsyncOpParams(AsyncOpParams const &)=default
ioExecutor.hpp
daq::op::AsyncOpParams::meta_sources
std::vector< Source< MetaSource > > & meta_sources
Note: Consider vector immutable!a.
Definition: asyncOpParams.hpp:105
daq::op::AwaitOpParams::AwaitOpParams
AwaitOpParams(AwaitOpParams &&)=default
daq::op::AsyncOpParams::AsyncOpParams
AsyncOpParams(ObservableStatus &status_arg, ObservableEventLog &event_log_arg, AlertState &alerts, rad::IoExecutor &executor_arg, log4cplus::Logger &logger_arg, std::string const &id_arg, PendingReplies &pending_replies_arg, std::vector< Source< PrimSource >> &prim_sources_arg, std::vector< Source< MetaSource >> &meta_sources_arg)
Definition: asyncOpParams.hpp:68
daq::PendingReplies
Simple class that allows you to keep track of how many replies are pending.
Definition: pendingReplies.hpp:58
daq::SetAlert
void SetAlert(std::vector< Alert > &alerts, Alert alert)
Set alert.
Definition: status.cpp:9
rad::IoExecutor
Adapts boost::asio::io_context into a compatible boost::thread Executor type.
Definition: ioExecutor.hpp:12
daq::op::AsyncOpParams::event_log
ObservableEventLog & event_log
Definition: asyncOpParams.hpp:95
daq::Source
Simple class that holds the source and associated state.
Definition: source.hpp:29
daq::op::AlertState
Definition: asyncOpParams.hpp:18
daq::op::AsyncOpParams::status
ObservableStatus & status
Async operations should not modify state directly DaqController does that.
Definition: asyncOpParams.hpp:94
daq::op::AwaitOpParams
Await specific parameters that is not provided with AsyncOpParams.
Definition: asyncOpParams.hpp:114
daq::op::AwaitOpParams::AwaitOpParams
AwaitOpParams(AsyncOpParams common_arg, AwaitOpParams::Duration wait_interval_arg) noexcept
Definition: asyncOpParams.hpp:116
daq::op::AwaitOpParams::Duration
std::chrono::milliseconds Duration
Definition: asyncOpParams.hpp:115
daq::op
Definition: abort.hpp:19
daq::op::AsyncOpParams::executor
rad::IoExecutor & executor
Definition: asyncOpParams.hpp:100
daq::op::AsyncOpParams::alerts
AlertState & alerts
Alerts to be merged only after completion of async operation.
Definition: asyncOpParams.hpp:99
daq::ObservableStatus
Stores data acquisition status and allows subscription to status changes.
Definition: status.hpp:161
daq::op::AlertState::cleared
std::vector< Alert > cleared
Definition: asyncOpParams.hpp:20
daq::Alert
Describes an active Data Acquisition alert.
Definition: status.hpp:71
daq::op::AsyncOpParams::id
std::string const & id
Definition: asyncOpParams.hpp:102
daq::op::AlertState::Set
void Set(Alert alert)
Definition: asyncOpParams.hpp:22
daq::ObservableStatus::ClearAlert
void ClearAlert(AlertId const &alert)
Clear alert.
Definition: status.cpp:223
daq::MakeAlert
Alert MakeAlert(std::string_view category, std::string key, std::string description)
Construct alert.
Definition: status.cpp:29
daq::op::AwaitOpParams::AwaitOpParams
AwaitOpParams(AwaitOpParams const &)=default
daq::op::AlertState::Clear
void Clear(AlertId id)
Definition: asyncOpParams.hpp:25
daq::ObservableStatus::GetAlerts
std::vector< Alert > const & GetAlerts() const noexcept
Definition: status.cpp:176
daq::op::AwaitOpParams::common
AsyncOpParams common
Definition: asyncOpParams.hpp:122
daq::AlertId
Uniquely identfies an alert.
Definition: status.hpp:52
daq::op::MergeAlerts
void MergeAlerts(ObservableStatus &dest, AlertState &src)
Merge alerts.
Definition: asyncOpParams.hpp:43
daq::op::AsyncOpParams
Parameters required for each async operation.
Definition: asyncOpParams.hpp:67
daq::op::AwaitOpParams::wait_interval
Duration wait_interval
Total amount of time to wait for condition to be fulfilled.
Definition: asyncOpParams.hpp:127
daq::op::AlertState::set
std::vector< Alert > set
Definition: asyncOpParams.hpp:19
daq::ObservableEventLog
Stores data acquisition status and allows subscription to status changes.
Definition: eventLog.hpp:107
daq::op::AsyncOpParams::AsyncOpParams
AsyncOpParams(AsyncOpParams &&)=default