ifw-daq  1.0.0
IFW Data Acquisition modules
source.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_ocm_libdaq
4  * @copyright 2021 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 <boost/signals2/signal.hpp>
16 #include <Metadaqif.hpp>
17 #include <Recif.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_ocm_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  friend std::ostream& operator<<(std::ostream& os, Source<T> const& s) {
34  os << s.GetSource() << ", state=" << s.GetState() << ", error_flag="
35  << (s.GetErrorFlag() ? "true" : "false");
36  return os;
37  }
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_ocm_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) : m_name(std::move(name)), m_rr_client(std::move(client)) {
104  if (m_name.empty()) {
105  throw std::invalid_argument("'name' is invalid");
106  }
107  if (!m_rr_client) {
108  throw std::invalid_argument("'client' is invalid");
109  }
110  }
111  PrimSource(PrimSource&&) = default;
112  PrimSource(PrimSource const&) = default;
115 
116  std::string_view GetName() const {
117  return m_name;
118  }
119 
121  return *m_rr_client;
122  }
123 
124  friend std::ostream& operator<<(std::ostream& os, PrimSource const& s) {
125  os << "PrimSource: " << s.m_name;
126  return os;
127  }
128 
129  private:
130  std::string m_name;
131  std::shared_ptr<RrClient> m_rr_client;
132 };
133 
134 /**
135  * Keeps relevant state to be able to communicate with a primary data source.
136  *
137  * @ingroup daq_ocm_libdaq
138  */
139 class MetaSource {
140  public:
141  using RrClient = metadaqif::MetaDaqAsync;
142 
143  /**
144  * @throws std::invalid_argument if name or client is invalid.
145  */
146  MetaSource(std::string name, std::shared_ptr<RrClient> client) : m_name(std::move(name)), m_rr_client(std::move(client)) {
147  if (m_name.empty()) {
148  throw std::invalid_argument("'name' is invalid");
149  }
150  if (!m_rr_client) {
151  throw std::invalid_argument("'client' is invalid");
152  }
153  }
154  MetaSource(MetaSource&&) = default;
155  MetaSource(MetaSource const&) = default;
156  MetaSource& operator=(MetaSource const&) = default;
158 
159  std::string_view GetName() const {
160  return m_name;
161  }
162 
164  return *m_rr_client;
165  }
166 
167  friend std::ostream& operator<<(std::ostream& os, MetaSource const& s) {
168  os << "MetaSource: " << s.m_name;
169  return os;
170  }
171 
172 private:
173  std::string m_name;
174  std::shared_ptr<RrClient> m_rr_client;
175 };
176 
177 using SourceVariant = std::variant<PrimSource, MetaSource>;
178 }
179 
180 #endif // #ifndef OCF_DAQ_SOURCE_HPP_
daq::State
State
Observable states of the data acquisition process.
Definition: state.hpp:41
daq::MetaSource::GetRrClient
RrClient & GetRrClient()
Definition: source.hpp:163
daq::Source::m_error_flag
bool m_error_flag
Definition: source.hpp:87
daq::Source::GetSource
T & GetSource()
Definition: source.hpp:76
daq::MetaSource::MetaSource
MetaSource(MetaSource const &)=default
daq::MetaSource
Keeps relevant state to be able to communicate with a primary data source.
Definition: source.hpp:139
daq::Source::ClearErrorFlag
void ClearErrorFlag()
Definition: source.hpp:59
daq::MetaSource::operator=
MetaSource & operator=(MetaSource const &)=default
daq::Source::m_state_signal
StateSignal m_state_signal
Definition: source.hpp:88
daq::MetaSource::GetName
std::string_view GetName() const
Definition: source.hpp:159
daq::PrimSource::PrimSource
PrimSource(PrimSource const &)=default
daq::PrimSource::GetRrClient
RrClient & GetRrClient()
Definition: source.hpp:120
daq::Source
Simple class that holds the source and associated state.
Definition: source.hpp:29
daq
Definition: daqController.cpp:18
daq::Source::m_source
T m_source
Definition: source.hpp:85
daq::Source::GetState
State GetState() const
Definition: source.hpp:72
config.hpp
Config class header file.
daq::MetaSource::operator=
MetaSource & operator=(MetaSource &&)=default
daq::Source::SetState
void SetState(State state, std::optional< bool > error_flag={})
Definition: source.hpp:51
daq::PrimSource::PrimSource
PrimSource(PrimSource &&)=default
daq::PrimSource::PrimSource
PrimSource(std::string name, std::shared_ptr< RrClient > client)
Definition: source.hpp:103
daq::Source::operator<<
friend std::ostream & operator<<(std::ostream &os, Source< T > const &s)
Definition: source.hpp:33
daq::PrimSource::operator=
PrimSource & operator=(PrimSource &)=default
daq::Source::m_state
State m_state
Definition: source.hpp:86
daq::Source::Source
Source(T &&s)
Definition: source.hpp:32
daq::Source::ConnectStateListener
boost::signals2::connection ConnectStateListener(Subscriber subscriber)
Connect subscriber that is invoked on state changes.
Definition: source.hpp:47
daq::Source::GetSource
T const & GetSource() const
Definition: source.hpp:80
daq::Source::StateSignal
boost::signals2::signal< void(State, bool)> StateSignal
Definition: source.hpp:30
daq::PrimSource::GetName
std::string_view GetName() const
Definition: source.hpp:116
daq::PrimSource::operator=
PrimSource & operator=(PrimSource &&)=default
daq::Source::GetErrorFlag
bool GetErrorFlag() const
Definition: source.hpp:69
state.hpp
Declares daq::State and related functions.
daq::MetaSource::RrClient
metadaqif::MetaDaqAsync RrClient
Definition: source.hpp:141
daq::PrimSource::operator<<
friend std::ostream & operator<<(std::ostream &os, PrimSource const &s)
Definition: source.hpp:124
daq::SourceVariant
std::variant< PrimSource, MetaSource > SourceVariant
Definition: source.hpp:177
daq::Source::SetErrorFlag
void SetErrorFlag()
Definition: source.hpp:64
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:146
daq::MetaSource::MetaSource
MetaSource(MetaSource &&)=default
daq::MetaSource::operator<<
friend std::ostream & operator<<(std::ostream &os, MetaSource const &s)
Definition: source.hpp:167
daq::PrimSource
Keeps relevant state to be able to communicate with a primary data source.
Definition: source.hpp:96