i2pd/qt/i2pd_qt/DaemonQT.cpp

181 lines
4.4 KiB
C++
Raw Normal View History

#include "DaemonQT.h"
#include "Daemon.h"
2016-06-17 15:25:28 +00:00
#include "mainwindow.h"
#include <QMessageBox>
#include <QApplication>
#include <QMutexLocker>
2016-06-17 15:25:28 +00:00
#include <QThread>
2016-06-15 16:20:31 +00:00
namespace i2p
{
2016-06-17 14:26:51 +00:00
namespace qt
2016-06-15 16:20:31 +00:00
{
2016-06-17 15:25:28 +00:00
Worker::Worker (DaemonQTImpl& daemon):
m_Daemon (daemon)
{
}
2016-06-15 18:43:29 +00:00
2016-06-17 15:25:28 +00:00
void Worker::startDaemon()
{
2016-06-17 14:26:51 +00:00
qDebug("Performing daemon start...");
2017-02-01 19:09:57 +00:00
//try{
m_Daemon.start();
qDebug("Daemon started.");
emit resultReady(false, "");
/*}catch(std::exception ex){
emit resultReady(true, ex.what());
}catch(...){
emit resultReady(true, QObject::tr("Error: unknown exception"));
}*/
2016-06-17 14:26:51 +00:00
}
2016-06-17 15:25:28 +00:00
void Worker::restartDaemon()
{
2016-06-17 14:26:51 +00:00
qDebug("Performing daemon restart...");
2017-02-01 19:09:57 +00:00
//try{
m_Daemon.restart();
qDebug("Daemon restarted.");
emit resultReady(false, "");
/*}catch(std::exception ex){
emit resultReady(true, ex.what());
}catch(...){
emit resultReady(true, QObject::tr("Error: unknown exception"));
}*/
}
2016-06-17 14:26:51 +00:00
void Worker::stopDaemon() {
qDebug("Performing daemon stop...");
2017-02-01 19:09:57 +00:00
//try{
m_Daemon.stop();
qDebug("Daemon stopped.");
emit resultReady(false, "");
/*}catch(std::exception ex){
emit resultReady(true, ex.what());
}catch(...){
emit resultReady(true, QObject::tr("Error: unknown exception"));
}*/
}
2016-06-15 18:43:29 +00:00
2016-06-17 15:25:28 +00:00
Controller::Controller(DaemonQTImpl& daemon):
m_Daemon (daemon)
{
Worker *worker = new Worker (m_Daemon);
2016-06-17 14:26:51 +00:00
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();
}
2016-06-17 15:25:28 +00:00
Controller::~Controller()
{
2016-06-17 14:26:51 +00:00
qDebug("Closing and waiting for daemon worker thread...");
workerThread.quit();
workerThread.wait();
qDebug("Waiting for daemon worker thread finished.");
2016-06-17 15:25:28 +00:00
if(m_Daemon.isRunning())
{
2016-06-17 14:26:51 +00:00
qDebug("Stopping the daemon...");
2016-06-17 15:25:28 +00:00
m_Daemon.stop();
2016-06-17 14:26:51 +00:00
qDebug("Stopped the daemon.");
}
}
2016-06-17 15:25:28 +00:00
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);
}
2016-06-15 18:43:29 +00:00
2016-06-17 15:25:28 +00:00
void DaemonQTImpl::start()
{
QMutexLocker locker(mutex);
setRunning(true);
Daemon.start();
}
2016-06-15 18:43:29 +00:00
2016-06-17 15:25:28 +00:00
void DaemonQTImpl::stop()
{
QMutexLocker locker(mutex);
Daemon.stop();
setRunning(false);
}
2016-06-15 18:43:29 +00:00
2016-06-17 15:25:28 +00:00
void DaemonQTImpl::restart()
{
QMutexLocker locker(mutex);
stop();
start();
}
2016-06-15 16:20:31 +00:00
2016-06-17 15:25:28 +00:00
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();
2016-06-15 18:43:29 +00:00
}
2016-06-17 15:25:28 +00:00
}
2016-06-17 14:26:51 +00:00
2016-06-17 15:25:28 +00:00
int RunQT (int argc, char* argv[])
{
QApplication app(argc, argv);
int result;
{
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 ();
{
i2p::qt::Controller daemonQtController(daemon);
2017-07-28 03:59:45 +00:00
w.setI2PController(&daemonQtController);
qDebug("Starting the daemon...");
emit daemonQtController.startDaemon();
//daemon.start ();
qDebug("Starting GUI event loop...");
result = app.exec();
//daemon.stop ();
}
}
2016-06-17 15:25:28 +00:00
//QMessageBox::information(&w, "Debug", "demon stopped");
qDebug("Exiting the application");
return result;
}
2016-06-15 16:20:31 +00:00
}
}
2016-06-17 14:26:51 +00:00