ifw-daq  1.0.0
IFW Data Acquisition modules
ocmDaqService.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_ocm
4  * @copyright 2021 ESO - European Southern Observatory
5  *
6  * @brief Declaration of OcmDaqService
7  */
8 #ifndef DAQ_OCM_OCM_DAQ_SERVICE_HPP_
9 #define DAQ_OCM_OCM_DAQ_SERVICE_HPP_
10 #include <daq/config.hpp>
11 #include <rad/ioExecutor.hpp>
12 
13 #include <Ocmif.hpp>
14 
15 #include <boost/asio/io_context.hpp>
16 #include <daq/eventLog.hpp>
17 #include <daq/eventLogObserver.hpp>
18 #include <daq/manager.hpp>
19 #include <daq/daqProperties.hpp>
20 #include <log4cplus/logger.h>
21 #include <mal/Mal.hpp>
22 
23 namespace mal = ::elt::mal;
24 
25 struct ParsedSource {
26  ParsedSource(std::string name, std::string rr_uri);
27  ParsedSource() = default;
28  ParsedSource(ParsedSource const&) = default;
29  ParsedSource(ParsedSource&&) = default;
30  ParsedSource& operator=(ParsedSource const& rhs) = default;
31  ParsedSource& operator=(ParsedSource&& rhs) = default;
32 
33  bool operator==(ParsedSource const& rhs) const;
34  std::string name;
35  std::string rr_uri;
36 };
37 
38 std::ostream& operator<<(std::ostream& os, ParsedSource const& s);
39 
40 /**
41  * Parse user provided string in the format
42  * "<name>@<rr-uri>"
43  *
44  * @throw std::invalid_argument on errors.
45  */
46 ParsedSource ParseSourceUri(std::string_view s);
47 
48 /**
49  * Parse user provided string in the format
50  * "<name>@<rr-uri>[ <name>@...]"
51  *
52  * @throw std::invalid_argument on errors.
53  */
54 std::vector<ParsedSource> ParseSourceUris(std::string_view s);
55 
56 
57 /**
58  * Parse the JSON properties user provides with StartDaq
59  *
60  * {"keywords": KEYWORDS,
61  * "awaitInterval": DURATION}
62  *
63  * @throws nlohmann::json::exception on parsing errors.
64  */
65 daq::DaqProperties ParseStartDaqProperties(std::string const& properties);
66 
67 /**
68  * Implements the MAL interface ocmif::OcmDaq (async version).
69  *
70  * The server is only safe to use as a shared pointer, as weak pointers to this object is
71  * stored in asynchronous continuations, apart from the initiating operation.
72  * Provided boost::asio::io_context must outlive object and any pending operations.
73  *
74  * This object can be deleted while there are operations that have not complete yet.
75  * In that case, when the operation finally completes, an exception is sent as reply, even if
76  * the requested operation would have succeeded otherwise. The io_context must stay alive however,
77  * as it will be used as an executor when asynchonous operation is continued.
78  *
79  * A note on thread safety:
80  *
81  * The public (asynchronous) interface will immediately defer the execution to the provided
82  * executor, so depending on the executor this can provide thread safety.
83  *
84  *
85  * @ingroup daq_ocm_server
86  */
87 class OcmDaqService : public ocmif::AsyncOcmDaq,
88  public std::enable_shared_from_this<OcmDaqService> {
89 public:
90  static constexpr char const* LOGGER_NAME = "ocm.service.daq";
91 
92  /**
93  *
94  * @param io_ctx ASIO io context to use as an executor. This object must outlive OcmDaqService.
95  * The io context will be progated to any created `daq::DaqController` instances as well.
96  * @param mal The mal instance to use to create instances. This object must outlive
97  * OcmDaqService.
98  * @param mgr The data acquisition manager to use. This object must outlive OcmDaqService.
99  */
100  OcmDaqService(boost::asio::io_context& io_ctx, mal::Mal& mal, daq::Manager& mgr, std::string
101  output_path);
102  ~OcmDaqService();
103 
104  boost::future<std::shared_ptr<::ocmif::DaqReply>>
105  StartDaq(const std::string& id,
106  const std::string& file_prefix,
107  const std::string& primary_sources,
108  const std::string& metadata_sources,
109  const std::string& properties) override;
110 
111  boost::future<std::shared_ptr<::ocmif::DaqReply>> StopDaq(const std::string& id) override;
112 
113  boost::future<std::shared_ptr<::ocmif::DaqReply>>
114  ForceStopDaq(const std::string& id) override;
115 
116  boost::future<std::shared_ptr<::ocmif::DaqReply>>
117  AbortDaq(const std::string& id) override;
118 
119  boost::future<std::shared_ptr<::ocmif::DaqReply>>
120  ForceAbortDaq(const std::string& id) override;
121 
122  boost::future<std::shared_ptr<::ocmif::DaqReply>>
123  UpdateKeywords(const std::string& id, const std::string& keywords) override;
124 
125  boost::future<std::shared_ptr<::ocmif::AwaitDaqReply>>
126  AwaitDaqState(const std::string& id,
127  ocmif::DaqState state,
128  ocmif::DaqSubState substate,
129  double timeout) override;
130 
131  boost::future<std::shared_ptr<::ocmif::DaqStatus>>
132  GetStatus(const std::string& id) override;
133 
134  boost::future<std::vector<std::shared_ptr<::ocmif::DaqStatus>>>
135  GetActiveList() override;
136 
137 private:
138  boost::future<std::shared_ptr<::ocmif::DaqReply>>
139  StopDaq(const std::string& id, bool forced);
140  boost::future<std::shared_ptr<::ocmif::DaqReply>>
141  AbortDaq(const std::string& id, bool forced);
142 
143  boost::asio::io_context& m_io_ctx;
144  rad::IoExecutor m_executor;
145  mal::Mal& m_mal;
146  daq::Manager& m_mgr;
147  /**
148  * Directory in which FITS files are written
149  */
150  std::string m_output_path;
151 
152  std::shared_ptr<daq::ObservableEventLog> m_event_log;
153  boost::signals2::connection m_log_observer_connection;
154  /**
155  * Observer for m_event_log.
156  */
157  daq::EventLogObserverLogger m_log_observer;
158 
159  log4cplus::Logger m_logger;
160 };
161 
162 #endif // #ifndef DAQ_OCM_OCM_DAQ_SERVICE_HPP_
ParsedSource::operator==
bool operator==(ParsedSource const &rhs) const
Definition: ocmDaqService.cpp:111
ParsedSource::operator=
ParsedSource & operator=(ParsedSource &&rhs)=default
ParseSourceUri
ParsedSource ParseSourceUri(std::string_view s)
Parse user provided string in the format "<name>@<rr-uri>".
Definition: ocmDaqService.cpp:123
ioExecutor.hpp
ParseStartDaqProperties
daq::DaqProperties ParseStartDaqProperties(std::string const &properties)
Parse the JSON properties user provides with StartDaq.
Definition: ocmDaqService.cpp:76
config.hpp
ParsedSource::operator=
ParsedSource & operator=(ParsedSource const &rhs)=default
OcmDaqService::LOGGER_NAME
static constexpr char const * LOGGER_NAME
Definition: ocmDaqService.hpp:90
daq::DaqProperties
Structure carrying properties needed to start a DataAcquisition.
Definition: daqProperties.hpp:28
rad::IoExecutor
Adapts boost::asio::io_context into a compatible boost::thread Executor type.
Definition: ioExecutor.hpp:12
manager.hpp
Declaration of daq::Manager
OcmDaqService::ForceStopDaq
boost::future< std::shared_ptr<::ocmif::DaqReply > > ForceStopDaq(const std::string &id) override
Definition: ocmDaqService.cpp:353
operator<<
std::ostream & operator<<(std::ostream &os, ParsedSource const &s)
Definition: ocmDaqService.cpp:118
eventLogObserver.hpp
Contains declaration for EventLogObserverLogger.
OcmDaqService::ForceAbortDaq
boost::future< std::shared_ptr<::ocmif::DaqReply > > ForceAbortDaq(const std::string &id) override
Definition: ocmDaqService.cpp:419
daq::EventLogObserverLogger
A simple daq::ObservableEventLog observer that logs observed events to provided logger.
Definition: eventLogObserver.hpp:22
eventLog.hpp
Contains declaration for EventLog, ObservableEventLog and related events.
OcmDaqService
Implements the MAL interface ocmif::OcmDaq (async version).
Definition: ocmDaqService.hpp:88
ParsedSource::name
std::string name
Definition: ocmDaqService.hpp:34
OcmDaqService::AbortDaq
boost::future< std::shared_ptr<::ocmif::DaqReply > > AbortDaq(const std::string &id) override
Definition: ocmDaqService.cpp:405
OcmDaqService::GetActiveList
boost::future< std::vector< std::shared_ptr<::ocmif::DaqStatus > > > GetActiveList() override
Definition: ocmDaqService.cpp:593
OcmDaqService::AwaitDaqState
boost::future< std::shared_ptr<::ocmif::AwaitDaqReply > > AwaitDaqState(const std::string &id, ocmif::DaqState state, ocmif::DaqSubState substate, double timeout) override
Definition: ocmDaqService.cpp:621
ParseSourceUris
std::vector< ParsedSource > ParseSourceUris(std::string_view s)
Parse user provided string in the format "<name>@<rr-uri>[ <name>@...]".
Definition: ocmDaqService.cpp:157
ParsedSource::ParsedSource
ParsedSource(ParsedSource &&)=default
OcmDaqService::OcmDaqService
OcmDaqService(boost::asio::io_context &io_ctx, mal::Mal &mal, daq::Manager &mgr, std::string output_path)
Definition: ocmDaqService.cpp:179
ParsedSource::ParsedSource
ParsedSource()=default
OcmDaqService::StopDaq
boost::future< std::shared_ptr<::ocmif::DaqReply > > StopDaq(const std::string &id) override
Definition: ocmDaqService.cpp:339
OcmDaqService::~OcmDaqService
~OcmDaqService()
Definition: ocmDaqService.cpp:194
daq::Manager
Manager owns DaqController and FitsController (active data acquisitions) instances and multiplexes re...
Definition: manager.hpp:86
OcmDaqService::UpdateKeywords
boost::future< std::shared_ptr<::ocmif::DaqReply > > UpdateKeywords(const std::string &id, const std::string &keywords) override
Definition: ocmDaqService.cpp:491
ParsedSource::ParsedSource
ParsedSource(ParsedSource const &)=default
daqProperties.hpp
Contains declaration of daq::Properties.
OcmDaqService::StartDaq
boost::future< std::shared_ptr<::ocmif::DaqReply > > StartDaq(const std::string &id, const std::string &file_prefix, const std::string &primary_sources, const std::string &metadata_sources, const std::string &properties) override
Definition: ocmDaqService.cpp:199
ParsedSource::rr_uri
std::string rr_uri
Definition: ocmDaqService.hpp:35
ParsedSource
Definition: ocmDaqService.hpp:25
OcmDaqService::GetStatus
boost::future< std::shared_ptr<::ocmif::DaqStatus > > GetStatus(const std::string &id) override
Definition: ocmDaqService.cpp:558