mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-02 23:34:14 +00:00
eliminated DaemonQTImpl singleton
This commit is contained in:
parent
f76c04b7a6
commit
4fc80fd366
57
i2pd.cpp
57
i2pd.cpp
@ -1,46 +1,23 @@
|
|||||||
#ifndef ANDROID
|
#include <stdlib.h>
|
||||||
# include <stdlib.h>
|
#include "Daemon.h"
|
||||||
# include "Daemon.h"
|
|
||||||
#else
|
|
||||||
# include "qt/i2pd_qt/i2pd_qt_gui.h"
|
|
||||||
# include <QMessageBox>
|
|
||||||
# include <QApplication>
|
|
||||||
# include "DaemonQT.h"
|
|
||||||
# include "mainwindow.h"
|
|
||||||
# include <QThread>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
#if defined(QT_GUI_LIB)
|
||||||
|
|
||||||
|
namespace i2p
|
||||||
|
{
|
||||||
|
namespace qt
|
||||||
|
{
|
||||||
|
int RunQT (int argc, char* argv[]);
|
||||||
|
}
|
||||||
|
}
|
||||||
int main( int argc, char* argv[] )
|
int main( int argc, char* argv[] )
|
||||||
{
|
{
|
||||||
#ifdef ANDROID
|
return i2p::qt::RunQT (argc, argv);
|
||||||
//int result = runGUI(argc, argv);
|
}
|
||||||
//QMessageBox::information(0,"Debug","runGUI completed");
|
|
||||||
QApplication app(argc, argv);
|
|
||||||
qDebug("Initialising the daemon...");
|
|
||||||
bool daemonInitSuccess = i2p::qt::DaemonQTImpl::init(argc, argv);
|
|
||||||
if(!daemonInitSuccess) {
|
|
||||||
QMessageBox::critical(0, "Error", "Daemon init failed");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
qDebug("Initialised, creating the main window...");
|
|
||||||
MainWindow w;
|
|
||||||
qDebug("Before main window.show()...");
|
|
||||||
w.show ();
|
|
||||||
int result;
|
|
||||||
{
|
|
||||||
i2p::qt::Controller daemonQtController;
|
|
||||||
qDebug("Starting the daemon...");
|
|
||||||
emit daemonQtController.startDaemon();
|
|
||||||
qDebug("Starting gui event loop...");
|
|
||||||
result = app.exec();
|
|
||||||
//QMessageBox::information(&w, "Debug", "exec finished");
|
|
||||||
}
|
|
||||||
i2p::qt::DaemonQTImpl::deinit();
|
|
||||||
//QMessageBox::information(&w, "Debug", "demon stopped");
|
|
||||||
//exit(result); //return from main() causes intermittent sigsegv bugs in some Androids. exit() is a workaround for this
|
|
||||||
qDebug("Exiting the application");
|
|
||||||
return result;
|
|
||||||
#else
|
#else
|
||||||
|
int main( int argc, char* argv[] )
|
||||||
|
{
|
||||||
if (Daemon.init(argc, argv))
|
if (Daemon.init(argc, argv))
|
||||||
{
|
{
|
||||||
if (Daemon.start())
|
if (Daemon.start())
|
||||||
@ -48,8 +25,8 @@ int main( int argc, char* argv[] )
|
|||||||
Daemon.stop();
|
Daemon.stop();
|
||||||
}
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
#include "DaemonQT.h"
|
#include "DaemonQT.h"
|
||||||
#include "../../Daemon.h"
|
#include "../../Daemon.h"
|
||||||
#include <QMutex>
|
#include "i2pd_qt_gui.h"
|
||||||
|
#include "mainwindow.h"
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QApplication>
|
||||||
#include <QMutexLocker>
|
#include <QMutexLocker>
|
||||||
|
#include <QThread>
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
@ -18,28 +22,36 @@ namespace i2p
|
|||||||
{
|
{
|
||||||
namespace qt
|
namespace qt
|
||||||
{
|
{
|
||||||
|
Worker::Worker (DaemonQTImpl& daemon):
|
||||||
|
m_Daemon (daemon)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void Worker::startDaemon() {
|
void Worker::startDaemon()
|
||||||
|
{
|
||||||
qDebug("Performing daemon start...");
|
qDebug("Performing daemon start...");
|
||||||
DaemonQTImpl::start();
|
m_Daemon.start();
|
||||||
qDebug("Daemon started.");
|
qDebug("Daemon started.");
|
||||||
emit resultReady();
|
emit resultReady();
|
||||||
}
|
}
|
||||||
void Worker::restartDaemon() {
|
void Worker::restartDaemon()
|
||||||
|
{
|
||||||
qDebug("Performing daemon restart...");
|
qDebug("Performing daemon restart...");
|
||||||
DaemonQTImpl::restart();
|
m_Daemon.restart();
|
||||||
qDebug("Daemon restarted.");
|
qDebug("Daemon restarted.");
|
||||||
emit resultReady();
|
emit resultReady();
|
||||||
}
|
}
|
||||||
void Worker::stopDaemon() {
|
void Worker::stopDaemon() {
|
||||||
qDebug("Performing daemon stop...");
|
qDebug("Performing daemon stop...");
|
||||||
DaemonQTImpl::stop();
|
m_Daemon.stop();
|
||||||
qDebug("Daemon stopped.");
|
qDebug("Daemon stopped.");
|
||||||
emit resultReady();
|
emit resultReady();
|
||||||
}
|
}
|
||||||
|
|
||||||
Controller::Controller() {
|
Controller::Controller(DaemonQTImpl& daemon):
|
||||||
Worker *worker = new Worker;
|
m_Daemon (daemon)
|
||||||
|
{
|
||||||
|
Worker *worker = new Worker (m_Daemon);
|
||||||
worker->moveToThread(&workerThread);
|
worker->moveToThread(&workerThread);
|
||||||
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
|
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
|
||||||
connect(this, &Controller::startDaemon, worker, &Worker::startDaemon);
|
connect(this, &Controller::startDaemon, worker, &Worker::startDaemon);
|
||||||
@ -48,40 +60,117 @@ namespace qt
|
|||||||
connect(worker, &Worker::resultReady, this, &Controller::handleResults);
|
connect(worker, &Worker::resultReady, this, &Controller::handleResults);
|
||||||
workerThread.start();
|
workerThread.start();
|
||||||
}
|
}
|
||||||
Controller::~Controller() {
|
Controller::~Controller()
|
||||||
|
{
|
||||||
qDebug("Closing and waiting for daemon worker thread...");
|
qDebug("Closing and waiting for daemon worker thread...");
|
||||||
workerThread.quit();
|
workerThread.quit();
|
||||||
workerThread.wait();
|
workerThread.wait();
|
||||||
qDebug("Waiting for daemon worker thread finished.");
|
qDebug("Waiting for daemon worker thread finished.");
|
||||||
if(DaemonQTImpl::isRunning()) {
|
if(m_Daemon.isRunning())
|
||||||
|
{
|
||||||
qDebug("Stopping the daemon...");
|
qDebug("Stopping the daemon...");
|
||||||
DaemonQTImpl::stop();
|
m_Daemon.stop();
|
||||||
qDebug("Stopped the daemon.");
|
qDebug("Stopped the daemon.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DaemonQTImpl::DaemonQTImpl ():
|
||||||
|
mutex(nullptr), m_IsRunning(nullptr), m_RunningChangedCallback(nullptr)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
DaemonQTImpl::~DaemonQTImpl ()
|
||||||
|
{
|
||||||
|
delete mutex;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DaemonQTImpl::init(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
mutex=new QMutex(QMutex::Recursive);
|
||||||
|
setRunningCallback(0);
|
||||||
|
m_IsRunning=false;
|
||||||
|
return Daemon.init(argc,argv);
|
||||||
|
|
||||||
static DaemonQTImpl::runningChangedCallback DaemonQTImpl_runningChanged;
|
}
|
||||||
static bool DaemonQTImpl_running;
|
void DaemonQTImpl::deinit()
|
||||||
static QMutex* mutex;
|
{
|
||||||
|
delete mutex; mutex = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
bool DaemonQTImpl::init(int argc, char* argv[]){mutex=new QMutex(QMutex::Recursive);setRunningCallback(0);DaemonQTImpl_running=false;return Daemon.init(argc,argv);}
|
void DaemonQTImpl::start()
|
||||||
void DaemonQTImpl::deinit(){delete mutex;}
|
{
|
||||||
void DaemonQTImpl::start(){QMutexLocker locker(mutex);setRunning(true);Daemon.start();}
|
QMutexLocker locker(mutex);
|
||||||
void DaemonQTImpl::stop(){QMutexLocker locker(mutex);Daemon.stop();setRunning(false);}
|
setRunning(true);
|
||||||
void DaemonQTImpl::restart(){QMutexLocker locker(mutex);stop();start();}
|
Daemon.start();
|
||||||
|
}
|
||||||
|
|
||||||
void DaemonQTImpl::setRunningCallback(runningChangedCallback cb){DaemonQTImpl_runningChanged=cb;}
|
void DaemonQTImpl::stop()
|
||||||
bool DaemonQTImpl::isRunning(){return DaemonQTImpl_running;}
|
{
|
||||||
void DaemonQTImpl::setRunning(bool newValue){
|
QMutexLocker locker(mutex);
|
||||||
bool oldValue = DaemonQTImpl_running;
|
Daemon.stop();
|
||||||
if(oldValue!=newValue) {
|
setRunning(false);
|
||||||
DaemonQTImpl_running = newValue;
|
}
|
||||||
if(DaemonQTImpl_runningChanged!=0)DaemonQTImpl_runningChanged();
|
|
||||||
|
void DaemonQTImpl::restart()
|
||||||
|
{
|
||||||
|
QMutexLocker locker(mutex);
|
||||||
|
stop();
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DaemonQTImpl::setRunningCallback(runningChangedCallback cb)
|
||||||
|
{
|
||||||
|
m_RunningChangedCallback = cb;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DaemonQTImpl::isRunning()
|
||||||
|
{
|
||||||
|
return m_IsRunning;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DaemonQTImpl::setRunning(bool newValue)
|
||||||
|
{
|
||||||
|
bool oldValue = m_IsRunning;
|
||||||
|
if(oldValue!=newValue)
|
||||||
|
{
|
||||||
|
m_IsRunning = newValue;
|
||||||
|
if(m_RunningChangedCallback)
|
||||||
|
m_RunningChangedCallback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int RunQT (int argc, char* argv[])
|
||||||
|
{
|
||||||
|
//int result = runGUI(argc, argv);
|
||||||
|
//QMessageBox::information(0,"Debug","runGUI completed");
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
DaemonQTImpl daemon;
|
||||||
|
qDebug("Initialising the daemon...");
|
||||||
|
bool daemonInitSuccess = daemon.init(argc, argv);
|
||||||
|
if(!daemonInitSuccess)
|
||||||
|
{
|
||||||
|
QMessageBox::critical(0, "Error", "Daemon init failed");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
qDebug("Initialised, creating the main window...");
|
||||||
|
MainWindow w;
|
||||||
|
qDebug("Before main window.show()...");
|
||||||
|
w.show ();
|
||||||
|
int result;
|
||||||
|
{
|
||||||
|
i2p::qt::Controller daemonQtController(daemon);
|
||||||
|
qDebug("Starting the daemon...");
|
||||||
|
emit daemonQtController.startDaemon();
|
||||||
|
qDebug("Starting gui event loop...");
|
||||||
|
result = app.exec();
|
||||||
|
//QMessageBox::information(&w, "Debug", "exec finished");
|
||||||
|
}
|
||||||
|
daemon.deinit();
|
||||||
|
//QMessageBox::information(&w, "Debug", "demon stopped");
|
||||||
|
//exit(result); //return from main() causes intermittent sigsegv bugs in some Androids. exit() is a workaround for this
|
||||||
|
qDebug("Exiting the application");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,27 +3,19 @@
|
|||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
#include <QMutex>
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
namespace qt
|
namespace qt
|
||||||
{
|
{
|
||||||
class Worker : public QObject
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
void startDaemon();
|
|
||||||
void restartDaemon();
|
|
||||||
void stopDaemon();
|
|
||||||
|
|
||||||
signals:
|
|
||||||
void resultReady();
|
|
||||||
};
|
|
||||||
|
|
||||||
class DaemonQTImpl
|
class DaemonQTImpl
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
DaemonQTImpl ();
|
||||||
|
~DaemonQTImpl ();
|
||||||
|
|
||||||
typedef void (*runningChangedCallback)();
|
typedef void (*runningChangedCallback)();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,24 +24,51 @@ namespace qt
|
|||||||
* @param argv
|
* @param argv
|
||||||
* @return success
|
* @return success
|
||||||
*/
|
*/
|
||||||
bool static init(int argc, char* argv[]);
|
bool init(int argc, char* argv[]);
|
||||||
void static deinit();
|
void deinit();
|
||||||
void static start();
|
void start();
|
||||||
void static stop();
|
void stop();
|
||||||
void static restart();
|
void restart();
|
||||||
void static setRunningCallback(runningChangedCallback cb);
|
void setRunningCallback(runningChangedCallback cb);
|
||||||
bool static isRunning();
|
bool isRunning();
|
||||||
private:
|
private:
|
||||||
void static setRunning(bool running);
|
void setRunning(bool running);
|
||||||
|
private:
|
||||||
|
QMutex* mutex;
|
||||||
|
bool m_IsRunning;
|
||||||
|
runningChangedCallback m_RunningChangedCallback;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Worker : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
|
||||||
|
Worker (DaemonQTImpl& daemon);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
DaemonQTImpl& m_Daemon;
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void startDaemon();
|
||||||
|
void restartDaemon();
|
||||||
|
void stopDaemon();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void resultReady();
|
||||||
|
};
|
||||||
|
|
||||||
class Controller : public QObject
|
class Controller : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
QThread workerThread;
|
QThread workerThread;
|
||||||
public:
|
public:
|
||||||
Controller();
|
Controller(DaemonQTImpl& daemon);
|
||||||
~Controller();
|
~Controller();
|
||||||
|
private:
|
||||||
|
DaemonQTImpl& m_Daemon;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void handleResults(){}
|
void handleResults(){}
|
||||||
signals:
|
signals:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user