This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
/
runtime.cpp
97 lines (75 loc) · 1.93 KB
/
runtime.cpp
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
#include "runtime.h"
Runtime::Runtime(QString binary, QString logsDir)
{
this->binaryPath = binary;
this->logsDir = logsDir;
}
Runtime::~Runtime()
{
// do not allow processes running
this->stopAll();
}
vector<RT_INSTANCE> Runtime::getInstances()
{
return this->instances;
}
void Runtime::restart(RT_INSTANCE instance)
{
this->stop(instance);
this->start(instance);
}
void Runtime::stop(RT_INSTANCE instance)
{
qDebug() << "Stopping Share Daemon (" << instance.process->pid() << ")";
instance.process->terminate();
}
void Runtime::start(RT_INSTANCE instance)
{
instance.process->start(
instance.binary,
this->getArgsFromUrlAndPath(instance.url, instance.folder)
);
qDebug() << "Starting Share Daemon (" << instance.process->pid() << ")";
}
RT_INSTANCE Runtime::createAddInstance(QString url, QString folder)
{
RT_INSTANCE instance;
instance.folder = folder;
instance.url = url;
instance.binary = this->binaryPath;
instance.process = this->createProcess();
this->instances.push_back(instance);
return instance;
}
void Runtime::restartAll()
{
for (auto & instance : this->instances) {
this->restart(instance);
}
}
void Runtime::startAll()
{
for (auto & instance : this->instances) {
this->start(instance);
}
}
void Runtime::stopAll()
{
for (auto & instance : this->instances) {
this->stop(instance);
}
}
QStringList Runtime::getArgsFromUrlAndPath(QString url, QString path)
{
QStringList arguments;
arguments << path << url;
return arguments;
}
QProcess* Runtime::createProcess()
{
QProcess* process = new QProcess();
QString logFile = this->logsDir + QString("/") + QDate::currentDate().toString() + QString("_log.txt");
process->setStandardErrorFile(logFile, QIODevice::Append);
process->setStandardOutputFile(logFile, QIODevice::Append);
return process;
}