2021-08-22 20:46:52 +01:00
|
|
|
#include "Application.hpp"
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <csignal>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
|
|
|
|
|
#include "src/Logger/Logger.hpp"
|
2021-05-30 19:05:18 +01:00
|
|
|
#include "src/Helpers/Paths.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
#include "Exceptions/InvalidConfig.hpp"
|
|
|
|
|
|
|
|
|
|
using namespace Bloom;
|
|
|
|
|
using namespace Bloom::Events;
|
|
|
|
|
using namespace Bloom::Exceptions;
|
|
|
|
|
|
|
|
|
|
int Application::run(const std::vector<std::string>& arguments) {
|
|
|
|
|
try {
|
|
|
|
|
this->setName("Bloom");
|
|
|
|
|
|
|
|
|
|
if (!arguments.empty()) {
|
|
|
|
|
auto firstArg = arguments.front();
|
2021-04-06 02:10:14 +01:00
|
|
|
auto commandsToCallbackMapping = this->getCommandToHandlerMapping();
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
if (commandsToCallbackMapping.contains(firstArg)) {
|
|
|
|
|
// User has passed an argument that maps to a command callback - invoke the callback and shutdown
|
|
|
|
|
auto returnValue = commandsToCallbackMapping.at(firstArg)();
|
|
|
|
|
|
|
|
|
|
this->shutdown();
|
|
|
|
|
return returnValue;
|
|
|
|
|
}
|
2022-01-02 20:45:14 +00:00
|
|
|
|
|
|
|
|
// If the first argument didn't map to a command, we assume it's an environment name
|
|
|
|
|
this->selectedEnvironmentName = firstArg;
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-24 16:22:04 +01:00
|
|
|
#ifdef BLOOM_DEBUG_BUILD
|
|
|
|
|
Logger::warning("This is a debug build - some functions may not work as expected.");
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
this->startup();
|
|
|
|
|
|
2021-12-31 19:45:15 +00:00
|
|
|
if (this->insightConfig->insightEnabled) {
|
|
|
|
|
this->insight = std::make_unique<Insight>(
|
|
|
|
|
this->eventManager,
|
|
|
|
|
this->projectConfig.value(),
|
|
|
|
|
this->environmentConfig.value(),
|
2022-01-02 20:44:39 +00:00
|
|
|
this->insightConfig.value(),
|
|
|
|
|
this->projectSettings.value().insightSettings
|
2021-12-31 19:45:15 +00:00
|
|
|
);
|
2021-09-24 23:23:22 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Before letting Insight occupy the main thread, process any pending events that accumulated
|
|
|
|
|
* during startup.
|
|
|
|
|
*/
|
|
|
|
|
this->applicationEventListener->dispatchCurrentEvents();
|
|
|
|
|
|
|
|
|
|
if (Thread::getThreadState() == ThreadState::READY) {
|
|
|
|
|
this->insight->run();
|
|
|
|
|
Logger::debug("Insight closed");
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
this->shutdown();
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Main event loop
|
2021-05-29 21:39:00 +01:00
|
|
|
while (Thread::getThreadState() == ThreadState::READY) {
|
2021-04-04 21:04:12 +01:00
|
|
|
this->applicationEventListener->waitAndDispatch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (const InvalidConfig& exception) {
|
|
|
|
|
Logger::error(exception.getMessage());
|
|
|
|
|
|
|
|
|
|
} catch (const Exception& exception) {
|
|
|
|
|
Logger::error(exception.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->shutdown();
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 21:12:31 +01:00
|
|
|
bool Application::isRunningAsRoot() {
|
|
|
|
|
return geteuid() == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
void Application::startup() {
|
|
|
|
|
auto applicationEventListener = this->applicationEventListener;
|
|
|
|
|
this->eventManager.registerListener(applicationEventListener);
|
|
|
|
|
applicationEventListener->registerCallbackForEventType<Events::ShutdownApplication>(
|
2021-04-25 16:03:07 +01:00
|
|
|
std::bind(&Application::onShutdownApplicationRequest, this, std::placeholders::_1)
|
2021-04-04 21:04:12 +01:00
|
|
|
);
|
|
|
|
|
|
2022-01-02 20:44:39 +00:00
|
|
|
this->loadProjectSettings();
|
2022-01-02 20:13:18 +00:00
|
|
|
this->loadProjectConfiguration();
|
2021-12-31 19:45:15 +00:00
|
|
|
Logger::configure(this->projectConfig.value());
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-01-22 16:51:21 +00:00
|
|
|
Logger::debug("Bloom version: " + Application::VERSION.toString());
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
this->blockAllSignalsOnCurrentThread();
|
2022-01-09 14:25:52 +00:00
|
|
|
this->startSignalHandler();
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2021-11-17 21:45:56 +00:00
|
|
|
Logger::info("Selected environment: \"" + this->selectedEnvironmentName + "\"");
|
2021-04-04 21:04:12 +01:00
|
|
|
Logger::debug("Number of environments extracted from config: "
|
2021-12-31 19:45:15 +00:00
|
|
|
+ std::to_string(this->projectConfig->environments.size()));
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2021-05-24 21:09:50 +01:00
|
|
|
applicationEventListener->registerCallbackForEventType<Events::TargetControllerThreadStateChanged>(
|
|
|
|
|
std::bind(&Application::onTargetControllerThreadStateChanged, this, std::placeholders::_1)
|
2021-04-04 21:04:12 +01:00
|
|
|
);
|
|
|
|
|
|
2021-05-24 21:09:50 +01:00
|
|
|
applicationEventListener->registerCallbackForEventType<Events::DebugServerThreadStateChanged>(
|
|
|
|
|
std::bind(&Application::onDebugServerThreadStateChanged, this, std::placeholders::_1)
|
2021-04-04 21:04:12 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this->startTargetController();
|
|
|
|
|
this->startDebugServer();
|
|
|
|
|
|
2021-05-29 21:39:00 +01:00
|
|
|
Thread::setThreadState(ThreadState::READY);
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-10-06 21:12:31 +01:00
|
|
|
void Application::shutdown() {
|
|
|
|
|
auto appState = Thread::getThreadState();
|
|
|
|
|
if (appState == ThreadState::STOPPED || appState == ThreadState::SHUTDOWN_INITIATED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread::setThreadState(ThreadState::SHUTDOWN_INITIATED);
|
|
|
|
|
Logger::info("Shutting down Bloom");
|
|
|
|
|
|
|
|
|
|
this->stopDebugServer();
|
|
|
|
|
this->stopTargetController();
|
2022-01-09 14:25:52 +00:00
|
|
|
this->stopSignalHandler();
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-01-22 16:14:29 +00:00
|
|
|
this->saveProjectSettings();
|
2021-10-06 21:12:31 +01:00
|
|
|
Thread::setThreadState(ThreadState::STOPPED);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-02 20:44:39 +00:00
|
|
|
void Application::loadProjectSettings() {
|
|
|
|
|
const auto projectSettingsPath = Paths::projectSettingsPath();
|
|
|
|
|
auto jsonSettingsFile = QFile(QString::fromStdString(projectSettingsPath));
|
|
|
|
|
|
|
|
|
|
if (jsonSettingsFile.exists()) {
|
|
|
|
|
try {
|
2022-01-22 16:14:29 +00:00
|
|
|
if (!jsonSettingsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
|
throw Exception("Failed to open settings file.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto jsonObject = QJsonDocument::fromJson(jsonSettingsFile.readAll()).object();
|
|
|
|
|
jsonSettingsFile.close();
|
|
|
|
|
|
2022-01-02 20:44:39 +00:00
|
|
|
this->projectSettings = ProjectSettings(jsonObject);
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
} catch (const std::exception& exception) {
|
|
|
|
|
Logger::error(
|
|
|
|
|
"Failed to load project settings from " + projectSettingsPath + " - " + exception.what()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->projectSettings = ProjectSettings();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-22 16:14:29 +00:00
|
|
|
void Application::saveProjectSettings() {
|
2022-01-22 16:45:42 +00:00
|
|
|
if (!this->projectSettings.has_value()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-22 16:14:29 +00:00
|
|
|
const auto projectSettingsPath = Paths::projectSettingsPath();
|
|
|
|
|
auto jsonSettingsFile = QFile(QString::fromStdString(projectSettingsPath));
|
|
|
|
|
|
|
|
|
|
Logger::debug("Saving project settings to " + projectSettingsPath);
|
|
|
|
|
|
|
|
|
|
QDir().mkpath(QString::fromStdString(Paths::projectSettingsDirPath()));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const auto jsonDocument = QJsonDocument(this->projectSettings->toJson());
|
|
|
|
|
|
|
|
|
|
if (!jsonSettingsFile.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) {
|
|
|
|
|
throw Exception(
|
|
|
|
|
"failed to open/create settings file (" + projectSettingsPath + "). Check file permissions."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonSettingsFile.write(jsonDocument.toJson());
|
|
|
|
|
jsonSettingsFile.close();
|
|
|
|
|
|
|
|
|
|
} catch (const Exception& exception) {
|
|
|
|
|
Logger::error(
|
|
|
|
|
"Failed to save project settings - " + exception.getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-02 20:13:18 +00:00
|
|
|
void Application::loadProjectConfiguration() {
|
2022-01-02 20:25:25 +00:00
|
|
|
auto jsonConfigFile = QFile(QString::fromStdString(Paths::projectConfigPath()));
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
if (!jsonConfigFile.exists()) {
|
|
|
|
|
throw InvalidConfig("Bloom configuration file (bloom.json) not found. Working directory: "
|
2022-01-02 20:25:25 +00:00
|
|
|
+ Paths::projectDirPath());
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-22 16:14:29 +00:00
|
|
|
if (!jsonConfigFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2021-04-04 21:04:12 +01:00
|
|
|
throw InvalidConfig("Failed to load Bloom configuration file (bloom.json) Working directory: "
|
2022-01-02 20:25:25 +00:00
|
|
|
+ Paths::projectDirPath());
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto jsonObject = QJsonDocument::fromJson(jsonConfigFile.readAll()).object();
|
|
|
|
|
jsonConfigFile.close();
|
2021-12-31 19:44:20 +00:00
|
|
|
|
2022-01-02 20:13:18 +00:00
|
|
|
this->projectConfig = ProjectConfig(jsonObject);
|
|
|
|
|
|
|
|
|
|
// Validate the selected environment
|
|
|
|
|
if (!this->projectConfig->environments.contains(this->selectedEnvironmentName)) {
|
|
|
|
|
throw InvalidConfig(
|
|
|
|
|
"Environment (\"" + this->selectedEnvironmentName + "\") not found in configuration."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->environmentConfig = this->projectConfig->environments.at(this->selectedEnvironmentName);
|
|
|
|
|
|
|
|
|
|
if (this->environmentConfig->insightConfig.has_value()) {
|
|
|
|
|
this->insightConfig = this->environmentConfig->insightConfig.value();
|
|
|
|
|
|
|
|
|
|
} else if (this->projectConfig->insightConfig.has_value()) {
|
|
|
|
|
this->insightConfig = this->projectConfig->insightConfig.value();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
throw InvalidConfig("Insight configuration missing.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this->environmentConfig->debugServerConfig.has_value()) {
|
|
|
|
|
this->debugServerConfig = this->environmentConfig->debugServerConfig.value();
|
|
|
|
|
|
|
|
|
|
} else if (this->projectConfig->debugServerConfig.has_value()) {
|
|
|
|
|
this->debugServerConfig = this->projectConfig->debugServerConfig.value();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
throw InvalidConfig("Debug server configuration missing.");
|
|
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Application::presentHelpText() {
|
|
|
|
|
/*
|
|
|
|
|
* Silence all logging here, as we're just to display the help text and then exit the application. Any
|
|
|
|
|
* further logging will just be noise.
|
|
|
|
|
*/
|
|
|
|
|
Logger::silence();
|
|
|
|
|
|
|
|
|
|
// The file help.txt is included in the binary image as a resource. See src/resource.qrc
|
2021-05-30 19:05:18 +01:00
|
|
|
auto helpFile = QFile(QString::fromStdString(
|
|
|
|
|
Paths::compiledResourcesPath()
|
|
|
|
|
+ "/resources/help.txt"
|
|
|
|
|
)
|
|
|
|
|
);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
if (!helpFile.open(QIODevice::ReadOnly)) {
|
|
|
|
|
// This should never happen - if it does, something has gone very wrong
|
2021-11-02 23:26:11 +00:00
|
|
|
throw Exception(
|
|
|
|
|
"Failed to open help file - please report this issue at " + Paths::homeDomainName() + "/report-issue"
|
|
|
|
|
);
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-10-19 23:00:54 +01:00
|
|
|
std::cout << "Bloom v" << Application::VERSION.toString() << "\n";
|
2021-05-25 21:50:17 +01:00
|
|
|
std::cout << QTextStream(&helpFile).readAll().toUtf8().constData() << "\n";
|
2021-04-04 21:04:12 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Application::presentVersionText() {
|
|
|
|
|
Logger::silence();
|
|
|
|
|
|
2021-10-19 23:00:54 +01:00
|
|
|
std::cout << "Bloom v" << Application::VERSION.toString() << "\n";
|
2021-04-24 16:22:04 +01:00
|
|
|
|
|
|
|
|
#ifdef BLOOM_DEBUG_BUILD
|
|
|
|
|
std::cout << "DEBUG BUILD - Compilation timestamp: " << __DATE__ << " " << __TIME__ << "\n";
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-11-02 23:26:11 +00:00
|
|
|
std::cout << Paths::homeDomainName() + "/\n";
|
2021-05-25 21:50:17 +01:00
|
|
|
std::cout << "Nav Mohammed\n";
|
2021-04-04 21:04:12 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Application::initProject() {
|
|
|
|
|
auto configFile = QFile(QString::fromStdString(std::filesystem::current_path().string() + "/bloom.json"));
|
|
|
|
|
|
|
|
|
|
if (configFile.exists()) {
|
|
|
|
|
throw Exception("Bloom configuration file (bloom.json) already exists in working directory.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The file bloom.template.json is just a template Bloom config file that is included in the binary image as
|
|
|
|
|
* a resource. See src/resource.qrc
|
|
|
|
|
*
|
|
|
|
|
* We simply copy the template file into the user's working directory.
|
|
|
|
|
*/
|
2021-05-30 19:05:18 +01:00
|
|
|
auto templateConfigFile = QFile(QString::fromStdString(
|
|
|
|
|
Paths::compiledResourcesPath()
|
|
|
|
|
+ "/resources/bloom.template.json"
|
|
|
|
|
)
|
|
|
|
|
);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
if (!templateConfigFile.open(QIODevice::ReadOnly)) {
|
2021-11-02 23:26:11 +00:00
|
|
|
throw Exception(
|
|
|
|
|
"Failed to open template configuration file - please report this issue at "
|
|
|
|
|
+ Paths::homeDomainName() + "/report-issue"
|
|
|
|
|
);
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!configFile.open(QIODevice::ReadWrite)) {
|
|
|
|
|
throw Exception("Failed to create Bloom configuration file (bloom.json)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
configFile.write(templateConfigFile.readAll());
|
|
|
|
|
|
|
|
|
|
configFile.close();
|
|
|
|
|
templateConfigFile.close();
|
|
|
|
|
|
|
|
|
|
Logger::info("Bloom configuration file (bloom.json) created in working directory.");
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 14:25:52 +00:00
|
|
|
void Application::startSignalHandler() {
|
|
|
|
|
this->signalHandlerThread = std::thread(&SignalHandler::run, std::ref(this->signalHandler));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::stopSignalHandler() {
|
|
|
|
|
if (this->signalHandler.getThreadState() != ThreadState::STOPPED
|
|
|
|
|
&& this->signalHandler.getThreadState() != ThreadState::UNINITIALISED
|
|
|
|
|
) {
|
|
|
|
|
this->signalHandler.triggerShutdown();
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Send meaningless signal to the SignalHandler thread to have it shutdown. The signal will pull it out of a
|
|
|
|
|
* blocking state and allow it to action the shutdown. See SignalHandler::run() for more.
|
|
|
|
|
*/
|
|
|
|
|
pthread_kill(this->signalHandlerThread.native_handle(), SIGUSR1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this->signalHandlerThread.joinable()) {
|
|
|
|
|
Logger::debug("Joining SignalHandler thread");
|
|
|
|
|
this->signalHandlerThread.join();
|
|
|
|
|
Logger::debug("SignalHandler thread joined");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
void Application::startTargetController() {
|
2021-12-31 19:45:15 +00:00
|
|
|
this->targetController = std::make_unique<TargetController>(
|
|
|
|
|
this->eventManager,
|
|
|
|
|
this->projectConfig.value(),
|
|
|
|
|
this->environmentConfig.value()
|
|
|
|
|
);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
this->targetControllerThread = std::thread(
|
|
|
|
|
&TargetController::run,
|
2021-12-31 19:45:15 +00:00
|
|
|
this->targetController.get()
|
2021-04-04 21:04:12 +01:00
|
|
|
);
|
|
|
|
|
|
2021-05-24 21:09:50 +01:00
|
|
|
auto tcStateChangeEvent = this->applicationEventListener->waitForEvent<Events::TargetControllerThreadStateChanged>();
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
if (!tcStateChangeEvent.has_value() || tcStateChangeEvent->get()->getState() != ThreadState::READY) {
|
|
|
|
|
throw Exception("TargetController failed to startup.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::stopTargetController() {
|
2022-01-02 21:30:24 +00:00
|
|
|
if (this->targetController == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-31 19:45:15 +00:00
|
|
|
auto targetControllerState = this->targetController->getThreadState();
|
2021-04-04 21:04:12 +01:00
|
|
|
if (targetControllerState == ThreadState::STARTING || targetControllerState == ThreadState::READY) {
|
|
|
|
|
this->eventManager.triggerEvent(std::make_shared<Events::ShutdownTargetController>());
|
2021-05-24 21:09:50 +01:00
|
|
|
this->applicationEventListener->waitForEvent<Events::TargetControllerThreadStateChanged>(
|
2021-04-04 21:04:12 +01:00
|
|
|
std::chrono::milliseconds(10000)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this->targetControllerThread.joinable()) {
|
|
|
|
|
Logger::debug("Joining TargetController thread");
|
|
|
|
|
this->targetControllerThread.join();
|
|
|
|
|
Logger::debug("TargetController thread joined");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::startDebugServer() {
|
|
|
|
|
auto supportedDebugServers = this->getSupportedDebugServers();
|
2021-12-31 19:45:15 +00:00
|
|
|
if (!supportedDebugServers.contains(this->debugServerConfig->name)) {
|
|
|
|
|
throw Exceptions::InvalidConfig("DebugServer \"" + this->debugServerConfig->name + "\" not found.");
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-31 19:45:15 +00:00
|
|
|
this->debugServer = supportedDebugServers.at(this->debugServerConfig->name)();
|
2021-04-04 21:04:12 +01:00
|
|
|
Logger::info("Selected DebugServer: " + this->debugServer->getName());
|
|
|
|
|
|
|
|
|
|
this->debugServerThread = std::thread(
|
2021-05-24 20:58:49 +01:00
|
|
|
&DebugServers::DebugServer::run,
|
2021-04-04 21:04:12 +01:00
|
|
|
this->debugServer.get()
|
|
|
|
|
);
|
|
|
|
|
|
2021-05-24 21:09:50 +01:00
|
|
|
auto dsStateChangeEvent = this->applicationEventListener->waitForEvent<Events::DebugServerThreadStateChanged>();
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
if (!dsStateChangeEvent.has_value() || dsStateChangeEvent->get()->getState() != ThreadState::READY) {
|
|
|
|
|
throw Exception("DebugServer failed to startup.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::stopDebugServer() {
|
|
|
|
|
if (this->debugServer == nullptr) {
|
|
|
|
|
// DebugServer hasn't been resolved yet.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-29 21:39:00 +01:00
|
|
|
auto debugServerState = this->debugServer->getThreadState();
|
2021-04-04 21:04:12 +01:00
|
|
|
if (debugServerState == ThreadState::STARTING || debugServerState == ThreadState::READY) {
|
|
|
|
|
this->eventManager.triggerEvent(std::make_shared<Events::ShutdownDebugServer>());
|
2021-05-24 21:09:50 +01:00
|
|
|
this->applicationEventListener->waitForEvent<Events::DebugServerThreadStateChanged>(
|
2021-04-04 21:04:12 +01:00
|
|
|
std::chrono::milliseconds(5000)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this->debugServerThread.joinable()) {
|
|
|
|
|
Logger::debug("Joining DebugServer thread");
|
|
|
|
|
this->debugServerThread.join();
|
|
|
|
|
Logger::debug("DebugServer thread joined");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-22 14:44:00 +01:00
|
|
|
void Application::onTargetControllerThreadStateChanged(const Events::TargetControllerThreadStateChanged& event) {
|
2021-06-22 03:06:20 +01:00
|
|
|
if (event.getState() == ThreadState::STOPPED || event.getState() == ThreadState::SHUTDOWN_INITIATED) {
|
2021-04-04 21:04:12 +01:00
|
|
|
// TargetController has unexpectedly shutdown - it must have encountered a fatal error.
|
|
|
|
|
this->shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-22 14:44:00 +01:00
|
|
|
void Application::onShutdownApplicationRequest(const Events::ShutdownApplication&) {
|
2021-04-04 21:04:12 +01:00
|
|
|
Logger::debug("ShutdownApplication event received.");
|
|
|
|
|
this->shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-22 14:44:00 +01:00
|
|
|
void Application::onDebugServerThreadStateChanged(const Events::DebugServerThreadStateChanged& event) {
|
2021-06-22 03:06:20 +01:00
|
|
|
if (event.getState() == ThreadState::STOPPED || event.getState() == ThreadState::SHUTDOWN_INITIATED) {
|
2021-04-04 21:04:12 +01:00
|
|
|
// DebugServer has unexpectedly shutdown - it must have encountered a fatal error.
|
|
|
|
|
this->shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|