From 57c69f5e010896d34e3c8ad41310f0b45f7b57f1 Mon Sep 17 00:00:00 2001 From: Henry Winkel Date: Thu, 15 Sep 2022 11:15:11 +0200 Subject: [PATCH] ADD: added libs; added CMakeLists.txt; added framework of the app --- CMakeLists.txt | 35 ++++--- include/SimControl/SimControl.hpp | 93 +++++++++++++++++++ libs/loguru/CMakeLists.txt | 8 ++ src/SimControl/SimControl.cpp | 148 ++++++++++++++++++++++++++++++ src/main.cpp | 44 +++++++++ 5 files changed, 310 insertions(+), 18 deletions(-) create mode 100644 include/SimControl/SimControl.hpp create mode 100644 libs/loguru/CMakeLists.txt create mode 100644 src/SimControl/SimControl.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8fc6baa..b83fa4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,13 +7,13 @@ include(defaultOptions) IF (NOT TARGET CLI11) set(CLI11_TESTING OFF CACHE BOOL "disable testing") - add_subdirectory(libs/CLI11 EXCLUDE_FROM_ALL) + add_subdirectory(libs/cli11 EXCLUDE_FROM_ALL) ENDIF() -IF(NOT TARGET nlohmann_json::nlohmann_json) - set(JSON_BuildTests OFF CACHE INTERNAL "") - add_subdirectory(libs/json EXCLUDE_FROM_ALL) -ENDIF() +# IF(NOT TARGET nlohmann_json::nlohmann_json) +# set(JSON_BuildTests OFF CACHE INTERNAL "") +# add_subdirectory(libs/json EXCLUDE_FROM_ALL) +# ENDIF() IF(NOT TARGET loguru) add_subdirectory(libs/loguru EXCLUDE_FROM_ALL) @@ -21,32 +21,31 @@ IF(NOT TARGET loguru) ENDIF() -IF (NOT TARGET UDPService) - set(UDPService_Testing OFF CACHE BOOL "disable testing") - add_subdirectory(libs/UDPService EXCLUDE_FROM_ALL) +IF (NOT TARGET CommService) + add_subdirectory(libs/CommService EXCLUDE_FROM_ALL) ENDIF() -add_library(EntityManager STATIC +add_library(SimControl STATIC - include/EntityManager/EntityManager.hpp - src/EntityManager/EntityManager.cpp + include/SimControl/SimControl.hpp + src/SimControl/SimControl.cpp ) -target_link_libraries(EntityManager +target_link_libraries(SimControl PUBLIC - UDPService + CommService pthread - nlohmann_json::nlohmann_json + loguru ) -target_include_directories(EntityManager +target_include_directories(SimControl PUBLIC $ $ @@ -55,13 +54,13 @@ target_include_directories(EntityManager ) -add_executable(EntitiyManagerApplication +add_executable(SimControlApplication src/main.cpp ) -target_link_libraries(EntitiyManagerApplication +target_link_libraries(SimControlApplication CLI11 - EntityManager + SimControl ) # target_include_directories(EntitiyManagerApplication diff --git a/include/SimControl/SimControl.hpp b/include/SimControl/SimControl.hpp new file mode 100644 index 0000000..6284a3e --- /dev/null +++ b/include/SimControl/SimControl.hpp @@ -0,0 +1,93 @@ +#ifndef __SIMCONTROL__ +#define __SIMCONTROL__ + +#include "CommService/Message.hpp" +#include + + +#include +#include +#include +#include +#include +#include + +#include + + + +namespace SimControl { + + +class SimControl : public std::enable_shared_from_this { + public: + SimControl(const std::string &addr,const std::int16_t port): + // UDPInboundQueue_(std::make_shared>()), + // UDPOutboundQueue_(std::make_shared>()), + + isMainThreadRunning_(false), + stopMainThread_(false), + isCommServiceThreadRunning_(false), + stopCommServiceThread_(false), + CommServiceThread(nullptr), + MainFunctionThread_(nullptr) + { + OutgoingMessageQueue_ = std::make_shared>(); + IncommingMessageQueue_ = nullptr; + } + + void start(); + void stop(); + + private: + + std::unique_ptr CommServiceThread = nullptr; + std::unique_ptr MainFunctionThread_ = nullptr; + + std::shared_ptr> UDPInboundQueue_ = nullptr; + std::shared_ptr> UDPOutboundQueue_ = nullptr; + + std::atomic isCommServiceThreadRunning_; + std::atomic stopCommServiceThread_; + + std::atomic isMainThreadRunning_; + std::atomic stopMainThread_; + + // std::mutex mutexInboundQueue_; + // std::mutex mutexOutboundQueue_; + + ///pointer to the commservice + std::unique_ptr CommService_; + + ///threadsafe message queue from CommService + std::shared_ptr> IncommingMessageQueue_; + std::shared_ptr> OutgoingMessageQueue_; + + + void startMainFunction_(); + void stopMainFunction_(); + + void startCommService_(); + void stopCommService_(); + + + + void CommServiceFunction_(); + void MainFunction_(); + + + + + +}; + + + + +} + + + + + +#endif \ No newline at end of file diff --git a/libs/loguru/CMakeLists.txt b/libs/loguru/CMakeLists.txt new file mode 100644 index 0000000..c66e16c --- /dev/null +++ b/libs/loguru/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required (VERSION 3.1 FATAL_ERROR) +project (loguru VERSION 0.0.1 LANGUAGES CXX C) +IF(NOT TARGET loguru) +add_library(loguru STATIC loguru.cpp loguru.hpp) +target_include_directories(loguru PUBLIC .) +target_compile_options(loguru PUBLIC -DLOGURU_WITH_STREAMS=1) +target_link_libraries(loguru PUBLIC dl) +ENDIF() \ No newline at end of file diff --git a/src/SimControl/SimControl.cpp b/src/SimControl/SimControl.cpp new file mode 100644 index 0000000..b4dc3a4 --- /dev/null +++ b/src/SimControl/SimControl.cpp @@ -0,0 +1,148 @@ +#include "CommService/CommService.hpp" +#include "CommService/Message.hpp" +#include + + + +#include + +#include +#include +#include +#include +#include + +namespace SimControl { + + + +void SimControl::start() +{ + startMainFunction_(); + startCommService_(); +} + +void SimControl::stop() +{ + stopCommService_(); + stopMainFunction_(); + +} + + +void SimControl::startMainFunction_(){ + + if (isMainThreadRunning_ == true) + { + throw std::runtime_error("Main thread already running"); + } + + + // MainFunctionThread_ = std::make_shared(Args &&args...)e + MainFunctionThread_ = std::make_unique(&SimControl::MainFunction_,this); + + +} + + +void SimControl::stopMainFunction_(){ + + std::int32_t timeout = 6000; + stopMainThread_=true; + + + while(isMainThreadRunning_ && timeout > 0) + { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + timeout-=100; + } + if (timeout <= 0) + { + throw std::runtime_error("can not stop main thread"); + } + MainFunctionThread_->join(); + LOG_S(INFO)<< "stoped main function" ; + +} + +void SimControl::MainFunction_() +{ +isMainThreadRunning_=true; +LOG_S(INFO)<< "main thread is started"; + + while(!stopMainThread_) + { + + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + + + } + isMainThreadRunning_=false; + +} + + +void SimControl::startCommService_(){ + + + if (isCommServiceThreadRunning_ == true) + { + throw std::runtime_error("Comm thread already running"); + } + + + // MainFunctionThread_ = std::make_shared(Args &&args...)e + CommServiceThread = std::make_unique(&SimControl::CommServiceFunction_,this); + +} + + +void SimControl::stopCommService_(){ + + std::int32_t timeout = 6000; + stopCommServiceThread_ = true; + + + while(isCommServiceThreadRunning_ && timeout > 0) + { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + timeout-=100; + } + if (timeout <= 0) + { + throw std::runtime_error("can not stop UDPService thread"); + } + CommServiceThread->join(); + LOG_S(INFO) << "stoped CommService function" ; + +} + +void SimControl::CommServiceFunction_(){ + isCommServiceThreadRunning_ = true; + LOG_S(INFO)<< "Comm thread is started"; + CommService_ = std::make_unique("127.0.0.255","127.0.0.1",1,CommService::ID_SERVICE,"SimControl",8000); + CommService_->connect(); + IncommingMessageQueue_ = CommService_->getReceivedMessageQueue(); + + while(!stopCommServiceThread_) + { + LOG_S(INFO)<< "received Messages" << IncommingMessageQueue_->size(); + LOG_S(INFO) << "out going Messages:" << OutgoingMessageQueue_->size(); + if (OutgoingMessageQueue_->size()> 0) { + CommService_->publish(OutgoingMessageQueue_->pop().value()); + } + + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + + } + CommService_->stop(); + isCommServiceThreadRunning_=false; + +} + + + + +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e69de29..5c4d7e6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -0,0 +1,44 @@ +#include +#include + +#include + +/// variable for stopping the application +bool running = true; + +/** + * @brief killhandler to set running to false on CTRL-C + * + * @param s - the signal to manage + */ +void killHandlerPing(int s) { + + if (s == SIGINT) { + running = false; + } +} + + + +int main() +{ + +// setup signal handler + struct sigaction sigIntHandler; + sigIntHandler.sa_handler = killHandlerPing; + sigemptyset(&sigIntHandler.sa_mask); + sigIntHandler.sa_flags = 0; + sigaction(SIGINT, &sigIntHandler, NULL); + + + SimControl::SimControl sc("127.0.0.255",8000); + + sc.start(); + while (running) { + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } + sc.stop(); + + + return 0; +} \ No newline at end of file