Initial commit
This commit is contained in:
commit
b7d90b9afe
16245 files changed
+3396666
No files matched your search
Executable
+224
@@ -0,0 +1,224 @@
|
||||
#include "Server.h"
|
||||
|
||||
#include <QWebSocketServer>
|
||||
#include <QWebSocket>
|
||||
#include <QDateTime>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <QCoreApplication>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
Server::Server()
|
||||
: QObject(nullptr)
|
||||
, m_server(new QWebSocketServer(QString(), QWebSocketServer::NonSecureMode, this))
|
||||
{
|
||||
qsrand(time(nullptr));
|
||||
}
|
||||
|
||||
|
||||
qint64 randomBetween(qint64 min, qint64 max)
|
||||
{
|
||||
qint64 val = qrand(); // qrand returns int (32 bit)
|
||||
val <<= 32;
|
||||
val |= qrand();
|
||||
return val % (max - min) + min;
|
||||
}
|
||||
|
||||
|
||||
bool Server::init(quint16 port)
|
||||
{
|
||||
if (QCoreApplication::arguments().count() < 3)
|
||||
{
|
||||
qWarning() << "usage:" << QCoreApplication::arguments().at(0) << "<event count> <hours backlog> (example: '30000 48' which creates 30.000 events between now and a 48 hour backlog)";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool k = true;
|
||||
int events = QCoreApplication::arguments().at(1).toInt(&k);
|
||||
|
||||
if (!k)
|
||||
{
|
||||
qWarning() << "invalid event count" << QCoreApplication::arguments().at(1);
|
||||
return false;
|
||||
}
|
||||
|
||||
int hours = QCoreApplication::arguments().at(2).toInt(&k);
|
||||
|
||||
if (!k)
|
||||
{
|
||||
qWarning() << "invalid hours backlog" << QCoreApplication::arguments().at(2);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool createDurationEvents = false;
|
||||
if (QCoreApplication::arguments().contains(QStringLiteral("duration")))
|
||||
{
|
||||
createDurationEvents = true;
|
||||
qWarning() << "creating duration events";
|
||||
}
|
||||
|
||||
|
||||
// create fake events
|
||||
QVector<qint64> times;
|
||||
times.reserve(events);
|
||||
|
||||
qWarning() << "creating"<<events<<"random events between now and a" << hours << "hours backlog..";
|
||||
const QDateTime now(QDateTime::currentDateTime());
|
||||
for (int i = 0; i < events; i++)
|
||||
{
|
||||
QDateTime then(now);
|
||||
then = then.addSecs((-hours) * 60 * 60);
|
||||
|
||||
qint64 time = randomBetween(then.toMSecsSinceEpoch(), now.toMSecsSinceEpoch());
|
||||
times += time;
|
||||
}
|
||||
|
||||
std::sort(times.begin(), times.end());
|
||||
|
||||
|
||||
for (int i = 0; i < events; i++)
|
||||
{
|
||||
qint64 duration = 0;
|
||||
int type = static_cast<int>(randomBetween(0, 5));
|
||||
int partyId = static_cast<int>(randomBetween(1, 4));
|
||||
duration = (createDurationEvents && !!(randomBetween(1, 3) - 1)) ? randomBetween(1, 300000) : 0;
|
||||
m_events.append({ i, type, partyId, times.at(i), duration });
|
||||
}
|
||||
|
||||
|
||||
// for (int i = 0; i < 10; i++)
|
||||
// {
|
||||
// qWarning() << "time" << times.at(i);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
qWarning() << ".. done!";
|
||||
|
||||
|
||||
if (!m_server->listen(QHostAddress::AnyIPv4, port))
|
||||
{
|
||||
qWarning() << "cannot listen on port" << port;
|
||||
return false;
|
||||
}
|
||||
|
||||
qWarning() << "server running on port" << port;
|
||||
|
||||
|
||||
connect(m_server, &QWebSocketServer::newConnection,
|
||||
this, [this]()
|
||||
{
|
||||
QWebSocket *client = m_server->nextPendingConnection();
|
||||
|
||||
if (client == nullptr)
|
||||
{
|
||||
qWarning() << "invalid client";
|
||||
return;
|
||||
}
|
||||
|
||||
qWarning() << "----------------------------------------------------";
|
||||
qWarning() << "new connection";
|
||||
|
||||
connect(client, &QWebSocket::disconnected,
|
||||
this, [client]()
|
||||
{
|
||||
client->deleteLater();
|
||||
});
|
||||
|
||||
const QUrl requestUrl = client->requestUrl();
|
||||
const QString requestPath = requestUrl.path();
|
||||
|
||||
qWarning() << "request" << requestUrl.toString();
|
||||
|
||||
if (requestPath != QStringLiteral("/request"))
|
||||
{
|
||||
qWarning() << "Client: only requests supported";
|
||||
client->close();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const QUrlQuery query(requestUrl);
|
||||
//const int requestId = query.queryItemValue(QStringLiteral("requestId")).toInt();
|
||||
const qint64 from = query.queryItemValue(QStringLiteral("from")).toLongLong();
|
||||
const qint64 to = query.queryItemValue(QStringLiteral("to")).toLongLong();
|
||||
const QString typesString = query.queryItemValue(QStringLiteral("eventTypeId"));
|
||||
|
||||
bool k = true;
|
||||
int limit = query.queryItemValue(QStringLiteral("limit")).toInt(&k);
|
||||
if (!k)
|
||||
limit = -1;
|
||||
|
||||
QStringList typesStringList(typesString.split(QLatin1Char(','), QString::SkipEmptyParts));
|
||||
|
||||
qWarning() << "new request" << from << to << typesString << "limit" << limit;
|
||||
|
||||
QVector<int> types;
|
||||
|
||||
for (const QString &s : qAsConst(typesStringList))
|
||||
{
|
||||
bool k = true;
|
||||
int type = s.toInt(&k);
|
||||
|
||||
if (!k)
|
||||
continue;
|
||||
|
||||
types += type;
|
||||
}
|
||||
|
||||
|
||||
QVector<Event> eventsToSend;
|
||||
|
||||
qint64 eventCount = 0;
|
||||
for (const Event &e : qAsConst(m_events))
|
||||
{
|
||||
if (e.time >= from && e.time <= to && (types.isEmpty() || types.contains(e.type)))
|
||||
{
|
||||
eventsToSend += e;
|
||||
eventCount++;
|
||||
}
|
||||
|
||||
if (eventCount >= limit && limit != -1)
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
QJsonArray eventArray;
|
||||
|
||||
for (const Event &e : qAsConst(eventsToSend))
|
||||
{
|
||||
QJsonObject eObj;
|
||||
|
||||
//int id = eventObj[QStringLiteral("id")].toInt(-1);
|
||||
//int eventTypeId = eventObj[QStringLiteral("eventTypeId")].toInt(-1);
|
||||
//qint64 startTime = eventObj[QStringLiteral("sampleTime")].toDouble(-1);
|
||||
|
||||
eObj[QStringLiteral("id")] = e.id;
|
||||
eObj[QStringLiteral("eventTypeId")] = e.type;
|
||||
eObj[QStringLiteral("partyId")] = e.partyId;
|
||||
eObj[QStringLiteral("sampleTime")] = e.time;
|
||||
eObj[QStringLiteral("properties")] = QStringLiteral("{ \"endTime\": %1 }").arg(e.time + e.duration);
|
||||
|
||||
eventArray += eObj;
|
||||
}
|
||||
|
||||
qWarning() << "sending" << eventArray.count() << "events";
|
||||
|
||||
QJsonDocument doc(eventArray);
|
||||
client->sendTextMessage(QString::fromUtf8(doc.toJson(QJsonDocument::Compact)));
|
||||
client->sendTextMessage(QStringLiteral("{}"));
|
||||
|
||||
});
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
Reference in new issue
Block a user