diff --git a/CMakeLists.txt b/CMakeLists.txt index ebb69a0..88fe982 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,18 +89,18 @@ target_include_directories(whisper-com PUBLIC $ src) - # add_executable(main + add_executable(main - # src/main.cpp + src/main.cpp - # ) - # add_dependencies(main protoc) + ) + add_dependencies(main protoc) - # target_link_libraries(main - # whisper-com - # loguru - # ) + target_link_libraries(main + whisper-com + loguru + ) # add_executable(mainRcv diff --git a/include/WHISPER/InternalUDPService.hpp b/include/WHISPER/InternalUDPService.hpp index 4d2cd94..928134f 100644 --- a/include/WHISPER/InternalUDPService.hpp +++ b/include/WHISPER/InternalUDPService.hpp @@ -51,7 +51,6 @@ namespace WHISPER { std::list> localclients; - std::atomic stopReceiving = false; /** @@ -76,7 +75,7 @@ namespace WHISPER { public: InternalUDPService(std::uint32_t id, SourceType owndevicetype,std::uint16_t port, std::string destinationAdress, std::string myAdress); - + ~InternalUDPService(); diff --git a/include/WHISPER/whisper.hpp b/include/WHISPER/whisper.hpp index 7180e95..86279e1 100644 --- a/include/WHISPER/whisper.hpp +++ b/include/WHISPER/whisper.hpp @@ -39,8 +39,6 @@ namespace WHISPER std::uint32_t ownID_; /// device Type SourceType ownDeviceType_; - /// all topics this service subscribed - std::vector subscribedTopics; /// show if the service is connected or not std::atomic connected; @@ -56,7 +54,8 @@ namespace WHISPER std::shared_ptr> receiveQueue = nullptr; - + std::atomic Connected = false; + void receive(); @@ -75,13 +74,17 @@ namespace WHISPER protected: + /// all topics this service subscribed + std::vector subscribedTopics; void addMsgToReceiverQueue(WHISPER::Message); void setGateway(bool); bool isGateway(); - + void setConnected(bool val); + bool isConnected(); + virtual void derivedConnect() = 0; virtual void derivedDisconnect() = 0; diff --git a/src/WHISPER/InternalUDPService.cpp b/src/WHISPER/InternalUDPService.cpp index cb451f2..96433bc 100644 --- a/src/WHISPER/InternalUDPService.cpp +++ b/src/WHISPER/InternalUDPService.cpp @@ -5,6 +5,7 @@ #include "WHISPER/Messages/Leave.hpp" #include "WHISPER/Messages/Message.hpp" #include "WHISPER/whisper.hpp" +#include #include #include #include @@ -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::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(); + } diff --git a/src/WHISPER/whisper.cpp b/src/WHISPER/whisper.cpp index 35454f6..8d2b05f 100644 --- a/src/WHISPER/whisper.cpp +++ b/src/WHISPER/whisper.cpp @@ -11,6 +11,7 @@ #include #include +#include #include /** * @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::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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5862bf0..b52d267 100644 --- a/src/main.cpp +++ b/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 #include @@ -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; } \ No newline at end of file diff --git a/tests/test_test.cpp b/tests/test_test.cpp index d34d50a..eea1ba3 100644 --- a/tests/test_test.cpp +++ b/tests/test_test.cpp @@ -34,14 +34,25 @@ SCENARIO("A test scenario","[keywords]") { GIVEN("Preliminaries") { - int i = 40; - WHEN("doing something") - { - i = i + 2; + WHISPER::InternalUDPService service(1,WHISPER::SourceType::SIMCOMTROLER,8000,"127.0.0.255","127.0.0.1"); + auto receiver = std::make_shared>(); + service.connect(receiver) ; + // service.subscribe(WHISPER::MsgTopicsMap[WHISPER::MsgTopics::TRACK]); + int i = 0; + WHEN("doing something") + { + + LOG_S(INFO)<<"wating"; + // std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + service.disconnect(); + + LOG_S(INFO)<<"closing"; THEN("expecting something to happen") { - REQUIRE(i == 42 ); + // service.unsubscribe(WHISPER::MsgTopicsMap[WHISPER::MsgTopics::TRACK]); + REQUIRE(receiver.unique() == true ); } // THEN } // WHEN } // GIVEN