DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Receiver.hpp
Go to the documentation of this file.
1
23#ifndef IPM_INCLUDE_IPM_RECEIVER_HPP_
24#define IPM_INCLUDE_IPM_RECEIVER_HPP_
25
26#include "cetlib/BasicPluginFactory.h"
27#include "cetlib/compiler_macros.h"
28#include "ers/Issue.hpp"
29#include "logging/Logging.hpp" // NOTE: if ISSUES ARE DECLARED BEFORE include logging/Logging.hpp, TLOG_DEBUG<<issue wont work.
30#include "nlohmann/json.hpp"
32
33#include <atomic>
34#include <memory>
35#include <string>
36#include <vector>
37
38namespace dunedaq {
39// Disable coverage collection LCOV_EXCL_START
40ERS_DECLARE_ISSUE(ipm, KnownStateForbidsReceive, "Receiver not in a state to receive data", )
42 UnexpectedNumberOfBytes,
43 "Expected " << bytes1 << " bytes in message but received " << bytes2,
44 ((int)bytes1)((int)bytes2)) // NOLINT
47 "Unable to receive within timeout period (timeout period was " << timeout << " milliseconds)",
48 ((int)timeout)) // NOLINT
49// Reenable coverage collection LCOV_EXCL_STOP
50} // namespace dunedaq
51
52#ifndef EXTERN_C_FUNC_DECLARE_START
53// NOLINTNEXTLINE(build/define_used)
54#define EXTERN_C_FUNC_DECLARE_START \
55 extern "C" \
56 {
57#endif
58
63// NOLINTNEXTLINE
64#define DEFINE_DUNE_IPM_RECEIVER(klass) \
65 EXTERN_C_FUNC_DECLARE_START \
66 std::shared_ptr<dunedaq::ipm::Receiver> make() \
67 { \
68 return std::shared_ptr<dunedaq::ipm::Receiver>(new klass()); \
69 } \
70 }
71
72namespace dunedaq::ipm {
73
75{
76
77public:
78 using duration_t = std::chrono::milliseconds;
79 static constexpr duration_t s_block = duration_t::max();
80 static constexpr duration_t s_no_block = duration_t::zero();
81
82 using message_size_t = int;
83 static constexpr message_size_t s_any_size =
84 0; // Since "I want 0 bytes" is pointless, "0" denotes "I don't care about the size"
85
86 Receiver() = default;
87 virtual ~Receiver() = default;
88
89 virtual std::string connect_for_receives(const nlohmann::json& connection_info) = 0;
90
91 virtual bool can_receive() const noexcept = 0;
92
93 // receive() will perform some universally-desirable checks before calling user-implemented receive_:
94 // -Throws KnownStateForbidsReceive if can_receive() == false
95 // -Throws UnexpectedNumberOfBytes if the "nbytes" argument isn't anysize, and the
96 // received bytes inside the function aren't the same number as nbytes
97
98 struct Response
99 {
100 std::string metadata{ "" };
101 std::vector<char> data{};
102 };
103
104 Response receive(const duration_t& timeout, message_size_t num_bytes = s_any_size, bool no_tmoexcept_mode = false);
105
106 virtual void register_callback(std::function<void(Response&)>) = 0;
107 virtual void unregister_callback() = 0;
108
109 Receiver(const Receiver&) = delete;
110 Receiver& operator=(const Receiver&) = delete;
111
112 Receiver(Receiver&&) = delete;
114
115protected:
116 void generate_opmon_data() override;
117
118 virtual Response receive_(const duration_t& timeout, bool no_tmoexcept_mode) = 0;
119
120private:
121 mutable std::atomic<size_t> m_bytes = { 0 };
122 mutable std::atomic<size_t> m_messages = { 0 };
123};
124
125inline std::shared_ptr<Receiver>
126make_ipm_receiver(std::string const& plugin_name)
127{
128 static cet::BasicPluginFactory bpf("duneIPM", "make");
129 return bpf.makePlugin<std::shared_ptr<Receiver>>(plugin_name);
130}
131
132} // namespace dunedaq::ipm
133
134#endif // IPM_INCLUDE_IPM_RECEIVER_HPP_
#define ERS_DECLARE_ISSUE(namespace_name, class_name, message, attributes)
Receiver(const Receiver &)=delete
virtual void unregister_callback()=0
virtual ~Receiver()=default
std::chrono::milliseconds duration_t
Definition Receiver.hpp:78
Receiver(Receiver &&)=delete
Receiver & operator=(const Receiver &)=delete
virtual std::string connect_for_receives(const nlohmann::json &connection_info)=0
Receiver & operator=(Receiver &&)=delete
virtual Response receive_(const duration_t &timeout, bool no_tmoexcept_mode)=0
virtual bool can_receive() const noexcept=0
virtual void register_callback(std::function< void(Response &)>)=0
An ERS Error indicating that an exception was thrown from ZMQ while performing an operation.
std::shared_ptr< Receiver > make_ipm_receiver(std::string const &plugin_name)
Definition Receiver.hpp:126
The DUNE-DAQ namespace.
Definition DataStore.hpp:57
ReceiveTimeoutExpired
Definition Receiver.hpp:46
Unable to receive within timeout period(timeout period was " << timeout << " milliseconds)"