Repo-Init
 
Loading...
Searching...
No Matches
ZeroMQServer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "zeromq/ZeroMQ.hpp"
6
7#include <functional>
8
9using FPTR_MessageCallback = std::function<bool(const std::vector<zmq::message_t> &, std::vector<zmq::message_t> &)>;
10
11class ZeroMQServer : private ZeroMQ, private ZeroMQMonitor {
12 private:
13 // Thread for processing messages
14 std::unique_ptr<std::thread> _serverThread;
15 // Flag to stop processing messages
16 std::atomic_flag _shouldStop{false};
17 // Flag to check if the server is running
18 std::shared_ptr<std::atomic_flag> _checkFlag;
19 // Statistics
20 std::unique_ptr<ZeroMQStats> _stats;
21 // Called after every message function(std::vector<zmq::message_t>) {}
23
25 void update();
26
28 void threadFunc() noexcept;
29
30 public:
38 ZeroMQServer(const std::string &hostAddr, std::shared_ptr<std::atomic_flag> checkFlag,
39 const std::shared_ptr<prometheus::Registry> &reg = nullptr, const std::string &prependName = "");
40
42 ZeroMQServer(const ZeroMQServer & /*unused*/) = delete;
43
45 ZeroMQServer(ZeroMQServer && /*unused*/) = delete;
46
48 ZeroMQServer &operator=(ZeroMQServer /*unused*/) = delete;
49
51 ZeroMQServer &operator=(ZeroMQServer && /*unused*/) = delete;
52
58 bool initialise();
59
61 void shutdown();
62
66 ~ZeroMQServer() override { shutdown(); }
67
73
79};
80
88bool ZeroMQServerMessageCallback(const std::vector<zmq::message_t> &recvMsgs, std::vector<zmq::message_t> &replyMsgs);
std::function< bool(const std::vector< zmq::message_t > &, std::vector< zmq::message_t > &)> FPTR_MessageCallback
bool ZeroMQServerMessageCallback(const std::vector< zmq::message_t > &recvMsgs, std::vector< zmq::message_t > &replyMsgs)
std::atomic_flag _shouldStop
void update()
Processes new messages.
std::unique_ptr< ZeroMQStats > _stats
void messageCallback(FPTR_MessageCallback func)
std::shared_ptr< std::atomic_flag > _checkFlag
void threadFunc() noexcept
Main thread function.
FPTR_MessageCallback messageCallback() const
FPTR_MessageCallback _m_messageCallback
std::unique_ptr< std::thread > _serverThread
void shutdown()
Closes the ZeroMQ Server.