ifw-daq  2.1.0-pre1
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:
86  State m_state = State::NotStarted;
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_
daq::PrimSource::PrimSource
PrimSource(PrimSource const &)=default
daq::MetaSource::GetName
std::string const & GetName() const
Definition: source.hpp:161
daq::State
State
Observable states of the data acquisition process.
Definition: state.hpp:39
daq::Source::operator<<
friend std::ostream & operator<<(std::ostream &os, Source< T > const &s)
Definition: source.hpp:34
daq::PrimSource::PrimSource
PrimSource(PrimSource &&)=default
daq::PrimSource::PrimSource
PrimSource(std::string name, std::shared_ptr< RrClient > client)
Definition: source.hpp:103
daq::MetaSource::RrClient
metadaqif::MetaDaqAsync RrClient
Definition: source.hpp:142
daq::PrimSource::operator=
PrimSource & operator=(PrimSource &)=default
daq::Source::m_error_flag
bool m_error_flag
Definition: source.hpp:87
daq::DaqSources::DaqSources
DaqSources(std::vector< PrimSource > prim_sources, std::vector< MetaSource > meta_sources)
Definition: source.hpp:187
daq::Source::m_state
State m_state
Definition: source.hpp:86
daq::Source
Simple class that holds the source and associated state.
Definition: source.hpp:29
daq::Source::GetErrorFlag
bool GetErrorFlag() const
Definition: source.hpp:69
daq::MetaSource::GetRrClient
RrClient & GetRrClient()
Definition: source.hpp:165
daq
Definition: asyncProcess.cpp:15
daq::DaqSources::GetMetadataSources
std::vector< MetaSource > const & GetMetadataSources() const noexcept
Definition: source.hpp:198
daq::DaqSources::DaqSources
DaqSources()=default
daq::PrimSource::operator<<
friend std::ostream & operator<<(std::ostream &os, PrimSource const &s)
Definition: source.hpp:125
config.hpp
Config class header file.
daq::PrimSource::GetName
std::string const & GetName() const
Definition: source.hpp:117
daq::PrimSource::RrClient
recif::RecCmdsAsync RrClient
Definition: source.hpp:98
daq::MetaSource::MetaSource
MetaSource(std::string name, std::shared_ptr< RrClient > client)
Definition: source.hpp:147
daq::DaqSources
Data acquisition sources.
Definition: source.hpp:184
daq::Source::m_source
T m_source
Definition: source.hpp:85
daq::Source::SetErrorFlag
void SetErrorFlag()
Definition: source.hpp:64
daq::MetaSource::operator=
MetaSource & operator=(MetaSource const &)=default
daq::Source::ClearErrorFlag
void ClearErrorFlag()
Definition: source.hpp:59
daq::DaqSources::GetPrimarySources
std::vector< PrimSource > & GetPrimarySources() noexcept
Definition: source.hpp:194
daq::Source::GetSource
T & GetSource()
Definition: source.hpp:76
daq::MetaSource::operator=
MetaSource & operator=(MetaSource &&)=default
daq::MetaSource::MetaSource
MetaSource(MetaSource &&)=default
daq::MetaSource::operator<<
friend std::ostream & operator<<(std::ostream &os, MetaSource const &s)
Definition: source.hpp:169
state.hpp
Declares daq::State and related functions.
daq::MetaSource
Keeps relevant state to be able to communicate with a primary data source.
Definition: source.hpp:140
daq::Source::GetState
State GetState() const
Definition: source.hpp:72
daq::Source::m_state_signal
StateSignal m_state_signal
Definition: source.hpp:88
daq::Source::Source
Source(T &&s)
Definition: source.hpp:32
daq::PrimSource::GetRrClient
RrClient & GetRrClient()
Definition: source.hpp:121
daq::Source::SetState
void SetState(State state, std::optional< bool > error_flag={})
Definition: source.hpp:51
daq::DaqSources::GetPrimarySources
std::vector< PrimSource > const & GetPrimarySources() const noexcept
Definition: source.hpp:190
daq::Source::StateSignal
boost::signals2::signal< void(State, bool)> StateSignal
Definition: source.hpp:30
daq::Source::ConnectStateListener
boost::signals2::connection ConnectStateListener(Subscriber subscriber)
Connect subscriber that is invoked on state changes.
Definition: source.hpp:47
daq::PrimSource::operator=
PrimSource & operator=(PrimSource &&)=default
daq::Source::GetSource
T const & GetSource() const
Definition: source.hpp:80
daq::PrimSource
Keeps relevant state to be able to communicate with a primary data source.
Definition: source.hpp:96
daq::SourceVariant
std::variant< PrimSource, MetaSource > SourceVariant
Definition: source.hpp:179
daq::MetaSource::MetaSource
MetaSource(MetaSource const &)=default
daq::DaqSources::GetMetadataSources
std::vector< MetaSource > & GetMetadataSources() noexcept
Definition: source.hpp:202