From 90cab1dfda009c70e6189549cfbbb319f47e3a45 Mon Sep 17 00:00:00 2001 From: f4exb Date: Sun, 23 Oct 2022 20:33:51 +0200 Subject: [PATCH] FileInput: Make settings assignments atomic. Part of #1329 --- plugins/samplesource/fileinput/fileinput.cpp | 53 +++++++++---------- plugins/samplesource/fileinput/fileinput.h | 14 ++--- .../samplesource/fileinput/fileinputgui.cpp | 12 +++-- plugins/samplesource/fileinput/fileinputgui.h | 1 + .../fileinput/fileinputsettings.cpp | 51 +++++++++++++++++- .../fileinput/fileinputsettings.h | 2 + 6 files changed, 94 insertions(+), 39 deletions(-) diff --git a/plugins/samplesource/fileinput/fileinput.cpp b/plugins/samplesource/fileinput/fileinput.cpp index d3a362874..503bd9100 100644 --- a/plugins/samplesource/fileinput/fileinput.cpp +++ b/plugins/samplesource/fileinput/fileinput.cpp @@ -325,12 +325,12 @@ bool FileInput::deserialize(const QByteArray& data) success = false; } - MsgConfigureFileInput* message = MsgConfigureFileInput::create(m_settings, true); + MsgConfigureFileInput* message = MsgConfigureFileInput::create(m_settings, QList(), true); m_inputMessageQueue.push(message); if (getMessageQueueToGUI()) { - MsgConfigureFileInput* messageToGUI = MsgConfigureFileInput::create(m_settings, true); + MsgConfigureFileInput* messageToGUI = MsgConfigureFileInput::create(m_settings, QList(), true); getMessageQueueToGUI()->push(messageToGUI); } @@ -357,12 +357,12 @@ void FileInput::setCenterFrequency(qint64 centerFrequency) FileInputSettings settings = m_settings; m_centerFrequency = centerFrequency; - MsgConfigureFileInput* message = MsgConfigureFileInput::create(m_settings, false); + MsgConfigureFileInput* message = MsgConfigureFileInput::create(m_settings, QList{"centerFrequency"}, false); m_inputMessageQueue.push(message); if (getMessageQueueToGUI()) { - MsgConfigureFileInput* messageToGUI = MsgConfigureFileInput::create(m_settings, false); + MsgConfigureFileInput* messageToGUI = MsgConfigureFileInput::create(m_settings, QList{"centerFrequency"}, false); getMessageQueueToGUI()->push(messageToGUI); } } @@ -377,8 +377,7 @@ bool FileInput::handleMessage(const Message& message) if (MsgConfigureFileInput::match(message)) { MsgConfigureFileInput& conf = (MsgConfigureFileInput&) message; - FileInputSettings settings = conf.getSettings(); - applySettings(settings); + applySettings(conf.getSettings(), conf.getSettingsKeys(), conf.getForce()); return true; } else if (MsgConfigureFileSourceName::match(message)) @@ -482,14 +481,12 @@ bool FileInput::handleMessage(const Message& message) } } -bool FileInput::applySettings(const FileInputSettings& settings, bool force) +bool FileInput::applySettings(const FileInputSettings& settings, const QList& settingsKeys, bool force) { - QList reverseAPIKeys; + qDebug() << "FileInput::applySettings: force: " << force << settings.getDebugString(settingsKeys, force); - if ((m_settings.m_accelerationFactor != settings.m_accelerationFactor) || force) + if (settingsKeys.contains("accelerationFactor") || force) { - reverseAPIKeys.append("accelerationFactor"); - if (m_fileInputWorker) { QMutexLocker mutexLocker(&m_mutex); @@ -501,27 +498,25 @@ bool FileInput::applySettings(const FileInputSettings& settings, bool force) } } - if ((m_settings.m_loop != settings.m_loop)) { - reverseAPIKeys.append("loop"); - } - if ((m_settings.m_fileName != settings.m_fileName)) { - reverseAPIKeys.append("fileName"); - } - - if (settings.m_useReverseAPI) + if (settingsKeys.contains("useReverseAPI")) { - bool fullUpdate = ((m_settings.m_useReverseAPI != settings.m_useReverseAPI) && settings.m_useReverseAPI) || - (m_settings.m_reverseAPIAddress != settings.m_reverseAPIAddress) || - (m_settings.m_reverseAPIPort != settings.m_reverseAPIPort) || - (m_settings.m_reverseAPIDeviceIndex != settings.m_reverseAPIDeviceIndex); - webapiReverseSendSettings(reverseAPIKeys, settings, fullUpdate || force); + bool fullUpdate = (settingsKeys.contains("useReverseAPI") && settings.m_useReverseAPI) || + settingsKeys.contains("reverseAPIAddress") || + settingsKeys.contains("reverseAPIPort") || + settingsKeys.contains("reverseAPIDeviceIndex"); + webapiReverseSendSettings(settingsKeys, settings, fullUpdate || force); } - m_settings = settings; + if (force) { + m_settings = settings; + } else { + m_settings.applySettings(settingsKeys, settings); + } // Open the file if there isn't a GUI which will open it - if ((m_guiMessageQueue == nullptr) && reverseAPIKeys.contains("fileName") && !m_settings.m_fileName.isEmpty()) + if ((m_guiMessageQueue == nullptr) && settingsKeys.contains("fileName") && !m_settings.m_fileName.isEmpty()) { openFileStream(); + } return true; } @@ -547,12 +542,12 @@ int FileInput::webapiSettingsPutPatch( FileInputSettings settings = m_settings; webapiUpdateDeviceSettings(settings, deviceSettingsKeys, response); - MsgConfigureFileInput *msg = MsgConfigureFileInput::create(settings, force); + MsgConfigureFileInput *msg = MsgConfigureFileInput::create(settings, deviceSettingsKeys, force); m_inputMessageQueue.push(msg); if (m_guiMessageQueue) // forward to GUI if any { - MsgConfigureFileInput *msgToGUI = MsgConfigureFileInput::create(settings, force); + MsgConfigureFileInput *msgToGUI = MsgConfigureFileInput::create(settings, deviceSettingsKeys, force); m_guiMessageQueue->push(msgToGUI); } @@ -681,7 +676,7 @@ void FileInput::webapiFormatDeviceReport(SWGSDRangel::SWGDeviceReport& response) response.getFileInputReport()->setSampleSize(m_sampleSize); } -void FileInput::webapiReverseSendSettings(QList& deviceSettingsKeys, const FileInputSettings& settings, bool force) +void FileInput::webapiReverseSendSettings(const QList& deviceSettingsKeys, const FileInputSettings& settings, bool force) { SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = new SWGSDRangel::SWGDeviceSettings(); swgDeviceSettings->setDirection(0); // single Rx diff --git a/plugins/samplesource/fileinput/fileinput.h b/plugins/samplesource/fileinput/fileinput.h index 2d9e90b0a..08e3e9c81 100644 --- a/plugins/samplesource/fileinput/fileinput.h +++ b/plugins/samplesource/fileinput/fileinput.h @@ -45,20 +45,22 @@ public: public: const FileInputSettings& getSettings() const { return m_settings; } + const QList& getSettingsKeys() const { return m_settingsKeys; } bool getForce() const { return m_force; } - static MsgConfigureFileInput* create(const FileInputSettings& settings, bool force) - { - return new MsgConfigureFileInput(settings, force); + static MsgConfigureFileInput* create(const FileInputSettings& settings, const QList& settingsKeys, bool force) { + return new MsgConfigureFileInput(settings, settingsKeys, force); } private: FileInputSettings m_settings; + QList m_settingsKeys; bool m_force; - MsgConfigureFileInput(const FileInputSettings& settings, bool force) : + MsgConfigureFileInput(const FileInputSettings& settings, const QList& settingsKeys, bool force) : Message(), m_settings(settings), + m_settingsKeys(settingsKeys), m_force(force) { } }; @@ -349,9 +351,9 @@ public: void stopWorker(); void openFileStream(); void seekFileStream(int seekMillis); - bool applySettings(const FileInputSettings& settings, bool force = false); + bool applySettings(const FileInputSettings& settings, const QList& settingsKeys, bool force = false); void webapiFormatDeviceReport(SWGSDRangel::SWGDeviceReport& response); - void webapiReverseSendSettings(QList& deviceSettingsKeys, const FileInputSettings& settings, bool force); + void webapiReverseSendSettings(const QList& deviceSettingsKeys, const FileInputSettings& settings, bool force); void webapiReverseSendStartStop(bool start); bool handleMessage(const Message& message); diff --git a/plugins/samplesource/fileinput/fileinputgui.cpp b/plugins/samplesource/fileinput/fileinputgui.cpp index 9b1a891b8..1b8bdd33c 100644 --- a/plugins/samplesource/fileinput/fileinputgui.cpp +++ b/plugins/samplesource/fileinput/fileinputgui.cpp @@ -156,7 +156,13 @@ bool FileInputGUI::handleMessage(const Message& message) if (FileInput::MsgConfigureFileInput::match(message)) { const FileInput::MsgConfigureFileInput& cfg = (FileInput::MsgConfigureFileInput&) message; - m_settings = cfg.getSettings(); + + if (cfg.getForce()) { + m_settings = cfg.getSettings(); + } else { + m_settings.applySettings(cfg.getSettingsKeys(), cfg.getSettings()); + } + displaySettings(); return true; } @@ -249,7 +255,7 @@ void FileInputGUI::on_playLoop_toggled(bool checked) if (m_doApplySettings) { m_settings.m_loop = checked; - FileInput::MsgConfigureFileInput *message = FileInput::MsgConfigureFileInput::create(m_settings, false); + FileInput::MsgConfigureFileInput *message = FileInput::MsgConfigureFileInput::create(m_settings, QList{"loop"}, false); m_sampleSource->getInputMessageQueue()->push(message); } } @@ -330,7 +336,7 @@ void FileInputGUI::on_acceleration_currentIndexChanged(int index) if (m_doApplySettings) { m_settings.m_accelerationFactor = FileInputSettings::getAccelerationValue(index); - FileInput::MsgConfigureFileInput *message = FileInput::MsgConfigureFileInput::create(m_settings, false); + FileInput::MsgConfigureFileInput *message = FileInput::MsgConfigureFileInput::create(m_settings, QList{"accelerationFactor"}, false); m_sampleSource->getInputMessageQueue()->push(message); } } diff --git a/plugins/samplesource/fileinput/fileinputgui.h b/plugins/samplesource/fileinput/fileinputgui.h index 561c8f34d..8c5be3ec3 100644 --- a/plugins/samplesource/fileinput/fileinputgui.h +++ b/plugins/samplesource/fileinput/fileinputgui.h @@ -54,6 +54,7 @@ private: Ui::FileInputGUI* ui; FileInputSettings m_settings; + QList m_settingsKeys; bool m_doApplySettings; QTimer m_statusTimer; std::vector m_gains; diff --git a/plugins/samplesource/fileinput/fileinputsettings.cpp b/plugins/samplesource/fileinput/fileinputsettings.cpp index c63128502..f87270b58 100644 --- a/plugins/samplesource/fileinput/fileinputsettings.cpp +++ b/plugins/samplesource/fileinput/fileinputsettings.cpp @@ -143,7 +143,56 @@ int FileInputSettings::getAccelerationValue(int accelerationIndex) return x * m; } +void FileInputSettings::applySettings(const QStringList& settingsKeys, const FileInputSettings& settings) +{ + if (settingsKeys.contains("fileName")) { + m_fileName = settings.m_fileName; + } + if (settingsKeys.contains("accelerationFactor")) { + m_accelerationFactor = settings.m_accelerationFactor; + } + if (settingsKeys.contains("loop")) { + m_loop = settings.m_loop; + } + if (settingsKeys.contains("useReverseAPI")) { + m_useReverseAPI = settings.m_useReverseAPI; + } + if (settingsKeys.contains("reverseAPIAddress")) { + m_reverseAPIAddress = settings.m_reverseAPIAddress; + } + if (settingsKeys.contains("reverseAPIPort")) { + m_reverseAPIPort = settings.m_reverseAPIPort; + } + if (settingsKeys.contains("reverseAPIDeviceIndex")) { + m_reverseAPIDeviceIndex = settings.m_reverseAPIDeviceIndex; + } +} +QString FileInputSettings::getDebugString(const QStringList& settingsKeys, bool force) const +{ + std::ostringstream ostr; + if (settingsKeys.contains("m_fileName") || force) { + ostr << " m_fileName: " << m_fileName.toStdString(); + } + if (settingsKeys.contains("accelerationFactor") || force) { + ostr << " m_accelerationFactor: " << m_accelerationFactor; + } + if (settingsKeys.contains("loop") || force) { + ostr << " m_loop: " << m_loop; + } + if (settingsKeys.contains("useReverseAPI") || force) { + ostr << " m_useReverseAPI: " << m_useReverseAPI; + } + if (settingsKeys.contains("reverseAPIAddress") || force) { + ostr << " m_reverseAPIAddress: " << m_reverseAPIAddress.toStdString(); + } + if (settingsKeys.contains("reverseAPIPort") || force) { + ostr << " m_reverseAPIPort: " << m_reverseAPIPort; + } + if (settingsKeys.contains("reverseAPIDeviceIndex") || force) { + ostr << " m_reverseAPIDeviceIndex: " << m_reverseAPIDeviceIndex; + } - + return QString(ostr.str().c_str()); +} diff --git a/plugins/samplesource/fileinput/fileinputsettings.h b/plugins/samplesource/fileinput/fileinputsettings.h index d007000e5..d2ee05061 100644 --- a/plugins/samplesource/fileinput/fileinputsettings.h +++ b/plugins/samplesource/fileinput/fileinputsettings.h @@ -38,6 +38,8 @@ struct FileInputSettings { void resetToDefaults(); QByteArray serialize() const; bool deserialize(const QByteArray& data); + void applySettings(const QStringList& settingsKeys, const FileInputSettings& settings); + QString getDebugString(const QStringList& settingsKeys, bool force=false) const; static int getAccelerationIndex(int averaging); static int getAccelerationValue(int averagingIndex); };