7#include <spdlog/spdlog.h>
9constexpr uint32_t
LOG_LEVEL_ID = (
static_cast<uint32_t
>(
'L') | (
static_cast<uint32_t
>(
'O') << 8) |
10 (
static_cast<uint32_t
>(
'G') << 16) | (
static_cast<uint32_t
>(
'L') << 24));
11constexpr uint32_t
VERSION_INFO_ID = (
static_cast<uint32_t
>(
'V') | (
static_cast<uint32_t
>(
'E') << 8) |
12 (
static_cast<uint32_t
>(
'R') << 16) | (
static_cast<uint32_t
>(
'I') << 24));
13constexpr uint32_t
PING_PONG_ID = (
static_cast<uint32_t
>(
'P') | (
static_cast<uint32_t
>(
'I') << 8) |
14 (
static_cast<uint32_t
>(
'N') << 16) | (
static_cast<uint32_t
>(
'G') << 24));
15constexpr uint32_t
STATUS_CHECK_ID = (
static_cast<uint32_t
>(
'S') | (
static_cast<uint32_t
>(
'C') << 8) |
16 (
static_cast<uint32_t
>(
'H') << 16) | (
static_cast<uint32_t
>(
'K') << 24));
29 if (!recvMsgs.empty())
31 std::vector<zmq::message_t> replyMsgs;
37 if (
size_t nSentMsg =
sendMessages(replyMsgs); nSentMsg != replyMsgs.size())
39 spdlog::warn(
"Can't send whole reply: Sent messages {} / {}", nSentMsg, replyMsgs.size());
45 _stats->consumeStats(recvMsgs, replyMsgs, serverStats);
52 spdlog::info(
"ZeroMQ server started");
63 catch (
const std::exception &e)
65 spdlog::error(
"ZeroMQ server failed: {}", e.what());
68 spdlog::info(
"ZeroMQ server stopped");
72 const std::shared_ptr<prometheus::Registry> ®,
const std::string &prependName)
73 :
ZeroMQ(zmq::socket_type::rep, hostAddr, true), _checkFlag(std::move(checkFlag))
77 _stats = std::make_unique<ZeroMQStats>(reg, prependName);
109 spdlog::trace(
"Received {} messages", recvMsgs.size());
112 std::string replyBody;
113 int reply = ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL;
114 switch (*(
static_cast<const uint64_t *
>(recvMsgs[0].data())))
117 if (recvMsgs.size() != 2)
119 spdlog::error(
"Received unknown number of messages for log level change");
123 spdlog::warn(
"Log level change request received");
124 const auto receivedMsg = std::string(
static_cast<const char *
>(recvMsgs[1].data()), recvMsgs[1].size());
126 if (receivedMsg ==
"v")
128 spdlog::set_level(spdlog::level::info);
130 if (receivedMsg ==
"vv")
132 spdlog::set_level(spdlog::level::debug);
134 if (receivedMsg ==
"vvv")
136 spdlog::set_level(spdlog::level::trace);
138 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
142 if (recvMsgs.size() != 1)
144 spdlog::error(
"Received unknown number of messages for version information");
148 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
149 replyBody = PROJECT_FULL_VERSION_STRING;
153 if (recvMsgs.size() != 1)
155 spdlog::error(
"Received unknown number of messages for ping");
159 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
164 if (recvMsgs.size() != 1)
166 spdlog::error(
"Received unknown number of messages for status check");
170 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
172 std::ostringstream oss;
174 for (
const auto &[process, statusFlag] :
vCheckFlag)
176 oss <<
"\"" << process <<
"\":" << (statusFlag->_M_i ?
"1," :
"0,");
178 replyBody = oss.str();
179 replyBody.replace(replyBody.size() - 1, 1,
"}");
190 spdlog::error(
"Unknown command received from control");
195 replyMsgs.emplace_back(&reply,
sizeof(reply));
196 replyMsgs.emplace_back(replyBody.c_str(), replyBody.size());
198 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.