mirror of https://github.com/PurpleI2P/i2pd.git
![i2porignal@yandex.ru](/assets/img/avatar_default.png)
![GitHub](/assets/img/avatar_default.png)
11 changed files with 370 additions and 83 deletions
@ -1,2 +1,3 @@
@@ -1,2 +1,3 @@
|
||||
/build-i2pd_qt-Android_armeabi_v7a_GCC_4_9_Qt_5_6_0-Debug/ |
||||
/build-i2pd_qt-Desktop_Qt_5_6_0_GCC_64bit-Debug/ |
||||
/build-i2pd_qt-Android_x86_GCC_4_9_Qt_5_6_0-Debug/ |
||||
|
@ -1,64 +1,165 @@
@@ -1,64 +1,165 @@
|
||||
#include <memory> |
||||
#include "DaemonQT.h" |
||||
#include "../../Daemon.h" |
||||
#include "mainwindow.h" |
||||
#include <QMessageBox> |
||||
#include <QApplication> |
||||
#include <stdlib.h> |
||||
#include "../../Daemon.h" |
||||
#include <QMutexLocker> |
||||
#include <QThread> |
||||
|
||||
namespace i2p |
||||
{ |
||||
namespace util |
||||
namespace qt |
||||
{ |
||||
class DaemonQTImpl: public std::enable_shared_from_this<DaemonQTImpl> |
||||
Worker::Worker (DaemonQTImpl& daemon): |
||||
m_Daemon (daemon) |
||||
{ |
||||
public: |
||||
} |
||||
|
||||
DaemonQTImpl (int argc, char* argv[]): |
||||
m_App (argc, argv) |
||||
{ |
||||
} |
||||
void Worker::startDaemon() |
||||
{ |
||||
qDebug("Performing daemon start..."); |
||||
m_Daemon.start(); |
||||
qDebug("Daemon started."); |
||||
emit resultReady(); |
||||
} |
||||
void Worker::restartDaemon() |
||||
{ |
||||
qDebug("Performing daemon restart..."); |
||||
m_Daemon.restart(); |
||||
qDebug("Daemon restarted."); |
||||
emit resultReady(); |
||||
} |
||||
void Worker::stopDaemon() { |
||||
qDebug("Performing daemon stop..."); |
||||
m_Daemon.stop(); |
||||
qDebug("Daemon stopped."); |
||||
emit resultReady(); |
||||
} |
||||
|
||||
void Run () |
||||
{ |
||||
MainWindow w; |
||||
w.show (); |
||||
m_App.exec(); |
||||
} |
||||
Controller::Controller(DaemonQTImpl& daemon): |
||||
m_Daemon (daemon) |
||||
{ |
||||
Worker *worker = new Worker (m_Daemon); |
||||
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(m_Daemon.isRunning()) |
||||
{ |
||||
qDebug("Stopping the daemon..."); |
||||
m_Daemon.stop(); |
||||
qDebug("Stopped the daemon."); |
||||
} |
||||
} |
||||
|
||||
private: |
||||
DaemonQTImpl::DaemonQTImpl (): |
||||
mutex(nullptr), m_IsRunning(nullptr), m_RunningChangedCallback(nullptr) |
||||
{ |
||||
} |
||||
|
||||
void StartDaemon () |
||||
{ |
||||
Daemon.start (); |
||||
} |
||||
DaemonQTImpl::~DaemonQTImpl () |
||||
{ |
||||
delete mutex; |
||||
} |
||||
|
||||
void StopDaemon () |
||||
{ |
||||
Daemon.stop (); |
||||
} |
||||
bool DaemonQTImpl::init(int argc, char* argv[]) |
||||
{ |
||||
mutex=new QMutex(QMutex::Recursive); |
||||
setRunningCallback(0); |
||||
m_IsRunning=false; |
||||
return Daemon.init(argc,argv); |
||||
|
||||
bool IsRunning () const |
||||
{ |
||||
return Daemon.running; |
||||
} |
||||
} |
||||
void DaemonQTImpl::deinit() |
||||
{ |
||||
delete mutex; mutex = nullptr; |
||||
} |
||||
|
||||
private: |
||||
void DaemonQTImpl::start() |
||||
{ |
||||
QMutexLocker locker(mutex); |
||||
setRunning(true); |
||||
Daemon.start(); |
||||
} |
||||
|
||||
void DaemonQTImpl::stop() |
||||
{ |
||||
QMutexLocker locker(mutex); |
||||
Daemon.stop(); |
||||
setRunning(false); |
||||
} |
||||
|
||||
QApplication m_App; |
||||
}; |
||||
void DaemonQTImpl::restart() |
||||
{ |
||||
QMutexLocker locker(mutex); |
||||
stop(); |
||||
start(); |
||||
} |
||||
|
||||
bool DaemonQT::init(int argc, char* argv[]) |
||||
void DaemonQTImpl::setRunningCallback(runningChangedCallback cb) |
||||
{ |
||||
m_Impl = std::make_shared<DaemonQTImpl> (argc, argv); |
||||
return Daemon_Singleton::init(argc, argv); |
||||
m_RunningChangedCallback = cb; |
||||
} |
||||
|
||||
void DaemonQT::run () |
||||
bool DaemonQTImpl::isRunning() |
||||
{ |
||||
if (m_Impl) |
||||
return m_IsRunning; |
||||
} |
||||
|
||||
void DaemonQTImpl::setRunning(bool newValue) |
||||
{ |
||||
bool oldValue = m_IsRunning; |
||||
if(oldValue!=newValue) |
||||
{ |
||||
m_Impl->Run (); |
||||
m_Impl = nullptr; |
||||
m_IsRunning = newValue; |
||||
if(m_RunningChangedCallback) |
||||
m_RunningChangedCallback(); |
||||
} |
||||
} |
||||
|
||||
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; |
||||
{ |
||||
/* i2p::qt::Controller daemonQtController(daemon);
|
||||
qDebug("Starting the daemon..."); |
||||
emit daemonQtController.startDaemon(); |
||||
qDebug("Starting gui event loop...");*/ |
||||
daemon.start (); |
||||
result = app.exec(); |
||||
daemon.stop (); |
||||
} |
||||
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; |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
@ -0,0 +1,82 @@
@@ -0,0 +1,82 @@
|
||||
#ifndef DAEMONQT_H |
||||
#define DAEMONQT_H |
||||
|
||||
#include <QObject> |
||||
#include <QThread> |
||||
#include <QMutex> |
||||
|
||||
namespace i2p |
||||
{ |
||||
namespace qt |
||||
{ |
||||
class DaemonQTImpl |
||||
{ |
||||
public: |
||||
|
||||
DaemonQTImpl (); |
||||
~DaemonQTImpl (); |
||||
|
||||
typedef void (*runningChangedCallback)(); |
||||
|
||||
/**
|
||||
* @brief init |
||||
* @param argc |
||||
* @param argv |
||||
* @return success |
||||
*/ |
||||
bool init(int argc, char* argv[]); |
||||
void deinit(); |
||||
void start(); |
||||
void stop(); |
||||
void restart(); |
||||
void setRunningCallback(runningChangedCallback cb); |
||||
bool isRunning(); |
||||
private: |
||||
void setRunning(bool running); |
||||
private: |
||||
QMutex* mutex; |
||||
bool m_IsRunning; |
||||
runningChangedCallback m_RunningChangedCallback; |
||||
}; |
||||
|
||||
class Worker : public QObject |
||||
{ |
||||
Q_OBJECT |
||||
public: |
||||
|
||||
Worker (DaemonQTImpl& daemon); |
||||
|
||||
private: |
||||
|
||||
DaemonQTImpl& m_Daemon; |
||||
|
||||
public slots: |
||||
void startDaemon(); |
||||
void restartDaemon(); |
||||
void stopDaemon(); |
||||
|
||||
signals: |
||||
void resultReady(); |
||||
}; |
||||
|
||||
class Controller : public QObject |
||||
{ |
||||
Q_OBJECT |
||||
QThread workerThread; |
||||
public: |
||||
Controller(DaemonQTImpl& daemon); |
||||
~Controller(); |
||||
private: |
||||
DaemonQTImpl& m_Daemon; |
||||
|
||||
public slots: |
||||
void handleResults(){} |
||||
signals: |
||||
void startDaemon(); |
||||
void stopDaemon(); |
||||
void restartDaemon(); |
||||
}; |
||||
} |
||||
} |
||||
|
||||
#endif // DAEMONQT_H
|
@ -1,14 +1,55 @@
@@ -1,14 +1,55 @@
|
||||
#include "mainwindow.h" |
||||
#include "ui_mainwindow.h" |
||||
//#include "ui_mainwindow.h"
|
||||
#include <QMessageBox> |
||||
|
||||
MainWindow::MainWindow(QWidget *parent) : |
||||
QMainWindow(parent), |
||||
ui(new Ui::MainWindow) |
||||
QMainWindow(parent)/*,
|
||||
ui(new Ui::MainWindow)*/ |
||||
{ |
||||
ui->setupUi(this); |
||||
//ui->setupUi(this);
|
||||
if (objectName().isEmpty()) |
||||
setObjectName(QStringLiteral("MainWindow")); |
||||
resize(800, 480); |
||||
centralWidget = new QWidget(this); |
||||
centralWidget->setObjectName(QStringLiteral("centralWidget")); |
||||
verticalLayoutWidget = new QWidget(centralWidget); |
||||
verticalLayoutWidget->setObjectName(QStringLiteral("verticalLayoutWidget")); |
||||
//verticalLayoutWidget->setGeometry(QRect(10, 20, 771, 441));
|
||||
verticalLayout1 = new QVBoxLayout(verticalLayoutWidget); |
||||
verticalLayout1->setSpacing(6); |
||||
verticalLayout1->setContentsMargins(11, 11, 11, 11); |
||||
verticalLayout1->setObjectName(QStringLiteral("verticalLayout1")); |
||||
verticalLayout1->setContentsMargins(0, 0, 0, 0); |
||||
quitButton = new QPushButton(verticalLayoutWidget); |
||||
quitButton->setObjectName(QStringLiteral("quitButton")); |
||||
QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed); |
||||
sizePolicy.setHorizontalStretch(0); |
||||
sizePolicy.setVerticalStretch(0); |
||||
sizePolicy.setHeightForWidth(quitButton->sizePolicy().hasHeightForWidth()); |
||||
quitButton->setSizePolicy(sizePolicy); |
||||
|
||||
verticalLayout1->addWidget(quitButton); |
||||
|
||||
setCentralWidget(centralWidget); |
||||
|
||||
setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0)); |
||||
quitButton->setText(QApplication::translate("MainWindow", "Quit", 0)); |
||||
|
||||
QObject::connect(quitButton, SIGNAL(released()), this, SLOT(handleQuitButton())); |
||||
|
||||
//QMetaObject::connectSlotsByName(this);
|
||||
} |
||||
|
||||
void MainWindow::handleQuitButton() { |
||||
qDebug("Quit pressed. Hiding the main window"); |
||||
close(); |
||||
QApplication::instance()->quit(); |
||||
} |
||||
|
||||
MainWindow::~MainWindow() |
||||
{ |
||||
delete ui; |
||||
qDebug("Destroying main window"); |
||||
//QMessageBox::information(0, "Debug", "mw destructor 1");
|
||||
//delete ui;
|
||||
//QMessageBox::information(0, "Debug", "mw destructor 2");
|
||||
} |
||||
|
Loading…
Reference in new issue