7#include <spdlog/spdlog.h>
9constexpr uint32_t
LOG_LEVEL_ID = (
'L' | (
'O' << 8) | (
'G' << 16) | (
'L' << 24));
10constexpr uint32_t
VERSION_INFO_ID = (
'V' | (
'E' << 8) | (
'R' << 16) | (
'I' << 24));
11constexpr uint32_t
PING_PONG_ID = (
'P' | (
'I' << 8) | (
'N' << 16) | (
'G' << 24));
12constexpr uint32_t
STATUS_CHECK_ID = (
'S' | (
'C' << 8) | (
'H' << 16) | (
'K' << 24));
25 if (!recvMsgs.empty())
27 std::vector<zmq::message_t> replyMsgs;
34 if (nSentMsg != replyMsgs.size())
36 spdlog::warn(
"Can't send whole reply: Sent messages {} / {}", nSentMsg, replyMsgs.size());
42 _stats->consumeStats(recvMsgs, replyMsgs, serverStats);
49 spdlog::info(
"ZeroMQ server started");
60 catch (
const std::exception &e)
62 spdlog::error(
"ZeroMQ server failed: {}", e.what());
65 spdlog::info(
"ZeroMQ server stopped");
69 const std::shared_ptr<prometheus::Registry> ®,
const std::string &prependName)
70 :
ZeroMQ(zmq::socket_type::rep, hostAddr, true), _checkFlag(std::move(checkFlag))
74 _stats = std::make_unique<ZeroMQStats>(reg, prependName);
106 spdlog::trace(
"Received {} messages", recvMsgs.size());
109 std::string replyBody;
110 int reply = ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL;
111 switch (*(
static_cast<const uint64_t *
>(recvMsgs[0].data())))
114 if (recvMsgs.size() != 2)
116 spdlog::error(
"Received unknown number of messages for log level change");
120 spdlog::warn(
"Log level change request received");
121 const auto receivedMsg = std::string(
static_cast<const char *
>(recvMsgs[1].data()), recvMsgs[1].size());
123 if (receivedMsg ==
"v")
125 spdlog::set_level(spdlog::level::info);
127 if (receivedMsg ==
"vv")
129 spdlog::set_level(spdlog::level::debug);
131 if (receivedMsg ==
"vvv")
133 spdlog::set_level(spdlog::level::trace);
135 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
139 if (recvMsgs.size() != 1)
141 spdlog::error(
"Received unknown number of messages for version information");
145 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
146 replyBody = PROJECT_FULL_VERSION_STRING;
150 if (recvMsgs.size() != 1)
152 spdlog::error(
"Received unknown number of messages for ping");
156 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
161 if (recvMsgs.size() != 1)
163 spdlog::error(
"Received unknown number of messages for status check");
167 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
169 std::ostringstream oss;
173 oss <<
"\"" << entry.first <<
"\":" << (entry.second->_M_i ?
"1," :
"0,");
175 replyBody = oss.str();
176 replyBody.replace(replyBody.size() - 1, 1,
"}");
187 spdlog::error(
"Unknown command received from control");
192 replyMsgs.emplace_back(&reply,
sizeof(reply));
193 replyMsgs.emplace_back(replyBody.c_str(), replyBody.size());
195 return reply == ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
std::vector< std::pair< std::string, std::shared_ptr< std::atomic_flag > > > vCheckFlag
Global variable to check if the servers are running.
constexpr size_t constHasher(const char *s)
constexpr uint32_t PING_PONG_ID
constexpr uint32_t VERSION_INFO_ID
constexpr uint32_t LOG_LEVEL_ID
constexpr uint32_t STATUS_CHECK_ID
bool ZeroMQServerMessageCallback(const std::vector< zmq::message_t > &recvMsgs, std::vector< zmq::message_t > &replyMsgs)
void startMonitoring(zmq::socket_t *socket, const std::string &monitorAddress)
std::atomic_flag _shouldStop
void update()
Processes new messages.
std::unique_ptr< ZeroMQStats > _stats
std::shared_ptr< std::atomic_flag > _checkFlag
void threadFunc() noexcept
Main thread function.
FPTR_MessageCallback messageCallback() const
ZeroMQServer(const std::string &hostAddr, std::shared_ptr< std::atomic_flag > checkFlag, const std::shared_ptr< prometheus::Registry > ®=nullptr, const std::string &prependName="")
std::unique_ptr< std::thread > _serverThread
void shutdown()
Closes the ZeroMQ Server.
std::vector< zmq::message_t > recvMessages()
const std::unique_ptr< zmq::socket_t > & getSocket() const
size_t sendMessages(std::vector< zmq::message_t > &msg)
bool isSuccessful
Indicates if processing was successful for this connection.
std::chrono::high_resolution_clock::time_point processingTimeEnd
Processing time end.
std::chrono::high_resolution_clock::time_point processingTimeStart
Processing time start.