ADD:added tracklist response

This commit is contained in:
hwinkel
2024-03-05 13:33:06 +01:00
parent 3f7b7ebcae
commit e947d29669
5 changed files with 174 additions and 24 deletions

View File

@@ -46,7 +46,7 @@ COPY build/libs/OrderLibrary/libs/SimCore/libs/whisper-com/libs/protobuf/third_p
COPY build/libs/OrderLibrary/libs/SimCore/libs/geographiclib/src/libGeographicLib.so.23 /usr/lib/ COPY build/libs/OrderLibrary/libs/SimCore/libs/geographiclib/src/libGeographicLib.so.23 /usr/lib/
# COPY build/libs/KubeControl/libs/yaml-cpp/libyaml-cpp.so.0.8 /usr/lib/ COPY build/libs/KubeControl/libs/yaml-cpp/libyaml-cpp.so.0.8 /usr/lib/
COPY build/libs/KubeControl/libs/curlpp/libcurlpp.so.1 /usr/lib/ COPY build/libs/KubeControl/libs/curlpp/libcurlpp.so.1 /usr/lib/
# COPY build/libs/EntityLibrary/libs/SimCore/libs/crossguid/libcrossguid.so.0 /usr/lib/ # COPY build/libs/EntityLibrary/libs/SimCore/libs/crossguid/libcrossguid.so.0 /usr/lib/
# COPY build/libs/SimCore/libs/crossguid/libcrossguid.so.0 /usr/lib/ # COPY build/libs/SimCore/libs/crossguid/libcrossguid.so.0 /usr/lib/

View File

