removal of slignal/slots

This commit is contained in:
csoler 2021-07-04 18:27:12 +02:00
parent b6156bff00
commit abf481b0a1
6 changed files with 122 additions and 61 deletions

View File

@ -36,6 +36,8 @@ enum class RsTorManagerEventCode: uint8_t
TOR_STATUS_CHANGED = 0x01, TOR_STATUS_CHANGED = 0x01,
BOOTSTRAP_STATUS_CHANGED = 0x02, BOOTSTRAP_STATUS_CHANGED = 0x02,
TOR_CONNECTIVITY_CHANGED = 0x03, TOR_CONNECTIVITY_CHANGED = 0x03,
TOR_MANAGER_ERROR = 0x04,
CONFIGURATION_NEEDED = 0x05,
}; };
// Status of the Tor hidden service setup/loaded by RS // Status of the Tor hidden service setup/loaded by RS
@ -75,6 +77,7 @@ struct RsTorManagerEvent: public RsEvent
RsTorConnectivityStatus mTorConnectivityStatus; RsTorConnectivityStatus mTorConnectivityStatus;
RsTorStatus mTorStatus; RsTorStatus mTorStatus;
std::string mErrorMessage;
///* @see RsEvent @see RsSerializable ///* @see RsEvent @see RsSerializable
void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) override 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(mTorManagerEventType);
RS_SERIAL_PROCESS(mTorConnectivityStatus); RS_SERIAL_PROCESS(mTorConnectivityStatus);
RS_SERIAL_PROCESS(mTorStatus); RS_SERIAL_PROCESS(mTorStatus);
RS_SERIAL_PROCESS(mErrorMessage);
} }
~RsTorManagerEvent() = default; ~RsTorManagerEvent() = default;

View File

