diff --git a/src/daemon/DaemonManager.cpp b/src/daemon/DaemonManager.cpp new file mode 100644 index 00000000..30ae06cd --- /dev/null +++ b/src/daemon/DaemonManager.cpp @@ -0,0 +1,87 @@ +#include "DaemonManager.h" +#include +#include +#include +#include +#include +#include +#include +#include + +DaemonManager * DaemonManager::m_instance = nullptr; + +DaemonManager *DaemonManager::instance() +{ + if (!m_instance) { + m_instance = new DaemonManager; + } + + return m_instance; +} + +bool DaemonManager::start() +{ + QString program = QApplication::applicationDirPath() + "/monerod"; + qDebug() << "starting monerod " + program; + QStringList arguments; + + m_daemon = new QProcess(); + + connect (m_daemon, SIGNAL(readyReadStandardOutput()), this, SLOT(printOutput())); + connect (m_daemon, SIGNAL(readyReadStandardError()), this, SLOT(printError())); + + + m_daemon->start(program); + bool started = m_daemon->waitForStarted(); + + if(!started){ + qDebug() << "Daemon start error: " + m_daemon->errorString(); + } + + return started; +} + +bool DaemonManager::stop() +{ + return true; +} + +void DaemonManager::printOutput() +{ + QByteArray byteArray = m_daemon->readAllStandardOutput(); + QStringList strLines = QString(byteArray).split("\n"); + + foreach (QString line, strLines){ + // dConsole.append(line+"\n"); + qDebug() << "Daemon: " + line; + } +} + +void DaemonManager::printError() +{ + QByteArray byteArray = m_daemon->readAllStandardError(); + QStringList strLines = QString(byteArray).split("\n"); + + foreach (QString line, strLines){ + // dConsole.append(line+"\n"); + qDebug() << "Daemon ERROR: " + line; + } +} + +bool DaemonManager::running() const +{ + return m_daemon && m_daemon->state() > QProcess::NotRunning; +} + +QString DaemonManager::console() const +{ + return dConsole; +} + + +DaemonManager::DaemonManager(QObject *parent) + : QObject(parent) +{ + +} + diff --git a/src/daemon/DaemonManager.h b/src/daemon/DaemonManager.h new file mode 100644 index 00000000..669f9119 --- /dev/null +++ b/src/daemon/DaemonManager.h @@ -0,0 +1,41 @@ +#ifndef DAEMONMANAGER_H +#define DAEMONMANAGER_H + +#include +#include +#include + +class DaemonManager : public QObject +{ + Q_OBJECT + +public: + + static DaemonManager * instance(); + + Q_INVOKABLE bool start(); + Q_INVOKABLE bool stop(); + Q_INVOKABLE QString console() const; + + // return true if daemon process is started + Q_INVOKABLE bool running() const; + +signals: + + void daemonStarted(const QProcess &d); + void daemonStopped(); + +public slots: + void printOutput(); + void printError(); + +private: + + explicit DaemonManager(QObject *parent = 0); + static DaemonManager * m_instance; + QProcess *m_daemon; + QString dConsole; + +}; + +#endif // DAEMONMANAGER_H