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

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