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(build/define_used)
64#define DEFINE_DUNE_IPM_RECEIVER(klass) \
65 EXTERN_C_FUNC_DECLARE_START \
66 std::shared_ptr<dunedaq::ipm::Receiver> make() { return std::shared_ptr<dunedaq::ipm::Receiver>(new klass()); } \
67 }
68
69namespace dunedaq::ipm {
70
72{
73
74public:
75 using duration_t = std::chrono::milliseconds;
76 static constexpr duration_t s_block = duration_t::max();
77 static constexpr duration_t s_no_block = duration_t::zero();
78
79 using message_size_t = int;
80 static constexpr message_size_t s_any_size =
81 0; // Since "I want 0 bytes" is pointless, "0" denotes "I don't care about the size"
82
83 Receiver() = default;
84 virtual ~Receiver() = default;
85
86 virtual std::string connect_for_receives(const nlohmann::json& connection_info) = 0;
87
88 virtual bool can_receive() const noexcept = 0;
89
90 // receive() will perform some universally-desirable checks before calling user-implemented receive_:
91 // -Throws KnownStateForbidsReceive if can_receive() == false
92 // -Throws UnexpectedNumberOfBytes if the "nbytes" argument isn't anysize, and the
93 // received bytes inside the function aren't the same number as nbytes
94
95 struct Response
96 {
97 std::string metadata{ "" };
98 std::vector<char> data{};
99 };
100
101 Response receive(const duration_t& timeout, message_size_t num_bytes = s_any_size, bool no_tmoexcept_mode = false);
102
103 virtual void register_callback(std::function<void(Response&)>) = 0;
104 virtual void unregister_callback() = 0;
105
106 Receiver(const Receiver&) = delete;
107 Receiver& operator=(const Receiver&) = delete;
108
109 Receiver(Receiver&&) = delete;
111
112protected:
113
114 void generate_opmon_data() override ;
115
116 virtual Response receive_(const duration_t& timeout, bool no_tmoexcept_mode) = 0;
117
118private:
119 mutable std::atomic<size_t> m_bytes = { 0 };
120 mutable std::atomic<size_t> m_messages = { 0 };
121};
122
123inline std::shared_ptr<Receiver>
124make_ipm_receiver(std::string const& plugin_name)
125{
126 static cet::BasicPluginFactory bpf("duneIPM", "make");
127 return bpf.makePlugin<std::shared_ptr<Receiver>>(plugin_name);
128}
129
130} // namespace dunedaq::ipm
131
132#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:75
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:124
Including Qt Headers.
ReceiveTimeoutExpired
Definition Receiver.hpp:46
Unable to receive within timeout period(timeout period was " << timeout << " milliseconds)"