From 3f7b7ebcae71f0549d418d6b1894760b5f10217f Mon Sep 17 00:00:00 2001 From: Henry Winkel Date: Thu, 15 Feb 2024 19:05:23 +0100 Subject: [PATCH] ADD: added easywebsocketclient for testing and test_simcontrol --- .gitmodules | 6 ++ CMakeLists.txt | 10 ++++ libs/easywsclient | 1 + src/SimControl/SimControl.cpp | 59 +++++++++++------- tests/test_Simcontrol.cpp | 110 ++++++++++++++++++++++++++++++++++ 5 files changed, 163 insertions(+), 23 deletions(-) create mode 160000 libs/easywsclient create mode 100644 tests/test_Simcontrol.cpp diff --git a/.gitmodules b/.gitmodules index fdca28b..805ffbb 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,9 @@ [submodule "libs/ixwebsocket"] path = libs/ixwebsocket url = https://github.com/machinezone/IXWebSocket.git +[submodule "libs/WebsocketClient"] + path = libs/WebsocketClient + url = https://github.com/zaphoyd/websocketpp.git +[submodule "libs/easywsclient"] + path = libs/easywsclient + url = https://github.com/dhbaird/easywsclient.git diff --git a/CMakeLists.txt b/CMakeLists.txt index c9d48b3..797a745 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,12 @@ IF(NOT TARGET ixwebsocket) ENDIF() +IF(NOT TARGET easywsclient) +add_library(easywsclient STATIC libs/easywsclient/easywsclient.hpp libs/easywsclient/easywsclient.cpp) +target_include_directories( easywsclient PUBLIC libs/easywsclient/ ) +ENDIF() + + add_library(SimControl STATIC include/SimControl/SimControl.hpp @@ -105,6 +111,10 @@ add_executable(test_tracklist tests/test_tracklist.cpp) target_link_libraries(test_tracklist Catch2::Catch2 SimControl loguru) catch_discover_tests(test_tracklist) +add_executable(test_Simcontrol tests/test_Simcontrol.cpp) +target_link_libraries(test_Simcontrol Catch2::Catch2 SimControl easywsclient loguru) +catch_discover_tests(test_Simcontrol) + ENDIF() diff --git a/libs/easywsclient b/libs/easywsclient new file mode 160000 index 0000000..afc1d8c --- /dev/null +++ b/libs/easywsclient @@ -0,0 +1 @@ +Subproject commit afc1d8cfc584e0f1f4a77e8c0ce3e979d9fe7ce2 diff --git a/src/SimControl/SimControl.cpp b/src/SimControl/SimControl.cpp index c6042a6..43b5ecf 100644 --- a/src/SimControl/SimControl.cpp +++ b/src/SimControl/SimControl.cpp @@ -49,7 +49,11 @@ namespace SimControl { BroadcastListener_->connect(); BroadcastListener_->subscribe(WHISPER::MsgTopics::TRACK); - PodController_ = std::make_unique("docs/config"); + if(online) + { + PodController_ = std::make_unique("docs/config"); + } + 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"); @@ -108,8 +112,10 @@ namespace SimControl { void SimControl::stop() { - - PodController_->stopAllPods(); + if(PodController_ != nullptr) + { + PodController_->stopAllPods(); + } } void SimControl::HandleMessage(std::string msg) @@ -198,7 +204,6 @@ namespace SimControl { { try { nlohmann::json j = nlohmann::json::parse(msg->str); - if (j["Data"] == "COP") { if (j["Type"] == "Request") { @@ -261,6 +266,8 @@ namespace SimControl { { nlohmann::json response; response["Message"] = "Hello world"; + LOG_S(INFO)<GroundTruthAddr_+"\""); // ShipPod1.setEnvironmentVar("COMMAND_PORT", "\""+CommandPort_+"\""); - PodController_->startPod(ShipPod1); + if(PodController_ != nullptr) + { + PodController_->startPod(ShipPod1); + } } @@ -354,7 +364,10 @@ namespace SimControl { ShipPod1.setEnvironmentVar("COMMAND_PORT", "\""+CommandPort_+"\""); ShipPod1.setEnvironmentVar("CONFIG", vars.dump()); - PodController_->startPod(ShipPod1,WaitTillRunning); + if(PodController_ != nullptr) + { + PodController_->startPod(ShipPod1,WaitTillRunning); + } } @@ -392,6 +405,10 @@ namespace SimControl { } + if(PodController_ == nullptr) + { + return; + } auto info = PodController_->getInfoForPod(request["ID"].get()); @@ -407,6 +424,10 @@ namespace SimControl { { LOG_S(INFO)<<"REQUEST: "<getInfoForPod(request["ID"].get()); LOG_S(INFO)<<"PODS Info: " <getInfoForPod("hamburg"); - - // auto IP = PodController_->getPodsInfo("hamburg")->Ip; - // TCPClient_ = std::make_shared(5555,IP); - // // Orders::MoveOrder order(this->ID_); - // order.Speed.setValue(0); - // TCPClient_->sendMessage(order.buildMessage().serialize()); - - - // std::this_thread::sleep_for(std::chrono::milliseconds(10000)); - // SimCore::Control shutdown(this->ID_,SimCore::SHUT_DOWN,""); - // TCPClient_->sendMessage(shutdown.buildMessage().serialize()); - // // TCPClient_->disconnect(); - // // TCPClient_.reset(); - - + while (!stopMainThread_) diff --git a/tests/test_Simcontrol.cpp b/tests/test_Simcontrol.cpp new file mode 100644 index 0000000..5929669 --- /dev/null +++ b/tests/test_Simcontrol.cpp @@ -0,0 +1,110 @@ + + +#include "SimControl/SimControl.hpp" + +#include "nlohmann/json_fwd.hpp" +#include +#include +#include +#include +#include +#define CATCH_CONFIG_MAIN +#include "SimControl/PodList.hpp" +#include +#include + +#include "string.cpp" + +#include "easywsclient.hpp" +#include + + + +int runSimConntrol(bool *running) +{ + SimControl::SimControl sc(false,"CommandPortChar","5555",SimCore::UtilFunctions::StringToUShort("10000"),"simulator"); + while (*running) { + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // LOG_S(INFO)<<*running; + } + LOG_S(INFO)<<"stopping "; + sc.stop(); +return 0; +} + +std::queue msgqueue_in_; +std::queue msgqueue_out_; + + + +void handle_message(std::string m) +{ + LOG_S(INFO)< ws(easywsclient::WebSocket::from_url("ws://localhost:9999/")); + + +while (*running) + { + ws->poll(); + ws->dispatch(handle_message); + while(msgqueue_out_.size() > 0) + { + ws->send(msgqueue_out_.front()); + msgqueue_out_.pop(); + } + +} + ws->close(); + +} + +SCENARIO("Testing the the podlist") +{ +bool *running = new bool(true); + std::thread t1(runSimConntrol,running) ; +// auto res = std::async(std::launch::deferred,runSimConntrol,running); +std::this_thread::sleep_for(std::chrono::milliseconds(300)); +std::thread t2(wsFunction,running) ; + +nlohmann::json j; +j["Data"] = "TEST"; + +bool gotMessage = false; + +msgqueue_out_.push(j.dump()); + + + GIVEN("different Attributes for a Track in different forms") + { + + WHEN("constructing Track Object with data") + { + + THEN("check if Track attributes are correct") + { + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + 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(); + REQUIRE(j["Message"] == "Hello world" ); + + t1.join(); + t2.join(); + + + } //THEN + } // WHEN + } // GIVEN +} //SCENARIO \ No newline at end of file