From 3a195b519cb74dd21a2100ac198c9e8738c023dd Mon Sep 17 00:00:00 2001 From: Henry Winkel Date: Wed, 9 Aug 2023 15:09:57 +0200 Subject: [PATCH] ADD: added methode for message handling in the directCommsClient and Server --- include/DirectCommunicationClient.hpp | 6 ++++++ include/DirectCommunicationServer.hpp | 5 +++++ libs/protobuf/third_party/googletest | 2 +- src/DirectCommunicationClient.cpp | 14 +++++++++++++- src/DirectCommunicationServer.cpp | 17 +++++++++++++++-- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/include/DirectCommunicationClient.hpp b/include/DirectCommunicationClient.hpp index 838999a..2760ea4 100644 --- a/include/DirectCommunicationClient.hpp +++ b/include/DirectCommunicationClient.hpp @@ -7,6 +7,7 @@ #include "zmq.hpp" #include #include +#include #include #include #include @@ -27,6 +28,9 @@ namespace DirectCommunication void sendMessage(std::string msg); std::string getLatestMessage(); + void registerMessageCallback(std::function MessageHandle); + + private: ushort port_; std::string serverAddress_; @@ -40,6 +44,8 @@ namespace DirectCommunication std::atomic_bool isConnected = true; + std::function MessageHandle_ = nullptr; + WHISPER::threadSafeQueue receivedMessages_; diff --git a/include/DirectCommunicationServer.hpp b/include/DirectCommunicationServer.hpp index 7b9873f..32a8284 100644 --- a/include/DirectCommunicationServer.hpp +++ b/include/DirectCommunicationServer.hpp @@ -7,6 +7,7 @@ #include "zmq.hpp" #include #include +#include #include #include #include @@ -27,6 +28,8 @@ namespace DirectCommunication void sendMessage(std::string msg); std::string getLatestMessage(); + void registerMessageCallback(std::function MessageHandle); + int countClients(); private: @@ -44,6 +47,8 @@ namespace DirectCommunication bool hasClient(std::uint32_t clientId); + std::function MessageHandle_ = nullptr; + }; diff --git a/libs/protobuf/third_party/googletest b/libs/protobuf/third_party/googletest index 4c9a3bb..46db91e 160000 --- a/libs/protobuf/third_party/googletest +++ b/libs/protobuf/third_party/googletest @@ -1 +1 @@ -Subproject commit 4c9a3bb62bf3ba1f1010bf96f9c8ed767b363774 +Subproject commit 46db91ef6ffcc128b2d5f31118ae1108109e3400 diff --git a/src/DirectCommunicationClient.cpp b/src/DirectCommunicationClient.cpp index f59c015..50ddafb 100644 --- a/src/DirectCommunicationClient.cpp +++ b/src/DirectCommunicationClient.cpp @@ -36,6 +36,11 @@ namespace DirectCommunication socket_.close(); } + void DirectCommunicationClient::registerMessageCallback(std::function MessageHandle) + { + MessageHandle_ = MessageHandle; + } + void DirectCommunicationClient::sendMessage(std::string msg) { @@ -77,7 +82,14 @@ namespace DirectCommunication }else { - receivedMessages_.addElement(msg.to_string()); + if (MessageHandle_ != nullptr) + { + MessageHandle_(msg.to_string()); + } + else + { + receivedMessages_.addElement(msg.to_string()); + } } diff --git a/src/DirectCommunicationServer.cpp b/src/DirectCommunicationServer.cpp index 43aaf17..07a6990 100644 --- a/src/DirectCommunicationServer.cpp +++ b/src/DirectCommunicationServer.cpp @@ -35,6 +35,12 @@ namespace DirectCommunication socket_.close(); } + void DirectCommunicationServer::registerMessageCallback(std::function MessageHandle) + { + MessageHandle_ = MessageHandle; + } + + void DirectCommunicationServer::sendMessage(std::string msg) { if (connectedClients_.size() > 0) @@ -84,8 +90,15 @@ namespace DirectCommunication auto it = std::find(connectedClients_.begin(), connectedClients_.end(), msg.routing_id()); connectedClients_.erase(it); }else - { - receivedMessages_.addElement(msg.to_string()); + { + if (MessageHandle_ != nullptr) + { + MessageHandle_(msg.to_string()); + } + else + { + receivedMessages_.addElement(msg.to_string()); + } } } }