rogue
Loading...
Searching...
No Matches
ZmqClient.cpp
Go to the documentation of this file.
1
18#include "rogue/Directives.h"
19
21
22#include <inttypes.h>
23#include <zmq.h>
24
25#include <cstdio>
26#include <memory>
27#include <string>
28
29#include "rogue/GeneralError.h"
30#include "rogue/GilRelease.h"
31#include "rogue/ScopedGil.h"
32
33#ifndef NO_PYTHON
34 #include <boost/python.hpp>
35namespace bp = boost::python;
36#endif
37
38namespace {
39
40constexpr uint32_t kRetryLogIntervalSeconds = 30;
41
42void logWaitRetry(rogue::LoggingPtr log, double seconds, uint32_t& lastLoggedSeconds) {
43 uint32_t elapsedSeconds = static_cast<uint32_t>(seconds);
44
45 if (elapsedSeconds == 0) return;
46
47 if (lastLoggedSeconds == 0) {
48 log->warning(
49 "Timeout waiting for response after %" PRIu32 " seconds, server may be busy. Continuing to wait...",
50 elapsedSeconds);
51 lastLoggedSeconds = elapsedSeconds;
52 return;
53 }
54
55 if (elapsedSeconds >= (lastLoggedSeconds + kRetryLogIntervalSeconds)) {
56 log->warning("Still waiting for response after %" PRIu32 " seconds, server may be busy...", elapsedSeconds);
57 lastLoggedSeconds = elapsedSeconds;
58 }
59}
60
61void logWaitRecovered(rogue::LoggingPtr log, double seconds) {
62 uint32_t elapsedSeconds = static_cast<uint32_t>(seconds);
63
64 if (elapsedSeconds != 0) {
65 log->warning("Received response from server after %" PRIu32 " seconds.", elapsedSeconds);
66 }
67}
68
69#ifndef NO_PYTHON
70
71// Python-facing setTimeout wrapper. A Python bool is an int subclass, so the
72// removed (msecs, waitRetry) form would silently coerce `True` to failTime=1.
73// Reject bools loudly so stale 2-arg callers (e.g. pysmurf's
74// setTimeout(10000, True)) get a clear TypeError instead of misbehaving.
75void setTimeoutPy(rogue::interfaces::ZmqClient& self, bp::object warnTime, bp::object failTime) {
76 if (PyBool_Check(warnTime.ptr()) || PyBool_Check(failTime.ptr())) {
77 PyErr_SetString(PyExc_TypeError,
78 "ZmqClient.setTimeout(warnTime, failTime=0): arguments must be int "
79 "milliseconds, not bool. The legacy (msecs, waitRetry[, maxRetries]) "
80 "form was removed; pass failTime=0 to retry forever.");
81 bp::throw_error_already_set();
82 }
83 self.setTimeout(bp::extract<uint32_t>(warnTime), bp::extract<uint32_t>(failTime));
84}
85
86#endif
87
88} // namespace
89
90rogue::interfaces::ZmqClientPtr rogue::interfaces::ZmqClient::create(const std::string& addr, uint16_t port, bool doString) {
91 rogue::interfaces::ZmqClientPtr ret = std::make_shared<rogue::interfaces::ZmqClient>(addr, port, doString);
92 return (ret);
93}
94
97#ifndef NO_PYTHON
98
99 bp::class_<rogue::interfaces::ZmqClientWrap, rogue::interfaces::ZmqClientWrapPtr, boost::noncopyable>(
100 "ZmqClient",
101 bp::init<std::string, uint16_t, bool>())
104 .def("setTimeout", &setTimeoutPy, (bp::arg("warnTime"), bp::arg("failTime") = bp::object(0u)))
110#endif
111}
112
113rogue::interfaces::ZmqClient::ZmqClient(const std::string& addr, uint16_t port, bool doString) {
114 std::string temp;
115 uint32_t val;
116 uint32_t reqPort;
117
118 this->doString_ = doString;
119 this->zmqCtx_ = zmq_ctx_new();
120 this->zmqSub_ = zmq_socket(this->zmqCtx_, ZMQ_SUB);
121 this->zmqReq_ = zmq_socket(this->zmqCtx_, ZMQ_REQ);
122
123 log_ = rogue::Logging::create("ZmqClient");
124
125 // running_ is the dtor's cleanup sentinel; it is set last on success, so a
126 // throw in here would otherwise leak the context and sockets.
127 try {
128 if (!doString_) {
129 // Setup sub port
130 temp = "tcp://";
131 temp.append(addr);
132 temp.append(":");
133 temp.append(std::to_string(static_cast<int64_t>(port)));
134
135 if (zmq_setsockopt(this->zmqSub_, ZMQ_SUBSCRIBE, "", 0) != 0)
136 throw(rogue::GeneralError("ZmqClient::ZmqClient", "Failed to set socket subscribe"));
137
138 val = 0;
139 if (zmq_setsockopt(this->zmqSub_, ZMQ_LINGER, &val, sizeof(int32_t)) != 0)
140 throw(rogue::GeneralError("ZmqClient::ZmqClient", "Failed to set socket linger"));
141
142 val = 100;
143 if (zmq_setsockopt(this->zmqSub_, ZMQ_RCVTIMEO, &val, sizeof(int32_t)) != 0)
144 throw(rogue::GeneralError("ZmqClient::ZmqClient", "Failed to set socket timeout"));
145
146 if (zmq_connect(this->zmqSub_, temp.c_str()) < 0)
147 throw(rogue::GeneralError::create("ZmqClient::ZmqClient",
148 "Failed to connect to port %" PRIu16 " at address %s",
149 port,
150 addr.c_str()));
151
152 reqPort = port + 1;
153 } else {
154 reqPort = port + 2;
155 }
156
157 // Setup request port
158 temp = "tcp://";
159 temp.append(addr);
160 temp.append(":");
161 temp.append(std::to_string(static_cast<int64_t>(reqPort)));
162
163 timeout_ = 1000; // 1 second warn/poll period
164 failTime_ = 1000; // fail on first timeout by default (no retry)
165 if (zmq_setsockopt(this->zmqReq_, ZMQ_RCVTIMEO, &timeout_, sizeof(int32_t)) != 0)
166 throw(rogue::GeneralError("ZmqClient::ZmqClient", "Failed to set socket timeout"));
167
168 val = 1;
169 if (zmq_setsockopt(this->zmqReq_, ZMQ_REQ_CORRELATE, &val, sizeof(int32_t)) != 0)
170 throw(rogue::GeneralError("ZmqClient::ZmqClient", "Failed to set socket correlate"));
171
172 if (zmq_setsockopt(this->zmqReq_, ZMQ_REQ_RELAXED, &val, sizeof(int32_t)) != 0)
173 throw(rogue::GeneralError("ZmqClient::ZmqClient", "Failed to set socket relaxed"));
174
175 val = 0;
176 if (zmq_setsockopt(this->zmqReq_, ZMQ_LINGER, &val, sizeof(int32_t)) != 0)
177 throw(rogue::GeneralError("ZmqClient::ZmqClient", "Failed to set socket linger"));
178
179 if (zmq_connect(this->zmqReq_, temp.c_str()) < 0)
180 throw(rogue::GeneralError::create("ZmqClient::ZmqClient",
181 "Failed to connect to port %" PRIu32 " at address %s",
182 reqPort,
183 addr.c_str()));
184
185 if (doString_) {
186 threadEn_ = false;
187 log_->info("Connected to Rogue server at port %" PRIu32, reqPort);
188 } else {
189 log_->info("Connected to Rogue server at ports %" PRIu16 ":%" PRIu32, port, reqPort);
190
191 threadEn_ = true;
192 thread_ = new std::thread(&rogue::interfaces::ZmqClient::runThread, this);
193 }
194 running_ = true;
195 } catch (...) {
196 if (zmqSub_ != nullptr) {
197 zmq_close(zmqSub_);
198 zmqSub_ = nullptr;
199 }
200 if (zmqReq_ != nullptr) {
201 zmq_close(zmqReq_);
202 zmqReq_ = nullptr;
203 }
204 if (zmqCtx_ != nullptr) {
205 zmq_ctx_destroy(zmqCtx_);
206 zmqCtx_ = nullptr;
207 }
208 throw;
209 }
210}
211
215
217 if (running_) {
218 running_ = false;
219 // Break any in-flight forever loop (failTime_ == 0) in send()/sendString()
220 // so the recv loop throws and releases reqLock_ within one RCVTIMEO
221 // period. Covers both string and binary modes.
222 stopping_ = true;
223
224 rogue::GilRelease noGil;
225
226 if (threadEn_) {
227 threadEn_ = false;
228 thread_->join();
229 delete thread_;
230 thread_ = nullptr;
231 }
232
233 // ZMQ sockets are not thread-safe. Serialize with any in-flight
234 // send()/sendString() before tearing down zmqReq_ and the context:
235 // stopping_ guarantees such a call releases reqLock_ promptly, so this
236 // cannot deadlock. The SUB thread is already joined, so zmqSub_ is idle.
237 // zmqSub_ is created in both modes (only connected in standard mode);
238 // leaving it open makes zmq_ctx_destroy block forever.
239 std::lock_guard<std::mutex> lock(reqLock_);
240 zmq_close(this->zmqSub_);
241 zmq_close(this->zmqReq_);
242 zmq_ctx_destroy(this->zmqCtx_);
243 }
244}
245
246void rogue::interfaces::ZmqClient::setTimeout(uint32_t warnTime, uint32_t failTime) {
247 // warnTime drives ZMQ_RCVTIMEO and the warn cadence. A value of 0 would make
248 // recv non-blocking, so the send()/sendString() loop would busy-spin without
249 // ever advancing seconds toward failTime. Reject it.
250 if (warnTime == 0)
251 throw rogue::GeneralError("ZmqClient::setTimeout", "warnTime must be greater than 0 milliseconds");
252
253 // ZMQ sockets are not thread-safe; serialize zmqReq_ access.
254 rogue::GilRelease noGil;
255 std::lock_guard<std::mutex> lock(reqLock_);
256
257 timeout_ = warnTime;
258 failTime_ = failTime;
259
260 log_->debug("Setting timeout to warnTime = %" PRIu32 " msecs, failTime = %" PRIu32 " msecs",
261 timeout_,
262 failTime_);
263
264 if (zmq_setsockopt(this->zmqReq_, ZMQ_RCVTIMEO, &timeout_, sizeof(int32_t)) != 0)
265 throw(rogue::GeneralError("ZmqClient::setTimeout", "Failed to set socket timeout"));
266}
267
268std::string rogue::interfaces::ZmqClient::sendString(const std::string& path, const std::string& attr, const std::string& arg) {
269 std::string snd;
270 std::string ret;
271 zmq_msg_t msg;
272 std::string data;
273 double seconds = 0;
274 uint32_t lastLoggedSeconds = 0;
275
276 if (!doString_) throw rogue::GeneralError::create("ZmqClient::sendString", "Invalid send call in standard mode");
277
278 snd = "{\"attr\": \"" + attr + "\",";
279 snd += "\"path\": \"" + path + "\"";
280
281 if (arg != "") snd += ",\"args\": [\"" + arg + "\"]";
282
283 snd += "}";
284
285 rogue::GilRelease noGil;
286 std::lock_guard<std::mutex> lock(reqLock_);
287 zmq_send(this->zmqReq_, snd.c_str(), snd.size(), 0);
288
289 while (1) {
290 zmq_msg_init(&msg);
291 if (zmq_recvmsg(this->zmqReq_, &msg, 0) < 0) {
292 seconds += static_cast<double>(timeout_) / 1000.0;
293 // failTime_ == 0 preserves the historic forever-retry contract
294 // (issue #1236). A finite failTime_ throws once the accumulated
295 // wait reaches the deadline; stopping_ breaks a forever loop on stop().
296 const bool deadlineReached = (failTime_ != 0 && seconds * 1000.0 >= static_cast<double>(failTime_));
297 if (!deadlineReached && !stopping_) {
298 logWaitRetry(log_, seconds, lastLoggedSeconds);
299 zmq_msg_close(&msg);
300 } else {
301 zmq_msg_close(&msg);
302 throw rogue::GeneralError::create("ZmqClient::sendString",
303 "Timeout waiting for response after %d Seconds.",
304 static_cast<int>(seconds));
305 }
306 } else {
307 break;
308 }
309 }
310
311 logWaitRecovered(log_, seconds);
312
313 data = std::string((const char*)zmq_msg_data(&msg), zmq_msg_size(&msg));
314 zmq_msg_close(&msg);
315 return data;
316}
317
318std::string rogue::interfaces::ZmqClient::getDisp(const std::string& path) {
319 return sendString(path, "getDisp", "");
320}
321
322void rogue::interfaces::ZmqClient::setDisp(const std::string& path, const std::string& value) {
323 sendString(path, "setDisp", value);
324}
325
326std::string rogue::interfaces::ZmqClient::exec(const std::string& path, const std::string& arg) {
327 return sendString(path, "__call__", arg);
328}
329
330std::string rogue::interfaces::ZmqClient::valueDisp(const std::string& path) {
331 return sendString(path, "valueDisp", "");
332}
333
334#ifndef NO_PYTHON
335
336bp::object rogue::interfaces::ZmqClient::send(bp::object value) {
337 zmq_msg_t txMsg;
338 zmq_msg_t rxMsg;
339 Py_buffer valueBuf;
340 bp::object ret;
341 double seconds = 0;
342 uint32_t lastLoggedSeconds = 0;
343
344 if (doString_) throw rogue::GeneralError::create("ZmqClient::send", "Invalid send call in string mode");
345
346 // PyErr_Print surfaces the Python-level error (e.g. TypeError from a
347 // non-buffer argument) AND clears the thread's error indicator; without
348 // it boost::python may observe "exception already set" when translating
349 // our throw, masking the original cause.
350 if (PyObject_GetBuffer(value.ptr(), &(valueBuf), PyBUF_SIMPLE) < 0) {
351 PyErr_Print();
352 throw(rogue::GeneralError::create("ZmqClient::send", "Failed to extract object data"));
353 }
354
355 // zmq_msg_init_size returns -1 on allocation failure; the message is left
356 // uninitialized so memcpy/zmq_msg_data must not run, and we still own
357 // valueBuf and have to release it before propagating the error.
358 if (zmq_msg_init_size(&txMsg, valueBuf.len) < 0) {
359 PyBuffer_Release(&valueBuf);
360 throw(rogue::GeneralError::create("ZmqClient::send", "zmq_msg_init_size failed"));
361 }
362 memcpy(zmq_msg_data(&txMsg), valueBuf.buf, valueBuf.len);
363 PyBuffer_Release(&valueBuf);
364
365 {
366 rogue::GilRelease noGil;
367 std::lock_guard<std::mutex> lock(reqLock_);
368 // On success, zmq_sendmsg transfers ownership and zeroes the message;
369 // on failure (-1, e.g. ETERM during shutdown) we retain ownership and
370 // must close to avoid leaking the libzmq message buffer. Throwing here
371 // also prevents the recv loop below from blocking on a request that
372 // was never put on the wire.
373 if (zmq_sendmsg(this->zmqReq_, &txMsg, 0) < 0) {
374 // Capture errno before zmq_msg_close, which may overwrite it.
375 // ETERM here is the issue #1234 signature: VirtualClient.__init__
376 // tore down the context via _cleanupFailedInit() but returned
377 // without raising, so the caller is now sending on a dead socket.
378 int err = zmq_errno();
379 zmq_msg_close(&txMsg);
380 throw rogue::GeneralError::create("ZmqClient::send",
381 "zmq_sendmsg failed: errno=%d %s",
382 err,
383 zmq_strerror(err));
384 }
385
386 while (1) {
387 zmq_msg_init(&rxMsg);
388 if (zmq_recvmsg(this->zmqReq_, &rxMsg, 0) < 0) {
389 seconds += static_cast<double>(timeout_) / 1000.0;
390 // failTime_ == 0 preserves the historic forever-retry contract
391 // (issue #1236). A finite failTime_ throws once the accumulated
392 // wait reaches the deadline; stopping_ breaks a forever loop on stop().
393 const bool deadlineReached = (failTime_ != 0 && seconds * 1000.0 >= static_cast<double>(failTime_));
394 if (!deadlineReached && !stopping_) {
395 logWaitRetry(log_, seconds, lastLoggedSeconds);
396 zmq_msg_close(&rxMsg);
397 } else {
398 zmq_msg_close(&rxMsg);
400 "ZmqClient::send",
401 "Timeout waiting for response after %d Seconds, server may be busy!",
402 static_cast<int>(seconds));
403 }
404 } else {
405 break;
406 }
407 }
408 }
409
410 logWaitRecovered(log_, seconds);
411
412 PyObject* val = Py_BuildValue("y#", zmq_msg_data(&rxMsg), zmq_msg_size(&rxMsg));
413
414 // PyErr_Print surfaces the underlying Python error (e.g. MemoryError) AND
415 // clears the thread's error indicator; without it boost::python may
416 // observe "exception already set" when translating our throw.
417 if (val == NULL) {
418 PyErr_Print();
419 zmq_msg_close(&rxMsg);
420 throw(rogue::GeneralError::create("ZmqClient::send", "Failed to generate bytearray"));
421 }
422
423 zmq_msg_close(&rxMsg);
424
425 bp::handle<> handle(val);
426 ret = bp::object(handle);
427 return ret;
428}
429
431
432rogue::interfaces::ZmqClientWrap::ZmqClientWrap(const std::string& addr, uint16_t port, bool doString)
433 : rogue::interfaces::ZmqClient(addr, port, doString) {}
434
436 if (bp::override f = this->get_override("_doUpdate")) {
437 try {
438 f(data);
439 } catch (...) {
440 PyErr_Print();
441 }
442 }
444}
445
449
450#endif
451
452void rogue::interfaces::ZmqClient::runThread() {
453 zmq_msg_t msg;
454
455 log_->logThreadId();
456
457 while (threadEn_) {
458 zmq_msg_init(&msg);
459
460 // Get the message
461 if (zmq_recvmsg(this->zmqSub_, &msg, 0) > 0) {
462#ifndef NO_PYTHON
463 // A Python application can exit with this thread still subscribed.
464 // Py_IsInitialized() goes false when finalization starts, well
465 // before the interpreter state that PyGILState_Ensure() needs is
466 // torn down, so drop updates from then on instead of taking the GIL.
467 if (!Py_IsInitialized()) {
468 zmq_msg_close(&msg);
469 continue;
470 }
471
472 try {
474 PyObject* val = Py_BuildValue("y#", zmq_msg_data(&msg), zmq_msg_size(&msg));
475
476 // PyErr_Print surfaces the Python-level error (e.g. MemoryError) AND clears
477 // the thread's error indicator; without this, the pending exception leaks
478 // into the next loop iteration's Py_BuildValue/bp::object calls.
479 if (val == NULL) {
480 PyErr_Print();
481 throw(rogue::GeneralError::create("ZmqClient::runThread", "Failed to generate bytearray"));
482 }
483
484 bp::handle<> handle(val);
485 bp::object dat = bp::object(handle);
486 this->doUpdate(dat);
487 } catch (const std::exception& e) {
488 log_->warning("ZmqClient::runThread: dropping update after exception: %s", e.what());
489 } catch (...) {
490 log_->warning("ZmqClient::runThread: dropping update after unknown exception");
491 }
492#endif
493 }
494 // Close even on RCVTIMEO: zmq_recvmsg() initializes msg regardless.
495 zmq_msg_close(&msg);
496 }
497}
Generic Rogue exception type.
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
Definition Logging.cpp:95
RAII helper that acquires the Python GIL for a scope.
Definition ScopedGil.h:35
void defDoUpdate(boost::python::object data)
Calls base-class doUpdate() implementation.
void doUpdate(boost::python::object data)
Handles an update message from the subscription path.
ZmqClientWrap(const std::string &addr, uint16_t port, bool doString)
Constructs wrapper client.
ZeroMQ client for Rogue control and update messaging.
Definition ZmqClient.h:50
std::string sendString(const std::string &path, const std::string &attr, const std::string &arg)
Sends a string-mode request.
void stop()
Stops client sockets and background thread.
std::string getDisp(const std::string &path)
Reads display-formatted value at a path (string mode).
virtual void doUpdate(boost::python::object data)
Handles async update payloads received on subscriber socket.
std::string exec(const std::string &path, const std::string &arg="")
Executes callable node at path (string mode).
boost::python::object send(boost::python::object data)
Sends binary request payload and receives binary response.
static void setup_python()
Registers Python bindings for this class.
Definition ZmqClient.cpp:96
void setTimeout(uint32_t warnTime, uint32_t failTime=0)
Sets request timeout behavior.
std::string valueDisp(const std::string &path)
Reads compact value display at a path (string mode).
static std::shared_ptr< rogue::interfaces::ZmqClient > create(const std::string &addr, uint16_t port, bool doString)
Creates a ZeroMQ client.
Definition ZmqClient.cpp:90
ZmqClient(const std::string &addr, uint16_t port, bool doString)
Constructs a ZeroMQ client and connects sockets.
virtual ~ZmqClient()
Destroys client and stops background activity.
void setDisp(const std::string &path, const std::string &value)
Writes display-formatted value at a path (string mode).
std::shared_ptr< rogue::interfaces::ZmqClient > ZmqClientPtr
Definition ZmqClient.h:209
std::shared_ptr< rogue::Logging > LoggingPtr
Shared pointer alias for Logging.
Definition Logging.h:205