ADD: added methode for message handling in the directCommsClient and Server

This commit is contained in:
Henry Winkel
2023-08-09 15:09:57 +02:00
parent 9ffa95877b
commit 3a195b519c
5 changed files with 40 additions and 4 deletions

View File

@@ -7,6 +7,7 @@
#include "zmq.hpp" #include "zmq.hpp"
#include <atomic> #include <atomic>
#include <cstdint> #include <cstdint>
#include <functional>
#include <memory> #include <memory>
#include <string> #include <string>
#include <sys/types.h> #include <sys/types.h>
@@ -27,6 +28,9 @@ namespace DirectCommunication
void sendMessage(std::string msg); void sendMessage(std::string msg);
std::string getLatestMessage(); std::string getLatestMessage();
void registerMessageCallback(std::function<void(std::string)> MessageHandle);
private: private:
ushort port_; ushort port_;
std::string serverAddress_; std::string serverAddress_;
@@ -40,6 +44,8 @@ namespace DirectCommunication
std::atomic_bool isConnected = true; std::atomic_bool isConnected = true;
std::function<void(std::string)> MessageHandle_ = nullptr;
WHISPER::threadSafeQueue<std::string> receivedMessages_; WHISPER::threadSafeQueue<std::string> receivedMessages_;

View File

@@ -7,6 +7,7 @@
#include "zmq.hpp" #include "zmq.hpp"
#include <atomic> #include <atomic>
#include <cstdint> #include <cstdint>
#include <functional>
#include <memory> #include <memory>
#include <string> #include <string>
#include <sys/types.h> #include <sys/types.h>
@@ -27,6 +28,8 @@ namespace DirectCommunication
void sendMessage(std::string msg); void sendMessage(std::string msg);
std::string getLatestMessage(); std::string getLatestMessage();
void registerMessageCallback(std::function<void(std::string)> MessageHandle);
int countClients(); int countClients();
private: private:
@@ -44,6 +47,8 @@ namespace DirectCommunication
bool hasClient(std::uint32_t clientId); bool hasClient(std::uint32_t clientId);
std::function<void(std::string)> MessageHandle_ = nullptr;
}; };

View File

@@ -36,6 +36,11 @@ namespace DirectCommunication
socket_.close(); socket_.close();
} }
void DirectCommunicationClient::registerMessageCallback(std::function<void(std::string)> MessageHandle)
{
MessageHandle_ = MessageHandle;
}
void DirectCommunicationClient::sendMessage(std::string msg) void DirectCommunicationClient::sendMessage(std::string msg)
{ {
@@ -76,9 +81,16 @@ namespace DirectCommunication
socket_.disconnect("tcp://"+serverAddress_+":"+std::to_string(port_)); socket_.disconnect("tcp://"+serverAddress_+":"+std::to_string(port_));
}else }else
{
if (MessageHandle_ != nullptr)
{
MessageHandle_(msg.to_string());
}
else
{ {
receivedMessages_.addElement(msg.to_string()); receivedMessages_.addElement(msg.to_string());
} }
}
} }

View File

@@ -35,6 +35,12 @@ namespace DirectCommunication
socket_.close(); socket_.close();
} }
void DirectCommunicationServer::registerMessageCallback(std::function<void(std::string)> MessageHandle)
{
MessageHandle_ = MessageHandle;
}
void DirectCommunicationServer::sendMessage(std::string msg) void DirectCommunicationServer::sendMessage(std::string msg)
{ {
if (connectedClients_.size() > 0) if (connectedClients_.size() > 0)
@@ -84,11 +90,18 @@ namespace DirectCommunication
auto it = std::find(connectedClients_.begin(), connectedClients_.end(), msg.routing_id()); auto it = std::find(connectedClients_.begin(), connectedClients_.end(), msg.routing_id());
connectedClients_.erase(it); connectedClients_.erase(it);
}else }else
{
if (MessageHandle_ != nullptr)
{
MessageHandle_(msg.to_string());
}
else
{ {
receivedMessages_.addElement(msg.to_string()); receivedMessages_.addElement(msg.to_string());
} }
} }
} }
}
} }