ADD: added a sanitising function for the tracklist and tests
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -6,13 +6,18 @@
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include <SimCore/Identifier.hpp>
|
||||
#include <SimCore/Messages/SimTrack.hpp>
|
||||
#include <cstddef>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, std::shared_ptr<SimCore::SimTrack>> TrackStore_;
|
||||
|
||||
std::map<std::string, std::time_t> markedAsDeletedTracks_;
|
||||
|
||||
void checkTracksTimeout();
|
||||
|
||||
const int TrackTimeoutTime = 6;
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
#include "SimCore/SimCore.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include <SimControl/Tracklist.hpp>
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
|
||||
|
||||
@@ -8,76 +11,119 @@
|
||||
namespace SimControl
|
||||
{
|
||||
|
||||
TrackList::TrackList()
|
||||
{
|
||||
TrackList::TrackList()
|
||||
{
|
||||
|
||||
TrackStore_ = std::map<std::string, std::shared_ptr<SimCore::SimTrack>>();
|
||||
TrackStore_ = std::map<std::string, std::shared_ptr<SimCore::SimTrack>>();
|
||||
}
|
||||
|
||||
void TrackList::addTrack(std::shared_ptr<SimCore::SimTrack> Track)
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<SimCore::SimTrack> Track)
|
||||
for ( std::map<std::string,std::time_t>::iterator it=markedAsDeletedTracks_.begin(); it != markedAsDeletedTracks_.end();)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mx_);
|
||||
|
||||
auto [iterator, inserted] = TrackStore_.try_emplace(Track->getIdentifier().getUUID(),Track);
|
||||
if (!inserted) { iterator->second = Track; }
|
||||
|
||||
|
||||
}
|
||||
std::shared_ptr<SimCore::SimTrack> TrackList::getTrack(const SimCore::Identifier ID)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mx_);
|
||||
|
||||
auto it = TrackStore_.find(ID.getUUID());
|
||||
return it->second;
|
||||
}
|
||||
|
||||
std::shared_ptr<SimCore::SimTrack> TrackList::getTrackBySringID(std::string ID)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mx_);
|
||||
|
||||
auto it = TrackStore_.find(ID);
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void TrackList::deleteTrack(std::string ID)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mx_);
|
||||
auto it = TrackStore_.find(ID);
|
||||
TrackStore_.erase(it);
|
||||
}
|
||||
|
||||
void TrackList::deleteTrack(SimCore::Identifier ID)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mx_);
|
||||
auto it = TrackStore_.find(ID.getUUID());
|
||||
TrackStore_.erase(it);
|
||||
}
|
||||
|
||||
|
||||
void TrackList::getJsonTRackList(nlohmann::json &message)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mx_);
|
||||
try {
|
||||
for (std::map<std::string, std::shared_ptr<SimCore::SimTrack>>::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<SimCore::SimTrack> TrackList::getTrack(const SimCore::Identifier ID)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mx_);
|
||||
|
||||
auto it = TrackStore_.find(ID.getUUID());
|
||||
return it->second;
|
||||
}
|
||||
|
||||
std::shared_ptr<SimCore::SimTrack> TrackList::getTrackBySringID(std::string ID)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mx_);
|
||||
|
||||
auto it = TrackStore_.find(ID);
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void TrackList::deleteTrack(std::string ID)
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> lock(mx_);
|
||||
try {
|
||||
for (std::map<std::string, std::shared_ptr<SimCore::SimTrack>>::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<std::mutex> lock(mx_);
|
||||
|
||||
return this->TrackStore_.size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
110
tests/test_tracklist.cpp
Normal file
110
tests/test_tracklist.cpp
Normal file
@@ -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 <fstream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include "SimControl/PodList.hpp"
|
||||
#include <catch2/catch.hpp>
|
||||
#include <loguru.hpp>
|
||||
|
||||
#include "string.cpp"
|
||||
|
||||
|
||||
|
||||
void createScenario(SimControl::TrackList *list,std::vector<std::string> *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<SimCore::SimTrack>(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<std::string> 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
|
||||
Reference in New Issue
Block a user