i2pd/qt/i2pd_qt/DaemonQT.cpp

196 lines
4.9 KiB
C++
Raw Normal View History

2018-05-19 18:03:49 +03:00
#include <memory>
#include "DaemonQT.h"
#include "Daemon.h"
2016-06-17 18:25:28 +03:00
#include "mainwindow.h"
2018-05-19 18:03:49 +03:00
#include "Log.h"
2016-06-17 18:25:28 +03:00
#include <QMessageBox>
#include <QApplication>
#include <QMutexLocker>
2016-06-17 18:25:28 +03:00
#include <QThread>
2019-08-27 12:10:53 +03:00
//#define DEBUG_WITH_DEFAULT_LOGGING (1)
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
2018-01-06 06:48:51 +03:00
void Worker::startDaemon()
2016-06-17 18:25:28 +03:00
{
2016-06-17 17:26:51 +03:00
qDebug("Performing daemon start...");
2017-02-01 22:09:57 +03: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 17:26:51 +03:00
}
2018-01-06 06:48:51 +03:00
void Worker::restartDaemon()
2016-06-17 18:25:28 +03:00
{
2016-06-17 17:26:51 +03:00
qDebug("Performing daemon restart...");
2017-02-01 22:09:57 +03: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 17:26:51 +03:00
void Worker::stopDaemon() {
qDebug("Performing daemon stop...");
2017-02-01 22:09:57 +03: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 21:43:29 +03:00
2016-06-17 18:25:28 +03:00
Controller::Controller(DaemonQTImpl& daemon):
2018-01-06 06:48:51 +03:00
m_Daemon (daemon)
2016-06-17 18:25:28 +03:00
{
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();
}
2018-01-06 06:48:51 +03:00
Controller::~Controller()
2016-06-17 18:25:28 +03:00
{
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.");
}
}
2018-01-06 06:48:51 +03:00
2016-06-17 18:25:28 +03:00
DaemonQTImpl::DaemonQTImpl ():
mutex(nullptr), m_IsRunning(nullptr), m_RunningChangedCallback(nullptr)
{
}
DaemonQTImpl::~DaemonQTImpl ()
{
delete mutex;
}
2018-05-19 18:03:49 +03:00
bool DaemonQTImpl::init(int argc, char* argv[], std::shared_ptr<std::ostream> logstream)
2016-06-17 18:25:28 +03:00
{
mutex=new QMutex(QMutex::Recursive);
setRunningCallback(0);
m_IsRunning=false;
2018-05-19 18:03:49 +03:00
return Daemon.init(argc,argv,logstream);
2016-06-17 18:25:28 +03:00
}
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
2018-01-06 06:48:51 +03:00
void DaemonQTImpl::setRunningCallback(runningChangedCallback cb)
2016-06-17 18:25:28 +03:00
{
m_RunningChangedCallback = cb;
}
bool DaemonQTImpl::isRunning()
{
return m_IsRunning;
}
void DaemonQTImpl::setRunning(bool newValue)
{
bool oldValue = m_IsRunning;
2018-01-06 06:48:51 +03:00
if(oldValue!=newValue)
2016-06-17 18:25:28 +03:00
{
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[])
{
QApplication app(argc, argv);
int result;
{
2019-08-27 12:10:53 +03:00
std::shared_ptr<std::iostream> logstreamptr=
#ifdef DEBUG_WITH_DEFAULT_LOGGING
nullptr
#else
std::make_shared<std::stringstream>()
#endif
;
2018-05-19 18:03:49 +03:00
//TODO move daemon init deinit to a bg thread
DaemonQTImpl daemon;
2019-08-27 12:10:53 +03:00
if(logstreamptr) (*logstreamptr) << "Initialising the daemon..." << std::endl;
2018-05-19 18:03:49 +03:00
bool daemonInitSuccess = daemon.init(argc, argv, logstreamptr);
if(!daemonInitSuccess)
{
QMessageBox::critical(0, "Error", "Daemon init failed");
return 1;
}
2018-05-19 18:03:49 +03: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 06:59:45 +03:00
w.setI2PController(&daemonQtController);
2018-05-19 18:03:49 +03:00
LogPrint(eLogDebug, "Starting the daemon...");
emit daemonQtController.startDaemon();
//daemon.start ();
2018-05-19 18:03:49 +03:00
LogPrint(eLogDebug, "Starting GUI event loop...");
result = app.exec();
//daemon.stop ();
}
}
2016-06-17 18:25:28 +03:00
//QMessageBox::information(&w, "Debug", "demon stopped");
2018-05-19 18:03:49 +03:00
LogPrint(eLogDebug, "Exiting the application");
2016-06-17 18:25:28 +03:00
return result;
}
2016-06-15 19:20:31 +03:00
}
}
2016-06-17 17:26:51 +03:00