i2pd/qt/i2pd_qt/DaemonQT.cpp

188 lines
4.8 KiB
C++
Raw Normal View History

2018-05-19 15:03:49 +00:00
#include <memory>
#include "DaemonQT.h"
#include "Daemon.h"
2016-06-17 15:25:28 +00:00
#include "mainwindow.h"
2018-05-19 15:03:49 +00:00
#include "Log.h"
2016-06-17 15:25:28 +00:00
#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
2018-01-06 03:48:51 +00:00
void Worker::startDaemon()
2016-06-17 15:25:28 +00:00
{
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
}
2018-01-06 03:48:51 +00:00
void Worker::restartDaemon()
2016-06-17 15:25:28 +00:00
{
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):
2018-01-06 03:48:51 +00:00
m_Daemon (daemon)
2016-06-17 15:25:28 +00:00
{
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();
}
2018-01-06 03:48:51 +00:00
Controller::~Controller()
2016-06-17 15:25:28 +00:00
{
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.");
}
}
2018-01-06 03:48:51 +00:00
2016-06-17 15:25:28 +00:00
DaemonQTImpl::DaemonQTImpl ():
mutex(nullptr), m_IsRunning(nullptr), m_RunningChangedCallback(nullptr)
{
}
DaemonQTImpl::~DaemonQTImpl ()
{
delete mutex;
}
2018-05-19 15:03:49 +00:00
bool DaemonQTImpl::init(int argc, char* argv[], std::shared_ptr<std::ostream> logstream)
2016-06-17 15:25:28 +00:00
{
mutex=new QMutex(QMutex::Recursive);
setRunningCallback(0);
m_IsRunning=false;
2018-05-19 15:03:49 +00:00
return Daemon.init(argc,argv,logstream);
2016-06-17 15:25:28 +00:00
}
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
2018-01-06 03:48:51 +00:00
void DaemonQTImpl::setRunningCallback(runningChangedCallback cb)
2016-06-17 15:25:28 +00:00
{
m_RunningChangedCallback = cb;
}
bool DaemonQTImpl::isRunning()
{
return m_IsRunning;
}
void DaemonQTImpl::setRunning(bool newValue)
{
bool oldValue = m_IsRunning;
2018-01-06 03:48:51 +00:00
if(oldValue!=newValue)
2016-06-17 15:25:28 +00:00
{
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;
{
2018-05-19 15:03:49 +00:00
std::shared_ptr<std::iostream> logstreamptr=std::make_shared<std::stringstream>();
//TODO move daemon init deinit to a bg thread
DaemonQTImpl daemon;
2018-05-19 15:03:49 +00:00
(*logstreamptr) << "Initialising the daemon..." << std::endl;
bool daemonInitSuccess = daemon.init(argc, argv, logstreamptr);
if(!daemonInitSuccess)
{
QMessageBox::critical(0, "Error", "Daemon init failed");
return 1;
}
2018-05-19 15:03:49 +00:00
LogPrint(eLogDebug, "Initialised, creating the main window...");
MainWindow w(logstreamptr);
LogPrint(eLogDebug, "Before main window.show()...");
w.show ();
{
i2p::qt::Controller daemonQtController(daemon);
2017-07-28 03:59:45 +00:00
w.setI2PController(&daemonQtController);
2018-05-19 15:03:49 +00:00
LogPrint(eLogDebug, "Starting the daemon...");
emit daemonQtController.startDaemon();
//daemon.start ();
2018-05-19 15:03:49 +00:00
LogPrint(eLogDebug, "Starting GUI event loop...");
result = app.exec();
//daemon.stop ();
}
}
2016-06-17 15:25:28 +00:00
//QMessageBox::information(&w, "Debug", "demon stopped");
2018-05-19 15:03:49 +00:00
LogPrint(eLogDebug, "Exiting the application");
2016-06-17 15:25:28 +00:00
return result;
}
2016-06-15 16:20:31 +00:00
}
}
2016-06-17 14:26:51 +00:00