From ee185b68e62cbd104adb178fb9e198a8c79c0679 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 13 Oct 2025 21:44:00 +0200 Subject: [PATCH] Adds check for active simulator before commands Implements a check to ensure a simulator is active before processing commands. This prevents errors and unexpected behavior when commands are sent without an active simulator. The check is performed at the beginning of the command parsing, and logs an error if no simulator is active, returning an empty JSON object. --- server/message_parser.cpp | 10 ++++++++++ server/simulators.cpp | 5 +++++ server/simulators.hpp | 1 + 3 files changed, 16 insertions(+) diff --git a/server/message_parser.cpp b/server/message_parser.cpp index d660ab6..fd2f101 100644 --- a/server/message_parser.cpp +++ b/server/message_parser.cpp @@ -32,12 +32,22 @@ json Simulators::command_parser(json command) { case Command::get_UI_element: SPDLOG_INFO("Command {}: {}", type, (size_t)command.at("id")); + if (is_not_active()) + { + SPDLOG_ERROR("Command got called but there is no active simulator"); + return j; + } j["response"]["type"] = type; j["response"]["UI_item"] = invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id")); break; case Command::get_UI_elements: SPDLOG_INFO("Command {}", type); + if (is_not_active()) + { + SPDLOG_ERROR("Command got called but there is no active simulator"); + return j; + } j["response"]["type"] = type; j["response"]["UI_items"] = invoke_active_simulator()->get_UI_items()["UI_items"]; break; diff --git a/server/simulators.cpp b/server/simulators.cpp index 2c9f6f9..3b4add8 100644 --- a/server/simulators.cpp +++ b/server/simulators.cpp @@ -151,6 +151,11 @@ std::vector Simulators::list_simulators() const return names; } +bool Simulators::is_not_active() const +{ + return this->m_current_simulator.empty(); +} + Simulator_base *Simulators::invoke_active_simulator() { std::lock_guard lock(m_mutex); diff --git a/server/simulators.hpp b/server/simulators.hpp index 00b5a9e..f103806 100644 --- a/server/simulators.hpp +++ b/server/simulators.hpp @@ -52,6 +52,7 @@ class Simulators : public QObject void stop(); void switch_simulator(std::string name); std::vector list_simulators() const; + bool is_not_active() const; Simulator_base *invoke_active_simulator(); json changed_UI_items();