@@ -38,6 +38,9 @@ class SimControl{
~SimControl(); ~SimControl();
void stop(); void stop();
std::string sendRecvMessageToClient( std::string ip, std::string port,WHISPER::Message msg, bool waitForResponse );
private: private:
bool online_; bool online_;

View File

@@ -54,8 +54,10 @@ namespace SimControl {
PodController_ = std::make_unique<kubecontrol::PodController>("docs/config"); PodController_ = std::make_unique<kubecontrol::PodController>("docs/config");
} }
ExternalTCPServer_ = std::make_shared<DirectCommunication::DirectCommunicationServer>(5000,ID_.getUUID()); // ExternalTCPServer_ = std::make_shared<DirectCommunication::DirectCommunicationServer>(5000,ID_.getUUID());
ExternalTCPServer_->registerMessageCallback(std::bind(&SimControl::HandleTCPMessage,this,std::placeholders::_1)); // ExternalTCPServer_->registerMessageCallback(std::bind(&SimControl::HandleTCPMessage,this,std::placeholders::_1));
// TCPClient_ = std::make_unique<DirectCommunication::DirectCommunicationClient>(30200,"192.168.252.6"); // TCPClient_ = std::make_unique<DirectCommunication::DirectCommunicationClient>(30200,"192.168.252.6");
// TCPClient_ = std::make_unique<DirectCommunication::DirectCommunicationClient>(5555,"127.0.0.1"); // TCPClient_ = std::make_unique<DirectCommunication::DirectCommunicationClient>(5555,"127.0.0.1");
@@ -372,6 +374,26 @@ namespace SimControl {
} }
std::string SimControl::sendRecvMessageToClient(std::string ip, std::string port,WHISPER::Message msg, bool waitForResponse)
{
DirectCommunication::DirectCommunicationClient client(std::stoi(port),ip,this->ID_.getUUID());
LOG_S(INFO)<<"send message";
client.sendMessage("hello");
client.sendMessage(msg);
if(waitForResponse == true)
{
std::string message = client.getLatestMessage();
LOG_S(INFO)<<"message received " << message;
return message;
}
client.disconnect();
return "";
}
void SimControl::updateShip(nlohmann::json request) void SimControl::updateShip(nlohmann::json request)
{ {
@@ -466,20 +488,25 @@ namespace SimControl {
// SimCore::Control control(this->ID_,SimCore::ControlType::GET_TRACKLIST,"Tracklist"); // SimCore::Control control(this->ID_,SimCore::ControlType::GET_TRACKLIST,"Tracklist");
client.sendMessage(TrackListRequest.buildMessage()); client.sendMessage(TrackListRequest.buildMessage());
response = client.getLatestMessage(); response = client.getLatestMessage();
client.disconnect(); client.disconnect();
auto update = SimCore::TracklistUpdate::unpack(WHISPER::Message(response));
if (response != "NULL") if (response != "NULL")
{ {
auto update = SimCore::TracklistUpdate::unpack(WHISPER::Message(response));
if(update == nullptr)
{
return;
}
try try
{ {
nlohmann::json Jsontracks = nlohmann::json::parse(response);
nlohmann::json Jsontracks;
auto tracks = update->getTracks(); auto tracks = update->getTracks();
LOG_S(INFO)<<"Tracklist Update Size: " <<tracks.size();
for(auto item : tracks) for(auto item : tracks)
{ {
Jsontracks.push_back(item.getsTrackListItemAsJSON()); Jsontracks.push_back(item.getsTrackListItemAsJSON());
} }
// j["Tracks"] = ; j["Tracks"] = Jsontracks;
}catch (const std::exception e) { }catch (const std::exception e) {
LOG_S(ERROR)<< e.what(); LOG_S(ERROR)<< e.what();
@@ -582,5 +609,4 @@ namespace SimControl {
} }

View File

@@ -13,35 +13,55 @@
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
#include <loguru.hpp> #include <loguru.hpp>
#include "string.cpp" #include <Orders/TracklistRequest.hpp>
#include "easywsclient.hpp" #include "easywsclient.hpp"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <SimCore/Messages/TracklistUpdate.hpp>
#include <SimCore/Messages/TracklistItem.hpp>
// std::shared_ptr<SimControl::SimControl> sc;
struct HelpMessage{
std::string IP;
std::string Port;
WHISPER::Message msg;
} ;
WHISPER::threadSafeQueue<HelpMessage> Wmsgqueue_in_;
int runSimConntrol(bool *running) int runSimConntrol(bool *running)
{ {
SimControl::SimControl sc(false,"CommandPortChar","5555",SimCore::UtilFunctions::StringToUShort("10000"),"simulator"); SimControl::SimControl sc(false,"CommandPortChar","5555",SimCore::UtilFunctions::StringToUShort("10000"),"simulator");
// sc = std::make_shared<SimControl::SimControl>(false,"CommandPortChar","5555",SimCore::UtilFunctions::StringToUShort("10000"),"simulator");
while (*running) { while (*running) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
// LOG_S(INFO)<<*running; // LOG_S(INFO)<<*running;
if (Wmsgqueue_in_.size()> 0)
{
auto msg = Wmsgqueue_in_.get();
sc.sendRecvMessageToClient(msg.IP,msg.Port,msg.msg, true);
}
} }
LOG_S(INFO)<<"stopping "; LOG_S(INFO)<<"stopping ";
sc.stop(); sc.stop();
return 0; return 0;
} }
WHISPER::threadSafeQueue<std::string> msgqueue_in_;
WHISPER::threadSafeQueue<std::string> msgqueue_out_;
std::queue<std::string> msgqueue_in_; // std::queue<std::string> msgqueue_in_;
std::queue<std::string> msgqueue_out_; // std::queue<std::string> msgqueue_out_;
void handle_message(std::string m) void handle_message(std::string m)
{ {
LOG_S(INFO)<<m; LOG_S(INFO)<<m;
msgqueue_in_.push(m); msgqueue_in_.addElement(m);
} }
@@ -57,13 +77,76 @@ while (*running)
ws->dispatch(handle_message); ws->dispatch(handle_message);
while(msgqueue_out_.size() > 0) while(msgqueue_out_.size() > 0)
{ {
ws->send(msgqueue_out_.front()); ws->send(msgqueue_out_.get());
msgqueue_out_.pop();
} }
} }
ws->close(); ws->close();
}
double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
void addTracksToTracklistUpdate(SimCore::TracklistUpdate *update, int amount)
{
for(int i = 0; i < amount; i++)
{
std::string name = "test1-" + std::to_string(i);
auto track = std::make_shared<SimCore::SimTrack>(name,SimCore::Kind::EntityKind::SURFACE,SimCore::Side::NEUTRAL);
SimCore::Position pos;
double lat = fRand(-90, 90);
double lon = fRand(-180, 180);
pos.setGeodesicPos( lat, lon, 0);
track->setPosition(pos);
SimCore::TracklistItem item(track);
update->addTrack(item);
}
}
std::atomic_bool TracklistRequestReceived;
void serverCallback(std::string msg)
{
// LOG_S(INFO)<<msg;
WHISPER::Message Wmsg_(msg);
switch (Wmsg_.msgType_)
{
case WHISPER::MsgType::ORDER:
{
Orders::OrderType OrderType = Orders::Order::getType(Wmsg_);
switch (OrderType)
{
case Orders::TRACKLIST_REQUEST:
{
auto TrackListRequest = Orders::TracklistRequest::unpack(Wmsg_);
if (TrackListRequest == nullptr)
{
break;
}
LOG_S(INFO)<<"tracklist request received";
TracklistRequestReceived = true;
break;
}
}
break;
}
}
} }
SCENARIO("Testing the the podlist") SCENARIO("Testing the the podlist")
@@ -79,32 +162,70 @@ j["Data"] = "TEST";
bool gotMessage = false; bool gotMessage = false;
msgqueue_out_.push(j.dump()); msgqueue_out_.addElement(j.dump());
GIVEN("different Attributes for a Track in different forms") GIVEN("different Attributes for a Track in different forms")
{ {
SimCore::Identifier ID_;
SimCore::Identifier ID1_;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
DirectCommunication::DirectCommunicationServer server(5555,ID1_.getUUID());
server.registerMessageCallback(serverCallback);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
// auto client = std::make_unique<DirectCommunication::DirectCommunicationClient>(5001,"127.0.0.1",ID_.getUUID());
// client->sendMessage("test");
Orders::TracklistRequest TrackListRequest(ID_,ID1_);
HelpMessage msg1;
msg1.IP = "127.0.0.1";
msg1.Port = "5555";
msg1.msg = TrackListRequest.buildMessage();
Wmsgqueue_in_.addElement(msg1);
WHEN("constructing Track Object with data") WHEN("constructing Track Object with data")
{ {
// std::string response = sc->sendRecvMessageToClient("127.0.0.1","5555",TrackListRequest.buildMessage(),false);
LOG_S(INFO)<<"sended";
// LOG_S(INFO)<<Helper.getLatesMessage().msgType_;
THEN("check if Track attributes are correct") THEN("check if Track attributes are correct")
{ {
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::this_thread::sleep_for(std::chrono::milliseconds(500));
REQUIRE(TracklistRequestReceived == true );
if (TracklistRequestReceived == true)
{
SimCore::TracklistUpdate *update = new SimCore::TracklistUpdate(ID_);
int amount = 5;
addTracksToTracklistUpdate(update, amount);
LOG_S(INFO)<<update->getTracks().size();
REQUIRE(update->getTracks().size() == amount );
server.sendMessage(update->buildMessage());
}
LOG_S(INFO)<<"stop"; LOG_S(INFO)<<"stop";
*running = false; *running = false;
std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::this_thread::sleep_for(std::chrono::milliseconds(200));
// REQUIRE(t1.joinable());
auto j = nlohmann::json::parse(msgqueue_in_.front()); auto j = nlohmann::json::parse(msgqueue_in_.get());
msgqueue_in_.pop();
REQUIRE(j["Message"] == "Hello world" ); REQUIRE(j["Message"] == "Hello world" );
t1.join(); t1.join();
t2.join(); t2.join();
} //THEN } //THEN
// client->disconnect();
} // WHEN } // WHEN
} // GIVEN } // GIVEN
} //SCENARIO } //SCENARIO