-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholympusdaemonprocess.cpp
More file actions
418 lines (369 loc) · 14.2 KB
/
olympusdaemonprocess.cpp
File metadata and controls
418 lines (369 loc) · 14.2 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "olympusdaemonprocess.h"
QRegularExpression OlympusDaemonProcess::numericRegex("(\\d+)$");
OlympusDaemonProcess::OlympusDaemonProcess(QObject *parent)
: QObject{parent}
{
// Assign private member variables
server = new Server(this);
olympusPM = nullptr;
client = nullptr;
// Default values
port = 1500;
exclusive = true;
time = 5000;
// Connect signals & slots
QObject::connect(server, SIGNAL(requestForDaemon(MessageSocket)),
this, SLOT(receiveRequest(MessageSocket)), Qt::QueuedConnection);
QObject::connect(this, SIGNAL(sendResponse(MessageSocket)),
server, SLOT(receiveResponse(MessageSocket)), Qt::QueuedConnection);
// Initialize the handlers map
handlers = QMap<QString, RequestHandler> ();
// Alias for quit daemon process
handlers.insert("quitdaemon", &OlympusDaemonProcess::handleQuitDaemon);
handlers.insert("quitprogram", &OlympusDaemonProcess::handleQuitDaemon);
handlers.insert("quitapp", &OlympusDaemonProcess::handleQuitDaemon);
handlers.insert("exit", &OlympusDaemonProcess::handleQuitDaemon);
// Alias for quit Olympus port manager
handlers.insert("quitportmanager", &OlympusDaemonProcess::handleQuitPortManager);
handlers.insert("quitpm", &OlympusDaemonProcess::handleQuitPortManager);
handlers.insert("exitportmanager", &OlympusDaemonProcess::handleQuitPortManager);
handlers.insert("exitpm", &OlympusDaemonProcess::handleQuitPortManager);
// Alias for show/hide console window
handlers.insert("showconsole", &OlympusDaemonProcess::handleShowConsole);
handlers.insert("showwindow", &OlympusDaemonProcess::handleShowConsole);
handlers.insert("show", &OlympusDaemonProcess::handleShowConsole);
handlers.insert("hideconsole", &OlympusDaemonProcess::handleHideConsole);
handlers.insert("hidewindow", &OlympusDaemonProcess::handleHideConsole);
handlers.insert("hide", &OlympusDaemonProcess::handleHideConsole);
// Alias for initialize Olympus port manager
handlers.insert("initializeportmanager", &OlympusDaemonProcess::handleInitializePortManager);
handlers.insert("initportmanager", &OlympusDaemonProcess::handleInitializePortManager);
handlers.insert("initpm", &OlympusDaemonProcess::handleInitializePortManager);
// Alias for enumerate interface
handlers.insert("enumerateinterface", &OlympusDaemonProcess::handleEnumerateInterface);
handlers.insert("enuminterface", &OlympusDaemonProcess::handleEnumerateInterface);
handlers.insert("enumerateport", &OlympusDaemonProcess::handleEnumerateInterface);
handlers.insert("enumport", &OlympusDaemonProcess::handleEnumerateInterface);
// Alias for open interface
handlers.insert("openinterface", &OlympusDaemonProcess::handleOpenInterface);
handlers.insert("openport", &OlympusDaemonProcess::handleOpenInterface);
// Alias for close interface
handlers.insert("closeinterface", &OlympusDaemonProcess::handleCloseInterface);
handlers.insert("closeport", &OlympusDaemonProcess::handleCloseInterface);
handlers.insert("isconnected", &OlympusDaemonProcess::handleIsConnected);
handlers.insert("login", &OlympusDaemonProcess::handleLogIn);
handlers.insert("logout", &OlympusDaemonProcess::handleLogOut);
}
void OlympusDaemonProcess::setPort(int port)
{
this->port = port;
}
void OlympusDaemonProcess::setExclusive(bool exclusive)
{
this->exclusive = exclusive;
}
void OlympusDaemonProcess::setTimer(int time)
{
this->time = time;
}
void OlympusDaemonProcess::waitWithoutBlocking(int time)
{
QEventLoop loop;
QTimer::singleShot(time, &loop, SLOT(quit())); // time unit: msec
loop.exec();
}
bool OlympusDaemonProcess::initializeServer()
{
int attempts = 0;
while (!server->openServer(port, exclusive)) {
if (attempts++ > 10) {
return false;
}
}
if (time > 0) {
numDaemonStartServerAttempts = 0;
// Set up a timer to check the server status
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &OlympusDaemonProcess::checkServerStatus);
timer->start(time*1000); // unit milliseconds
qDebug() << "[Daemon] Server status check is enabled with a time interval of" << time << "seconds";
}
else {
qDebug() << "[Daemon] Server status check is disabled";
}
return true;
}
bool OlympusDaemonProcess::initializePortManager()
{
if (!olympusPM) {
QObject::disconnect(server, SIGNAL(requestForOlympus(MessageSocket)),
this, SLOT(receiveException(MessageSocket)));
olympusPM = new OlympusPortManager(this);
if (olympusPM->initialize()) {
QObject::connect(server, SIGNAL(requestForOlympus(MessageSocket)),
olympusPM, SLOT(receiveRequest(MessageSocket)), Qt::QueuedConnection);
QObject::connect(olympusPM, SIGNAL(sendMessageSocket(MessageSocket)),
server, SLOT(receiveResponse(MessageSocket)), Qt::QueuedConnection);
QObject::connect(olympusPM, SIGNAL(sendGlobalQuit()), this, SLOT(quitDaemon()));
return true;
}
else {
quitPortManager();
QObject::connect(server, SIGNAL(requestForOlympus(MessageSocket)),
this, SLOT(receiveException(MessageSocket)), Qt::QueuedConnection);
return false;
}
}
return true;
}
void OlympusDaemonProcess::quitPortManager()
{
if (olympusPM) {
if (olympusPM->isConnected()) {
olympusPM->closeInterface();
while (olympusPM->isConnected())
waitWithoutBlocking(50);
}
delete olympusPM;
olympusPM = nullptr;
}
}
void OlympusDaemonProcess::quitDaemon()
{
quitPortManager();
delete server;
qApp->quit();
}
void OlympusDaemonProcess::checkServerStatus()
{
bool serverStatus = server->isServerOpen();
if (!serverStatus) {
// Server is not running, try to start it
if (!server->openServer(port, exclusive)) {
// Server failed to start
if (++numDaemonStartServerAttempts > 10) {
qCritical() << "[Daemon] Unable to open server on port" << port
<< "Please check firewall configurations.";
::ShowWindow(::GetConsoleWindow(), SW_SHOW);
std::cout << "Press any key to exit..." << std::endl;
_getch();
quitDaemon();
}
} else {
// Server started successfully
numDaemonStartServerAttempts = 0;
}
} else {
// Server is running
numDaemonStartServerAttempts = 0;
}
}
void OlympusDaemonProcess::receiveRequest(MessageSocket ms)
{
// Get the request string and remove any leading or trailing whitespace
QString request = ms.request.trimmed().toLower();
// Use a regular expression to extract the numeric part of the request
QRegularExpressionMatch match = numericRegex.match(request);
if (match.hasMatch()) {
QString numericPart = match.captured(1);
request.chop(numericPart.length());
}
// Look up the handler function using the modified request string
RequestHandler handler = handlers.value(request, &OlympusDaemonProcess::handleDefault);
// Call the handler function with the message socket
(this->*handler)(ms);
}
void OlympusDaemonProcess::receiveException(MessageSocket ms)
{
ms.response = "Error:-8081:The port manager is not running. "
"Please use '{Daemon}InitializePortManager' to initialize port manager first.";
emit sendResponse(ms);
}
void OlympusDaemonProcess::handleDefault(MessageSocket ms)
{
ms.response = "Successfully reached the daemon!\n"
"The daemon accepts the following requests:\n"
" - {Daemon}InitializePortManager\n"
" - {Daemon}IsConnected\n"
" - {Daemon}EnumerateInterface\n"
" - {Daemon}OpenInterfaceX\n"
" - {Daemon}CloseInterface\n"
" - {Daemon}LogIn\n"
" - {Daemon}LogOut\n"
" - {Daemon}QuitPortManager\n"
" - {Daemon}QuitDaemon\n"
"These commands are not case-sensitive, "
"but please note that the 'OpenInterfaceX' "
"command must include the interface index X, "
"such as {Daemon}OpenInterface0.";
emit sendResponse(ms);
}
void OlympusDaemonProcess::handleQuitDaemon(MessageSocket ms)
{
ms.response = "Daemon is quitting now.";
emit sendResponse(ms);
QTimer::singleShot(100, this, SLOT(quitDaemon()));
}
void OlympusDaemonProcess::handleQuitPortManager(MessageSocket ms)
{
quitPortManager();
ms.response = "1";
emit sendResponse(ms);
}
void OlympusDaemonProcess::handleShowConsole(MessageSocket ms)
{
::ShowWindow(::GetConsoleWindow(), SW_SHOW);
ms.response = "1";
emit sendResponse(ms);
}
void OlympusDaemonProcess::handleHideConsole(MessageSocket ms)
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
ms.response = "1";
emit sendResponse(ms);
}
void OlympusDaemonProcess::handleInitializePortManager(MessageSocket ms)
{
if (olympusPM) {
ms.response = "1";
}
else
{
if (initializePortManager())
ms.response = "1";
else
ms.response = "Error:-8080:Port Manager initialization failed. "
"Please check if the DLL files are intact. "
"For more details, please refer to the error message "
"on the server console.";
}
emit sendResponse(ms);
}
void OlympusDaemonProcess::handleEnumerateInterface(MessageSocket ms)
{
if (olympusPM) {
int numInterface = olympusPM->enumerateInterface();
ms.response = QString::number(numInterface);
}
else {
ms.response = "Error:-8081:The port manager is not running. "
"Please use '{Daemon}InitializePortManager' to "
"initialize port manager first.";
}
emit sendResponse(ms);
}
void OlympusDaemonProcess::handleOpenInterface(MessageSocket ms)
{
if (olympusPM) {
QString request = ms.request.trimmed().toLower();
QRegularExpressionMatch match = numericRegex.match(request);
if (match.hasMatch()) {
QStringList capturedTexts = match.capturedTexts();
int interfaceIndex = capturedTexts.at(1).toInt();
qDebug() << "[Server] Receive request to open interface" << interfaceIndex;
if (olympusPM->isConnected()) {
ms.response = "Error:-8084:An interface is opened. "
"Before opening a new interface, "
"please use {Daemon}CloseInterface to close the older first.";
}
else {
if (olympusPM->openInterface(interfaceIndex)) {
client = new Client(this);
client->setPort(port+1);
client->initialize();
QObject::connect(olympusPM, SIGNAL(sendNotify(QString)),
client, SLOT(receiveNotify(QString)), Qt::QueuedConnection);
ms.response = "1";
}
else {
ms.response = "Error:-8082:Failed to open interface "
+ QString::number(interfaceIndex)
+ ". Please refer to the error message on the server console for more details.";
}
}
}
else {
qWarning() << "Request to open interface, but without a index";
ms.response = "Error:-8083:The open interface message should "
"specify the interface index number.";
}
}
else {
ms.response = "Error:-8081:The port manager is not running. "
"Please use '{Daemon}InitializePortManager' to initialize port manager first.";
}
emit sendResponse(ms);
}
void OlympusDaemonProcess::handleCloseInterface(MessageSocket ms)
{
if (olympusPM) {
olympusPM->closeInterface();
}
if (client) {
delete client;
client = nullptr;
}
ms.response = "1";
emit sendResponse(ms);
}
void OlympusDaemonProcess::handleIsConnected(MessageSocket ms)
{
if (olympusPM) {
if (olympusPM->isConnected())
ms.response = "1";
else
ms.response = "0";
}
else {
ms.response = "Error:-8081:The port manager is not running. "
"Please use '{Daemon}InitializePortManager' to "
"initialize port manager first.";
}
emit sendResponse(ms);
}
void OlympusDaemonProcess::handleLogIn(MessageSocket ms)
{
if (olympusPM) {
if (olympusPM->isConnected()) {
olympusPM->logIn();
while (!olympusPM->isLogged()) {
waitWithoutBlocking(50);
}
ms.response = "1";
}
else {
ms.response = "Error:-8085:No interface was opened. "
"Please use '{Daemon}OpenInterfaceX' to "
"open an interface first.";
}
}
else {
ms.response = "Error:-8081:The port manager is not running. "
"Please use '{Daemon}InitializePortManager' to "
"initialize port manager first.";
}
emit sendResponse(ms);
}
void OlympusDaemonProcess::handleLogOut(MessageSocket ms)
{
if (olympusPM) {
if (olympusPM->isConnected()) {
olympusPM->logOut();
while (olympusPM->isLogged()) {
waitWithoutBlocking(50);
}
ms.response = "1";
}
else {
ms.response = "Error:-8085:No interface was opened. "
"Please use '{Daemon}OpenInterfaceX' to "
"open an interface first.";
}
}
else {
ms.response = "Error:-8081:The port manager is not running. "
"Please use '{Daemon}InitializePortManager' to "
"initialize port manager first.";
}
emit sendResponse(ms);
}