FIX: fixed some issues with subscribung and unsubscribing from topics
This commit is contained in:
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
33
src/main.cpp
33
src/main.cpp
@@ -1,7 +1,6 @@
|
||||
#include "WHISPER/InternalUDPService.hpp"
|
||||
#include "WHISPER/Messages/Message.hpp"
|
||||
#include "WHISPER/Messages/Track.hpp"
|
||||
#include "zmq.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <loguru.hpp>
|
||||
|
||||
@@ -48,34 +47,34 @@ int main()
|
||||
service.connect(receiver);
|
||||
|
||||
service.subscribe(WHISPER::MsgTopicsMap[WHISPER::MsgTopics::DATA]);
|
||||
service.subscribe(WHISPER::MsgTopicsMap[WHISPER::MsgTopics::TRACK]);
|
||||
service.subscribe(WHISPER::MsgTopicsMap[WHISPER::MsgTopics::TRACK]);
|
||||
|
||||
|
||||
// service.publish(join.serialize(), WHISPER::MsgTopicsMap[WHISPER::MsgTopics::MANAGEMENT]);
|
||||
|
||||
|
||||
|
||||
WHISPER::RawTrack RawTrack(ID,WHISPER::MsgTopics::TRACK,WHISPER::SourceType::SHIP,0001);
|
||||
// WHISPER::RawTrack RawTrack(ID,WHISPER::MsgTopics::TRACK,WHISPER::SourceType::SHIP,0001);
|
||||
|
||||
int size = 0;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
|
||||
service.publish(RawTrack.serialize(),WHISPER::MsgTopicsMap[WHISPER::MsgTopics::TRACK]);
|
||||
// service.publish(RawTrack.serialize(),WHISPER::MsgTopicsMap[WHISPER::MsgTopics::TRACK]);
|
||||
|
||||
while (running) {
|
||||
// zmq::message_t msg(string.begin(),string.end());
|
||||
// msg.set_group("data");
|
||||
// sock.send(msg,zmq::send_flags::none);
|
||||
// while (running) {
|
||||
// // zmq::message_t msg(string.begin(),string.end());
|
||||
// // msg.set_group("data");
|
||||
// // sock.send(msg,zmq::send_flags::none);
|
||||
|
||||
if (size != receiver->size()) {
|
||||
LOG_S(INFO)<<"received messages " << size;
|
||||
size = receiver->size();
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
}
|
||||
// // if (size != receiver->size()) {
|
||||
// // LOG_S(INFO)<<"received messages " << size;
|
||||
// // size = receiver->size();
|
||||
// // }
|
||||
|
||||
// std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
// }
|
||||
|
||||
|
||||
service.disconnect();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user