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 // Request timeout in milliseconds.
59 uint32_t timeout_;
60
61 // Continue retrying after timeout when true.
62 bool waitRetry_;
63
64 // Maximum recv retries when waitRetry_ is true. 0 means unbounded (the
65 // default, preserving the pre-#1236 forever-retry contract that pysmurf
66 // and other downstream callers rely on).
67 uint32_t maxRetries_;
68
70 protected:
71 std::thread* thread_ = nullptr;
72 std::atomic<bool> threadEn_{false};
74
75 private:
76 bool running_ = false;
77
78 // True when operating in string request mode.
79 bool doString_;
80
81 // Mutex serializing the ZMQ_REQ socket's send/recv cycle.
82 std::mutex reqLock_;
83
84 void runThread();
85
86 public:
102 static std::shared_ptr<rogue::interfaces::ZmqClient> create(const std::string& addr, uint16_t port, bool doString);
103
105 static void setup_python();
106
118 ZmqClient(const std::string& addr, uint16_t port, bool doString);
119
121 virtual ~ZmqClient();
122
139 void setTimeout(uint32_t msecs, bool waitRetry, uint32_t maxRetries = 0);
140
148 std::string sendString(const std::string& path, const std::string& attr, const std::string& arg);
149
155 std::string getDisp(const std::string& path);
156
162 void setDisp(const std::string& path, const std::string& value);
163
170 std::string exec(const std::string& path, const std::string& arg = "");
171
177 std::string valueDisp(const std::string& path);
178
179#ifndef NO_PYTHON
185 boost::python::object send(boost::python::object data);
186
191 virtual void doUpdate(boost::python::object data);
192#endif
193
195 void stop();
196};
197typedef std::shared_ptr<rogue::interfaces::ZmqClient> ZmqClientPtr;
198
199#ifndef NO_PYTHON
200
204class ZmqClientWrap : public rogue::interfaces::ZmqClient, public boost::python::wrapper<rogue::interfaces::ZmqClient> {
205 public:
212 ZmqClientWrap(const std::string& addr, uint16_t port, bool doString);
213
221 void doUpdate(boost::python::object data);
222
230 void defDoUpdate(boost::python::object data);
231};
232
233typedef std::shared_ptr<rogue::interfaces::ZmqClientWrap> ZmqClientWrapPtr;
234#endif
235} // namespace interfaces
236} // namespace rogue
237
238#endif
Python-overridable wrapper for ZmqClient.
Definition ZmqClient.h:204
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.
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:77
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:71
virtual ~ZmqClient()
Destroys client and stops background activity.
void setTimeout(uint32_t msecs, bool waitRetry, uint32_t maxRetries=0)
Sets request timeout behavior.
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:197
std::shared_ptr< rogue::interfaces::ZmqClientWrap > ZmqClientWrapPtr
Definition ZmqClient.h:233