rogue
Loading...
Searching...
No Matches
ZmqClient.h
Go to the documentation of this file.
1
17#ifndef __ROGUE_ZMQ_CLIENT_H__
18#define __ROGUE_ZMQ_CLIENT_H__
19#include "rogue/Directives.h"
20
21#include <atomic>
22#include <memory>
23#include <mutex>
24#include <string>
25#include <thread>
26
27#include "rogue/Logging.h"
28
29#ifndef NO_PYTHON
30 #include <boost/python.hpp>
31#endif
32
33namespace rogue {
34namespace interfaces {
35
50class ZmqClient {
51 void* zmqCtx_ = nullptr;
52 void* zmqSub_ = nullptr;
53 void* zmqReq_ = nullptr;
54
55 // Logger instance.
56 std::shared_ptr<rogue::Logging> log_;
57
58 // Warn/poll period in milliseconds (drives ZMQ_RCVTIMEO). Each time it
59 // trips, the recv loop logs a "still waiting" warning and keeps waiting.
60 uint32_t timeout_;
61
62 // Absolute wait deadline in milliseconds. Once the accumulated wait reaches
63 // failTime_ the recv loop throws. 0 means no deadline (retry forever),
64 // preserving the pre-#1236 contract that pysmurf and other downstream
65 // callers rely on.
66 uint32_t failTime_;
67
68 // Shutdown sentinel. stop() sets this so an in-flight forever loop
69 // (failTime_ == 0) throws out of send()/sendString() instead of blocking
70 // teardown.
71 std::atomic<bool> stopping_{false};
72
74 protected:
75 std::thread* thread_ = nullptr;
76 std::atomic<bool> threadEn_{false};
78
79 private:
80 bool running_ = false;
81
82 // True when operating in string request mode.
83 bool doString_;
84
85 // Mutex serializing the ZMQ_REQ socket's send/recv cycle.
86 std::mutex reqLock_;
87
88 void runThread();
89
90 public:
106 static std::shared_ptr<rogue::interfaces::ZmqClient> create(const std::string& addr, uint16_t port, bool doString);
107
109 static void setup_python();
110
122 ZmqClient(const std::string& addr, uint16_t port, bool doString);
123
125 virtual ~ZmqClient();
126
144 void setTimeout(uint32_t warnTime, uint32_t failTime = 0);
145
146 // Reject legacy (msecs, waitRetry[, maxRetries]) calls at compile time: a
147 // bool argument would otherwise implicitly convert to uint32_t and silently
148 // set failTime = 1 ms. Stale C++ callers get a compile error instead. The
149 // Python binding rejects bools at runtime via the setTimeoutPy wrapper.
150 void setTimeout(uint32_t warnTime, bool failTime) = delete;
151 void setTimeout(uint32_t warnTime, bool waitRetry, uint32_t maxRetries) = delete;
152
160 std::string sendString(const std::string& path, const std::string& attr, const std::string& arg);
161
167 std::string getDisp(const std::string& path);
168
174 void setDisp(const std::string& path, const std::string& value);
175
182 std::string exec(const std::string& path, const std::string& arg = "");
183
189 std::string valueDisp(const std::string& path);
190
191#ifndef NO_PYTHON
197 boost::python::object send(boost::python::object data);
198
203 virtual void doUpdate(boost::python::object data);
204#endif
205
207 void stop();
208};
209typedef std::shared_ptr<rogue::interfaces::ZmqClient> ZmqClientPtr;
210
211#ifndef NO_PYTHON
212
216class ZmqClientWrap : public rogue::interfaces::ZmqClient, public boost::python::wrapper<rogue::interfaces::ZmqClient> {
217 public:
224 ZmqClientWrap(const std::string& addr, uint16_t port, bool doString);
225
233 void doUpdate(boost::python::object data);
234
242 void defDoUpdate(boost::python::object data);
243};
244
245typedef std::shared_ptr<rogue::interfaces::ZmqClientWrap> ZmqClientWrapPtr;
246#endif
247} // namespace interfaces
248} // namespace rogue
249
250#endif
Python-overridable wrapper for ZmqClient.
Definition ZmqClient.h:216
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.
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.
void setTimeout(uint32_t warnTime, bool waitRetry, uint32_t maxRetries)=delete
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.
void setTimeout(uint32_t warnTime, bool failTime)=delete
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
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::interfaces::ZmqClientWrap > ZmqClientWrapPtr
Definition ZmqClient.h:245