FIX: fixed some issues with subscribung and unsubscribing from topics

This commit is contained in:
Henry Winkel
2023-01-18 11:21:57 +01:00
parent f7d57ccbb8
commit 49bdb28535
7 changed files with 83 additions and 40 deletions

View File

@@ -5,6 +5,7 @@
#include "WHISPER/Messages/Leave.hpp"
#include "WHISPER/Messages/Message.hpp"
#include "WHISPER/whisper.hpp"
#include <vector>
#include <zmq.hpp>
#include <chrono>
#include <cstdint>
@@ -32,6 +33,14 @@ namespace WHISPER {
LOG_S(INFO)<< "sockets are created";
}
InternalUDPService::~InternalUDPService()
{
if (this->isConnected()== true) {
disconnect();
}
}
void InternalUDPService::derivedConnect()
{
ownReceivingPort_ = checkPort(port_);
@@ -66,6 +75,7 @@ namespace WHISPER {
// sender.set(zmq::sockopt::multicast_loop ,1);
LOG_S(INFO)<<"own ID: "<< getOwnID();
WHISPER::Join join(getOwnID(),getOwnDeviceType(),ownReceivingPort_,myAdress_);
this->publish(join.serialize(),WHISPER::MsgTopicsMap[WHISPER::MsgTopics::MANAGEMENT]);
@@ -77,10 +87,21 @@ namespace WHISPER {
{
WHISPER::Leave Leave(getOwnID(),getOwnDeviceType(),ownReceivingPort_,myAdress_);
this->publish(Leave.serialize(),WHISPER::MsgTopicsMap[(WHISPER::MsgTopics)Leave.topic_]);
unsubscribe(WHISPER::MsgTopicsMap[WHISPER::MsgTopics::MANAGEMENT]);
if (subscribedTopics.size() > 0) {
for ( std::vector<std::string>::iterator it = subscribedTopics.begin(); it != subscribedTopics.end(); it++) {
unsubscribe(*it);
}
}
sender.close();
loopbackSocket = nullptr;
if (loopbackSocket.unique()) loopbackSocket.reset();
receiver->close();
if (receiver.unique()) receiver.reset();
}

View File

@@ -11,6 +11,7 @@
#include <WHISPER/whisper.hpp>
#include <cstddef>
#include <cstring>
#include <iterator>
/**
* @brief namespace for all whisper-com related components
@@ -30,7 +31,7 @@ namespace WHISPER
this->derivedConnect();
receiveThread = std::thread(&WHISPER::whispercomm::receive,this);
this->setConnected(true);
}
void whispercomm::disconnect()
@@ -38,7 +39,7 @@ namespace WHISPER
stopReceiveThread = true;
if(receiveThread.joinable()) receiveThread.join();
derivedDisconnect();
this->setConnected(false);
}
void whispercomm::publish(std::string msg,std::string topic){
@@ -70,7 +71,8 @@ namespace WHISPER
}
void whispercomm::unsubscribe(std::string topic)
{
{
for (std::vector<std::string>::iterator it = subscribedTopics.begin(); it != subscribedTopics.end();it++)
{
if (*it == topic)
@@ -105,5 +107,13 @@ namespace WHISPER
}
void whispercomm::setConnected(bool val){
this->Connected = val;
}
bool whispercomm::isConnected()
{
return this->Connected;
}
// Add datatypes here
} // namespace WHISPER