DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
notification_interface.cpp
Go to the documentation of this file.
1
11
13
14#include <iostream>
15#include <string>
16#include <utility>
17
18namespace dunedaq::snbmodules {
19
20std::optional<NotificationData>
22 const std::string& expected_from /*= ""*/,
23 int timeout /*= -1*/,
24 int tries /*= -1*/)
25{ // NOLINT
26 // Default value for tries
27 if (tries == -1) {
28 tries = m_max_tries;
29 }
30 // TLOG() << "debug : Listening for request from " << id;
31
32 if (timeout == -1) {
33 timeout = m_timeout_receive;
34 }
35
36 TLOG() << "AAA " << __LINE__ << " getting receiver for UID \"" << id << "\"";
37 std::optional<NotificationData> msg =
38 iomanager::IOManager::get()->get_receiver<NotificationData>(id)->try_receive(std::chrono::milliseconds(timeout));
39
40 if (msg.has_value()) {
41 TLOG() << "debug : Received request " << msg->m_notification << " for " << msg->m_target_id;
42
43 if (expected_from != "" && expected_from.find(msg.value().m_source_id) == std::string::npos) {
44 TLOG() << "debug : Received request from " << msg->m_source_id << " but expected from " << expected_from
45 << " ignoring";
46 if (tries <= 1) {
47 return std::nullopt;
48 }
49 tries--;
50 return listen_for_notification(id, expected_from, timeout, tries);
51 }
52 }
53 return msg;
54}
55
56bool
58 const std::string& src,
59 const std::string& dst,
60 const std::string& id_conn,
61 const std::string& data,
62 int tries)
63{
64 // Default value for tries
65 if (tries == -1) {
66 tries = m_max_tries;
67 }
68
69 // find connection with dst in it
70 std::string real_conn_id = id_conn;
71 for (const auto& conn : m_bookkeepers_conn) {
72 if (conn.find(id_conn) != std::string::npos) {
73 real_conn_id = conn;
74 break;
75 }
76 }
77 for (const auto& conn : m_clients_conn) {
78 if (conn.find(id_conn) != std::string::npos) {
79 real_conn_id = conn;
80 break;
81 }
82 }
83
84 TLOG() << "debug : Sending request " << notification_type::notification_to_string(notif) << " to " << dst << " via "
85 << real_conn_id;
86
87 NotificationData notif_data(src, dst, notification_type::notification_to_string(notif), data);
88
89 bool result = iomanager::IOManager::get()
90 ->get_sender<NotificationData>(real_conn_id)
91 ->try_send(std::move(notif_data), std::chrono::milliseconds(m_timeout_send));
92
93 if (result == false) {
94 ers::error(NotificationSendError(ERS_HERE, real_conn_id));
95 if (tries <= 1) {
96 return false;
97 }
98
99 // wait
100 std::this_thread::sleep_for(std::chrono::milliseconds(m_timeout_send));
101 TLOG() << "debug : Retrying send notification";
102 tries--;
103 return send_notification(notif, src, dst, real_conn_id, data, tries);
104 }
105
106 return result;
107}
108} // namespace dunedaq::snbmodules
#define ERS_HERE
static std::shared_ptr< IOManager > get()
Definition IOManager.hpp:40
std::optional< NotificationData > listen_for_notification(const std::string &id, const std::string &expected_from="", int timeout=-1, int tries=-1)
Listen for a notification.
int m_timeout_send
Timeout for sending a notification in ms.
std::set< std::string > m_clients_conn
List of clients connections.
int m_timeout_receive
Timeout for receiving a notification in ms.
bool send_notification(const notification_type::e_notification_type &notif, const std::string &src, const std::string &dst, const std::string &id_conn, const std::string &data="", int tries=-1)
Send a notification during m_timeout_send ms.
std::vector< std::string > m_bookkeepers_conn
List of bookkeepers connections.
#define TLOG(...)
Definition macro.hpp:22
void error(const Issue &issue)
Definition ers.hpp:81
NotificationData class, represent a notification.
static std::string notification_to_string(e_notification_type e)
e_notification_type
Different type of notifications possible to send.