ifw-daq  3.0.0-pre2
IFW Data Acquisition modules
source.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 Declarations for `daq::Source` and related classes.
7  */
8 #ifndef OCF_DAQ_SOURCE_HPP_
9 #define OCF_DAQ_SOURCE_HPP_
10 #include "config.hpp"
11 
12 #include <memory>
13 #include <string>
14 
15 #include <Metadaqif.hpp>
16 #include <Recif.hpp>
17 #include <boost/signals2/signal.hpp>
18 
19 #include "state.hpp"
20 
21 namespace daq {
22 
23 /**
24  * Simple class that holds the source and associated state.
25  *
26  * @ingroup daq_common_libdaq
27  */
28 template <class T>
29 struct Source {
30  using StateSignal = boost::signals2::signal<void(State, bool)>;
31 
32  Source(T&& s) : m_source(std::move(s)) {
33  }
34  friend std::ostream& operator<<(std::ostream& os, Source<T> const& s) {
35  os << s.GetSource() << ", state=" << s.GetState()
36  << ", error_flag=" << (s.GetErrorFlag() ? "true" : "false");
37  return os;
38  }
39 
40  /**
41  * Connect `subscriber` that is invoked on state changes.
42  *
43  * Type requirements of `subscriber`:
44  * Signature void(State, bool)
45  */
46  template <class Subscriber>
47  boost::signals2::connection ConnectStateListener(Subscriber subscriber) {
48  return m_state_signal.connect(std::move(subscriber));
49  }
50 
51  void SetState(State state, std::optional<bool> error_flag = {}) {
52  if (error_flag) {
53  m_error_flag = *error_flag;
54  }
55  m_state = state;
57  }
58 
59  void ClearErrorFlag() {
60  m_error_flag = false;
62  }
63 
64  void SetErrorFlag() {
65  m_error_flag = true;
67  }
68 
69  bool GetErrorFlag() const {
70  return m_error_flag;
71  }
72  State GetState() const {
73  return m_state;
74  }
75 
76  T& GetSource() {
77  return m_source;
78  }
79 
80  T const& GetSource() const {
81  return m_source;
82  }
83 
84 protected:
87  bool m_error_flag = false;
89 };
90 
91 /**
92  * Keeps relevant state to be able to communicate with a primary data source.
93  *
94  * @ingroup daq_common_libdaq
95  */
96 class PrimSource {
97 public:
98  using RrClient = recif::RecCmdsAsync;
99 
100  /**
101  * @throws std::invalid_argument if name or client is invalid.
102  */
103  PrimSource(std::string name, std::shared_ptr<RrClient> client)
104  : m_name(std::move(name)), m_rr_client(std::move(client)) {
105  if (m_name.empty()) {
106  throw std::invalid_argument("'name' is invalid");
107  }
108  if (!m_rr_client) {
109  throw std::invalid_argument("'client' is invalid");
110  }
111  }
112  PrimSource(PrimSource&&) = default;
113  PrimSource(PrimSource const&) = default;
116 
117  std::string const& GetName() const {
118  return m_name;
119  }
120 
122  return *m_rr_client;
123  }
124 
125  friend std::ostream& operator<<(std::ostream& os, PrimSource const& s) {
126  os << "PrimSource: " << s.m_name;
127  return os;
128  }
129 
130 private:
131  std::string m_name;
132  std::shared_ptr<RrClient> m_rr_client;
133 };
134 
135 /**
136  * Keeps relevant state to be able to communicate with a primary data source.
137  *
138  * @ingroup daq_common_libdaq
139  */
140 class MetaSource {
141 public:
142  using RrClient = metadaqif::MetaDaqAsync;
143 
144  /**
145  * @throws std::invalid_argument if name or client is invalid.
146  */
147  MetaSource(std::string name, std::shared_ptr<RrClient> client)
148  : m_name(std::move(name)), m_rr_client(std::move(client)) {
149  if (m_name.empty()) {
150  throw std::invalid_argument("'name' is invalid");
151  }
152  if (!m_rr_client) {
153  throw std::invalid_argument("'client' is invalid");
154  }
155  }
156  MetaSource(MetaSource&&) = default;
157  MetaSource(MetaSource const&) = default;
158  MetaSource& operator=(MetaSource const&) = default;
160 
161  std::string const& GetName() const {
162  return m_name;
163  }
164 
166  return *m_rr_client;
167  }
168 
169  friend std::ostream& operator<<(std::ostream& os, MetaSource const& s) {
170  os << "MetaSource: " << s.m_name;
171  return os;
172  }
173 
174 private:
175  std::string m_name;
176  std::shared_ptr<RrClient> m_rr_client;
177 };
178 
179 using SourceVariant = std::variant<PrimSource, MetaSource>;
180 
181 /**
182  * Data acquisition sources.
183  */
184 class DaqSources {
185 public:
186  DaqSources() = default;
187  explicit DaqSources(std::vector<PrimSource> prim_sources, std::vector<MetaSource> meta_sources)
188  : m_prim_sources(std::move(prim_sources)), m_meta_sources(std::move(meta_sources)) {
189  }
190  std::vector<PrimSource> const& GetPrimarySources() const noexcept {
191  return m_prim_sources;
192  }
193 
194  std::vector<PrimSource>& GetPrimarySources() noexcept {
195  return m_prim_sources;
196  }
197 
198  std::vector<MetaSource> const& GetMetadataSources() const noexcept {
199  return m_meta_sources;
200  }
201 
202  std::vector<MetaSource>& GetMetadataSources() noexcept {
203  return m_meta_sources;
204  }
205 
206 private:
207  std::vector<PrimSource> m_prim_sources;
208  std::vector<MetaSource> m_meta_sources;
209 };
210 
211 } // namespace daq
212 
213 #endif // #ifndef OCF_DAQ_SOURCE_HPP_
Data acquisition sources.
Definition: source.hpp:184
std::vector< PrimSource > & GetPrimarySources() noexcept
Definition: source.hpp:194
std::vector< MetaSource > const & GetMetadataSources() const noexcept
Definition: source.hpp:198
std::vector< MetaSource > & GetMetadataSources() noexcept
Definition: source.hpp:202
std::vector< PrimSource > const & GetPrimarySources() const noexcept
Definition: source.hpp:190
DaqSources()=default
DaqSources(std::vector< PrimSource > prim_sources, std::vector< MetaSource > meta_sources)
Definition: source.hpp:187
Keeps relevant state to be able to communicate with a primary data source.
Definition: source.hpp:140
MetaSource(std::string name, std::shared_ptr< RrClient > client)
Definition: source.hpp:147
MetaSource & operator=(MetaSource &&)=default
MetaSource(MetaSource &&)=default
std::string const & GetName() const
Definition: source.hpp:161
MetaSource & operator=(MetaSource const &)=default
RrClient & GetRrClient()
Definition: source.hpp:165
friend std::ostream & operator<<(std::ostream &os, MetaSource const &s)
Definition: source.hpp:169
metadaqif::MetaDaqAsync RrClient
Definition: source.hpp:142
MetaSource(MetaSource const &)=default
Keeps relevant state to be able to communicate with a primary data source.
Definition: source.hpp:96
friend std::ostream & operator<<(std::ostream &os, PrimSource const &s)
Definition: source.hpp:125
std::string const & GetName() const
Definition: source.hpp:117
PrimSource & operator=(PrimSource &)=default
PrimSource(PrimSource &&)=default
PrimSource & operator=(PrimSource &&)=default
recif::RecCmdsAsync RrClient
Definition: source.hpp:98
PrimSource(std::string name, std::shared_ptr< RrClient > client)
Definition: source.hpp:103
PrimSource(PrimSource const &)=default
RrClient & GetRrClient()
Definition: source.hpp:121
Declares daq::State and related functions.
std::variant< PrimSource, MetaSource > SourceVariant
Definition: source.hpp:179
State
Observable states of the data acquisition process.
Definition: state.hpp:39
@ NotStarted
Initial state of data acquisition.
Config class header file.
Simple class that holds the source and associated state.
Definition: source.hpp:29
boost::signals2::connection ConnectStateListener(Subscriber subscriber)
Connect subscriber that is invoked on state changes.
Definition: source.hpp:47
State m_state
Definition: source.hpp:86
bool m_error_flag
Definition: source.hpp:87
void SetErrorFlag()
Definition: source.hpp:64
friend std::ostream & operator<<(std::ostream &os, Source< T > const &s)
Definition: source.hpp:34
T & GetSource()
Definition: source.hpp:76
Source(T &&s)
Definition: source.hpp:32
boost::signals2::signal< void(State, bool)> StateSignal
Definition: source.hpp:30
T const & GetSource() const
Definition: source.hpp:80
State GetState() const
Definition: source.hpp:72
bool GetErrorFlag() const
Definition: source.hpp:69
StateSignal m_state_signal
Definition: source.hpp:88
void SetState(State state, std::optional< bool > error_flag={})
Definition: source.hpp:51
void ClearErrorFlag()
Definition: source.hpp:59