ADD: added easywebsocketclient for testing and test_simcontrol

This commit is contained in:
Henry Winkel
2024-02-15 19:05:23 +01:00
parent 96b99afff9
commit 3f7b7ebcae
5 changed files with 163 additions and 23 deletions

6
.gitmodules vendored
View File

@@ -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

View File

@@ -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()

1
libs/easywsclient Submodule

Submodule libs/easywsclient added at afc1d8cfc5

View File

@@ -49,7 +49,11 @@ namespace SimControl {
BroadcastListener_->connect();
BroadcastListener_->subscribe(WHISPER::MsgTopics::TRACK);
PodController_ = std::make_unique<kubecontrol::PodController>("docs/config");
if(online)
{
PodController_ = std::make_unique<kubecontrol::PodController>("docs/config");
}
ExternalTCPServer_ = std::make_shared<DirectCommunication::DirectCommunicationServer>(5000,ID_.getUUID());
ExternalTCPServer_->registerMessageCallback(std::bind(&SimControl::HandleTCPMessage,this,std::placeholders::_1));
// TCPClient_ = std::make_unique<DirectCommunication::DirectCommunicationClient>(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)<<response;
websocket.send(response.dump());
}
@@ -316,7 +323,10 @@ namespace SimControl {
// ShipPod1.setEnvironmentVar("GROUNDTRUTH_ADDR", "\""+this->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<std::string>());
@@ -407,6 +424,10 @@ namespace SimControl {
{
LOG_S(INFO)<<"REQUEST: "<<request;
if(PodController_ == nullptr)
{
return;
}
auto info = PodController_->getInfoForPod(request["ID"].get<std::string>());
LOG_S(INFO)<<"PODS Info: " <<info.Component;
@@ -430,6 +451,10 @@ namespace SimControl {
void SimControl::getTrackListForEntity(std::string id,nlohmann::json &j)
{
if(PodController_ == nullptr)
{
return;
}
// LOG_S(INFO)<<"REQUEST: "<<request;
std::string response;
@@ -466,7 +491,10 @@ namespace SimControl {
void SimControl::startWebApp()
{
if(PodController_ == nullptr)
{
return;
}
kubecontrol::KubePod Webapp("controller","webapp","webapp","webapp:latest",Namespace_);
Webapp.setEnvironmentVar("REACT_APP_WEBAPP_WS_URL", SimCore::UtilFunctions::getOwnIP());
@@ -537,22 +565,7 @@ namespace SimControl {
}
// LOG_S(INFO)<<PodController_->getInfoForPod("hamburg");
// auto IP = PodController_->getPodsInfo("hamburg")->Ip;
// TCPClient_ = std::make_shared<DirectCommunication::DirectCommunicationClient>(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_)

110
tests/test_Simcontrol.cpp Normal file
View File

@@ -0,0 +1,110 @@
#include "SimControl/SimControl.hpp"
#include "nlohmann/json_fwd.hpp"
#include <fstream>
#include <future>
#include <memory>
#include <queue>
#include <string>
#define CATCH_CONFIG_MAIN
#include "SimControl/PodList.hpp"
#include <catch2/catch.hpp>
#include <loguru.hpp>
#include "string.cpp"
#include "easywsclient.hpp"
#include <nlohmann/json.hpp>
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<std::string> msgqueue_in_;
std::queue<std::string> msgqueue_out_;
void handle_message(std::string m)
{
LOG_S(INFO)<<m;
msgqueue_in_.push(m);
}
void wsFunction(bool *running)
{
std::unique_ptr<easywsclient::WebSocket> 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