ADD: added test for direct zmq tcp and a first version of the server and client class

This commit is contained in:
Henry Winkel
2023-07-05 17:05:03 +02:00
parent f03c00a1a8
commit 828d038d5f
6 changed files with 289 additions and 11 deletions

View File

@@ -1 +1,90 @@
#include <DirectCommunicationClient.hpp>
#include <DirectCommunicationClient.hpp>
#include "zmq.hpp"
#include <memory>
#include <string>
#include <sys/types.h>
#include <thread>
#include <loguru.hpp>
namespace DirectCommunication
{
DirectCommunicationClient::DirectCommunicationClient(ushort port, std::string ServerAddress):port_(port),serverAddress_(ServerAddress)
{
context_ = zmq::context_t(1);
socket_ = zmq::socket_t(context_,zmq::socket_type::client);
socket_.connect("tcp://"+serverAddress_+":"+std::to_string(port_));
socket_.set(zmq::sockopt::rcvtimeo,100);
Worker_ = std::thread(&DirectCommunicationClient::workerFunc_,this);
stopWorker_ = false;
}
DirectCommunicationClient::~DirectCommunicationClient()
{
sendMessage("CLOSE");
stopWorker_ = true;
Worker_.join();
socket_.close();
}
void DirectCommunicationClient::sendMessage(std::string msg)
{
zmq::message_t zmqMsg(msg.begin(),msg.end());
socket_.send(zmqMsg,zmq::send_flags::dontwait);
}
std::unique_ptr<std::string> DirectCommunicationClient::getLatestMessage()
{
if (receivedMessages_.size() > 0)
{
std::string msg;
receivedMessages_.get(msg);
return std::make_unique<std::string>(std::move(msg));
}
return nullptr;
}
void DirectCommunicationClient::workerFunc_()
{
while (stopWorker_ == false)
{
zmq::message_t msg;
auto recv = socket_.recv(msg,zmq::recv_flags::none);
if (recv > 0)
{
if (msg.to_string() == "CLOSE")
{
socket_.disconnect("tcp://"+serverAddress_+":"+std::to_string(port_));
}else
{
receivedMessages_.addElement(msg.to_string());
}
}
}
}
}

View File

@@ -1,6 +1,10 @@
#include "zmq.hpp"
#include <DirectCommunicationServer.hpp>
#include <memory>
#include <string>
#include <sys/types.h>
#include <thread>
#include <loguru.hpp>
namespace DirectCommunication
@@ -9,10 +13,13 @@ namespace DirectCommunication
{
context_ = zmq::context_t(1);
socket_ = zmq::socket_t(context_,zmq::socket_type::server);
socket_.bind("tcp://*:"+std::to_string(port_));
socket_.set(zmq::sockopt::rcvtimeo,100);
Worker_ = std::thread(&DirectCommunicationServer::workerFunc_,this);
stopWorker_ = false;
}
@@ -20,29 +27,82 @@ namespace DirectCommunication
DirectCommunicationServer::~DirectCommunicationServer()
{
sendMessage("CLOSE");
stopWorker_ = true;
Worker_.join();
socket_.close();
}
void DirectCommunicationServer::sendMessage(std::string msg)
{
if (connectedClients_.size() > 0)
{
for (auto it = connectedClients_.begin(); it != connectedClients_.end(); ++it) {
zmq::message_t zmqMsg(msg.begin(),msg.end());
zmqMsg.set_routing_id(*it);
socket_.send(zmqMsg,zmq::send_flags::dontwait);
}
}
}
std::unique_ptr<std::string> DirectCommunicationServer::getLatestMessage()
{
if (receivedMessages_.size() > 0)
{
std::string msg;
receivedMessages_.get(msg);
return std::make_unique<std::string>(std::move(msg));
}
return nullptr;
}
void DirectCommunicationServer::worker_()
int DirectCommunicationServer::countClients()
{
return connectedClients_.size();
}
void DirectCommunicationServer::workerFunc_()
{
LOG_S(INFO)<<"worker started";
while (stopWorker_ == false)
{
zmq::message_t msg;
auto recv = socket_.recv(msg,zmq::recv_flags::none);
if (recv > 0)
{
if (!hasClient(msg.routing_id()))
{
connectedClients_.push_back(msg.routing_id());
LOG_S(INFO)<< "new Client connected ID: " <<msg.routing_id();
}
if (msg.to_string() == "CLOSE")
{
auto it = std::find(connectedClients_.begin(), connectedClients_.end(), msg.routing_id());
connectedClients_.erase(it);
}else
{
receivedMessages_.addElement(msg.to_string());
}
}
}
}
bool DirectCommunicationServer::hasClient(std::uint32_t)
bool DirectCommunicationServer::hasClient(std::uint32_t clientId)
{
if ( std::find(connectedClients_.begin(), connectedClients_.end(), clientId) != connectedClients_.end() )
{
return true;
}
else
{
return false;
}
}