i2pd/qt/i2pd_qt/DaemonQT.cpp

88 lines
2.5 KiB
C++
Raw Normal View History

#include "DaemonQT.h"
#include "../../Daemon.h"
#include <QMutex>
#include <QMutexLocker>
namespace i2p
{
namespace util
{
2016-06-17 17:26:51 +03:00
bool DaemonQT::init(int argc, char* argv[])
{
return Daemon_Singleton::init(argc, argv);
}
}
}
2016-06-15 19:20:31 +03:00
namespace i2p
{
2016-06-17 17:26:51 +03:00
namespace qt
2016-06-15 19:20:31 +03:00
{
2016-06-15 21:43:29 +03:00
2016-06-17 17:26:51 +03:00
void Worker::startDaemon() {
qDebug("Performing daemon start...");
DaemonQTImpl::start();
qDebug("Daemon started.");
emit resultReady();
}
void Worker::restartDaemon() {
qDebug("Performing daemon restart...");
DaemonQTImpl::restart();
qDebug("Daemon restarted.");
emit resultReady();
}
void Worker::stopDaemon() {
qDebug("Performing daemon stop...");
DaemonQTImpl::stop();
qDebug("Daemon stopped.");
emit resultReady();
}
2016-06-15 21:43:29 +03:00
2016-06-17 17:26:51 +03:00
Controller::Controller() {
Worker *worker = new Worker;
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::startDaemon, worker, &Worker::startDaemon);
connect(this, &Controller::stopDaemon, worker, &Worker::stopDaemon);
connect(this, &Controller::restartDaemon, worker, &Worker::restartDaemon);
connect(worker, &Worker::resultReady, this, &Controller::handleResults);
workerThread.start();
}
Controller::~Controller() {
qDebug("Closing and waiting for daemon worker thread...");
workerThread.quit();
workerThread.wait();
qDebug("Waiting for daemon worker thread finished.");
if(DaemonQTImpl::isRunning()) {
qDebug("Stopping the daemon...");
DaemonQTImpl::stop();
qDebug("Stopped the daemon.");
}
}
2016-06-15 21:43:29 +03:00
2016-06-17 17:26:51 +03:00
static DaemonQTImpl::runningChangedCallback DaemonQTImpl_runningChanged;
static bool DaemonQTImpl_running;
static QMutex* mutex;
2016-06-15 21:43:29 +03:00
2016-06-17 17:26:51 +03:00
bool DaemonQTImpl::init(int argc, char* argv[]){mutex=new QMutex(QMutex::Recursive);setRunningCallback(0);DaemonQTImpl_running=false;return Daemon.init(argc,argv);}
void DaemonQTImpl::deinit(){delete mutex;}
void DaemonQTImpl::start(){QMutexLocker locker(mutex);setRunning(true);Daemon.start();}
void DaemonQTImpl::stop(){QMutexLocker locker(mutex);Daemon.stop();setRunning(false);}
void DaemonQTImpl::restart(){QMutexLocker locker(mutex);stop();start();}
2016-06-15 19:20:31 +03:00
2016-06-17 17:26:51 +03:00
void DaemonQTImpl::setRunningCallback(runningChangedCallback cb){DaemonQTImpl_runningChanged=cb;}
bool DaemonQTImpl::isRunning(){return DaemonQTImpl_running;}
void DaemonQTImpl::setRunning(bool newValue){
bool oldValue = DaemonQTImpl_running;
if(oldValue!=newValue) {
DaemonQTImpl_running = newValue;
if(DaemonQTImpl_runningChanged!=0)DaemonQTImpl_runningChanged();
2016-06-15 21:43:29 +03:00
}
2016-06-17 17:26:51 +03:00
}
2016-06-15 19:20:31 +03:00
}
}
2016-06-17 17:26:51 +03:00