ADD: added waiting to getLatestMessage

This commit is contained in:
hwinkel
2023-11-10 13:23:32 +01:00
parent 113b37b3e6
commit 6450427099

View File

@@ -1,6 +1,7 @@
#include <DirectCommunicationClient.hpp> #include <DirectCommunicationClient.hpp>
#include <chrono>
#include <thread>
#include "zmq.hpp" #include "zmq.hpp"
#include <functional> #include <functional>
#include <memory> #include <memory>
@@ -82,13 +83,38 @@ namespace DirectCommunication
std::string DirectCommunicationClient::getLatestMessage() std::string DirectCommunicationClient::getLatestMessage()
{ {
if (receivedMessages_.size() > 0) if (receivedMessages_.size() > 0)
{ {
std::string msg; std::string msg;
receivedMessages_.get(msg); receivedMessages_.get(msg);
return msg; return msg;
}else{
auto start = std::chrono::steady_clock::now();
while (receivedMessages_.size() == 0)
{
auto now = std::chrono::steady_clock::now();
auto elapsed = duration_cast<std::chrono::milliseconds>(now - start);
if (elapsed >= std::chrono::milliseconds(5000)) { // replace 5000 with the number of milliseconds you want to wait
return "NULL";
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::string msg;
receivedMessages_.get(msg);
return msg;
} }
return "NULL"; return "NULL";
} }
void DirectCommunicationClient::workerFunc_() void DirectCommunicationClient::workerFunc_()