Line data Source code
1 : /**
2 : *
3 : * @file ZmqSender.cpp ZmqSender messaging class definitions
4 : *
5 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
6 : * Licensing/copyright details are in the COPYING file that you should have
7 : * received with this code.
8 : */
9 :
10 : #include "ipm/Sender.hpp"
11 : #include "ipm/ZmqContext.hpp"
12 :
13 : #include "logging/Logging.hpp"
14 : #include "zmq.hpp"
15 :
16 : #include <string>
17 : #include <vector>
18 :
19 : namespace dunedaq::ipm {
20 : class ZmqSender : public Sender
21 : {
22 : public:
23 26 : ZmqSender()
24 26 : : m_socket(ZmqContext::instance().GetContext(), zmq::socket_type::push)
25 : {
26 26 : }
27 :
28 52 : ~ZmqSender()
29 26 : {
30 : // Probably (cpp)zmq does this in the socket dtor anyway, but I guess it doesn't hurt to be explicit
31 26 : if (m_connection_info.connection_string != "" && m_socket_connected) {
32 24 : try {
33 48 : TLOG_DEBUG(TLVL_ZMQSENDER_DESTRUCTOR)
34 24 : << m_connection_info.connection_name << ": Disconnecting socket from " << m_connection_info.connection_string;
35 :
36 24 : m_socket.disconnect(m_connection_info.connection_string);
37 24 : m_socket_connected = false;
38 0 : } catch (zmq::error_t const& err) {
39 0 : ers::error(ZmqOperationError(ERS_HERE,
40 0 : m_connection_info.connection_name,
41 : "disconnect",
42 : "send",
43 0 : err.what(),
44 0 : m_connection_info.connection_string));
45 0 : }
46 : }
47 26 : m_socket.close();
48 52 : }
49 :
50 20037 : bool can_send() const noexcept override { return m_socket_connected; }
51 27 : std::string connect_for_sends(const ConnectionInfo& connection_info) override
52 : {
53 27 : m_connection_info = connection_info;
54 27 : try {
55 27 : m_socket.set(zmq::sockopt::sndtimeo, 0); // Return immediately if we can't send
56 0 : } catch (zmq::error_t const& err) {
57 0 : throw ZmqOperationError(ERS_HERE,
58 0 : m_connection_info.connection_name,
59 : "set timeout",
60 : "send",
61 0 : err.what(),
62 0 : m_connection_info.connection_string);
63 0 : }
64 :
65 27 : auto hwm = connection_info.capacity;
66 27 : if (hwm > 0) {
67 0 : try {
68 0 : m_socket.set(zmq::sockopt::sndhwm, hwm);
69 0 : } catch (zmq::error_t const& err) {
70 0 : throw ZmqOperationError(ERS_HERE,
71 0 : m_connection_info.connection_name,
72 : "set hwm",
73 : "send",
74 0 : err.what(),
75 0 : m_connection_info.connection_string);
76 0 : }
77 : }
78 :
79 27 : auto connection_string = m_connection_info.connection_string;
80 :
81 27 : TLOG_DEBUG(TLVL_CONNECTIONSTRING) << "Connection String is " << connection_string;
82 27 : try {
83 27 : m_socket.set(zmq::sockopt::immediate, 1); // Don't queue messages to incomplete connections
84 0 : } catch (zmq::error_t const& err) {
85 0 : throw ZmqOperationError(
86 0 : ERS_HERE, m_connection_info.connection_name, "set immediate mode", "send", err.what(), connection_string);
87 0 : }
88 :
89 27 : try {
90 27 : m_socket.connect(connection_string);
91 24 : m_connection_info.connection_string = m_socket.get(zmq::sockopt::last_endpoint);
92 24 : m_socket_connected = true;
93 3 : } catch (zmq::error_t const& err) {
94 3 : ers::error(ZmqOperationError(
95 6 : ERS_HERE, m_connection_info.connection_name, "connect", "send", err.what(), connection_string));
96 3 : }
97 :
98 27 : if (!m_socket_connected) {
99 3 : throw ZmqOperationError(ERS_HERE,
100 3 : m_connection_info.connection_name,
101 : "connect",
102 : "send",
103 : "Operation failed for all resolved connection strings",
104 6 : "");
105 : }
106 48 : return m_connection_info.connection_string;
107 27 : }
108 :
109 : protected:
110 20028 : bool send_(const void* message,
111 : int N,
112 : const duration_t& timeout,
113 : std::string const& topic,
114 : bool no_tmoexcept_mode) override
115 : {
116 20028 : TLOG_DEBUG(TLVL_ZMQSENDER_SEND_START)
117 20028 : << m_connection_info.connection_name << ": Starting send of " << N << " bytes";
118 20028 : auto start_time = std::chrono::steady_clock::now();
119 20028 : zmq::send_result_t res{};
120 9979093 : do {
121 :
122 9979093 : zmq::message_t topic_msg(topic.c_str(), topic.size());
123 9979093 : try {
124 9979093 : res = m_socket.send(topic_msg, zmq::send_flags::sndmore);
125 0 : } catch (zmq::error_t const& err) {
126 0 : throw ZmqSendError(ERS_HERE, m_connection_info.connection_name, err.what(), topic.size(), topic);
127 0 : }
128 :
129 9979093 : if (!res || res != topic.size()) {
130 9959066 : TLOG_DEBUG(TLVL_ZMQSENDER_SEND_ERR) << m_connection_info.connection_name << ": Unable to send message";
131 9959066 : continue;
132 9959066 : }
133 :
134 20027 : zmq::message_t msg(message, N);
135 20027 : try {
136 20027 : res = m_socket.send(msg, zmq::send_flags::none);
137 0 : } catch (zmq::error_t const& err) {
138 0 : throw ZmqSendError(ERS_HERE, m_connection_info.connection_name, err.what(), N, topic);
139 0 : }
140 :
141 20027 : if (!res && timeout > duration_t::zero()) {
142 0 : usleep(1000);
143 : }
144 19978214 : } while (std::chrono::duration_cast<duration_t>(std::chrono::steady_clock::now() - start_time) < timeout && !res);
145 :
146 20028 : if (!res && !no_tmoexcept_mode) {
147 1 : throw SendTimeoutExpired(ERS_HERE, m_connection_info.connection_name, timeout.count());
148 : }
149 :
150 20027 : TLOG_DEBUG(TLVL_ZMQSENDER_SEND_END) << m_connection_info.connection_name << ": Completed send of " << N << " bytes";
151 20027 : return res && res == N;
152 : }
153 :
154 : private:
155 : zmq::socket_t m_socket;
156 : bool m_socket_connected{ false };
157 : };
158 :
159 : } // namespace dunedaq::ipm
160 :
161 26 : DEFINE_DUNE_IPM_SENDER(dunedaq::ipm::ZmqSender)
|