-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.h
187 lines (169 loc) · 5.25 KB
/
mainwindow.h
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextEdit>
#include <QDebug>
#include <QFuture>
#include <QProcess>
#include <QtConcurrent/QtConcurrent>
class Pinger : public QObject
{
Q_OBJECT
public:
explicit Pinger(QObject *parent = nullptr)
: QObject{parent}
, m_pProcessPing{nullptr}
, m_processPing(this)
{
qDebug() << "+Pinger()";
qDebug() << "-Pinger()";
}
~Pinger()
{
qDebug() << "+~Pinger()";
stopSync();
qDebug() << "-~Pinger()";
}
QFuture<int> startAsync()
{
qDebug() << "+startAsync()";
QPromise<int> promise;
auto future = QtConcurrent::run([this](QPromise<int>& promise)
{
auto errorCode = startSync();
promise.addResult(errorCode);
});
qDebug() << "-startAsync()";
return future;
}
int startSync()
{
qDebug() << "+startSync()";
if (m_pProcessPing)
{
throw QException();
}
m_pProcessPing = &m_processPing;
connect(&m_processPing, &QProcess::errorOccurred, this, [](QProcess::ProcessError error)
{
qDebug() << "errorOccurred error=" << error;
});
connect(&m_processPing, &QProcess::finished, this, [this](int exitCode, QProcess::ExitStatus exitStatus)
{
auto program = m_processPing.program();
auto args = m_processPing.arguments();
auto error = m_processPing.readAllStandardError();
auto output = m_processPing.readAllStandardOutput();
qDebug() << "finished args" << args;
qDebug() << "finished exitCode" << exitCode;
qDebug() << "finished exitStatus" << exitStatus;
qDebug() << "finished error" << QString::fromUtf8(error);
qDebug() << "finished output" << QString::fromUtf8(output);
if (exitCode)
{
emit onError(exitCode, program, args, output, error);
}
else
{
emit onStopped();
}
});
connect(&m_processPing, &QProcess::readyReadStandardError, this, [this]()
{
emit onStandardError(m_processPing.readAllStandardError());
});
connect(&m_processPing, &QProcess::readyReadStandardOutput, this, [this]()
{
emit onStandardOutput(m_processPing.readAllStandardOutput());
});
connect(&m_processPing, &QProcess::started, this, [this]()
{
qDebug() << "started";
emit onStart(0);
});
connect(&m_processPing, &QProcess::stateChanged, this, [](QProcess::ProcessState newState)
{
qDebug() << "stateChanged newState=" << newState;
});
m_processPing.start("ping", QStringList() << "-t" << "127.0.0.1");
int errorCode;
if (m_processPing.waitForStarted(1000))
{
// IF THIS LINE ISN'T EXECUTED THEN readyReadStandardError AND readyReadStandardOutput NEVER GET CALLED
// Strangely enough, all of the other signals get called. :/
m_processPing.waitForFinished(-1);
errorCode = 0;
}
else
{
qDebug() << "process failed to start";
errorCode = 1;
}
qDebug().nospace() << "errorCode=" << errorCode;
qDebug() << "-startSync()";
return errorCode;
}
QFuture<void> stopAsync()
{
qDebug() << "+stopAsync()";
QPromise<void> promise;
auto future = QtConcurrent::run([this](QPromise<void>& promise)
{
stopSync();
});
qDebug() << "-stopAsync()";
return future;
}
void stopSync()
{
qDebug() << "+stopSync()";
if (m_pProcessPing)
{
#if 0
// Why doesn't `QProcess::close()` work nicely?
qDebug() << "+m_pProcessPing->close()";
m_pProcessPing->close();
qDebug() << "-m_pProcessPing->close()";
#else
qDebug() << "+m_pProcessPing->kill()";
m_pProcessPing->kill();
qDebug() << "-m_pProcessPing->kill()";
#endif
m_pProcessPing = nullptr;
}
qDebug() << "-stopSync()";
}
QProcess* m_pProcessPing;
QProcess m_processPing;
signals:
void onStart(int errorCode);
void onStandardError(const QString& error);
void onStandardOutput(const QString& output);
void onError(int exitCode,
const QString& program,
const QStringList& args,
const QString& output,
const QString& error);
void onStopped();
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QTextEdit textEditLog;
Pinger pinger;
private slots:
void onPingStart(int errorCode);
void onPingStandardError(const QString& error);
void onPingStandardOutput(const QString& output);
void onPingError(int exitCode,
const QString& program,
const QStringList& args,
const QString& output,
const QString& error);
void onPingStopped();
};
#endif // MAINWINDOW_H