-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeasurementcontroller.h
273 lines (226 loc) · 7.04 KB
/
measurementcontroller.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#pragma once
#include <QObject>
#include "measurementmodel.h"
#include "statusqueue.h"
#if defined(Q_OS_ANDROID)
#include "androidscan.h"
#elif defined(Q_OS_LINUX)
#include "interfacemodel.h"
#include "iperf.h"
#include "linuxscan.h"
#elif defined(Q_OS_WIN)
#include "windowsinterfacemodel.h"
#include "windowsscan.h"
#endif
#include <QQueue>
class Task : public QObject {
Q_OBJECT
public:
Task(QVector<MeasurementEntry> &results) : mResults(results){};
void onFinished(QVector<MeasurementEntry> res) {
for (auto r : res)
mResults.push_back(r);
emit done(0, "");
this->disconnect();
this->deleteLater(); // FIXME:
}
void onFailed(int err, QString message) {
emit done(err, message);
this->disconnect();
this->deleteLater(); // FIXME:
}
virtual void run() = 0;
signals:
void done(int err, QString message);
private:
QVector<MeasurementEntry> &mResults;
};
#if defined(Q_OS_ANDROID)
class AndroidScanTask : public Task {
Q_OBJECT
public:
AndroidScanTask(AndroidScan *androidScan, QVector<MeasurementEntry> &results)
: Task(results), mAndroidScan(androidScan) {}
void run() {
mAndroidScan->connect(mAndroidScan, &AndroidScan::scanFinished, this,
&AndroidScanTask::onFinished);
mAndroidScan->connect(mAndroidScan, &AndroidScan::scanFailed, this,
&AndroidScanTask::onFailed);
if (!mAndroidScan->measure())
onFailed(254, "scan not allowed");
}
private:
AndroidScan *mAndroidScan;
};
class AndroidConnectedTask : public Task {
Q_OBJECT
public:
AndroidConnectedTask(AndroidScan *androidScan,
QVector<MeasurementEntry> &results)
: Task(results), mAndroidScan(androidScan) {}
void run() {
mAndroidScan->connect(mAndroidScan, &AndroidScan::scanFinished, this,
&AndroidScanTask::onFinished);
mAndroidScan->connect(mAndroidScan, &AndroidScan::scanFailed, this,
&AndroidScanTask::onFailed);
mAndroidScan->connected();
}
private:
AndroidScan *mAndroidScan;
};
#elif defined(Q_OS_LINUX)
class ScanTask : public Task {
Q_OBJECT
public:
ScanTask(LinuxScan *linuxScan, QVector<MeasurementEntry> &results)
: Task(results), mLinuxScan(linuxScan) {}
void run() {
mLinuxScan->connect(mLinuxScan, &LinuxScan::scanFinished, this,
&ScanTask::onFinished);
mLinuxScan->connect(mLinuxScan, &LinuxScan::scanFailed, this,
&ScanTask::onFailed);
mLinuxScan->measure();
}
private:
LinuxScan *mLinuxScan;
};
class IperfTask : public Task {
public:
IperfTask(IPerfScan *iPerfScan, LinuxScan *linuxScan,
QVector<MeasurementEntry> &results)
: Task(results), mLinuxScan(linuxScan), mIPerfScan(iPerfScan) {}
void run() {
mIPerfScan->connect(mIPerfScan, &IPerfScan::scanFinished, this,
&IperfTask::onFinished);
mIPerfScan->connect(mIPerfScan, &IPerfScan::scanFailed, this,
&IperfTask::onFailed);
mLinuxScan->connect(mLinuxScan, &LinuxScan::scanFailed, this,
&IperfTask::onFailed);
mLinuxScan->blockSignals(true); // FIXME
auto measurementEntry = mLinuxScan->connected();
mLinuxScan->blockSignals(false);
if (measurementEntry.has_value())
mIPerfScan->measure(measurementEntry.value());
else
onFailed(254, "not connected?");
}
private:
LinuxScan *mLinuxScan;
IPerfScan *mIPerfScan;
};
class ConnectedTask : public Task {
Q_OBJECT
public:
ConnectedTask(LinuxScan *linuxScan, QVector<MeasurementEntry> &results)
: Task(results), mLinuxScan(linuxScan) {}
void run() {
mLinuxScan->connect(mLinuxScan, &LinuxScan::scanFinished, this,
&ConnectedTask::onFinished);
mLinuxScan->connect(mLinuxScan, &LinuxScan::scanFailed, this,
&ConnectedTask::onFailed);
mLinuxScan->connected();
}
private:
LinuxScan *mLinuxScan;
};
#elif defined(Q_OS_WIN)
class ScanWindowsTask : public Task {
Q_OBJECT
public:
ScanWindowsTask(WindowsScan *windowsScan, QVector<MeasurementEntry> &results)
: Task(results), mWindowsScan(windowsScan) {}
void run() {
mWindowsScan->connect(mWindowsScan, &WindowsScan::scanFinished, this,
&ScanWindowsTask::onFinished);
mWindowsScan->connect(mWindowsScan, &WindowsScan::scanFailed, this,
&ScanWindowsTask::onFailed);
mWindowsScan->measure();
}
private:
WindowsScan *mWindowsScan;
};
#endif
class TaskQueue : public QObject { // TODO: timeout?
Q_OBJECT
public:
TaskQueue(QObject *parent = nullptr) : QObject(parent) {}
void enqueue(Task *task) { queue.enqueue(task); }
int size() { return queue.size(); }
void run(int err, QString message) {
if (err != 0) {
queue.clear();
emit done(err, message);
} else {
if (!queue.isEmpty()) {
auto task = queue.dequeue();
task->connect(task, &Task::done, this, &TaskQueue::run);
task->run();
} else {
emit done(0, "");
}
}
}
signals:
void done(int err, QString message);
private:
QQueue<Task *> queue{};
};
class MeasurementController : public QObject {
Q_OBJECT
Q_PROPERTY(bool iperf READ iperf WRITE setIperf NOTIFY iperfChanged)
Q_PROPERTY(bool scan READ scan WRITE setScan NOTIFY scanChanged)
#if defined(Q_OS_ANDROID) // FIXME:
#elif defined(Q_OS_LINUX)
Q_PROPERTY(IPerfScan *iPerfScan READ iperfScan NOTIFY iperfScanChanged)
Q_PROPERTY(InterfaceModel *interfaceModel READ interfaceModel NOTIFY
interfaceModelChanged)
Q_PROPERTY(LinuxScan *linuxScan READ linuxScan NOTIFY linuxScanChanged)
#elif defined(Q_OS_WIN)
Q_PROPERTY(WindowsInterfaceModel *windowsInterfaceModel READ
windowsInterfaceModel NOTIFY interfaceModelChanged)
#endif
public:
explicit MeasurementController(StatusQueue *statusStack,
QObject *parent = nullptr);
Q_INVOKABLE bool measure(QPoint pos);
bool iperf();
void setIperf(bool value);
bool scan();
void setScan(bool value);
#if defined(Q_OS_ANDROID) // FIXME:
#elif defined(Q_OS_LINUX)
IPerfScan *iperfScan();
InterfaceModel *interfaceModel();
LinuxScan *linuxScan();
#elif defined(Q_OS_WINDOWS)
WindowsInterfaceModel *windowsInterfaceModel();
#endif
signals:
void iperfChanged(bool value);
void scanChanged(bool value);
void iperfScanChanged();
void interfaceModelChanged();
void linuxScanChanged();
void scanStarted(QPoint pos);
void scanFinished(QVector<MeasurementEntry> results);
void scanFailed(int err);
public slots:
void onDone(int err, QString message);
private:
bool mScanning{false};
QVector<MeasurementEntry> mResults{};
StatusQueue *mStatusQueue;
bool mIperf{false};
bool mScan{false};
TaskQueue mTaskqueue;
#ifdef Q_OS_ANDROID
AndroidScan *mAndroidScan;
#elif defined(Q_OS_LINUX)
LinuxScan *mLinuxScan;
InterfaceModel *mInterfaceModel;
IPerfScan *mIPerfScan;
#elif defined(Q_OS_WIN)
WindowsScan *mWindowsScan;
WindowsInterfaceModel *mWindowsInterfaceModel;
#endif
};