diff --git a/tools/main.cpp b/tools/main.cpp index 484c46f..bc6c76d 100644 --- a/tools/main.cpp +++ b/tools/main.cpp @@ -27,7 +27,7 @@ private: std::function log_function; }; -Simulators g_simulators; +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); }); @@ -41,7 +41,7 @@ void int_handler(int s) g_out << "Caught Ctrl + C" << std::endl; if (g_web_socket != nullptr) g_web_socket->stop(); - g_simulators.stop(); + 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)); @@ -76,7 +76,10 @@ int main(void) // The server starts in this thread spdlog::info("Starting server"); - Websocket web_socket(9002, &g_out, &g_err, [](std::string message) { return message_parser(message); }, []() { return g_simulators.changed_UI_items().dump(); }); + Simulators simulators; + g_simulators = &simulators; + + 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 diff --git a/tools/message_parser.cpp b/tools/message_parser.cpp index d8c3968..210d5b5 100644 --- a/tools/message_parser.cpp +++ b/tools/message_parser.cpp @@ -2,7 +2,7 @@ #include #include -extern Simulators g_simulators; +extern Simulators* g_simulators; std::string message_parser(std::string message) { @@ -32,27 +32,27 @@ json command_parser(json command) case Command::get_UI_element: spdlog::info("Command {}: {}", type, std::string(command["id"])); j["response"]["type"] = type; - j["response"]["UI_item"] = g_simulators.invoke_active_simulator()->get_UI_item(command["id"])->to_json(); + j["response"]["UI_item"] = g_simulators->invoke_active_simulator()->get_UI_item(command["id"])->to_json(); break; case Command::get_UI_elements: spdlog::info("Command {}", type); j["response"]["type"] = type; - j["response"]["UI_items"] = g_simulators.invoke_active_simulator()->get_UI_items()["UI_items"]; + j["response"]["UI_items"] = g_simulators->invoke_active_simulator()->get_UI_items()["UI_items"]; break; case Command::switch_simulator: spdlog::info("Command {} to {}", type, std::string(command["name"])); - g_simulators.switch_simulator(command["name"]); + g_simulators->switch_simulator(command["name"]); j["response"]["type"] = type; break; case Command::get_active_simulator_name: spdlog::info("Command {}", type); j["response"]["type"] = type; - j["response"]["name"] = g_simulators.active_simulator_name(); + j["response"]["name"] = g_simulators->active_simulator_name(); break; case Command::get_simulators: spdlog::info("Command {}", type); j["response"]["type"] = type; - j["response"]["simulators"] = g_simulators.list_simulators(); + j["response"]["simulators"] = g_simulators->list_simulators(); break; default: break; @@ -70,25 +70,25 @@ void event_handler(json event) case Event::clicked: { spdlog::info("Event {}: {}", type, std::string(event["id"])); - g_simulators.invoke_active_simulator()->get_UI_item(std::string(event["id"]))->click(); + g_simulators->invoke_active_simulator()->get_UI_item(std::string(event["id"]))->click(); break; } case Event::value_changed: { spdlog::info("Event {}: {} {}", type, std::string(event["id"]), (double)(event["value"])); - g_simulators.invoke_active_simulator()->get_UI_item(std::string(event["id"]))->set_value((double)event["value"]); + g_simulators->invoke_active_simulator()->get_UI_item(std::string(event["id"]))->set_value((double)event["value"]); break; } case Event::text_changed: { spdlog::info("Event {}: {} {}", type, std::string(event["id"]), std::string(event["text"])); - g_simulators.invoke_active_simulator()->get_UI_item(std::string(event["id"]))->set_text(std::string(event["text"])); + g_simulators->invoke_active_simulator()->get_UI_item(std::string(event["id"]))->set_text(std::string(event["text"])); break; } case Event::selected: { spdlog::info("Event {}: {} {}", type, std::string(event["id"]), std::string(event["selected"])); - g_simulators.invoke_active_simulator()->get_UI_item(std::string(event["id"]))->set_selected(std::string(event["selected"])); + g_simulators->invoke_active_simulator()->get_UI_item(std::string(event["id"]))->set_selected(std::string(event["selected"])); break; } default: