ADD: added libs; added CMakeLists.txt; added framework of the app

This commit is contained in:
Henry Winkel
2022-09-15 11:15:11 +02:00
parent b7050044e5
commit 57c69f5e01
5 changed files with 310 additions and 18 deletions

View File

@@ -7,13 +7,13 @@ include(defaultOptions)
IF (NOT TARGET CLI11) IF (NOT TARGET CLI11)
set(CLI11_TESTING OFF CACHE BOOL "disable testing") set(CLI11_TESTING OFF CACHE BOOL "disable testing")
add_subdirectory(libs/CLI11 EXCLUDE_FROM_ALL) add_subdirectory(libs/cli11 EXCLUDE_FROM_ALL)
ENDIF() ENDIF()
IF(NOT TARGET nlohmann_json::nlohmann_json) # IF(NOT TARGET nlohmann_json::nlohmann_json)
set(JSON_BuildTests OFF CACHE INTERNAL "") # set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(libs/json EXCLUDE_FROM_ALL) # add_subdirectory(libs/json EXCLUDE_FROM_ALL)
ENDIF() # ENDIF()
IF(NOT TARGET loguru) IF(NOT TARGET loguru)
add_subdirectory(libs/loguru EXCLUDE_FROM_ALL) add_subdirectory(libs/loguru EXCLUDE_FROM_ALL)
@@ -21,32 +21,31 @@ IF(NOT TARGET loguru)
ENDIF() ENDIF()
IF (NOT TARGET UDPService) IF (NOT TARGET CommService)
set(UDPService_Testing OFF CACHE BOOL "disable testing") add_subdirectory(libs/CommService EXCLUDE_FROM_ALL)
add_subdirectory(libs/UDPService EXCLUDE_FROM_ALL)
ENDIF() ENDIF()
add_library(EntityManager STATIC add_library(SimControl STATIC
include/EntityManager/EntityManager.hpp include/SimControl/SimControl.hpp
src/EntityManager/EntityManager.cpp src/SimControl/SimControl.cpp
) )
target_link_libraries(EntityManager target_link_libraries(SimControl
PUBLIC PUBLIC
UDPService CommService
pthread pthread
nlohmann_json::nlohmann_json
loguru loguru
) )
target_include_directories(EntityManager target_include_directories(SimControl
PUBLIC PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include> $<INSTALL_INTERFACE:include>
@@ -55,13 +54,13 @@ target_include_directories(EntityManager
) )
add_executable(EntitiyManagerApplication add_executable(SimControlApplication
src/main.cpp src/main.cpp
) )
target_link_libraries(EntitiyManagerApplication target_link_libraries(SimControlApplication
CLI11 CLI11
EntityManager SimControl
) )
# target_include_directories(EntitiyManagerApplication # target_include_directories(EntitiyManagerApplication

View File

@@ -0,0 +1,93 @@
#ifndef __SIMCONTROL__
#define __SIMCONTROL__
#include "CommService/Message.hpp"
#include <iostream>
#include <atomic>
#include <cstdint>
#include <memory>
#include <thread>
#include <mutex>
#include <queue>
#include <CommService/CommService.hpp>
namespace SimControl {
class SimControl : public std::enable_shared_from_this<SimControl> {
public:
SimControl(const std::string &addr,const std::int16_t port):
// UDPInboundQueue_(std::make_shared<std::queue<std::string>>()),
// UDPOutboundQueue_(std::make_shared<std::queue<std::string>>()),
isMainThreadRunning_(false),
stopMainThread_(false),
isCommServiceThreadRunning_(false),
stopCommServiceThread_(false),
CommServiceThread(nullptr),
MainFunctionThread_(nullptr)
{
OutgoingMessageQueue_ = std::make_shared<ThreadsafeQueue<CommService::Message>>();
IncommingMessageQueue_ = nullptr;
}
void start();
void stop();
private:
std::unique_ptr<std::thread> CommServiceThread = nullptr;
std::unique_ptr<std::thread> MainFunctionThread_ = nullptr;
std::shared_ptr<std::queue<std::string>> UDPInboundQueue_ = nullptr;
std::shared_ptr<std::queue<std::string>> UDPOutboundQueue_ = nullptr;
std::atomic<bool> isCommServiceThreadRunning_;
std::atomic<bool> stopCommServiceThread_;
std::atomic<bool> isMainThreadRunning_;
std::atomic<bool> stopMainThread_;
// std::mutex mutexInboundQueue_;
// std::mutex mutexOutboundQueue_;
///pointer to the commservice
std::unique_ptr<CommService::CommService> CommService_;
///threadsafe message queue from CommService
std::shared_ptr<ThreadsafeQueue<CommService::Message>> IncommingMessageQueue_;
std::shared_ptr<ThreadsafeQueue<CommService::Message>> OutgoingMessageQueue_;
void startMainFunction_();
void stopMainFunction_();
void startCommService_();
void stopCommService_();
void CommServiceFunction_();
void MainFunction_();
};
}
#endif

View File

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

View File

@@ -0,0 +1,148 @@
#include "CommService/CommService.hpp"
#include "CommService/Message.hpp"
#include <SimControl/SimControl.hpp>
#include <loguru.hpp>
#include <cstdint>
#include <iostream>
#include <memory>
#include <string>
#include <unistd.h>
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<typename Tp>(Args &&args...)e
MainFunctionThread_ = std::make_unique<std::thread>(&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<typename Tp>(Args &&args...)e
CommServiceThread = std::make_unique<std::thread>(&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<CommService::CommService>("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;
}
}

View File

@@ -0,0 +1,44 @@
#include <iostream>
#include <csignal>
#include <SimControl/SimControl.hpp>
/// 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;
}