Line data Source code
1 : /**
2 : * @file notification_interface.cpp NotificationData class, NotificationInterface class, interface used by clients or
3 : * bookkeepers to send/receive notifications
4 : *
5 : * This is part of the DUNE DAQ , copyright 2020.
6 : * Licensing/copyright details are in the COPYING file that you should have
7 : * received with this code.
8 : */
9 :
10 : #include "snbmodules/notification_interface.hpp"
11 :
12 : #include "snbmodules/common/errors_declaration.hpp"
13 :
14 : #include <iostream>
15 : #include <string>
16 : #include <utility>
17 :
18 : namespace dunedaq::snbmodules {
19 :
20 : std::optional<NotificationData>
21 0 : NotificationInterface::listen_for_notification(const std::string& id,
22 : const std::string& expected_from /*= ""*/,
23 : int timeout /*= -1*/,
24 : int tries /*= -1*/)
25 : { // NOLINT
26 : // Default value for tries
27 0 : if (tries == -1) {
28 0 : tries = m_max_tries;
29 : }
30 : // TLOG() << "debug : Listening for request from " << id;
31 :
32 0 : if (timeout == -1) {
33 0 : timeout = m_timeout_receive;
34 : }
35 :
36 0 : TLOG() << "AAA " << __LINE__ << " getting receiver for UID \"" << id << "\"";
37 0 : std::optional<NotificationData> msg =
38 0 : iomanager::IOManager::get()->get_receiver<NotificationData>(id)->try_receive(std::chrono::milliseconds(timeout));
39 :
40 0 : if (msg.has_value()) {
41 0 : TLOG() << "debug : Received request " << msg->m_notification << " for " << msg->m_target_id;
42 :
43 0 : if (expected_from != "" && expected_from.find(msg.value().m_source_id) == std::string::npos) {
44 0 : TLOG() << "debug : Received request from " << msg->m_source_id << " but expected from " << expected_from
45 0 : << " ignoring";
46 0 : if (tries <= 1) {
47 0 : return std::nullopt;
48 : }
49 0 : tries--;
50 0 : return listen_for_notification(id, expected_from, timeout, tries);
51 : }
52 : }
53 0 : return msg;
54 0 : }
55 :
56 : bool
57 0 : NotificationInterface::send_notification(const notification_type::e_notification_type& notif,
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 0 : if (tries == -1) {
66 0 : tries = m_max_tries;
67 : }
68 :
69 : // find connection with dst in it
70 0 : std::string real_conn_id = id_conn;
71 0 : for (const auto& conn : m_bookkeepers_conn) {
72 0 : if (conn.find(id_conn) != std::string::npos) {
73 0 : real_conn_id = conn;
74 : break;
75 : }
76 : }
77 0 : for (const auto& conn : m_clients_conn) {
78 0 : if (conn.find(id_conn) != std::string::npos) {
79 0 : real_conn_id = conn;
80 : break;
81 : }
82 : }
83 :
84 0 : TLOG() << "debug : Sending request " << notification_type::notification_to_string(notif) << " to " << dst << " via "
85 0 : << real_conn_id;
86 :
87 0 : NotificationData notif_data(src, dst, notification_type::notification_to_string(notif), data);
88 :
89 0 : bool result = iomanager::IOManager::get()
90 0 : ->get_sender<NotificationData>(real_conn_id)
91 0 : ->try_send(std::move(notif_data), std::chrono::milliseconds(m_timeout_send));
92 :
93 0 : if (result == false) {
94 0 : ers::error(NotificationSendError(ERS_HERE, real_conn_id));
95 0 : if (tries <= 1) {
96 : return false;
97 : }
98 :
99 : // wait
100 0 : std::this_thread::sleep_for(std::chrono::milliseconds(m_timeout_send));
101 0 : TLOG() << "debug : Retrying send notification";
102 0 : tries--;
103 0 : return send_notification(notif, src, dst, real_conn_id, data, tries);
104 : }
105 :
106 : return result;
107 0 : }
108 : } // namespace dunedaq::snbmodules
|