@ -33,6 +33,8 @@
#include <syscall.h> #include <syscall.h>
#include <iostream> #include <iostream>
#include <QObject>
#include "TorManager.h" #include "TorManager.h"
#include "TorProcess.h" #include "TorProcess.h"
#include "TorControl.h" #include "TorControl.h"
@ -51,7 +53,7 @@ using namespace Tor;
namespace Tor namespace Tor
{ {
class TorManagerPrivate : public QObject class TorManagerPrivate : public QObject, public TorProcessClient
{ {
Q_OBJECT Q_OBJECT
@ -75,23 +77,24 @@ public:
void setError(const QString &errorMessage); 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: public slots:
void processStateChanged(int state);
void processErrorChanged(const QString &errorMessage);
void processLogMessage(const QString &message);
void controlStatusChanged(int status); void controlStatusChanged(int status);
void getConfFinished(); void getConfFinished();
}; };
} }
TorManager::TorManager(QObject *parent) TorManager::TorManager()
: QObject(parent), d(new TorManagerPrivate(this)) : d(new TorManagerPrivate(this))
{ {
} }
TorManagerPrivate::TorManagerPrivate(TorManager *parent) TorManagerPrivate::TorManagerPrivate(TorManager *parent)
: QObject(parent) : QObject(nullptr)
, q(parent) , q(parent)
, process(0) , process(0)
, control(new TorControl(this)) , control(new TorControl(this))
@ -105,7 +108,7 @@ TorManager *TorManager::instance()
{ {
static TorManager *p = 0; static TorManager *p = 0;
if (!p) if (!p)
p = new TorManager(qApp); p = new TorManager();
return p; return p;
} }
@ -293,7 +296,8 @@ bool TorManager::start()
{ {
if (!d->errorMessage.isEmpty()) { if (!d->errorMessage.isEmpty()) {
d->errorMessage.clear(); d->errorMessage.clear();
emit errorChanged();
//emit errorChanged(); // not needed because there's no error to handle
} }
SettingsObject settings(QStringLiteral("tor")); SettingsObject settings(QStringLiteral("tor"));
@ -341,11 +345,11 @@ bool TorManager::start()
} }
if (!d->process) { if (!d->process) {
d->process = new TorProcess(this); d->process = new TorProcess(d);
connect(d->process, SIGNAL(stateChanged(int)), d, SLOT(processStateChanged(int)));
connect(d->process, SIGNAL(errorMessageChanged(QString)), d, // QObject::connect(d->process, SIGNAL(stateChanged(int)), d, SLOT(processStateChanged(int)));
SLOT(processErrorChanged(QString))); // QObject::connect(d->process, SIGNAL(errorMessageChanged(QString)), d, SLOT(processErrorChanged(QString)));
connect(d->process, SIGNAL(logMessage(QString)), d, SLOT(processLogMessage(QString))); // QObject::connect(d->process, SIGNAL(logMessage(QString)), d, SLOT(processLogMessage(QString)));
} }
if (!QFile::exists(d->dataDir) && !d->createDataDir(d->dataDir)) { if (!QFile::exists(d->dataDir) && !d->createDataDir(d->dataDir)) {
@ -362,7 +366,14 @@ bool TorManager::start()
QFile torrc(d->dataDir + QStringLiteral("torrc")); QFile torrc(d->dataDir + QStringLiteral("torrc"));
if (!torrc.exists() || torrc.size() == 0) { if (!torrc.exists() || torrc.size() == 0) {
d->configNeeded = true; d->configNeeded = true;
emit configurationNeededChanged();
if(rsEvents)
{
auto ev = std::make_shared<RsTorManagerEvent>();
ev->mTorManagerEventType = RsTorManagerEventCode::CONFIGURATION_NEEDED;
rsEvents->sendEvent(ev);
}
//emit configurationNeededChanged();
} }
std::cerr << "Starting Tor process:" << std::endl; std::cerr << "Starting Tor process:" << std::endl;
@ -460,7 +471,14 @@ void TorManagerPrivate::getConfFinished()
if (command->get("DisableNetwork").toInt() == 1 && !configNeeded) { if (command->get("DisableNetwork").toInt() == 1 && !configNeeded) {
configNeeded = true; configNeeded = true;
emit q->configurationNeededChanged(); //emit q->configurationNeededChanged();
if(rsEvents)
{
auto ev = std::make_shared<RsTorManagerEvent>();
ev->mTorManagerEventType = RsTorManagerEventCode::CONFIGURATION_NEEDED;
rsEvents->sendEvent(ev);
}
} }
} }
@ -527,7 +545,16 @@ bool TorManagerPrivate::createDefaultTorrc(const QString &path)
void TorManagerPrivate::setError(const QString &message) void TorManagerPrivate::setError(const QString &message)
{ {
errorMessage = message; errorMessage = message;
emit q->errorChanged();
if(rsEvents)
{
auto ev = std::make_shared<RsTorManagerEvent>();
ev->mTorManagerEventType = RsTorManagerEventCode::TOR_MANAGER_ERROR;
ev->mErrorMessage = message.toStdString();
rsEvents->sendEvent(ev);
}
//emit q->errorChanged();
} }
#include "TorManager.moc" #include "TorManager.moc"

View File

