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 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
137 break;
138 }
139 case VERSION_INFO_ID: {
140 if (recvMsgs.size() != 1)
141 {
142 spdlog::error("Received unknown number of messages for version information");
143 break;
144 }
145
146 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
147 replyBody = PROJECT_FULL_VERSION_STRING;
148 break;
149 }
150 case PING_PONG_ID: {
151 if (recvMsgs.size() != 1)
152 {
153 spdlog::error("Received unknown number of messages for ping");
154 break;
155 }
156
157 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
158 replyBody = "PONG";
159 break;
160 }
161 case STATUS_CHECK_ID: {
162 if (recvMsgs.size() != 1)
163 {
164 spdlog::error("Received unknown number of messages for status check");
165 break;
166 }
167
168 reply = ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
169
170 std::ostringstream oss;
171 oss << "{";
172 for (const auto &[process, statusFlag] : vCheckFlag)
173 {
174 oss << "\"" << process << "\":" << (statusFlag->_M_i ? "1," : "0,");
175 }
176 replyBody = oss.str();
177 replyBody.replace(replyBody.size() - 1, 1, "}");
178 break;
179 }
180 /* ################################################################################### */
181 /* ############################# MAKE MODIFICATIONS HERE ############################# */
182 /* ################################################################################### */
183
184 /* ################################################################################### */
185 /* ################################ END MODIFICATIONS ################################ */
186 /* ################################################################################### */
187 default:
188 spdlog::error("Unknown command received from control");
189 break;
190 }
191
192 // Prepare reply
193 replyMsgs.emplace_back(&reply, sizeof(reply));
194 replyMsgs.emplace_back(replyBody.c_str(), replyBody.size());
195
196 return reply == ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
197}
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.