forked from xLightsSequencer/xLights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScanWork.h
401 lines (352 loc) · 10.4 KB
/
ScanWork.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#pragma once
#include <wx/wx.h>
#include <wx/socket.h>
#include <wx/thread.h>
#include <list>
#include <string>
#include <queue>
#include <optional>
#include <mutex>
#include <atomic>
#include "../xLights/outputs/OutputManager.h"
// seed with ... xLights defined stuff
// seed with networks we are attached to
// work order ... do discovery
// ping devices
// ping devices
// http open
// http open
// FPP/Falcon connect
class xScannerFrame;
class WorkManager;
class wxWindow;
class Controller;
class wxSocketClient;
class ScanWork;
template<typename T>
class ThreadsafeQueue {
std::queue<T> queue_;
mutable std::mutex mutex_;
// Moved out of public interface to prevent races between this
// and pop().
bool empty() const
{
return queue_.empty();
}
public:
ThreadsafeQueue() = default;
ThreadsafeQueue(const ThreadsafeQueue<T>&) = delete;
ThreadsafeQueue& operator=(const ThreadsafeQueue<T>&) = delete;
ThreadsafeQueue(ThreadsafeQueue<T>&& other)
{
std::lock_guard<std::mutex> lock(mutex_);
queue_ = std::move(other.queue_);
}
virtual ~ThreadsafeQueue() {}
unsigned long size() const
{
std::lock_guard<std::mutex> lock(mutex_);
return queue_.size();
}
void push(const T& item)
{
std::lock_guard<std::mutex> lock(mutex_);
queue_.push(item);
}
std::optional<T> pop()
{
std::lock_guard<std::mutex> lock(mutex_);
if (queue_.empty()) {
return {};
}
T tmp = queue_.front();
queue_.pop();
return tmp;
}
};
enum class WorkType {
WTCOMPUTER,
WTPING,
WTMAC,
WTDISCOVER,
WTHTTP,
WTFPP,
WTFALCON,
WTUNKNOWN
};
enum class ThreadType {
TTPING,
TTMAIN,
TTOTHER
};
class ScanThread : public wxThread
{
bool _terminate = false;
ThreadType _threadType = ThreadType::TTOTHER;
virtual ExitCode Entry();
WorkManager& _workManager;
wxSocketClient* _client = nullptr;
ScanWork* _activeWork = nullptr;
std::mutex _mutex;
public:
ScanThread(WorkManager& workManager, ThreadType tt, wxSocketClient* client = nullptr)
: wxThread(wxTHREAD_DETACHED), _workManager(workManager), _threadType(tt), _client(client) {}
~ScanThread() {}
void Terminate() { _terminate = true; }
void TerminateWork();
};
class ScanWork {
protected:
WorkType _type = WorkType::WTUNKNOWN;
wxWindow* GetFrameWindow();
bool _terminate = false;
public:
WorkType GetType() { return _type; }
ScanWork(WorkType type) { _type = type; }
virtual ~ScanWork() {}
WorkType GetWorkType() const { return _type; }
virtual void DoWork(WorkManager& workManager, wxSocketClient* client) = 0;
void PublishResult(WorkManager& workManager, std::list<std::pair<std::string, std::string>>& result);
virtual bool IsMainThread() { return false; }
void Terminate() { _terminate = true; }
};
class ComputerWork : public ScanWork
{
protected:
std::list<std::string> _macsDone;
std::string _xLightsShowFolder;
std::string _xScheduleShowFolder;
std::string GetXScheduleShowFolder();
std::string GetXLightsShowFolder();
std::string GetForceIP();
void ScanARP(WorkManager& workManager);
void ProcessController(WorkManager& workManager, Controller* controller, const std::string& why);
public:
ComputerWork() : ScanWork(WorkType::WTCOMPUTER) { }
virtual ~ComputerWork() {}
virtual void DoWork(WorkManager& workManager, wxSocketClient* client) override;
};
class WorkManager {
protected:
ThreadsafeQueue<ScanWork*> _queueHTTP;
ThreadsafeQueue<ScanWork*> _queuePing;
ThreadsafeQueue<ScanWork*> _queueOther;
mutable std::mutex _mutex;
std::list<std::string> _scannedIP;
std::list<std::string> _scannedHTTP;
ThreadsafeQueue < std::list<std::pair<std::string, std::string>>> _results;
std::list<ScanThread*> _threadsOther;
std::list<ScanThread*> _threadsPing;
std::list<ScanThread*> _threadsHTTP;
std::list<std::string> _found;
bool _started = false;
bool _singleThreaded = false;
public:
WorkManager();
virtual ~WorkManager();
void SetSingleThreaded(bool st)
{
_singleThreaded = st;
}
void Restart()
{
while (_queuePing.size() > 0) {
_queuePing.pop();
}
while (_queueHTTP.size() > 0) {
_queueHTTP.pop();
}
while (_queueOther.size() > 0) {
_queueOther.pop();
}
while (_results.size() > 0) {
_results.pop();
}
for (const auto& it : _threadsOther) {
it->TerminateWork();
}
for (const auto& it : _threadsPing) {
it->TerminateWork();
}
for (const auto& it : _threadsHTTP) {
it->TerminateWork();
}
_scannedIP.clear();
_scannedHTTP.clear();
_found.clear();
// give everything a chance to stop
wxSleep(1);
// now do it all again to make sure all is empty
while (_queuePing.size() > 0) {
_queuePing.pop();
}
while (_queueHTTP.size() > 0) {
_queueHTTP.pop();
}
while (_queueOther.size() > 0) {
_queueOther.pop();
}
while (_results.size() > 0) {
_results.pop();
}
for (const auto& it : _threadsOther) {
it->TerminateWork();
}
for (const auto& it : _threadsPing) {
it->TerminateWork();
}
for (const auto& it : _threadsHTTP) {
it->TerminateWork();
}
_scannedIP.clear();
_scannedHTTP.clear();
_found.clear();
}
std::string GetPendingWork()
{
return wxString::Format("HTTP %d : Ping %d : Other %d", (int)_queueHTTP.size(), (int)_queuePing.size(), (int)_queueOther.size());
}
void AddHTTP(const std::string& ip, int port, const std::string& proxy = "");
void AddIP(const std::string& ip, const std::string& why, const std::string& proxy = "");
void AddClassDSubnet(const std::string& ip, const std::string& proxy = "");
void AddComputer()
{
_queueOther.push(new ComputerWork());
}
void Start();
void Stop();
void AddWork(ScanWork* work)
{
switch (work->GetType()) {
case WorkType::WTHTTP:
_queueHTTP.push(work);
break;
case WorkType::WTPING:
_queuePing.push(work);
break;
default:
_queueOther.push(work);
break;
}
}
void PublishResult(std::list<std::pair<std::string, std::string>>& result)
{
_results.push(result);
}
std::optional<ScanWork*> GetWork(ThreadType tt)
{
switch (tt) {
case ThreadType::TTMAIN:
return _queueHTTP.pop();
break;
case ThreadType::TTPING:
return _queuePing.pop();
break;
case ThreadType::TTOTHER:
return _queueOther.pop();
break;
}
return {};
}
const std::list<std::string>& GetFound() const
{
return _found;
}
void AddFoundIP(const std::string& ip)
{
if (std::find(begin(_found), end(_found), ip) == end(_found)) {
_found.push_back(ip);
}
}
std::list<std::pair<std::string, std::string>> GetNextResult()
{
auto res = _results.pop();
if (res.has_value()) {
return *res;
}
else {
return std::list<std::pair<std::string, std::string>>();
}
}
};
class PingWork : public ScanWork
{
protected:
std::string _ip;
std::string _proxy;
std::string _why;
public:
PingWork(const std::string& ip, const std::string& why, const std::string& proxy = "") : ScanWork(WorkType::WTPING) { _ip = ip; _proxy = proxy; _why = why; }
virtual ~PingWork() {}
virtual void DoWork(WorkManager& workManager, wxSocketClient* client) override;
};
class MACWork : public ScanWork
{
protected:
std::string _ip;
std::string _mac;
public:
MACWork(const std::string& ip, const std::string& mac) : ScanWork(WorkType::WTMAC) { _ip = ip; _mac = mac; }
virtual ~MACWork() {}
virtual void DoWork(WorkManager& workManager, wxSocketClient* client) override;
};
class DiscoverWork : public ScanWork
{
OutputManager _om;
public:
DiscoverWork() : ScanWork(WorkType::WTDISCOVER) {}
virtual ~DiscoverWork() {}
virtual void DoWork(WorkManager& workManager, wxSocketClient* client) override;
};
class HTTPWork : public ScanWork
{
protected:
std::string _ip;
int _port = 80;
std::string _proxy;
std::string GetTitle(const std::string& page);
std::string GetControllerTypeBasedOnPageContent(const std::string& page);
public:
HTTPWork(const std::string& ip, int port = 80, const std::string& proxy = "") : ScanWork(WorkType::WTHTTP) { _ip = ip; _port = port; _proxy = proxy; }
virtual ~HTTPWork() {}
virtual void DoWork(WorkManager& workManager, wxSocketClient* client) override;
virtual bool IsMainThread() override { return true; }
};
class FPPWork : public ScanWork
{
protected:
std::string _ip;
std::string _proxy;
std::string DecodeWifiStrength(int w)
{
if (w > -50) return "Excellent";
if (w > -60) return "Good";
if (w > -70) return "Fair";
return "Weak";
}
public:
FPPWork(const std::string& ip, const std::string& proxy = "") : ScanWork(WorkType::WTFPP) { _ip = ip; _proxy = proxy; }
virtual ~FPPWork() {}
virtual void DoWork(WorkManager& workManager, wxSocketClient* client) override;
};
class FalconWork : public ScanWork
{
protected:
std::string _ip;
std::string _proxy;
public:
FalconWork(const std::string& ip, const std::string& proxy = "") : ScanWork(WorkType::WTFPP) { _ip = ip; _proxy = proxy; }
virtual ~FalconWork() {}
virtual void DoWork(WorkManager& workManager, wxSocketClient* client) override;
};
class xScheduleWork : public ScanWork
{
protected:
std::string _ip;
std::string _proxy;
int _port = 80;
public:
xScheduleWork(const std::string& ip, int port = 80, const std::string& proxy = "") : ScanWork(WorkType::WTFPP) { _ip = ip; _proxy = proxy; _port = port; }
virtual ~xScheduleWork() {}
virtual void DoWork(WorkManager& workManager, wxSocketClient* client) override;
};