-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaserveroverseer.cpp
More file actions
313 lines (262 loc) · 8.7 KB
/
aserveroverseer.cpp
File metadata and controls
313 lines (262 loc) · 8.7 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
#include "aserveroverseer.h"
#include "awebsocketserver.h"
#include <QProcess>
#include <QDebug>
#include <QJsonDocument>
#include <QFile>
#include <QFileInfo>
#include <QJsonObject>
#include <QJsonArray>
AServerOverseer::~AServerOverseer()
{
delete server;
}
void AServerOverseer::SetIP(const QString &ip)
{
IP = QHostAddress(ip);
}
bool AServerOverseer::ConfigureFromFile(const QString &fileName)
{
QFile f(fileName);
if (f.open(QIODevice::ReadOnly))
{
qDebug() << "Reading options from config file:" << fileName;
QByteArray ba = f.readAll();
QJsonDocument loadDoc(QJsonDocument::fromJson(ba));
QJsonObject json = loadDoc.object();
ServerApp = json["server"].toString();
QJsonArray ar = json["server_arguments"].toArray();
for (int i=0; i<ar.size(); i++) Arguments << ar[i].toString();
Port = json["port"].toInt();
QString ips = json["ip"].toString();
IP = QHostAddress(ips);
ar = json["server_ports"].toArray();
for (int i=0; i<ar.size(); i++) AllocatedPorts << ar[i].toInt();
MaxThreads = json["threads"].toInt();
f.close();
return true;
}
else return false;
}
bool AServerOverseer::StartListen()
{
if ( !isReady() ) return false;
server = new AWebSocketServer(this);
QObject::connect(server, &AWebSocketServer::textMessageReceived, this, &AServerOverseer::onMessageReceived);
server->StartListen(IP, Port);
return true;
}
const QString AServerOverseer::GetListOfPorts()
{
QString res;
QList<int> l;
for (auto i : AllocatedPorts) l << i;
qSort(l.begin(), l.end());
for (auto i : l)
{
if ( !res.isEmpty() ) res += ',';
res += QString::number(i);
}
return res;
}
bool AServerOverseer::isReady()
{
if (ServerApp.isEmpty())
{
qDebug() << "Server application was not provided";
return false;
}
QFileInfo fi(ServerApp);
if (!fi.exists())
{
qDebug() << "Not found server application:" << ServerApp;
return false;
}
if ( !fi.permission(QFile::ExeUser) )
{
qDebug() << "Server application: have no permission to execute:" << ServerApp;
return false;
}
if (Port == 0)
{
qDebug() << "Port for requests is not defined!";
return false;
}
if (IP.isNull())
{
qDebug() << "IP for requests is not defined!";
return false;
}
if (AllocatedPorts.isEmpty())
{
qDebug() << "No ports were allocated for the servers!";
return false;
}
if (MaxThreads < 1) MaxThreads = 1;
qDebug() << "Server executable:" << ServerApp;
if (!Arguments.isEmpty()) qDebug() << "Extra arguments:" << Arguments;
qDebug() << "Port for requests:" << Port;
qDebug() << "IP for requests:" << IP.toString();
qDebug() << "Allocated server ports:"<< GetListOfPorts();
qDebug() << "Maximum number of heavy threads:"<<MaxThreads;
return true;
}
void AServerOverseer::onMessageReceived(const QString message)
{
if ( !server->CanReply() ) return; // cannot reply so do not start a new ANTS2 server
QJsonObject jsIn = objectFromString(message);
qDebug() << jsIn;
QJsonObject jsOut;
if (jsIn.contains("command"))
{
QString com = jsIn["command"].toString();
if (com == "new") processCommandNew(jsIn, jsOut);
else if (com == "abort") processCommandAbort(jsIn, jsOut);
else if (com == "report") processCommandReport(jsIn, jsOut);
else if (com == "help") processCommandHelp(jsIn, jsOut);
else
{
jsOut["result"] = false;
jsOut["error"] = "bad format of request. use {\"command\":\"help\"} for info";
}
}
else
{
jsOut["result"] = false;
jsOut["error"] = "bad format of request. example of valid one: {\"command\":\"new\", \"cpus\":1}";
}
server->ReplyAndCloseConnection(jsOut);
}
void AServerOverseer::processCommandNew(const QJsonObject& jsIn, QJsonObject& jsOut)
{
int requestedThreads = 1;
if (jsIn.contains("threads"))
requestedThreads = jsIn["threads"].toInt();
if (requestedThreads < 1) requestedThreads = 1;
if (requestedThreads > MaxThreads)
{
jsOut["result"] = false;
jsOut["error"] = "currently available threads=" + QString::number(MaxThreads);
}
else
{
int port = findFreePort();
if (port == -1)
{
jsOut["result"] = false;
jsOut["error"] = "no free ports";
}
else
{
AServerRecord* sr = startProcess(port, requestedThreads);
jsOut["result"] = true;
jsOut["port"] = port;
jsOut["ticket"] = sr->Ticket;
}
}
}
void AServerOverseer::processCommandAbort(const QJsonObject &jsIn, QJsonObject &jsOut)
{
QString Ticket = jsIn["ticket"].toString();
for (AServerRecord* sr : RunningServers)
if (Ticket == sr->Ticket)
{
sr->Process->close();
jsOut["result"] = true;
return;
}
jsOut["result"] = false;
jsOut["error"] = "server with this ticket not found";
}
void AServerOverseer::processCommandReport(const QJsonObject &jsIn, QJsonObject &jsOut)
{
jsOut["result"] = true;
jsOut["threads"] = MaxThreads;
jsOut["ports"] = AllocatedPorts.size() - RunningServers.size();
jsOut["users"] = RunningServers.size();
}
void AServerOverseer::processCommandHelp(const QJsonObject &jsIn, QJsonObject &jsOut)
{
jsOut["result"] = true;
jsOut["new"] = "{\"command\":\"new\", \"cpus\":x} - threads=number of sim/rec threads to use; reply contains port and ticket";
jsOut["abort"] = "{\"command\":\"abort\"} - ticket=id of the server sesson to abort";
jsOut["report"] = "{\"command\":\"report\"} - returns the available resources (threads and ports)";
}
AServerRecord* AServerOverseer::startProcess(int port, int maxThreads)
{
QString ticket = generateTicket();
QStringList arguments = Arguments;
arguments << "-s" << "-i" << IP.toString() << "-p" << QString::number(port) << "-t" << ticket << "-m" << QString::number(maxThreads);
QProcess *process = new QProcess(this);
AServerRecord* sr = new AServerRecord(port, maxThreads, ticket, 0, process);
MaxThreads -= maxThreads;
RunningServers << sr;
QString str = ServerApp + " ";
for (const QString &s : arguments) str += s + " ";
qDebug() << "Executing command:" << str;
QObject::connect(process, SIGNAL(finished(int)), sr, SLOT(processTerminated()));
QObject::connect(process, SIGNAL(finished(int)), process, SLOT(deleteLater()));
QObject::connect(sr, &AServerRecord::finished, this, &AServerOverseer::oneProcessFinished);
process->start(ServerApp, arguments);
qDebug() << "-->New server started"<<sr;
qDebug() << " Available CPUs:"<<MaxThreads;
return sr;
}
void AServerOverseer::oneProcessFinished(AServerRecord *record)
{
qDebug() << "<--Server process finished" << record;
RunningServers.removeAll(record);
MaxThreads += record->NumCPUs;
qDebug() << " Available CPUs:"<<MaxThreads;
delete record;
}
int AServerOverseer::findFreePort()
{
for (int port : AllocatedPorts)
{
bool bFound = false;
for (AServerRecord* sr : RunningServers)
if (port == sr->Port)
{
bFound = true;
break;
}
if (!bFound) return port;
}
return -1;
}
void AServerRecord::processTerminated()
{
qDebug() << "finished!";
emit finished(this);
}
const QJsonObject AServerOverseer::objectFromString(const QString& in)
{
QJsonObject obj;
QJsonDocument doc = QJsonDocument::fromJson(in.toUtf8());
if ( !doc.isNull() && doc.isObject() ) obj = doc.object();
return obj;
}
const QString AServerOverseer::generateTicket()
{
const QString possibleSymbols("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
QString Ticket;
bool bFound = false;
do
{
for (int i=0; i<4; i++)
{
int index = qrand() % possibleSymbols.length();
QChar nextChar = possibleSymbols.at(index);
Ticket.append(nextChar);
}
for (AServerRecord* sr : RunningServers)
if (Ticket == sr->Ticket)
{
bFound = true;
break;
}
if (!bFound) return Ticket;
}
while (bFound);
}