1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#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;
QStringList DaemonManager::m_clArgs;
DaemonManager *DaemonManager::instance(const QStringList *args)
{
if (!m_instance) {
m_instance = new DaemonManager;
// store command line arguments for later use
m_clArgs = *args;
m_clArgs.removeFirst();
}
return m_instance;
}
bool DaemonManager::start()
{
//
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;
}
// prepare command line arguments and pass to monerod
QStringList arguments;
foreach (const QString &str, m_clArgs) {
qDebug() << QString(" [%1] ").arg(str);
arguments << str;
}
qDebug() << "starting monerod " + process;
qDebug() << "With command line arguments " << arguments;
m_daemon = new QProcess();
initialized = true;
// Connect output slots
connect (m_daemon, SIGNAL(readyReadStandardOutput()), this, SLOT(printOutput()));
connect (m_daemon, SIGNAL(readyReadStandardError()), this, SLOT(printError()));
// Start monerod
m_daemon->start(process,arguments);
bool started = m_daemon->waitForStarted();
// add state changed listener
connect(m_daemon,SIGNAL(stateChanged(QProcess::ProcessState)),this,SLOT(stateChanged(QProcess::ProcessState)));
if (!started) {
qDebug() << "Daemon start error: " + m_daemon->errorString();
} else {
emit daemonStarted();
}
return started;
}
bool DaemonManager::stop()
{
if (initialized) {
qDebug() << "stopping daemon";
// we can't use QProcess::terminate() on windows console process
// write exit command to stdin
m_daemon->write("exit\n");
}
return true;
}
void DaemonManager::stateChanged(QProcess::ProcessState state)
{
qDebug() << "STATE CHANGED: " << state;
if (state == QProcess::NotRunning) {
emit daemonStopped();
}
}
void DaemonManager::printOutput()
{
QByteArray byteArray = m_daemon->readAllStandardOutput();
QStringList strLines = QString(byteArray).split("\n");
foreach (QString line, strLines) {
emit daemonConsoleUpdated(line);
qDebug() << "Daemon: " + line;
}
}
void DaemonManager::printError()
{
QByteArray byteArray = m_daemon->readAllStandardError();
QStringList strLines = QString(byteArray).split("\n");
foreach (QString line, strLines) {
emit daemonConsoleUpdated(line);
qDebug() << "Daemon ERROR: " + line;
}
}
bool DaemonManager::running() const
{
if (initialized) {
qDebug() << m_daemon->state();
qDebug() << QProcess::NotRunning;
// m_daemon->write("status\n");
return m_daemon->state() > QProcess::NotRunning;
}
return false;
}
DaemonManager::DaemonManager(QObject *parent)
: QObject(parent)
{
}
void DaemonManager::closing()
{
qDebug() << __FUNCTION__;
stop();
// Wait for daemon to stop before exiting (max 10 secs)
if (initialized) {
m_daemon->waitForFinished(10000);
}
}
|