refactor: Use same mode for all contexts

This commit is contained in:
Lars Toenning
2024-12-23 13:49:24 +01:00
parent 740d1a9590
commit 391c08ce38
7 changed files with 41 additions and 119 deletions

View File

@@ -86,7 +86,7 @@ namespace swift::core
CApplication::CApplication(const QString &applicationName, CApplicationInfo::Application application, bool init) CApplication::CApplication(const QString &applicationName, CApplicationInfo::Application application, bool init)
: CIdentifiable(this), m_accessManager(new QNetworkAccessManager(this)), m_applicationInfo(application), : CIdentifiable(this), m_accessManager(new QNetworkAccessManager(this)), m_applicationInfo(application),
m_applicationName(applicationName), m_coreFacadeConfig(CCoreFacadeConfig::allEmpty()) m_applicationName(applicationName), m_coreFacadeConfig(CCoreFacadeConfig::NotUsed)
{ {
Q_ASSERT_X(!sApp, Q_FUNC_INFO, "already initialized"); Q_ASSERT_X(!sApp, Q_FUNC_INFO, "already initialized");
Q_ASSERT_X(QCoreApplication::instance(), Q_FUNC_INFO, "no application object"); Q_ASSERT_X(QCoreApplication::instance(), Q_FUNC_INFO, "no application object");
@@ -457,7 +457,7 @@ namespace swift::core
m_localSettingsLoaded = true; m_localSettingsLoaded = true;
// trigger loading and saving of settings in appropriate scenarios // trigger loading and saving of settings in appropriate scenarios
if (m_coreFacadeConfig.getModeApplication() != CCoreFacadeConfig::Remote) if (m_coreFacadeConfig.getMode() != CCoreFacadeConfig::Remote)
{ {
// facade running here locally // facade running here locally
const CStatusMessage msg = CSettingsCache::instance()->loadFromStore(); const CStatusMessage msg = CSettingsCache::instance()->loadFromStore();
@@ -727,7 +727,7 @@ namespace swift::core
Q_ASSERT_X(m_parsed, Q_FUNC_INFO, "Call this function after parsing"); Q_ASSERT_X(m_parsed, Q_FUNC_INFO, "Call this function after parsing");
m_useContexts = true; // otherwise startCoreFacade will early-return m_useContexts = true; // otherwise startCoreFacade will early-return
m_coreFacadeConfig = CCoreFacadeConfig::allEmpty(); m_coreFacadeConfig = CCoreFacadeConfig(CCoreFacadeConfig::NotUsed);
const CStatusMessage msg = this->initLocalSettings(); const CStatusMessage msg = this->initLocalSettings();
if (msg.isFailure()) { return msg; } if (msg.isFailure()) { return msg; }

View File

@@ -106,7 +106,7 @@ namespace swift::core
// shared state infrastructure // shared state infrastructure
m_dataLinkDBus = new shared_state::CDataLinkDBus(this); m_dataLinkDBus = new shared_state::CDataLinkDBus(this);
switch (m_config.getModeApplication()) switch (m_config.getMode())
{ {
case CCoreFacadeConfig::NotUsed: case CCoreFacadeConfig::NotUsed:
case CCoreFacadeConfig::Local: m_dataLinkDBus->initializeLocal(nullptr); break; case CCoreFacadeConfig::Local: m_dataLinkDBus->initializeLocal(nullptr); break;
@@ -120,13 +120,14 @@ namespace swift::core
// shared log history // shared log history
m_logHistorySource = new CLogHistorySource(this); m_logHistorySource = new CLogHistorySource(this);
m_logHistorySource->initialize(m_dataLinkDBus); m_logHistorySource->initialize(m_dataLinkDBus);
if (m_config.hasLocalCore()) if (m_config.getMode() == CCoreFacadeConfig::Local ||
m_config.getMode() == CCoreFacadeConfig::LocalInDBusServer)
{ {
m_logHistory = new CLogHistory(this); m_logHistory = new CLogHistory(this);
m_logHistory->initialize(m_dataLinkDBus); m_logHistory->initialize(m_dataLinkDBus);
} }
if (m_config.all(CCoreFacadeConfig::NotUsed)) if (m_config.getMode() == CCoreFacadeConfig::NotUsed)
{ {
m_initalized = true; m_initalized = true;
return; return;
@@ -134,28 +135,25 @@ namespace swift::core
// contexts // contexts
if (m_contextApplication) { m_contextApplication->deleteLater(); } if (m_contextApplication) { m_contextApplication->deleteLater(); }
m_contextApplication = m_contextApplication = IContextApplication::create(this, m_config.getMode(), m_dbusServer, m_dbusConnection);
IContextApplication::create(this, m_config.getModeApplication(), m_dbusServer, m_dbusConnection);
times.insert("Application", time.restart()); times.insert("Application", time.restart());
if (m_contextAudio) { m_contextAudio->deleteLater(); } if (m_contextAudio) { m_contextAudio->deleteLater(); }
m_contextAudio = qobject_cast<CContextAudioBase *>( m_contextAudio = qobject_cast<CContextAudioBase *>(
IContextAudio::create(this, m_config.getModeAudio(), m_dbusServer, m_dbusConnection)); IContextAudio::create(this, m_config.getMode(), m_dbusServer, m_dbusConnection));
times.insert("Audio", time.restart()); times.insert("Audio", time.restart());
if (m_contextOwnAircraft) { m_contextOwnAircraft->deleteLater(); } if (m_contextOwnAircraft) { m_contextOwnAircraft->deleteLater(); }
m_contextOwnAircraft = m_contextOwnAircraft = IContextOwnAircraft::create(this, m_config.getMode(), m_dbusServer, m_dbusConnection);
IContextOwnAircraft::create(this, m_config.getModeOwnAircraft(), m_dbusServer, m_dbusConnection);
times.insert("Own aircraft", time.restart()); times.insert("Own aircraft", time.restart());
if (m_contextSimulator) { m_contextSimulator->deleteLater(); } if (m_contextSimulator) { m_contextSimulator->deleteLater(); }
m_contextSimulator = m_contextSimulator = IContextSimulator::create(this, m_config.getMode(), m_dbusServer, m_dbusConnection);
IContextSimulator::create(this, m_config.getModeSimulator(), m_dbusServer, m_dbusConnection);
times.insert("Simulator", time.restart()); times.insert("Simulator", time.restart());
// depends on own aircraft and simulator context, which is bad style // depends on own aircraft and simulator context, which is bad style
if (m_contextNetwork) { m_contextNetwork->deleteLater(); } if (m_contextNetwork) { m_contextNetwork->deleteLater(); }
m_contextNetwork = IContextNetwork::create(this, m_config.getModeNetwork(), m_dbusServer, m_dbusConnection); m_contextNetwork = IContextNetwork::create(this, m_config.getMode(), m_dbusServer, m_dbusConnection);
times.insert("Network", time.restart()); times.insert("Network", time.restart());
// checks -------------- // checks --------------

View File

@@ -5,48 +5,7 @@
namespace swift::core namespace swift::core
{ {
bool CCoreFacadeConfig::requiresDBusSever() const bool CCoreFacadeConfig::requiresDBusSever() const { return m_contextMode == LocalInDBusServer; }
{
return (
// those 3 should decide whether we are running the server
m_network == LocalInDBusServer || m_ownAircraft == LocalInDBusServer || m_simulator == LocalInDBusServer ||
// added as work around bool CCoreFacadeConfig::requiresDBusConnection() const { return m_contextMode == Remote; }
m_audio == LocalInDBusServer);
}
bool CCoreFacadeConfig::requiresDBusConnection() const { return this->any(Remote); }
bool CCoreFacadeConfig::any(CCoreFacadeConfig::ContextMode mode) const
{
return (m_application == mode || m_audio == mode || m_network == mode || m_ownAircraft == mode ||
m_simulator == mode);
}
bool CCoreFacadeConfig::all(CCoreFacadeConfig::ContextMode mode) const
{
return (m_application == mode && m_audio == mode && m_network == mode && m_ownAircraft == mode &&
m_simulator == mode);
}
bool CCoreFacadeConfig::anyRemote() const { return this->any(Remote); }
bool CCoreFacadeConfig::anyLocalInDBusServer() const { return this->any(LocalInDBusServer); }
CCoreFacadeConfig CCoreFacadeConfig::forCoreAllLocalInDBus(const QString &dbusBootstrapAddress)
{
return { LocalInDBusServer, dbusBootstrapAddress };
}
CCoreFacadeConfig CCoreFacadeConfig::local(const QString &dbusBootstrapAddress)
{
return { Local, dbusBootstrapAddress };
}
CCoreFacadeConfig CCoreFacadeConfig::remote(const QString &dbusBootstrapAddress)
{
return { Remote, dbusBootstrapAddress };
}
CCoreFacadeConfig CCoreFacadeConfig::allEmpty() { return {}; }
} // namespace swift::core } // namespace swift::core

View File

@@ -12,7 +12,7 @@
namespace swift::core namespace swift::core
{ {
//! Helper to correctly run a context //! Configuration object for the contexts
class SWIFT_CORE_EXPORT CCoreFacadeConfig class SWIFT_CORE_EXPORT CCoreFacadeConfig
{ {
public: public:
@@ -20,43 +20,24 @@ namespace swift::core
enum ContextMode enum ContextMode
{ {
NotUsed, //!< during shutdown or not used at all NotUsed, //!< during shutdown or not used at all
Local, //!< in same process Local, //!< context runs in same process
LocalInDBusServer, //!< in same process, also registered in DBus, will be accessed by proxy objects too LocalInDBusServer, //!< context runs in same process. It is also registered in DBus to be accessed by proxy
Remote //!< proxy object //!< objects
Remote //!< context runs in a different process. Access will happen via proxy object
}; };
private: private:
ContextMode m_application; ContextMode m_contextMode; // All contexts run in the same mode
ContextMode m_audio;
ContextMode m_network;
ContextMode m_ownAircraft;
ContextMode m_simulator;
QString m_dbusAddress; //!< for boot strapping QString m_dbusAddress; //!< for boot strapping
public: public:
//! Constructor //! Constructor
CCoreFacadeConfig(ContextMode allTheSame = NotUsed, const QString &dbusBootstrapAddress = "") explicit CCoreFacadeConfig(ContextMode contextMode, const QString &dbusBootstrapAddress = "")
: m_application(allTheSame), m_audio(allTheSame), m_network(allTheSame), m_ownAircraft(allTheSame), : m_contextMode(contextMode), m_dbusAddress(dbusBootstrapAddress)
m_simulator(allTheSame), m_dbusAddress(dbusBootstrapAddress)
{} {}
//! Application mode //! Mode
ContextMode getModeApplication() const { return m_application; } ContextMode getMode() const { return m_contextMode; }
//! Audio mode
ContextMode getModeAudio() const { return m_audio; }
//! Network mode
ContextMode getModeNetwork() const { return m_network; }
//! Own aircraft
ContextMode getModeOwnAircraft() const { return m_ownAircraft; }
//! Simulator mode
ContextMode getModeSimulator() const { return m_simulator; }
//! Local core?
bool hasLocalCore() const { return m_application == Local || m_application == LocalInDBusServer; }
//! Requires server (at least one in server)? //! Requires server (at least one in server)?
bool requiresDBusSever() const; bool requiresDBusSever() const;
@@ -69,30 +50,6 @@ namespace swift::core
//! DBus address? //! DBus address?
bool hasDBusAddress() const { return !m_dbusAddress.isEmpty(); } bool hasDBusAddress() const { return !m_dbusAddress.isEmpty(); }
//! Any context in given mode
bool any(ContextMode mode) const;
//! All contexts in given mode
bool all(ContextMode mode) const;
//! Any remote context?
bool anyRemote() const;
//! Any local in DBus context?
bool anyLocalInDBusServer() const;
//! Predefined for Core
static CCoreFacadeConfig forCoreAllLocalInDBus(const QString &dbusBootstrapAddress = "");
//! Predefined, completely local (e.g. for unit tests)
static CCoreFacadeConfig local(const QString &dbusBootstrapAddress = "");
//! Predefined, completely remote
static CCoreFacadeConfig remote(const QString &dbusBootstrapAddress = "");
//! Predefined, all empty configs (normally used when the real config can only be determined later)
static CCoreFacadeConfig allEmpty();
}; };
} // namespace swift::core } // namespace swift::core
#endif // SWIFT_CORE_COREFACADECONFIG_H #endif // SWIFT_CORE_COREFACADECONFIG_H

View File

@@ -33,7 +33,7 @@ int main(int argc, char *argv[])
if (!a.parseCommandLineArgsAndLoadSetup()) { return EXIT_FAILURE; } if (!a.parseCommandLineArgsAndLoadSetup()) { return EXIT_FAILURE; }
const QString dBusAddress(a.getCmdDBusAddressValue()); const QString dBusAddress(a.getCmdDBusAddressValue());
a.initContextsAndStartCoreFacade(CCoreFacadeConfig::forCoreAllLocalInDBus(dBusAddress)); a.initContextsAndStartCoreFacade(CCoreFacadeConfig(CCoreFacadeConfig::LocalInDBusServer, dBusAddress));
if (!a.start()) if (!a.start())
{ {
a.gracefulShutdown(); a.gracefulShutdown();

View File

@@ -62,14 +62,7 @@ CStatusMessageList CSwiftGuiStdApplication::startHookIn()
msgs.push_back(m); msgs.push_back(m);
} }
CCoreFacadeConfig runtimeConfig; CCoreFacadeConfig runtimeConfig = coreModeToCoreFacadeConfig(coreMode, dBusAddress);
switch (coreMode)
{
case CoreModes::Distributed: runtimeConfig = CCoreFacadeConfig::remote(dBusAddress); break;
default:
case CoreModes::Standalone: runtimeConfig = CCoreFacadeConfig::local(dBusAddress); break;
}
const CStatusMessageList contextMsgs = this->initContextsAndStartCoreFacade(runtimeConfig); const CStatusMessageList contextMsgs = this->initContextsAndStartCoreFacade(runtimeConfig);
msgs.push_back(contextMsgs); msgs.push_back(contextMsgs);
return contextMsgs; return contextMsgs;
@@ -95,3 +88,14 @@ CSwiftGuiStdApplication *CSwiftGuiStdApplication::instance()
{ {
return qobject_cast<CSwiftGuiStdApplication *>(CApplication::instance()); return qobject_cast<CSwiftGuiStdApplication *>(CApplication::instance());
} }
CCoreFacadeConfig CSwiftGuiStdApplication::coreModeToCoreFacadeConfig(CoreModes::CoreMode coreMode,
const QString &dBusAddress)
{
switch (coreMode)
{
case CoreModes::Distributed: return CCoreFacadeConfig(CCoreFacadeConfig::Remote, dBusAddress);
case CoreModes::Standalone: return CCoreFacadeConfig(CCoreFacadeConfig::Local, dBusAddress); break;
default: Q_ASSERT_X(false, Q_FUNC_INFO, "Not handled core mode");
}
}

View File

@@ -11,6 +11,7 @@
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include "core/coremodeenums.h"
#include "gui/guiapplication.h" #include "gui/guiapplication.h"
/*! /*!
@@ -36,6 +37,9 @@ protected:
virtual swift::misc::CStatusMessageList startHookIn() override; virtual swift::misc::CStatusMessageList startHookIn() override;
private: private:
static swift::core::CCoreFacadeConfig coreModeToCoreFacadeConfig(swift::core::CoreModes::CoreMode,
const QString &dBusAddress);
QCommandLineOption m_cmdFacadeMode { { "c", "core" }, QCommandLineOption m_cmdFacadeMode { { "c", "core" },
QCoreApplication::translate("main", "Core mode: (d)istributed, (s)tandalone."), QCoreApplication::translate("main", "Core mode: (d)istributed, (s)tandalone."),
"coremode" }; //!< Facade startup mode "coremode" }; //!< Facade startup mode