@ -38,7 +38,6 @@
#include "retroshare/rstor.h" #include "retroshare/rstor.h"
#include "HiddenService.h" #include "HiddenService.h"
#include <QObject>
#include <QStringList> #include <QStringList>
#include <QHostAddress> #include <QHostAddress>
@ -52,17 +51,17 @@ class TorManagerPrivate;
/* Run/connect to an instance of Tor according to configuration, and manage /* Run/connect to an instance of Tor according to configuration, and manage
* UI interaction, first time configuration, etc. */ * 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(bool configurationNeeded READ configurationNeeded NOTIFY configurationNeededChanged)
Q_PROPERTY(QStringList logMessages READ logMessages CONSTANT) // Q_PROPERTY(QStringList logMessages READ logMessages CONSTANT)
Q_PROPERTY(Tor::TorProcess* process READ process CONSTANT) // Q_PROPERTY(Tor::TorProcess* process READ process CONSTANT)
Q_PROPERTY(Tor::TorControl* control READ control CONSTANT) // Q_PROPERTY(Tor::TorControl* control READ control CONSTANT)
Q_PROPERTY(bool hasError READ hasError NOTIFY errorChanged) // Q_PROPERTY(bool hasError READ hasError NOTIFY errorChanged)
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorChanged) // Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorChanged)
Q_PROPERTY(QString torDataDirectory READ torDataDirectory WRITE setTorDataDirectory) // Q_PROPERTY(QString torDataDirectory READ torDataDirectory WRITE setTorDataDirectory)
public: public:
static TorManager *instance(); static TorManager *instance();
@ -100,12 +99,12 @@ public:
virtual void hiddenServiceHostnameChanged() override; virtual void hiddenServiceHostnameChanged() override;
virtual void hiddenServiceStatusChanged(int old_status,int new_status) override; virtual void hiddenServiceStatusChanged(int old_status,int new_status) override;
signals: //signals:
void configurationNeededChanged(); // void configurationNeededChanged();
void errorChanged(); // void errorChanged();
private: private:
explicit TorManager(QObject *parent = 0); explicit TorManager();
TorManagerPrivate *d; TorManagerPrivate *d;
friend class RsTor; friend class RsTor;
}; };

View File

@ -39,8 +39,8 @@
using namespace Tor; using namespace Tor;
TorProcess::TorProcess(QObject *parent) TorProcess::TorProcess(TorProcessClient *client,QObject *parent)
: QObject(parent), d(new TorProcessPrivate(this)) : d(new TorProcessPrivate(this)),m_client(client)
{ {
} }
@ -51,7 +51,7 @@ TorProcess::~TorProcess()
} }
TorProcessPrivate::TorProcessPrivate(TorProcess *q) 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, &QProcess::started, this, &TorProcessPrivate::processStarted);
connect(&process, (void (QProcess::*)(int, QProcess::ExitStatus))&QProcess::finished, connect(&process, (void (QProcess::*)(int, QProcess::ExitStatus))&QProcess::finished,
@ -124,15 +124,16 @@ void TorProcess::start()
if (d->executable.isEmpty() || d->dataDir.isEmpty()) { if (d->executable.isEmpty() || d->dataDir.isEmpty()) {
d->errorMessage = QStringLiteral("Tor executable and data directory not specified"); d->errorMessage = QStringLiteral("Tor executable and data directory not specified");
d->state = Failed; 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; return;
} }
if (!d->ensureFilesExist()) { if (!d->ensureFilesExist()) {
d->state = Failed; d->state = Failed;
emit errorMessageChanged(d->errorMessage); if(m_client) m_client->processErrorChanged(d->errorMessage);// emit errorMessageChanged(d->errorMessage);
emit stateChanged(d->state); if(m_client) m_client->processStateChanged(d->state);// emit stateChanged(d->state);
return; return;
} }
@ -141,8 +142,8 @@ void TorProcess::start()
if (password.isEmpty() || hashedPassword.isEmpty()) { if (password.isEmpty() || hashedPassword.isEmpty()) {
d->errorMessage = QStringLiteral("Random password generation failed"); d->errorMessage = QStringLiteral("Random password generation failed");
d->state = Failed; d->state = Failed;
emit errorMessageChanged(d->errorMessage); if(m_client) m_client->processErrorChanged(d->errorMessage);// emit errorMessageChanged(d->errorMessage);
emit stateChanged(d->state); if(m_client) m_client->processStateChanged(d->state); // emit stateChanged(d->state);
} }
QStringList args; QStringList args;
@ -157,7 +158,8 @@ void TorProcess::start()
args << d->extraSettings; args << d->extraSettings;
d->state = Starting; d->state = Starting;
emit stateChanged(d->state);
if(m_client) m_client->processStateChanged(d->state);// emit stateChanged(d->state);
if (QFile::exists(d->controlPortFilePath())) if (QFile::exists(d->controlPortFilePath()))
QFile::remove(d->controlPortFilePath()); QFile::remove(d->controlPortFilePath());
@ -194,7 +196,23 @@ void TorProcess::stop()
} }
#endif #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() QByteArray TorProcess::controlPassword()
@ -246,7 +264,9 @@ QString TorProcessPrivate::controlPortFilePath() const
void TorProcessPrivate::processStarted() void TorProcessPrivate::processStarted()
{ {
state = TorProcess::Connecting; state = TorProcess::Connecting;
emit q->stateChanged(state);
/*emit*/ q->stateChanged(state);
/*emit*/ q->stateChanged(state);
controlPortAttempts = 0; controlPortAttempts = 0;
controlPortTimer.start(); controlPortTimer.start();
@ -262,8 +282,8 @@ void TorProcessPrivate::processFinished()
if (errorMessage.isEmpty()) if (errorMessage.isEmpty())
errorMessage = QStringLiteral("Process exited unexpectedly (code %1)").arg(process.exitCode()); errorMessage = QStringLiteral("Process exited unexpectedly (code %1)").arg(process.exitCode());
state = TorProcess::Failed; state = TorProcess::Failed;
emit q->errorMessageChanged(errorMessage); /*emit*/ q->errorMessageChanged(errorMessage);
emit q->stateChanged(state); /*emit*/ q->stateChanged(state);
} }
void TorProcessPrivate::processError(QProcess::ProcessError error) void TorProcessPrivate::processError(QProcess::ProcessError error)
@ -277,7 +297,7 @@ void TorProcessPrivate::processReadable()
while (process.bytesAvailable() > 0) { while (process.bytesAvailable() > 0) {
QByteArray line = process.readLine(2048).trimmed(); QByteArray line = process.readLine(2048).trimmed();
if (!line.isEmpty()) 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) { if (!controlHost.isNull() && controlPort > 0) {
controlPortTimer.stop(); controlPortTimer.stop();
state = TorProcess::Ready; state = TorProcess::Ready;
emit q->stateChanged(state); /*emit*/ q->stateChanged(state);
return; return;
} }
} }
@ -304,8 +324,8 @@ void TorProcessPrivate::tryReadControlPort()
if (++controlPortAttempts * controlPortTimer.interval() > 10000) { if (++controlPortAttempts * controlPortTimer.interval() > 10000) {
errorMessage = QStringLiteral("No control port available after launching process"); errorMessage = QStringLiteral("No control port available after launching process");
state = TorProcess::Failed; state = TorProcess::Failed;
emit q->errorMessageChanged(errorMessage); /*emit*/ q->errorMessageChanged(errorMessage);
emit q->stateChanged(state); /*emit*/ q->stateChanged(state);
} }
} }

