i2pd/qt/i2pd_qt/DaemonQT.cpp

166 lines
3.7 KiB
C++
Raw Normal View History

#include "DaemonQT.h"
#include "../../Daemon.h"
2016-06-17 18:25:28 +03:00
#include "mainwindow.h"
#include <QMessageBox>
#include <QApplication>
#include <QMutexLocker>
2016-06-17 18:25:28 +03:00
#include <QThread>
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-17 18:25:28 +03:00
Worker::Worker (DaemonQTImpl& daemon):
m_Daemon (daemon)
{
}
2016-06-15 21:43:29 +03:00
2016-06-17 18:25:28 +03:00
void Worker::startDaemon()
{
2016-06-17 17:26:51 +03:00
qDebug("Performing daemon start...");
2016-06-17 18:25:28 +03:00
m_Daemon.start();
2016-06-17 17:26:51 +03:00
qDebug("Daemon started.");
emit resultReady();
}
2016-06-17 18:25:28 +03:00
void Worker::restartDaemon()
{
2016-06-17 17:26:51 +03:00
qDebug("Performing daemon restart...");
2016-06-17 18:25:28 +03:00
m_Daemon.restart();
2016-06-17 17:26:51 +03:00
qDebug("Daemon restarted.");
emit resultReady();
}
void Worker::stopDaemon() {
qDebug("Performing daemon stop...");
2016-06-17 18:25:28 +03:00
m_Daemon.stop();
2016-06-17 17:26:51 +03:00
qDebug("Daemon stopped.");
emit resultReady();
}
2016-06-15 21:43:29 +03:00
2016-06-17 18:25:28 +03:00
Controller::Controller(DaemonQTImpl& daemon):
m_Daemon (daemon)
{
Worker *worker = new Worker (m_Daemon);
2016-06-17 17:26:51 +03: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 18:25:28 +03:00
Controller::~Controller()
{
2016-06-17 17:26:51 +03:00
qDebug("Closing and waiting for daemon worker thread...");
workerThread.quit();
workerThread.wait();
qDebug("Waiting for daemon worker thread finished.");
2016-06-17 18:25:28 +03:00
if(m_Daemon.isRunning())
{
2016-06-17 17:26:51 +03:00
qDebug("Stopping the daemon...");
2016-06-17 18:25:28 +03:00
m_Daemon.stop();
2016-06-17 17:26:51 +03:00
qDebug("Stopped the daemon.");
}
}
2016-06-17 18:25:28 +03: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 21:43:29 +03:00
2016-06-17 18:25:28 +03:00
}
void DaemonQTImpl::deinit()
{
delete mutex; mutex = nullptr;
}
2016-06-15 21:43:29 +03:00
2016-06-17 18:25:28 +03:00
void DaemonQTImpl::start()
{
QMutexLocker locker(mutex);
setRunning(true);
Daemon.start();
}
2016-06-15 21:43:29 +03:00
2016-06-17 18:25:28 +03:00
void DaemonQTImpl::stop()
{
QMutexLocker locker(mutex);
Daemon.stop();
setRunning(false);
}
2016-06-15 21:43:29 +03:00
2016-06-17 18:25:28 +03:00
void DaemonQTImpl::restart()
{
QMutexLocker locker(mutex);
stop();
start();
}
2016-06-15 19:20:31 +03:00
2016-06-17 18:25:28 +03: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 21:43:29 +03:00
}
2016-06-17 18:25:28 +03:00
}
2016-06-17 17:26:51 +03:00
2016-06-17 18:25:28 +03:00
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;
{
2016-06-17 23:37:08 +03:00
/* i2p::qt::Controller daemonQtController(daemon);
2016-06-17 18:25:28 +03:00
qDebug("Starting the daemon...");
emit daemonQtController.startDaemon();
2016-06-17 23:37:08 +03:00
qDebug("Starting gui event loop...");*/
daemon.start ();
2016-06-17 18:25:28 +03:00
result = app.exec();
2016-06-17 23:37:08 +03:00
daemon.stop ();
2016-06-17 18:25:28 +03:00
}
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;
}
2016-06-15 19:20:31 +03:00
}
}
2016-06-17 17:26:51 +03:00