From e947d29669b699c8bf3635067e915779539266d9 Mon Sep 17 00:00:00 2001 From: hwinkel Date: Tue, 5 Mar 2024 13:33:06 +0100 Subject: [PATCH] ADD:added tracklist response --- Dockerfile | 2 +- include/SimControl/SimControl.hpp | 3 + libs/ixwebsocket | 2 +- src/SimControl/SimControl.cpp | 40 ++++++-- tests/test_Simcontrol.cpp | 151 +++++++++++++++++++++++++++--- 5 files changed, 174 insertions(+), 24 deletions(-) diff --git a/Dockerfile b/Dockerfile index 49782f4..bdb9c66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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/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/EntityLibrary/libs/SimCore/libs/crossguid/libcrossguid.so.0 /usr/lib/ # COPY build/libs/SimCore/libs/crossguid/libcrossguid.so.0 /usr/lib/ diff --git a/include/SimControl/SimControl.hpp b/include/SimControl/SimControl.hpp index ffe5961..c19ecfb 100644 --- a/include/SimControl/SimControl.hpp +++ b/include/SimControl/SimControl.hpp @@ -38,6 +38,9 @@ class SimControl{ ~SimControl(); void stop(); + + std::string sendRecvMessageToClient( std::string ip, std::string port,WHISPER::Message msg, bool waitForResponse ); + private: bool online_; diff --git a/libs/ixwebsocket b/libs/ixwebsocket index f29906c..c5a02f1 160000 --- a/libs/ixwebsocket +++ b/libs/ixwebsocket @@ -1 +1 @@ -Subproject commit f29906c72f1739e0c373495cde068923a88c2cc2 +Subproject commit c5a02f1066fb0fde48f80f51178429a27f689a39 diff --git a/src/SimControl/SimControl.cpp b/src/SimControl/SimControl.cpp index 43b5ecf..781e17d 100644 --- a/src/SimControl/SimControl.cpp +++ b/src/SimControl/SimControl.cpp @@ -54,8 +54,10 @@ namespace SimControl { PodController_ = std::make_unique("docs/config"); } - ExternalTCPServer_ = std::make_shared(5000,ID_.getUUID()); - ExternalTCPServer_->registerMessageCallback(std::bind(&SimControl::HandleTCPMessage,this,std::placeholders::_1)); + // ExternalTCPServer_ = std::make_shared(5000,ID_.getUUID()); + // ExternalTCPServer_->registerMessageCallback(std::bind(&SimControl::HandleTCPMessage,this,std::placeholders::_1)); + + // TCPClient_ = std::make_unique(30200,"192.168.252.6"); // TCPClient_ = std::make_unique(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) { @@ -466,20 +488,25 @@ namespace SimControl { // SimCore::Control control(this->ID_,SimCore::ControlType::GET_TRACKLIST,"Tracklist"); client.sendMessage(TrackListRequest.buildMessage()); response = client.getLatestMessage(); - client.disconnect(); - auto update = SimCore::TracklistUpdate::unpack(WHISPER::Message(response)); if (response != "NULL") { + auto update = SimCore::TracklistUpdate::unpack(WHISPER::Message(response)); + if(update == nullptr) + { + return; + } try { - nlohmann::json Jsontracks = nlohmann::json::parse(response); + + nlohmann::json Jsontracks; auto tracks = update->getTracks(); + LOG_S(INFO)<<"Tracklist Update Size: " < #include -#include "string.cpp" +#include + + #include "easywsclient.hpp" #include +#include +#include - +// std::shared_ptr sc; +struct HelpMessage{ + std::string IP; + std::string Port; + WHISPER::Message msg; +} ; +WHISPER::threadSafeQueue Wmsgqueue_in_; int runSimConntrol(bool *running) { SimControl::SimControl sc(false,"CommandPortChar","5555",SimCore::UtilFunctions::StringToUShort("10000"),"simulator"); + // sc = std::make_shared(false,"CommandPortChar","5555",SimCore::UtilFunctions::StringToUShort("10000"),"simulator"); + while (*running) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 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 "; sc.stop(); return 0; } +WHISPER::threadSafeQueue msgqueue_in_; +WHISPER::threadSafeQueue msgqueue_out_; -std::queue msgqueue_in_; -std::queue msgqueue_out_; +// std::queue msgqueue_in_; +// std::queue msgqueue_out_; void handle_message(std::string m) { LOG_S(INFO)<dispatch(handle_message); while(msgqueue_out_.size() > 0) { - ws->send(msgqueue_out_.front()); - msgqueue_out_.pop(); + ws->send(msgqueue_out_.get()); } } 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(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)<(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") { - + // std::string response = sc->sendRecvMessageToClient("127.0.0.1","5555",TrackListRequest.buildMessage(),false); + LOG_S(INFO)<<"sended"; + // LOG_S(INFO)<getTracks().size(); + REQUIRE(update->getTracks().size() == amount ); + + + server.sendMessage(update->buildMessage()); + + + } + LOG_S(INFO)<<"stop"; *running = false; std::this_thread::sleep_for(std::chrono::milliseconds(200)); - // REQUIRE(t1.joinable()); - auto j = nlohmann::json::parse(msgqueue_in_.front()); - msgqueue_in_.pop(); + + auto j = nlohmann::json::parse(msgqueue_in_.get()); + REQUIRE(j["Message"] == "Hello world" ); t1.join(); t2.join(); - + } //THEN + // client->disconnect(); } // WHEN } // GIVEN } //SCENARIO \ No newline at end of file