View File

@ -41,15 +41,25 @@ namespace Tor
class TorProcessPrivate; 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 /* Launches and controls a Tor instance with behavior suitable for bundling
* an instance with the application. */ * an instance with the application. */
class TorProcess : public QObject class TorProcess
{ {
Q_OBJECT //Q_OBJECT
Q_ENUMS(State) //Q_ENUMS(State)
Q_PROPERTY(State state READ state NOTIFY stateChanged) //Q_PROPERTY(State state READ state NOTIFY stateChanged)
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged) //Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
public: public:
enum State { enum State {
@ -60,7 +70,7 @@ public:
Ready Ready
}; };
explicit TorProcess(QObject *parent = 0); explicit TorProcess(TorProcessClient *client,QObject *parent = 0);
virtual ~TorProcess(); virtual ~TorProcess();
QString executable() const; QString executable() const;
@ -81,17 +91,18 @@ public:
quint16 controlPort(); quint16 controlPort();
QByteArray controlPassword(); QByteArray controlPassword();
public slots: //signals:
void start();
void stop();
signals:
void stateChanged(int newState); void stateChanged(int newState);
void errorMessageChanged(const QString &errorMessage); void errorMessageChanged(const QString &errorMessage);
void logMessage(const QString &message); void logMessage(const QString &message);
//public slots:
void start();
void stop();
private: private:
TorProcessPrivate *d; TorProcessPrivate *d;
TorProcessClient *m_client;
}; };
} }