diff --git a/CMakeLists.txt b/CMakeLists.txt index c2ef752..f59c582 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,9 +98,9 @@ IF (${TEST_SIMCONTROLLER_LIBRARY}) -add_executable(test_podlist tests/test_podlist.cpp) -target_link_libraries(test_podlist Catch2::Catch2 SimControl loguru) -catch_discover_tests(test_podlist) +add_executable(test_tracklist tests/test_tracklist.cpp) +target_link_libraries(test_tracklist Catch2::Catch2 SimControl loguru) +catch_discover_tests(test_tracklist) diff --git a/include/SimControl/Tracklist.hpp b/include/SimControl/Tracklist.hpp index 9a7ca0b..8851727 100644 --- a/include/SimControl/Tracklist.hpp +++ b/include/SimControl/Tracklist.hpp @@ -6,13 +6,18 @@ #include "nlohmann/json_fwd.hpp" #include #include +#include #include #include #include #include +#include namespace SimControl { + + + class TrackList { public: @@ -26,12 +31,19 @@ namespace SimControl void getJsonTRackList(nlohmann::json &message); + size_t getTracklistSize(); private: mutable std::mutex mx_; std::map> TrackStore_; + std::map markedAsDeletedTracks_; + + void checkTracksTimeout(); + + const int TrackTimeoutTime = 6; + }; } \ No newline at end of file diff --git a/src/SimControl/Tracklist.cpp b/src/SimControl/Tracklist.cpp index 3a25b14..52bf971 100644 --- a/src/SimControl/Tracklist.cpp +++ b/src/SimControl/Tracklist.cpp @@ -1,6 +1,9 @@ #include "SimCore/SimCore.hpp" #include "nlohmann/json_fwd.hpp" #include +#include +#include +#include @@ -8,75 +11,118 @@ namespace SimControl { - TrackList::TrackList() - { + TrackList::TrackList() + { - TrackStore_ = std::map>(); + TrackStore_ = std::map>(); + } + + void TrackList::addTrack(std::shared_ptr Track) + { + std::lock_guard lock(mx_); + checkTracksTimeout(); + + auto it = markedAsDeletedTracks_.find(Track->getIdentifier().getUUID()); + if (it == markedAsDeletedTracks_.end() ) + { + auto [iterator, inserted] = TrackStore_.try_emplace(Track->getIdentifier().getUUID(),Track); + if (!inserted) { iterator->second = Track; } + + } + + + + + } + + + + void TrackList::checkTracksTimeout() + { + + if (markedAsDeletedTracks_.size() == 0) + { + return; } - void TrackList::addTrack(std::shared_ptr Track) + for ( std::map::iterator it=markedAsDeletedTracks_.begin(); it != markedAsDeletedTracks_.end();) { - std::lock_guard lock(mx_); - - auto [iterator, inserted] = TrackStore_.try_emplace(Track->getIdentifier().getUUID(),Track); - if (!inserted) { iterator->second = Track; } - - - } - std::shared_ptr TrackList::getTrack(const SimCore::Identifier ID) - { - std::lock_guard lock(mx_); - - auto it = TrackStore_.find(ID.getUUID()); - return it->second; - } - - std::shared_ptr TrackList::getTrackBySringID(std::string ID) - { - std::lock_guard lock(mx_); - - auto it = TrackStore_.find(ID); - return it->second; - } - - void TrackList::deleteTrack(std::string ID) - { - std::lock_guard lock(mx_); - auto it = TrackStore_.find(ID); - TrackStore_.erase(it); - } - - void TrackList::deleteTrack(SimCore::Identifier ID) - { - std::lock_guard lock(mx_); - auto it = TrackStore_.find(ID.getUUID()); - TrackStore_.erase(it); - } - - - void TrackList::getJsonTRackList(nlohmann::json &message) - { - std::lock_guard lock(mx_); - try { - for (std::map>::iterator it=TrackStore_.begin(); it!=TrackStore_.end(); ++it) - { - nlohmann::json j; - j["id"] = it->first; - j["Name"] = it->second->Name.getValue(); - j["Course"] = it->second->Course.getValue(); - j["Speed"] = it->second->Speed.getValue(); - j["External"] = false; - j["Position"] = {it->second->getPosition().getGeodesicPos()[SimCore::LATITUDE], it->second->getPosition().getGeodesicPos()[SimCore::LONGITUDE]}; - j["Height"] = it->second->getPosition().getGeodesicPos()[SimCore::HEIGHT]; - j["Type"] = SimCore::Kind::EntityKindMap[it->second->EntityKind.getValue()]; - j["Side"] = SimCore::Side::EntitySideMap[it->second->EntitySide.getValue()]; - - message["Entities"].push_back(j); - } - }catch (const std::exception e) { - LOG_S(ERROR)<< e.what(); + if (it->second + TrackTimeoutTime < time(0)) + { + it = markedAsDeletedTracks_.erase(it); + }else { + it++; } } + + } + + + std::shared_ptr TrackList::getTrack(const SimCore::Identifier ID) + { + std::lock_guard lock(mx_); + + auto it = TrackStore_.find(ID.getUUID()); + return it->second; + } + + std::shared_ptr TrackList::getTrackBySringID(std::string ID) + { + std::lock_guard lock(mx_); + + auto it = TrackStore_.find(ID); + return it->second; + } + + void TrackList::deleteTrack(std::string ID) + { + std::lock_guard lock(mx_); + markedAsDeletedTracks_.emplace(std::make_pair(ID, std::time(0))); + auto it = TrackStore_.find(ID); + TrackStore_.erase(it); + } + + void TrackList::deleteTrack(SimCore::Identifier ID) + { + std::lock_guard lock(mx_); + markedAsDeletedTracks_.emplace(std::make_pair(ID.getUUID(), std::time(0))); + auto it = TrackStore_.find(ID.getUUID()); + TrackStore_.erase(it); + } + + + void TrackList::getJsonTRackList(nlohmann::json &message) + { + std::lock_guard lock(mx_); + try { + for (std::map>::iterator it=TrackStore_.begin(); it!=TrackStore_.end(); ++it) + { + nlohmann::json j; + j["id"] = it->first; + j["Name"] = it->second->Name.getValue(); + j["Course"] = it->second->Course.getValue(); + j["Speed"] = it->second->Speed.getValue(); + j["External"] = false; + j["Position"] = {it->second->getPosition().getGeodesicPos()[SimCore::LATITUDE], it->second->getPosition().getGeodesicPos()[SimCore::LONGITUDE]}; + j["Height"] = it->second->getPosition().getGeodesicPos()[SimCore::HEIGHT]; + j["Type"] = SimCore::Kind::EntityKindMap[it->second->EntityKind.getValue()]; + j["Side"] = SimCore::Side::EntitySideMap[it->second->EntitySide.getValue()]; + + message["Entities"].push_back(j); + } + }catch (const std::exception e) { + LOG_S(ERROR)<< e.what(); + } + } + + + size_t TrackList::getTracklistSize() + { + std::lock_guard lock(mx_); + + return this->TrackStore_.size(); + } + diff --git a/tests/test_tracklist.cpp b/tests/test_tracklist.cpp new file mode 100644 index 0000000..a64d629 --- /dev/null +++ b/tests/test_tracklist.cpp @@ -0,0 +1,110 @@ + + +#include "SimControl/Tracklist.hpp" +#include "SimCore/Identifier.hpp" +#include "SimCore/Messages/SimTrack.hpp" +#include "SimCore/Position.hpp" +#include "SimCore/SimCore.hpp" +#include +#include +#include +#include +#include +#define CATCH_CONFIG_MAIN +#include "SimControl/PodList.hpp" +#include +#include + +#include "string.cpp" + + + +void createScenario(SimControl::TrackList *list,std::vector *ids) +{ + GeographicLib::Geodesic geod(GeographicLib::Constants::WGS84_a(), GeographicLib::Constants::WGS84_f()); + double lat = 54, lon = 1; + int counter = 0; + double distance = 10000; + + int rasterSize = 5; + + for (int i = 0; i < rasterSize; i++ ) + { + double lonTmp = lon; + for (int a = 0; a < rasterSize; a++) + { + + std::string name = "test"; + name += std::to_string(counter); + double lat2, lon2; + geod.Direct(lat, lonTmp, 90, distance, lat2, lon2); + + SimCore::Identifier id; + ids->push_back(id.getUUID()); + + SimCore::Position pos; + pos.setGeodesicPos(lat2, lon2, 0); + auto track = std::make_shared(id,name,SimCore::Kind::EntityKind::SURFACE,SimCore::Side::NEUTRAL); + track->setPosition(pos); + list->addTrack(track); + // SimControl::startNewShip(name, std::to_string(lat2), std::to_string(lon2), "0", "0", "0"); + lonTmp = lon2; + counter ++; + } + double lat2, lon2; + + geod.Direct(lat, lon, 0, distance, lat2, lon2); + + lat = lat2; + + } + + + +} + + +SCENARIO("Testing the the podlist") +{ + +std::vector ids; +SimControl::TrackList list; + +createScenario(&list,&ids); + +auto track = list.getTrackBySringID(ids[3]); + +// list.deleteTrack(track->getIdentifier()); + +// list.addTrack(track); +// std::this_thread::sleep_for(std::chrono::milliseconds(300)); + + + GIVEN("different Attributes for a Track in different forms") + { + + WHEN("constructing Track Object with data") + { + + THEN("check if Track attributes are correct") + { + REQUIRE(ids.size() == 25); + REQUIRE(list.getTracklistSize() == 25); + + list.deleteTrack(track->getIdentifier()); + + list.addTrack(track); + REQUIRE(list.getTracklistSize() == 24); + std::this_thread::sleep_for(std::chrono::milliseconds(7500)); + LOG_S(INFO)<<"add saved track"; + list.addTrack(track); + LOG_S(INFO)<<"added saved track"; + + REQUIRE(list.getTracklistSize() == 25); + + + + } //THEN + } // WHEN + } // GIVEN +} //SCENARIO \ No newline at end of file