Repo-Init
 
Loading...
Searching...
No Matches
ZeroMQServer.cpp File Reference
#include "zeromq/ZeroMQServer.hpp"
#include <format>
#include "Version.h"
#include "utils/ErrorHelpers.hpp"
#include "utils/Hasher.hpp"
#include <spdlog/spdlog.h>
Include dependency graph for ZeroMQServer.cpp:

Go to the source code of this file.

Functions

bool ZeroMQServerMessageCallback (const std::vector< zmq::message_t > &recvMsgs, std::vector< zmq::message_t > &replyMsgs)
 

Variables

constexpr uint32_t LOG_LEVEL_ID
 
constexpr uint32_t VERSION_INFO_ID
 
constexpr uint32_t PING_PONG_ID
 
constexpr uint32_t STATUS_CHECK_ID
 

Function Documentation

◆ ZeroMQServerMessageCallback()

bool ZeroMQServerMessageCallback ( const std::vector< zmq::message_t > & recvMsgs,
std::vector< zmq::message_t > & replyMsgs )

ZeroMQ message received callback

Parameters
[in]recvMsgsReceived messages
[out]replyMsgsReply messages returned by callback
Returns
true If the callback successfully processes the received messages
false otherwise

Definition at line 105 of file ZeroMQServer.cpp.

106{
107 spdlog::trace("Received {} messages", recvMsgs.size());
108 replyMsgs.clear();
109
110 std::string replyBody;
111 int reply = ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL;
112 switch (*(static_cast<const uint64_t *>(recvMsgs[0].data())))
113 {
114 case LOG_LEVEL_ID: {
115 if (recvMsgs.size() != 2)
116 {
117 spdlog::error("Received unknown number of messages for log level change");
118 break;
119 }
120
121 spdlog::warn("Log level change request received");
122 const auto receivedMsg = std::string(static_cast<const char *>(recvMsgs[1].data()), recvMsgs[1].size());
123
124 if (receivedMsg == "v")
125 {
126 spdlog::set_level(spdlog::level::info);
127 }
128 if (receivedMsg == "vv")
129 {
130 spdlog::set_level(spdlog::level::debug);
131 }
132 if (receivedMsg == "vvv")
133 {
134 spdlog::set_level(spdlog::level::trace);
135 }
136 if (receivedMsg == "r")
137 {
138#ifdef NDEBUG
139 spdlog::set_level(spdlog::level::warn);
140#else
141 spdlog::set_level(spdlog::level::info);
142#endif
143 }
144 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
145 break;
146 }
147 case VERSION_INFO_ID: {
148 if (recvMsgs.size() != 1)
149 {
150 spdlog::error("Received unknown number of messages for version information");
151 break;
152 }
153
154 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
155 replyBody = PROJECT_FULL_VERSION_STRING;
156 break;
157 }
158 case PING_PONG_ID: {
159 if (recvMsgs.size() != 1)
160 {
161 spdlog::error("Received unknown number of messages for ping");
162 break;
163 }
164
165 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
166 replyBody = "PONG";
167 break;
168 }
169 case STATUS_CHECK_ID: {
170 if (recvMsgs.size() != 1)
171 {
172 spdlog::error("Received unknown number of messages for status check");
173 break;
174 }
175
176 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
177
178 std::ostringstream oss;
179 oss << "{";
180 for (const auto &[process, statusFlag] : vCheckFlag)
181 {
182 oss << "\"" << process << "\":" << (statusFlag->_M_i ? "1," : "0,");
183 }
184 replyBody = oss.str();
185 replyBody.replace(replyBody.size() - 1, 1, "}");
186 break;
187 }
188 /* ################################################################################### */
189 /* ############################# MAKE MODIFICATIONS HERE ############################# */
190 /* ################################################################################### */
191
192 /* ################################################################################### */
193 /* ################################ END MODIFICATIONS ################################ */
194 /* ################################################################################### */
195 default:
196 spdlog::error("Unknown command received from control");
197 break;
198 }
199
200 // Prepare reply
201 replyMsgs.emplace_back(&reply, sizeof(reply));
202 replyMsgs.emplace_back(replyBody.c_str(), replyBody.size());
203
204 return reply == ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
205}
std::vector< std::pair< std::string, std::shared_ptr< std::atomic_flag > > > vCheckFlag
Global variable to check if the servers are running.
constexpr uint32_t PING_PONG_ID
constexpr uint32_t VERSION_INFO_ID
constexpr uint32_t LOG_LEVEL_ID
constexpr uint32_t STATUS_CHECK_ID
Here is the caller graph for this function:

Variable Documentation

◆ LOG_LEVEL_ID

uint32_t LOG_LEVEL_ID
constexpr
Initial value:
= (static_cast<uint32_t>('L') | (static_cast<uint32_t>('O') << 8) |
(static_cast<uint32_t>('G') << 16) | (static_cast<uint32_t>('L') << 24))

Definition at line 11 of file ZeroMQServer.cpp.

◆ PING_PONG_ID

uint32_t PING_PONG_ID
constexpr
Initial value:
= (static_cast<uint32_t>('P') | (static_cast<uint32_t>('I') << 8) |
(static_cast<uint32_t>('N') << 16) | (static_cast<uint32_t>('G') << 24))

Definition at line 15 of file ZeroMQServer.cpp.

◆ STATUS_CHECK_ID

uint32_t STATUS_CHECK_ID
constexpr
Initial value:
= (static_cast<uint32_t>('S') | (static_cast<uint32_t>('C') << 8) |
(static_cast<uint32_t>('H') << 16) | (static_cast<uint32_t>('K') << 24))

Definition at line 17 of file ZeroMQServer.cpp.

◆ VERSION_INFO_ID

uint32_t VERSION_INFO_ID
constexpr
Initial value:
= (static_cast<uint32_t>('V') | (static_cast<uint32_t>('E') << 8) |
(static_cast<uint32_t>('R') << 16) | (static_cast<uint32_t>('I') << 24))

Definition at line 13 of file ZeroMQServer.cpp.