2016-11-05 11:47:17 +02:00
|
|
|
#include "DaemonManager.h"
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QtConcurrent/QtConcurrent>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QProcess>
|
|
|
|
|
|
|
|
DaemonManager * DaemonManager::m_instance = nullptr;
|
2016-11-07 19:08:09 +02:00
|
|
|
QStringList DaemonManager::clArgs;
|
2016-11-05 11:47:17 +02:00
|
|
|
|
2016-11-07 19:08:09 +02:00
|
|
|
DaemonManager *DaemonManager::instance(QStringList args)
|
2016-11-05 11:47:17 +02:00
|
|
|
{
|
|
|
|
if (!m_instance) {
|
|
|
|
m_instance = new DaemonManager;
|
2016-11-07 19:08:09 +02:00
|
|
|
// store command line arguments for later use
|
|
|
|
clArgs = args;
|
|
|
|
clArgs.removeFirst();
|
2016-11-05 11:47:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DaemonManager::start()
|
|
|
|
{
|
2016-11-07 12:55:11 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
QString process;
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
process = QApplication::applicationDirPath() + "/monerod.exe";
|
|
|
|
#elif defined(Q_OS_UNIX)
|
|
|
|
process = QApplication::applicationDirPath() + "/monerod";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(process.length() == 0) {
|
|
|
|
qDebug() << "no daemon binary defined for current platform";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-07 19:08:09 +02:00
|
|
|
// prepare command line arguments and pass to monerod
|
2016-11-05 11:47:17 +02:00
|
|
|
QStringList arguments;
|
2016-11-07 19:08:09 +02:00
|
|
|
foreach (const QString &str, clArgs) {
|
|
|
|
qDebug() << QString(" [%1] ").arg(str);
|
|
|
|
arguments << str;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "starting monerod " + process;
|
|
|
|
qDebug() << "With command line arguments " << arguments;
|
2016-11-05 11:47:17 +02:00
|
|
|
|
|
|
|
m_daemon = new QProcess();
|
2016-11-07 14:10:10 +02:00
|
|
|
initialized = true;
|
2016-11-05 11:47:17 +02:00
|
|
|
|
2016-11-07 12:55:11 +02:00
|
|
|
// Connect output slots
|
2016-11-05 11:47:17 +02:00
|
|
|
connect (m_daemon, SIGNAL(readyReadStandardOutput()), this, SLOT(printOutput()));
|
|
|
|
connect (m_daemon, SIGNAL(readyReadStandardError()), this, SLOT(printError()));
|
|
|
|
|
2016-11-07 19:08:09 +02:00
|
|
|
// Start monerod
|
|
|
|
m_daemon->start(process,arguments);
|
2016-11-05 11:47:17 +02:00
|
|
|
bool started = m_daemon->waitForStarted();
|
|
|
|
|
2016-11-26 12:54:06 +02:00
|
|
|
// add state changed listener
|
|
|
|
connect(m_daemon,SIGNAL(stateChanged(QProcess::ProcessState)),this,SLOT(stateChanged(QProcess::ProcessState)));
|
|
|
|
|
2016-11-05 11:47:17 +02:00
|
|
|
if(!started){
|
|
|
|
qDebug() << "Daemon start error: " + m_daemon->errorString();
|
2016-11-25 22:09:32 +02:00
|
|
|
} else {
|
|
|
|
emit daemonStarted();
|
2016-11-05 11:47:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return started;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DaemonManager::stop()
|
|
|
|
{
|
2016-11-07 14:10:10 +02:00
|
|
|
if(initialized){
|
2016-11-25 22:09:32 +02:00
|
|
|
qDebug() << "stopping daemon";
|
2016-11-26 12:54:06 +02:00
|
|
|
// we can't use QProcess::terminate() on windows console process
|
|
|
|
// write exit command to stdin
|
|
|
|
m_daemon->write("exit\n");
|
2016-11-25 22:09:32 +02:00
|
|
|
}
|
|
|
|
|
2016-11-05 11:47:17 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-26 12:54:06 +02:00
|
|
|
void DaemonManager::stateChanged(QProcess::ProcessState state)
|
|
|
|
{
|
|
|
|
qDebug() << "STATE CHANGED: " << state;
|
|
|
|
if(state == QProcess::NotRunning) {
|
|
|
|
emit daemonStopped();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 11:47:17 +02:00
|
|
|
void DaemonManager::printOutput()
|
|
|
|
{
|
|
|
|
QByteArray byteArray = m_daemon->readAllStandardOutput();
|
|
|
|
QStringList strLines = QString(byteArray).split("\n");
|
|
|
|
|
|
|
|
foreach (QString line, strLines){
|
2016-11-07 14:10:10 +02:00
|
|
|
emit daemonConsoleUpdated(line);
|
2016-11-07 16:00:14 +02:00
|
|
|
qDebug() << "Daemon: " + line;
|
2016-11-05 11:47:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DaemonManager::printError()
|
|
|
|
{
|
|
|
|
QByteArray byteArray = m_daemon->readAllStandardError();
|
|
|
|
QStringList strLines = QString(byteArray).split("\n");
|
|
|
|
|
|
|
|
foreach (QString line, strLines){
|
2016-11-07 14:10:10 +02:00
|
|
|
emit daemonConsoleUpdated(line);
|
2016-11-07 16:00:14 +02:00
|
|
|
qDebug() << "Daemon ERROR: " + line;
|
2016-11-05 11:47:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DaemonManager::running() const
|
|
|
|
{
|
2016-11-07 14:10:10 +02:00
|
|
|
if(initialized){
|
|
|
|
qDebug() << m_daemon->state();
|
|
|
|
qDebug() << QProcess::NotRunning;
|
2016-11-26 12:54:06 +02:00
|
|
|
// m_daemon->write("status\n");
|
2016-11-07 14:10:10 +02:00
|
|
|
return m_daemon->state() > QProcess::NotRunning;
|
|
|
|
}
|
|
|
|
return false;
|
2016-11-05 11:47:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DaemonManager::DaemonManager(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-08 00:06:18 +02:00
|
|
|
void DaemonManager::closing()
|
|
|
|
{
|
|
|
|
qDebug() << __FUNCTION__;
|
|
|
|
stop();
|
|
|
|
}
|