DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
NetworkReceiverModel.hxx
Go to the documentation of this file.
4
5#include "ipm/Subscriber.hpp"
6#include "logging/Logging.hpp"
9
10#include <atomic>
11#include <memory>
12#include <optional>
13#include <string>
14#include <thread>
15#include <typeinfo>
16#include <utility>
17
18namespace dunedaq::iomanager {
19
20template<typename Datatype>
22 : ReceiverConcept<Datatype>(conn_id)
23{
24 TLOG() << "NetworkReceiverModel created with DT! ID: " << conn_id.uid << " Addr: " << static_cast<void*>(this);
25 try {
26 get_receiver(std::chrono::milliseconds(1000));
27 } catch (ConnectionNotFound const& ex) {
28 TLOG() << "Initial connection attempt failed: " << ex;
29 }
30}
31
32template<typename Datatype>
34 : ReceiverConcept<Datatype>(other.m_conn.uid)
35 , m_callback(std::move(other.m_callback))
36 , m_event_loop_runner(std::move(other.m_event_loop_runner))
37 , m_network_receiver_ptr(std::move(other.m_network_receiver_ptr))
38{
39}
40
41template<typename Datatype>
42inline Datatype
44{
45 try {
46 return read_network<Datatype>(timeout);
47 } catch (ipm::ReceiveTimeoutExpired& ex) {
48 throw TimeoutExpired(ERS_HERE, this->id().uid, "receive", timeout.count(), ex);
49 }
50}
51
52template<typename Datatype>
53inline void
55{
56 std::lock_guard<std::mutex> lk(m_callback_mutex);
57 if (m_event_loop_runner != nullptr && m_event_loop_runner->joinable()) {
58 m_event_loop_runner->request_stop();
59 m_event_loop_runner->join();
60 } else if (m_event_loop_runner != nullptr) {
61 TLOG() << "Event loop can't be closed!";
62 }
63 m_event_loop_runner.reset(nullptr);
64 // remove function.
65}
66
67template<typename Datatype>
68inline void
70{
71 if (NetworkManager::get().is_pubsub_connection(this->m_conn)) {
72 std::dynamic_pointer_cast<ipm::Subscriber>(m_network_receiver_ptr)->subscribe(topic);
73 }
74}
75
76template<typename Datatype>
77inline void
79{
80 if (NetworkManager::get().is_pubsub_connection(this->m_conn)) {
81 std::dynamic_pointer_cast<ipm::Subscriber>(m_network_receiver_ptr)->unsubscribe(topic);
82 }
83}
84
85template<typename Datatype>
86inline void
88{
89 // get network resources
90 auto start = std::chrono::steady_clock::now();
91 while (m_network_receiver_ptr == nullptr &&
92 std::chrono::duration_cast<Receiver::timeout_t>(std::chrono::steady_clock::now() - start) < timeout) {
93
94 try {
95 m_network_receiver_ptr = NetworkManager::get().get_receiver(this->id());
96 } catch (ConnectionNotFound const& ex) {
97 m_network_receiver_ptr = nullptr;
98 std::this_thread::sleep_for(std::chrono::milliseconds(10));
99 }
100 }
101}
102
103template<typename Datatype>
104template<typename MessageType>
105inline typename std::enable_if<serialization::is_serializable<MessageType>::value, MessageType>::type
107{
108 std::lock_guard<std::mutex> lk(m_receive_mutex);
109 get_receiver(timeout);
110
111 if (m_network_receiver_ptr == nullptr) {
112 throw ConnectionInstanceNotFound(ERS_HERE, this->id().uid);
113 }
114
115 auto response = m_network_receiver_ptr->receive(timeout);
116 if (response.data.size() > 0) {
117 return serialization::deserialize<MessageType>(response.data);
118 }
119
120 throw TimeoutExpired(ERS_HERE, this->id().uid, "network receive", timeout.count());
121 return MessageType();
122}
123
124template<typename Datatype>
125template<typename MessageType>
126inline typename std::enable_if<!serialization::is_serializable<MessageType>::value, MessageType>::type
128{
129 throw NetworkMessageNotSerializable(ERS_HERE, typeid(MessageType).name()); // NOLINT(runtime/rtti)
130 return MessageType();
131}
132
133template<typename Datatype>
134template<typename MessageType>
135inline typename std::enable_if<serialization::is_serializable<MessageType>::value, std::optional<MessageType>>::type
137{
138 std::lock_guard<std::mutex> lk(m_receive_mutex);
139 get_receiver(timeout);
140 if (m_network_receiver_ptr == nullptr) {
141 TLOG_DEBUG(5) << ConnectionInstanceNotFound(ERS_HERE, this->id().uid);
142 return std::nullopt;
143 }
144
146 res = m_network_receiver_ptr->receive(timeout, ipm::Receiver::s_any_size, true);
147
148 if (res.data.size() > 0) {
149 return std::make_optional<MessageType>(serialization::deserialize<MessageType>(res.data));
150 }
151
152 return std::nullopt;
153}
154
155template<typename Datatype>
156template<typename MessageType>
157inline typename std::enable_if<!serialization::is_serializable<MessageType>::value, std::optional<MessageType>>::type
159{
160 ers::error(NetworkMessageNotSerializable(ERS_HERE, typeid(MessageType).name())); // NOLINT(runtime/rtti)
161 return std::nullopt;
162}
163
164template<typename Datatype>
165template<typename MessageType>
166inline typename std::enable_if<serialization::is_serializable<MessageType>::value, void>::type
167NetworkReceiverModel<Datatype>::add_callback_impl(std::function<void(MessageType&)> callback)
168{
169 remove_callback();
170 {
171 // This ensures that add_callback_impl and remove_callback are not processing concurrently
172 std::lock_guard<std::mutex> lk(m_callback_mutex);
173 }
174 TLOG() << "Registering callback.";
175 m_callback = callback;
176 // start event loop (thread that calls when receive happens). remove_callback() is called in the destructor, so this
177 // will never go out-of-scope while this is running
178 m_event_loop_runner = std::make_unique<std::jthread>([&](std::stop_token token) {
179 std::optional<Datatype> message;
180 while (!token.stop_requested() || message) {
181 try {
182 // 0 timeout when we are trying to stop
183 message = try_read_network<Datatype>(token.stop_requested() ? std::chrono::milliseconds(0)
184 : std::chrono::milliseconds(20));
185 if (message) {
186 m_callback(*message);
187 }
188 } catch (const ers::Issue&) {
189 // Intentionally ignoring any ers::Issues that might have been raised
190 ;
191 }
192 }
193 });
194 auto handle = m_event_loop_runner->native_handle();
195 std::string name = "N_" + this->id().uid;
196 name.resize(15);
197 auto rc = pthread_setname_np(handle, name.c_str());
198 if (rc != 0) {
199 std::ostringstream s;
200 s << "The name " << name << " provided for the thread is too long.";
201 ers::warning(utilities::ThreadingIssue(ERS_HERE, s.str()));
202 }
203}
204
205template<typename Datatype>
206template<typename MessageType>
207inline typename std::enable_if<!serialization::is_serializable<MessageType>::value, void>::type
208NetworkReceiverModel<Datatype>::add_callback_impl(std::function<void(MessageType&)>)
209{
210 throw NetworkMessageNotSerializable(ERS_HERE, typeid(MessageType).name()); // NOLINT(runtime/rtti)
211}
212
213} // namespace dunedaq::iomanager
#define ERS_HERE
static NetworkManager & get()
std::shared_ptr< ipm::Receiver > get_receiver(ConnectionId const &conn_id)
void get_receiver(Receiver::timeout_t timeout)
void subscribe(std::string topic) override
std::enable_if< serialization::is_serializable< MessageType >::value, void >::type add_callback_impl(std::function< void(MessageType &)> callback)
std::enable_if< serialization::is_serializable< MessageType >::value, MessageType >::type read_network(Receiver::timeout_t const &timeout)
NetworkReceiverModel(ConnectionId const &conn_id)
void unsubscribe(std::string topic) override
std::enable_if< serialization::is_serializable< MessageType >::value, std::optional< MessageType > >::type try_read_network(Receiver::timeout_t const &timeout)
Datatype receive(Receiver::timeout_t timeout) override
std::chrono::milliseconds timeout_t
Definition Receiver.hpp:27
static constexpr message_size_t s_any_size
Definition Receiver.hpp:83
Base class for any user define issue.
Definition Issue.hpp:69
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
#define TLOG(...)
Definition macro.hpp:22
void warning(const Issue &issue)
Definition ers.hpp:115
void error(const Issue &issue)
Definition ers.hpp:81