ifw-daq 3.1.0
IFW Data Acquisition modules
Loading...
Searching...
No Matches
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
10#include <daq/eventLog.hpp>
12#include <daq/source.hpp>
13#include <daq/status.hpp>
14
15namespace daq::op {
16
17struct AlertState {
18 std::vector<Alert> set;
19 std::vector<Alert> cleared;
20
21 void Set(Alert alert) {
22 SetAlert(set, std::move(alert));
23 }
24 void Clear(AlertId id) {
25 auto alert = MakeAlert(std::move(id), "");
26 auto it = std::find(cleared.begin(), cleared.end(), alert);
27 if (it != cleared.end()) {
28 // Replace
29 *it = std::move(alert);
30 } else {
31 // Or add
32 cleared.emplace_back(std::move(alert));
33 }
34 }
35};
36
37/**
38 * Merge alerts.
39 *
40 * It only set/clears if timestamp is newer.
41 */
42inline void MergeAlerts(ObservableStatus& dest, AlertState& src) {
43 auto const& active = dest.GetAlerts();
44 // Order should not be important
45 for (auto& a : src.set) {
46 auto it = std::find(active.begin(), active.end(), a);
47 // Only set alert if new or newer
48 if (it == active.end() || a.timestamp > it->timestamp) {
49 dest.SetAlert(std::move(a));
50 }
51 }
52 for (auto& a : src.cleared) {
53 auto it = std::find(active.begin(), active.end(), a);
54 // Only clear alert if timestamp of `a` is newer
55 if (it != active.end() && a.timestamp > it->timestamp) {
56 dest.ClearAlert(a.id);
57 }
58 }
59}
60
61/**
62 * Parameters required for each async operation.
63 *
64 * Caller is responsible for keeping objects alive for the duration of the operation.
65 */
68 ObservableEventLog& event_log_arg,
70 rad::IoExecutor& executor_arg,
71 log4cplus::Logger& logger_arg,
72 std::string const& id_arg,
73 PendingReplies& pending_replies_arg,
74 std::vector<Source<PrimSource>>& prim_sources_arg,
75 std::vector<Source<MetaSource>>& meta_sources_arg,
76 fits::KeywordFormatter const& kw_formatter_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 , kw_formatter(kw_formatter_arg) {
87 }
88 AsyncOpParams(AsyncOpParams const&) = default;
90
91 /**
92 * Async operations should not modify state directly
93 * DaqController does that. The only allowed operation is to set/clear alerts.
94 */
97 /**
98 * Alerts to be merged only after completion of async operation.
99 */
102 log4cplus::Logger& logger;
103 std::string const& id;
105 std::vector<Source<PrimSource>>& prim_sources; ///< Note: Consider vector immutable!
106 std::vector<Source<MetaSource>>& meta_sources; ///< Note: Consider vector immutable!a
108};
109
110/**
111 * Await specific parameters that is not provided with AsyncOpParams.
112 *
113 * @important The clients provided must be configured with a reply timeout that takes the wait
114 * timeout into account, otherwise MAL will time out before reply is sent.
115 */
117 using Duration = std::chrono::milliseconds;
118 AwaitOpParams(AsyncOpParams common_arg, AwaitOpParams::Duration wait_interval_arg) noexcept
119 : common(common_arg), wait_interval(wait_interval_arg) {
120 }
121 AwaitOpParams(AwaitOpParams const&) = default;
123
125
126 /**
127 * Total amount of time to wait for condition to be fulfilled.
128 */
130};
131
132} // namespace daq::op
133
134#endif // #ifndef DAQ_OP_ASYNC_OP_PARAMS_HPP_
Stores data acquisition status and allows subscription to status changes.
Definition: eventLog.hpp:107
Stores data acquisition status and allows subscription to status changes.
Definition: status.hpp:224
void ClearAlert(AlertId const &alert)
Clear alert.
Definition: status.cpp:341
void SetAlert(Alert alert)
Set alert.
Definition: status.cpp:336
Alerts const & GetAlerts() const noexcept
Definition: status.cpp:285
Simple class that allows you to keep track of how many replies are pending.
Formats keyword against e.g.
Definition: keyword.hpp:551
Adapts boost::asio::io_context into a compatible boost::thread Executor type.
Definition: ioExecutor.hpp:12
Contains declaration for EventLog, ObservableEventLog and related events.
Contains data structure for FITS keywords.
void MergeAlerts(ObservableStatus &dest, AlertState &src)
Merge alerts.
void SetAlert(std::vector< Alert > &alerts, Alert alert)
Set alert.
Definition: status.cpp:20
Alert MakeAlert(std::string_view category, std::string key, std::string description)
Construct alert.
Definition: status.cpp:45
Describes an active Data Acquisition alert.
Definition: status.hpp:95
Uniquely identfies an alert.
Definition: status.hpp:68
Contains declaration for classes related to pending replies.
Declarations for daq::Source and related classes.
Contains declaration for Status and ObservableStatus.
Simple class that holds the source and associated state.
Definition: source.hpp:30
void Set(Alert alert)
void Clear(AlertId id)
std::vector< Alert > set
std::vector< Alert > cleared
Parameters required for each async operation.
std::string const & id
std::vector< Source< MetaSource > > & meta_sources
Note: Consider vector immutable!a.
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, fits::KeywordFormatter const &kw_formatter_arg)
ObservableEventLog & event_log
fits::KeywordFormatter const & kw_formatter
AsyncOpParams(AsyncOpParams &&)=default
AsyncOpParams(AsyncOpParams const &)=default
AlertState & alerts
Alerts to be merged only after completion of async operation.
std::vector< Source< PrimSource > > & prim_sources
Note: Consider vector immutable!
PendingReplies & pending_replies
rad::IoExecutor & executor
ObservableStatus & status
Async operations should not modify state directly DaqController does that.
log4cplus::Logger & logger
Await specific parameters that is not provided with AsyncOpParams.
AwaitOpParams(AwaitOpParams const &)=default
Duration wait_interval
Total amount of time to wait for condition to be fulfilled.
AwaitOpParams(AwaitOpParams &&)=default
AwaitOpParams(AsyncOpParams common_arg, AwaitOpParams::Duration wait_interval_arg) noexcept
std::chrono::milliseconds Duration