From abf481b0a181b51df03bca46715d80d3ed1bcda1 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 4 Jul 2021 18:27:12 +0200 Subject: [PATCH] removal of slignal/slots --- libretroshare/src/libretroshare.pro | 2 +- libretroshare/src/retroshare/rstor.h | 4 ++ libretroshare/src/tor/TorManager.cpp | 61 ++++++++++++++++++++-------- libretroshare/src/tor/TorManager.h | 27 ++++++------ libretroshare/src/tor/TorProcess.cpp | 56 +++++++++++++++++-------- libretroshare/src/tor/TorProcess.h | 33 ++++++++++----- 6 files changed, 122 insertions(+), 61 deletions(-) diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 0d3889708..21161433a 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -448,7 +448,7 @@ HEADERS += rsitems/rsitem.h \ serialiser/rstlvkeyvalue.h \ serialiser/rstlvgenericparam.h \ serialiser/rstlvgenericmap.h \ - serialiser/rstlvgenericmap.inl \ + serialiser/rstlvgenericmap.inl \ serialiser/rstlvlist.h \ serialiser/rstlvmaps.h \ serialiser/rstlvbanlist.h \ diff --git a/libretroshare/src/retroshare/rstor.h b/libretroshare/src/retroshare/rstor.h index 7538671b1..dd4739304 100644 --- a/libretroshare/src/retroshare/rstor.h +++ b/libretroshare/src/retroshare/rstor.h @@ -36,6 +36,8 @@ enum class RsTorManagerEventCode: uint8_t TOR_STATUS_CHANGED = 0x01, BOOTSTRAP_STATUS_CHANGED = 0x02, TOR_CONNECTIVITY_CHANGED = 0x03, + TOR_MANAGER_ERROR = 0x04, + CONFIGURATION_NEEDED = 0x05, }; // Status of the Tor hidden service setup/loaded by RS @@ -75,6 +77,7 @@ struct RsTorManagerEvent: public RsEvent RsTorConnectivityStatus mTorConnectivityStatus; RsTorStatus mTorStatus; + std::string mErrorMessage; ///* @see RsEvent @see RsSerializable void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) override @@ -83,6 +86,7 @@ struct RsTorManagerEvent: public RsEvent RS_SERIAL_PROCESS(mTorManagerEventType); RS_SERIAL_PROCESS(mTorConnectivityStatus); RS_SERIAL_PROCESS(mTorStatus); + RS_SERIAL_PROCESS(mErrorMessage); } ~RsTorManagerEvent() = default; diff --git a/libretroshare/src/tor/TorManager.cpp b/libretroshare/src/tor/TorManager.cpp index 18cb963cd..9983d8c1f 100644 --- a/libretroshare/src/tor/TorManager.cpp +++ b/libretroshare/src/tor/TorManager.cpp @@ -33,6 +33,8 @@ #include #include +#include + #include "TorManager.h" #include "TorProcess.h" #include "TorControl.h" @@ -51,7 +53,7 @@ using namespace Tor; namespace Tor { -class TorManagerPrivate : public QObject +class TorManagerPrivate : public QObject, public TorProcessClient { Q_OBJECT @@ -75,23 +77,24 @@ public: void setError(const QString &errorMessage); + virtual void processStateChanged(int state) override; + virtual void processErrorChanged(const QString &errorMessage) override; + virtual void processLogMessage(const QString &message) override; + public slots: - void processStateChanged(int state); - void processErrorChanged(const QString &errorMessage); - void processLogMessage(const QString &message); void controlStatusChanged(int status); void getConfFinished(); }; } -TorManager::TorManager(QObject *parent) - : QObject(parent), d(new TorManagerPrivate(this)) +TorManager::TorManager() + : d(new TorManagerPrivate(this)) { } TorManagerPrivate::TorManagerPrivate(TorManager *parent) - : QObject(parent) + : QObject(nullptr) , q(parent) , process(0) , control(new TorControl(this)) @@ -105,7 +108,7 @@ TorManager *TorManager::instance() { static TorManager *p = 0; if (!p) - p = new TorManager(qApp); + p = new TorManager(); return p; } @@ -293,7 +296,8 @@ bool TorManager::start() { if (!d->errorMessage.isEmpty()) { d->errorMessage.clear(); - emit errorChanged(); + + //emit errorChanged(); // not needed because there's no error to handle } SettingsObject settings(QStringLiteral("tor")); @@ -341,11 +345,11 @@ bool TorManager::start() } if (!d->process) { - d->process = new TorProcess(this); - connect(d->process, SIGNAL(stateChanged(int)), d, SLOT(processStateChanged(int))); - connect(d->process, SIGNAL(errorMessageChanged(QString)), d, - SLOT(processErrorChanged(QString))); - connect(d->process, SIGNAL(logMessage(QString)), d, SLOT(processLogMessage(QString))); + d->process = new TorProcess(d); + + // QObject::connect(d->process, SIGNAL(stateChanged(int)), d, SLOT(processStateChanged(int))); + // QObject::connect(d->process, SIGNAL(errorMessageChanged(QString)), d, SLOT(processErrorChanged(QString))); + // QObject::connect(d->process, SIGNAL(logMessage(QString)), d, SLOT(processLogMessage(QString))); } if (!QFile::exists(d->dataDir) && !d->createDataDir(d->dataDir)) { @@ -362,7 +366,14 @@ bool TorManager::start() QFile torrc(d->dataDir + QStringLiteral("torrc")); if (!torrc.exists() || torrc.size() == 0) { d->configNeeded = true; - emit configurationNeededChanged(); + + if(rsEvents) + { + auto ev = std::make_shared(); + ev->mTorManagerEventType = RsTorManagerEventCode::CONFIGURATION_NEEDED; + rsEvents->sendEvent(ev); + } + //emit configurationNeededChanged(); } std::cerr << "Starting Tor process:" << std::endl; @@ -460,7 +471,14 @@ void TorManagerPrivate::getConfFinished() if (command->get("DisableNetwork").toInt() == 1 && !configNeeded) { configNeeded = true; - emit q->configurationNeededChanged(); + //emit q->configurationNeededChanged(); + + if(rsEvents) + { + auto ev = std::make_shared(); + ev->mTorManagerEventType = RsTorManagerEventCode::CONFIGURATION_NEEDED; + rsEvents->sendEvent(ev); + } } } @@ -527,7 +545,16 @@ bool TorManagerPrivate::createDefaultTorrc(const QString &path) void TorManagerPrivate::setError(const QString &message) { errorMessage = message; - emit q->errorChanged(); + + if(rsEvents) + { + auto ev = std::make_shared(); + + ev->mTorManagerEventType = RsTorManagerEventCode::TOR_MANAGER_ERROR; + ev->mErrorMessage = message.toStdString(); + rsEvents->sendEvent(ev); + } + //emit q->errorChanged(); } #include "TorManager.moc" diff --git a/libretroshare/src/tor/TorManager.h b/libretroshare/src/tor/TorManager.h index 3c9733519..e0a24ae15 100644 --- a/libretroshare/src/tor/TorManager.h +++ b/libretroshare/src/tor/TorManager.h @@ -38,7 +38,6 @@ #include "retroshare/rstor.h" #include "HiddenService.h" -#include #include #include @@ -52,17 +51,17 @@ class TorManagerPrivate; /* Run/connect to an instance of Tor according to configuration, and manage * UI interaction, first time configuration, etc. */ -class TorManager : public QObject, public HiddenServiceClient, public RsTor +class TorManager : public HiddenServiceClient, public RsTor { - Q_OBJECT + // Q_OBJECT - Q_PROPERTY(bool configurationNeeded READ configurationNeeded NOTIFY configurationNeededChanged) - Q_PROPERTY(QStringList logMessages READ logMessages CONSTANT) - Q_PROPERTY(Tor::TorProcess* process READ process CONSTANT) - Q_PROPERTY(Tor::TorControl* control READ control CONSTANT) - Q_PROPERTY(bool hasError READ hasError NOTIFY errorChanged) - Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorChanged) - Q_PROPERTY(QString torDataDirectory READ torDataDirectory WRITE setTorDataDirectory) + // Q_PROPERTY(bool configurationNeeded READ configurationNeeded NOTIFY configurationNeededChanged) + // Q_PROPERTY(QStringList logMessages READ logMessages CONSTANT) + // Q_PROPERTY(Tor::TorProcess* process READ process CONSTANT) + // Q_PROPERTY(Tor::TorControl* control READ control CONSTANT) + // Q_PROPERTY(bool hasError READ hasError NOTIFY errorChanged) + // Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorChanged) + // Q_PROPERTY(QString torDataDirectory READ torDataDirectory WRITE setTorDataDirectory) public: static TorManager *instance(); @@ -100,12 +99,12 @@ public: virtual void hiddenServiceHostnameChanged() override; virtual void hiddenServiceStatusChanged(int old_status,int new_status) override; -signals: - void configurationNeededChanged(); - void errorChanged(); +//signals: +// void configurationNeededChanged(); +// void errorChanged(); private: - explicit TorManager(QObject *parent = 0); + explicit TorManager(); TorManagerPrivate *d; friend class RsTor; }; diff --git a/libretroshare/src/tor/TorProcess.cpp b/libretroshare/src/tor/TorProcess.cpp index ca1aecfe5..1ad48db2d 100644 --- a/libretroshare/src/tor/TorProcess.cpp +++ b/libretroshare/src/tor/TorProcess.cpp @@ -39,8 +39,8 @@ using namespace Tor; -TorProcess::TorProcess(QObject *parent) - : QObject(parent), d(new TorProcessPrivate(this)) +TorProcess::TorProcess(TorProcessClient *client,QObject *parent) + : d(new TorProcessPrivate(this)),m_client(client) { } @@ -51,7 +51,7 @@ TorProcess::~TorProcess() } TorProcessPrivate::TorProcessPrivate(TorProcess *q) - : QObject(q), q(q), state(TorProcess::NotStarted), controlPort(0), controlPortAttempts(0) + : q(q), state(TorProcess::NotStarted), controlPort(0), controlPortAttempts(0) { connect(&process, &QProcess::started, this, &TorProcessPrivate::processStarted); connect(&process, (void (QProcess::*)(int, QProcess::ExitStatus))&QProcess::finished, @@ -124,15 +124,16 @@ void TorProcess::start() if (d->executable.isEmpty() || d->dataDir.isEmpty()) { d->errorMessage = QStringLiteral("Tor executable and data directory not specified"); d->state = Failed; - emit errorMessageChanged(d->errorMessage); - emit stateChanged(d->state); + + if(m_client) m_client->processStateChanged(d->state); // emit stateChanged(d->state); + if(m_client) m_client->processErrorChanged(d->errorMessage); // emit errorMessageChanged(d->errorMessage); return; } if (!d->ensureFilesExist()) { d->state = Failed; - emit errorMessageChanged(d->errorMessage); - emit stateChanged(d->state); + if(m_client) m_client->processErrorChanged(d->errorMessage);// emit errorMessageChanged(d->errorMessage); + if(m_client) m_client->processStateChanged(d->state);// emit stateChanged(d->state); return; } @@ -141,8 +142,8 @@ void TorProcess::start() if (password.isEmpty() || hashedPassword.isEmpty()) { d->errorMessage = QStringLiteral("Random password generation failed"); d->state = Failed; - emit errorMessageChanged(d->errorMessage); - emit stateChanged(d->state); + if(m_client) m_client->processErrorChanged(d->errorMessage);// emit errorMessageChanged(d->errorMessage); + if(m_client) m_client->processStateChanged(d->state); // emit stateChanged(d->state); } QStringList args; @@ -157,7 +158,8 @@ void TorProcess::start() args << d->extraSettings; d->state = Starting; - emit stateChanged(d->state); + + if(m_client) m_client->processStateChanged(d->state);// emit stateChanged(d->state); if (QFile::exists(d->controlPortFilePath())) QFile::remove(d->controlPortFilePath()); @@ -194,7 +196,23 @@ void TorProcess::stop() } #endif - emit stateChanged(d->state); + if(m_client) m_client->processStateChanged(d->state);// emit stateChanged(d->state); +} + +void TorProcess::stateChanged(int newState) +{ + if(m_client) + m_client->processStateChanged(newState); +} +void TorProcess::errorMessageChanged(const QString &errorMessage) +{ + if(m_client) + m_client->processErrorChanged(errorMessage); +} +void TorProcess::logMessage(const QString &message) +{ + if(m_client) + m_client->processLogMessage(message); } QByteArray TorProcess::controlPassword() @@ -246,7 +264,9 @@ QString TorProcessPrivate::controlPortFilePath() const void TorProcessPrivate::processStarted() { state = TorProcess::Connecting; - emit q->stateChanged(state); + + /*emit*/ q->stateChanged(state); + /*emit*/ q->stateChanged(state); controlPortAttempts = 0; controlPortTimer.start(); @@ -262,8 +282,8 @@ void TorProcessPrivate::processFinished() if (errorMessage.isEmpty()) errorMessage = QStringLiteral("Process exited unexpectedly (code %1)").arg(process.exitCode()); state = TorProcess::Failed; - emit q->errorMessageChanged(errorMessage); - emit q->stateChanged(state); + /*emit*/ q->errorMessageChanged(errorMessage); + /*emit*/ q->stateChanged(state); } void TorProcessPrivate::processError(QProcess::ProcessError error) @@ -277,7 +297,7 @@ void TorProcessPrivate::processReadable() while (process.bytesAvailable() > 0) { QByteArray line = process.readLine(2048).trimmed(); if (!line.isEmpty()) - emit q->logMessage(QString::fromLatin1(line)); + /*emit*/ q->logMessage(QString::fromLatin1(line)); } } @@ -295,7 +315,7 @@ void TorProcessPrivate::tryReadControlPort() if (!controlHost.isNull() && controlPort > 0) { controlPortTimer.stop(); state = TorProcess::Ready; - emit q->stateChanged(state); + /*emit*/ q->stateChanged(state); return; } } @@ -304,8 +324,8 @@ void TorProcessPrivate::tryReadControlPort() if (++controlPortAttempts * controlPortTimer.interval() > 10000) { errorMessage = QStringLiteral("No control port available after launching process"); state = TorProcess::Failed; - emit q->errorMessageChanged(errorMessage); - emit q->stateChanged(state); + /*emit*/ q->errorMessageChanged(errorMessage); + /*emit*/ q->stateChanged(state); } } diff --git a/libretroshare/src/tor/TorProcess.h b/libretroshare/src/tor/TorProcess.h index ad489dc43..9a2511f09 100644 --- a/libretroshare/src/tor/TorProcess.h +++ b/libretroshare/src/tor/TorProcess.h @@ -41,15 +41,25 @@ namespace Tor class TorProcessPrivate; +// This class is used to inherit calls from the TorProcess + +class TorProcessClient +{ +public: + virtual void processStateChanged(int) = 0; + virtual void processErrorChanged(const QString&) = 0; + virtual void processLogMessage(const QString&) = 0; +}; + /* Launches and controls a Tor instance with behavior suitable for bundling * an instance with the application. */ -class TorProcess : public QObject +class TorProcess { - Q_OBJECT - Q_ENUMS(State) + //Q_OBJECT + //Q_ENUMS(State) - Q_PROPERTY(State state READ state NOTIFY stateChanged) - Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged) + //Q_PROPERTY(State state READ state NOTIFY stateChanged) + //Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged) public: enum State { @@ -60,7 +70,7 @@ public: Ready }; - explicit TorProcess(QObject *parent = 0); + explicit TorProcess(TorProcessClient *client,QObject *parent = 0); virtual ~TorProcess(); QString executable() const; @@ -81,17 +91,18 @@ public: quint16 controlPort(); QByteArray controlPassword(); -public slots: - void start(); - void stop(); - -signals: +//signals: void stateChanged(int newState); void errorMessageChanged(const QString &errorMessage); void logMessage(const QString &message); +//public slots: + void start(); + void stop(); + private: TorProcessPrivate *d; + TorProcessClient *m_client; }; }