Move to Qt

Everything seems to be working
Only timer is not moved over
This commit is contained in:
2024-09-12 16:59:54 +02:00
parent fc181726b9
commit 9eef1fc3ff
39 changed files with 399 additions and 501 deletions
+17 -71
View File
@@ -1,58 +1,15 @@
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <QtCore/QCoreApplication>
#include <QtCore/QCommandLineParser>
#include <QtCore/QCommandLineOption>
#include "spdlog/async.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
#include "message_parser.hpp"
#include "simulators.hpp"
#include "websocket.hpp"
class Spdlog_buffer : public std::stringbuf
{
public:
Spdlog_buffer(std::function<void(std::string)> log_function) : log_function(log_function)
{
}
int sync()
{
std::string str = this->str();
str.pop_back();
log_function(str);
this->str("");
return 0;
}
private:
std::function<void(std::string)> log_function;
};
Simulators *g_simulators;
Websocket *g_web_socket;
Spdlog_buffer g_info_buffer([](std::string message) { spdlog::info(message); });
Spdlog_buffer g_error_buffer([](std::string message) { spdlog::error(message); });
std::ostream g_out(&g_info_buffer);
std::ostream g_err(&g_error_buffer);
void int_handler(int s)
{
UNUSED(s);
std::cout << std::endl;
g_out << "Caught Ctrl + C" << std::endl;
if (g_web_socket != nullptr)
g_web_socket->stop();
if (g_simulators != nullptr)
g_simulators->stop();
g_out << "Shutting down." << std::endl;
// delay to allow the server to stop gracefully
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
exit(1);
}
void init_logger()
{
spdlog::init_thread_pool(8192, 1);
@@ -67,38 +24,27 @@ void init_logger()
spdlog::set_level(spdlog::level::trace);
}
int main(void)
int main(int argc, char *argv[])
{
// Setup the signal handler
struct sigaction sigIntHandler;
QCoreApplication a(argc, argv);
sigIntHandler.sa_handler = int_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
QCommandLineParser parser;
parser.setApplicationDescription("QtWebSockets example: echoserver");
parser.addHelpOption();
sigaction(SIGINT, &sigIntHandler, NULL);
QCommandLineOption portOption(QStringList() << "p" << "port",
QCoreApplication::translate("main", "Port for echoserver [default: 9002]."),
QCoreApplication::translate("main", "port"), QLatin1String("9002"));
parser.addOption(portOption);
parser.process(a);
uint16_t port = parser.value(portOption).toUShort();
// Initialize the logger
init_logger();
// The server starts in this thread
spdlog::info("Starting server");
Simulators simulators;
g_simulators = &simulators;
Simulators simulators(port);
Websocket web_socket(
9002, &g_out, &g_err, [](std::string message) { return message_parser(message); },
[]() { return g_simulators->changed_UI_items().dump(); });
g_web_socket = &web_socket;
// Start the message processor and broadcast processor in separate threads
std::thread message_processor_thread(std::bind(&Websocket::process_messages, &web_socket));
std::thread broadcast_processor_thred(std::bind(&Websocket::process_broadcast, &web_socket));
// Run the asio loop with the main thread
web_socket.run();
// Wait for the threads to finish (should never happen)
message_processor_thread.join();
broadcast_processor_thred.join();
}
return a.exec();
}