some improvements

This commit is contained in:
2017-04-07 00:28:49 +03:00
parent e0833f22a5
commit baa20af363
30 changed files with 712 additions and 215 deletions

View File

@ -50,12 +50,22 @@ void QueuedServer::init()
{
deinit();
m_server = new QueuedTcpServer(this);
m_server->listen(
QHostAddress(QueuedCoreAdaptor::getOption("ServerAddress").toString()),
QueuedCoreAdaptor::getOption("ServerPort").toUInt());
m_server
= new QueuedTcpServer(QueuedCoreAdaptor::getOption(
QueuedConfig::QueuedSettings::ServerTimeout)
.toInt(),
this);
QString address = QueuedCoreAdaptor::getOption(
QueuedConfig::QueuedSettings::ServerAddress)
.toString();
ushort port
= QueuedCoreAdaptor::getOption(QueuedConfig::QueuedSettings::ServerPort)
.toUInt();
m_server->listen(QHostAddress(address), port);
m_server->setMaxPendingConnections(
QueuedCoreAdaptor::getOption("ServerMaxConnections").toInt());
QueuedCoreAdaptor::getOption(
QueuedConfig::QueuedSettings::ServerMaxConnections)
.toInt());
qCInfo(LOG_SERV) << "Server listen on" << m_server->serverAddress()
<< m_server->serverPort();

View File

@ -21,8 +21,9 @@
#include "QueuedTcpServerThread.h"
QueuedTcpServer::QueuedTcpServer(QObject *parent)
QueuedTcpServer::QueuedTcpServer(const int timeout, QObject *parent)
: QTcpServer(parent)
, m_timeout(timeout)
{
qCDebug(LOG_SERV) << __PRETTY_FUNCTION__;
}
@ -50,7 +51,7 @@ void QueuedTcpServer::init()
void QueuedTcpServer::incomingConnection(qintptr socketDescriptor)
{
QueuedTcpServerThread *thread
= new QueuedTcpServerThread(socketDescriptor, this);
= new QueuedTcpServerThread(socketDescriptor, m_timeout, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}

View File

@ -25,7 +25,7 @@ class QueuedTcpServer : public QTcpServer
Q_OBJECT
public:
explicit QueuedTcpServer(QObject *parent);
explicit QueuedTcpServer(const int timeout, QObject *parent);
virtual ~QueuedTcpServer();
void deinit();
void init();
@ -34,6 +34,7 @@ protected:
void incomingConnection(qintptr socketDescriptor) override;
private:
int m_timeout = -1;
};

View File

@ -88,10 +88,16 @@ QueuedTcpServerResponseHelper::pathToEnum(const QString &_path)
return RequestPath::Plugins;
else if (_path == "reports")
return RequestPath::Reports;
else if (_path == "status")
return RequestPath::Status;
else if (_path == "task")
return RequestPath::Task;
else if (_path == "tasks")
return RequestPath::Tasks;
else if (_path == "user")
return RequestPath::User;
else if (_path == "users")
return RequestPath::Users;
return RequestPath::Unknown;
}

View File

@ -29,8 +29,11 @@ enum class RequestPath {
Permissions,
Plugins,
Reports,
Status,
Task,
User
Tasks,
User,
Users
};
typedef struct {
int apiVersion;

View File

@ -22,6 +22,8 @@
#include "QueuedTcpServerResponseHelperOption.h"
#include "QueuedTcpServerResponseHelperPermissions.h"
#include "QueuedTcpServerResponseHelperPlugins.h"
#include "QueuedTcpServerResponseHelperTask.h"
#include "QueuedTcpServerResponseHelperUser.h"
QVariantHash QueuedTcpServerResponseHelperApi1::getData(
@ -77,6 +79,75 @@ QVariantHash QueuedTcpServerResponseHelperApi1::getData(
else
output = {{"code", 405}};
break;
case QueuedTcpServerResponseHelper::RequestPath::Reports:
if (_type == "GET")
output
= QueuedTcpServerResponseHelperUser::getReport(_data, _token);
else
output = {{"code", 405}};
break;
case QueuedTcpServerResponseHelper::RequestPath::Status:
if (_type == "GET")
output = getStatus();
else
output = {{"code", 405}};
break;
case QueuedTcpServerResponseHelper::RequestPath::Task:
if (_type == "GET")
output = QueuedTcpServerResponseHelperTask::getTask(
_arg.toLongLong(), _data);
else if (_type == "POST")
output = QueuedTcpServerResponseHelperTask::addOrEditTask(
_arg.toLongLong(), _data, _token);
else
output = {{"code", 405}};
break;
case QueuedTcpServerResponseHelper::RequestPath::Tasks:
if (_type == "GET")
output = QueuedTcpServerResponseHelperTask::getTasks(_data, _token);
else
output = {{"code", 405}};
break;
break;
case QueuedTcpServerResponseHelper::RequestPath::User:
if (_type == "GET")
output = QueuedTcpServerResponseHelperUser::getUser(_arg, _data);
else if (_type == "POST")
output = QueuedTcpServerResponseHelperUser::addOrEditUser(
_arg, _data, _token);
else
output = {{"code", 405}};
break;
case QueuedTcpServerResponseHelper::RequestPath::Users:
if (_type == "GET")
output = QueuedTcpServerResponseHelperUser::getUsers(_data, _token);
else
output = {{"code", 405}};
break;
case QueuedTcpServerResponseHelper::RequestPath::Unknown:
output = {{"code", 404}};
break;
}
return output;
}
QVariantHash QueuedTcpServerResponseHelperApi1::getStatus()
{
QVariantHash output = {{"code", 200}};
auto data = QueuedCoreAdaptor::getStatus();
auto sections = data.keys();
sections.sort();
for (auto &section : sections) {
QVariantHash sectionData;
auto keys = data[section].keys();
keys.sort();
for (auto &key : keys)
sectionData[key] = data[section][key];
// append output
output[section] = sectionData;
}
return output;

View File

@ -27,6 +27,7 @@ namespace QueuedTcpServerResponseHelperApi1
QVariantHash getData(const QueuedTcpServerResponseHelper::RequestPath _request,
const QString &_arg, const QString &_type,
const QVariantHash &_data, const QString &_token);
QVariantHash getStatus();
};

View File

@ -33,9 +33,8 @@ QueuedTcpServerResponseHelperPlugins::addPlugin(const QString &_name,
QVariantHash QueuedTcpServerResponseHelperPlugins::listPlugins()
{
return {{"code", 200},
{"plugins",
QueuedCoreAdaptor::getOption(QueuedAdvancedSettings::internalId(
QueuedConfig::QueuedSettings::Plugins))}};
{"plugins", QueuedCoreAdaptor::getOption(
QueuedConfig::QueuedSettings::Plugins)}};
}

View File

@ -0,0 +1,113 @@
/*
* Copyright (c) 2017 Evgeniy Alekseev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#include "QueuedTcpServerResponseHelperTask.h"
#include <queued/Queued.h>
QVariantHash QueuedTcpServerResponseHelperTask::addOrEditTask(
const long long _id, const QVariantHash &_data, const QString &_token)
{
qCDebug(LOG_SERV) << "Add or edit task" << _id << "with data" << _data;
auto defs = getDefinitions(_data);
if (_id > 0) {
// edit existing task
bool status = QueuedCoreAdaptor::sendTaskEdit(_id, defs, _token);
return {{"code", status ? 200 : 400}};
} else {
// add new task
auto id = QueuedCoreAdaptor::sendTaskAdd(defs, _token);
return {{"code", id > 0 ? 200 : 400}, {"id", id}};
}
}
QueuedProcess::QueuedProcessDefinitions
QueuedTcpServerResponseHelperTask::getDefinitions(const QVariantHash &_data)
{
qCDebug(LOG_SERV) << "Get definitions from" << _data;
QueuedProcess::QueuedProcessDefinitions defs;
auto args = _data["arguments"].toList();
for (auto &arg : args)
defs.arguments.append(arg.toString());
defs.command = _data["command"].toString();
defs.endTime
= QDateTime::fromString(_data["end"].toString(), Qt::ISODateWithMs);
defs.gid = _data["gid"].toUInt();
defs.nice = _data["nice"].toUInt();
defs.startTime
= QDateTime::fromString(_data["start"].toString(), Qt::ISODateWithMs);
defs.uid = _data["uid"].toUInt();
defs.user = _data["user"].toLongLong();
defs.workingDirectory = _data["workingDirectory"].toString();
// limits
QueuedLimits::Limits limits;
limits.cpu = _data["limitCpu"].toLongLong();
limits.gpu = _data["limitGpu"].toLongLong();
limits.memory = _data["limitMemory"].toLongLong();
limits.gpumemory = _data["limitGpumemory"].toLongLong();
limits.storage = _data["limitStorage"].toLongLong();
defs.limits = limits.toString();
return defs;
}
QVariantHash
QueuedTcpServerResponseHelperTask::getTask(const long long _id,
const QVariantHash &_data)
{
qCDebug(LOG_SERV) << "Get task" << _id << _data;
auto property = _data["property"].toString();
QVariantHash output = {{"code", 200}};
// some conversion magic
auto value = QueuedCoreAdaptor::getTask(_id, property);
if (property.isEmpty())
output["properties"] = qdbus_cast<QVariantHash>(value);
else
output["properties"] = QVariantHash({{property, value}});
return output;
}
QVariantHash
QueuedTcpServerResponseHelperTask::getTasks(const QVariantHash &_data,
const QString &_token)
{
qCDebug(LOG_SERV) << "Get tasks" << _data;
long long user = _data.value("userId").toLongLong();
QDateTime start
= QDateTime::fromString(_data["start"].toString(), Qt::ISODateWithMs);
QDateTime stop
= QDateTime::fromString(_data["stop"].toString(), Qt::ISODateWithMs);
QVariantHash output = {{"code", 200}};
// some conversion magic
QVariantList outputReport;
auto report = QueuedCoreAdaptor::getTasks(user, start, stop, _token);
for (auto &user : report)
outputReport.append(user);
output["report"] = outputReport;
return output;
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2017 Evgeniy Alekseev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#ifndef QUEUEDTCPSERVERRESPONSEHELTASK_H
#define QUEUEDTCPSERVERRESPONSEHELTASK_H
#include <QVariant>
#include <queued/QueuedProcess.h>
namespace QueuedTcpServerResponseHelperTask
{
QVariantHash addOrEditTask(const long long _id, const QVariantHash &_data,
const QString &_token);
QueuedProcess::QueuedProcessDefinitions
getDefinitions(const QVariantHash &_data);
QVariantHash getTask(const long long _id, const QVariantHash &_data);
QVariantHash getTasks(const QVariantHash &_data, const QString &_token);
};
#endif /* QUEUEDTCPSERVERRESPONSEHELTASK_H */

View File

@ -0,0 +1,130 @@
/*
* Copyright (c) 2017 Evgeniy Alekseev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#include "QueuedTcpServerResponseHelperUser.h"
#include <queued/Queued.h>
QVariantHash QueuedTcpServerResponseHelperUser::addOrEditUser(
const QString &_user, const QVariantHash &_data, const QString &_token)
{
qCDebug(LOG_SERV) << "Add user" << _user << "with data" << _data;
// try define if user exists first
auto userId = QueuedCoreAdaptor::getUserId(_user);
auto defs = getDefinitions(_data);
defs.name = _user;
if (userId > 0) {
// edit existing user
bool status = QueuedCoreAdaptor::sendUserEdit(userId, defs, _token);
return {{"code", status ? 200 : 400}};
} else {
// add new user
auto id = QueuedCoreAdaptor::sendUserAdd(defs, _token);
return {{"code", id > 0 ? 200 : 400}, {"id", id}};
}
}
QueuedUser::QueuedUserDefinitions
QueuedTcpServerResponseHelperUser::getDefinitions(const QVariantHash &_data)
{
qCDebug(LOG_SERV) << "Generate definitions from" << _data;
QueuedUser::QueuedUserDefinitions defs;
defs.email = _data["email"].toString();
defs.password = QueuedUser::hashFromPassword(_data["password"].toString());
defs.permissions = _data["permissions"].toUInt();
// limits
QueuedLimits::Limits limits;
limits.cpu = _data["limitCpu"].toLongLong();
limits.gpu = _data["limitGpu"].toLongLong();
limits.memory = _data["limitMemory"].toLongLong();
limits.gpumemory = _data["limitGpumemory"].toLongLong();
limits.storage = _data["limitStorage"].toLongLong();
defs.limits = limits.toString();
return defs;
}
QVariantHash
QueuedTcpServerResponseHelperUser::getReport(const QVariantHash &_data,
const QString &_token)
{
qCDebug(LOG_SERV) << "Get report using payload" << _data;
QDateTime stop
= QDateTime::fromString(_data["stop"].toString(), Qt::ISODateWithMs);
QDateTime start
= QDateTime::fromString(_data["start"].toString(), Qt::ISODateWithMs);
QVariantHash output = {{"code", 200}};
// some conversion magic
QVariantList outputReport;
auto report = QueuedCoreAdaptor::getPerformance(start, stop, _token);
for (auto &user : report)
outputReport.append(user);
output["report"] = outputReport;
return output;
}
QVariantHash
QueuedTcpServerResponseHelperUser::getUser(const QString &_user,
const QVariantHash &_data)
{
qCDebug(LOG_SERV) << "Get user data for" << _user << _data;
auto userId = QueuedCoreAdaptor::getUserId(_user);
auto property = _data["property"].toString();
QVariantHash output = {{"code", 200}};
// some conversion magic
auto value = QueuedCoreAdaptor::getUser(userId, property);
if (property.isEmpty())
output["properties"] = qdbus_cast<QVariantHash>(value);
else
output["properties"] = QVariantHash({{property, value}});
return output;
}
QVariantHash
QueuedTcpServerResponseHelperUser::getUsers(const QVariantHash &_data,
const QString &_token)
{
qCDebug(LOG_SERV) << "Get users" << _data;
QDateTime lastLogin = QDateTime::fromString(_data["lastLogged"].toString(),
Qt::ISODateWithMs);
auto permission
= QueuedEnums::stringToPermission(_data["permission"].toString());
QVariantHash output = {{"code", 200}};
// some conversion magic
QVariantList outputReport;
auto report = QueuedCoreAdaptor::getUsers(lastLogin, permission, _token);
for (auto &user : report)
outputReport.append(user);
output["report"] = outputReport;
return output;
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2017 Evgeniy Alekseev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#ifndef QUEUEDTCPSERVERRESPONSEHELUSER_H
#define QUEUEDTCPSERVERRESPONSEHELUSER_H
#include <QVariant>
#include <queued/QueuedUser.h>
namespace QueuedTcpServerResponseHelperUser
{
QVariantHash addOrEditUser(const QString &_user, const QVariantHash &_data,
const QString &_token);
QueuedUser::QueuedUserDefinitions getDefinitions(const QVariantHash &_data);
QVariantHash getReport(const QVariantHash &_data, const QString &_token);
QVariantHash getUser(const QString &_user, const QVariantHash &_data);
QVariantHash getUsers(const QVariantHash &_data, const QString &_token);
};
#endif /* QUEUEDTCPSERVERRESPONSEHELUSER_H */

View File

@ -28,9 +28,10 @@
QueuedTcpServerThread::QueuedTcpServerThread(int socketDescriptor,
QObject *parent)
const int timeout, QObject *parent)
: QThread(parent)
, m_socketDescriptor(socketDescriptor)
, m_timeout(timeout)
{
qCDebug(LOG_SERV) << __PRETTY_FUNCTION__;
}
@ -39,6 +40,9 @@ QueuedTcpServerThread::QueuedTcpServerThread(int socketDescriptor,
QueuedTcpServerThread::~QueuedTcpServerThread()
{
qCDebug(LOG_SERV) << __PRETTY_FUNCTION__;
if (m_socket)
m_socket->deleteLater();
}
@ -133,7 +137,7 @@ QueuedTcpServerThread::QueuedTcpServerRequest QueuedTcpServerThread::getRequest(
values = QVariantList({request.data[key]});
break;
}
values.append(key);
values.append(value);
request.data[key] = values.count() == 1 ? values.first() : values;
}
@ -182,7 +186,7 @@ QueuedTcpServerThread::response(const QueuedTcpServerRequest &request) const
void QueuedTcpServerThread::run()
{
m_socket = new QTcpSocket(this);
m_socket = new QTcpSocket(nullptr);
if (!m_socket->setSocketDescriptor(m_socketDescriptor)) {
qCWarning(LOG_SERV) << "Socket error" << m_socket->error();
return;
@ -190,20 +194,11 @@ void QueuedTcpServerThread::run()
connect(m_socket, SIGNAL(readyRead()), this, SLOT(readyRead()),
Qt::DirectConnection);
connect(m_socket, SIGNAL(disconnected()), this, SLOT(disconnected()),
Qt::DirectConnection);
exec();
}
void QueuedTcpServerThread::disconnected()
{
m_socket->deleteLater();
exit(0);
}
void QueuedTcpServerThread::readyRead()
{
QStringList headers;
@ -222,8 +217,12 @@ void QueuedTcpServerThread::readyRead()
m_socket->write(resp);
m_socket->flush();
m_socket->waitForBytesWritten(3000);
// TODO use timeouts?
if (m_socket->state() != QAbstractSocket::UnconnectedState)
m_socket->waitForBytesWritten(m_timeout);
m_socket->disconnectFromHost();
if (m_socket->state() != QAbstractSocket::UnconnectedState)
m_socket->waitForDisconnected();
exit(0);
}

View File

@ -44,7 +44,8 @@ public:
QVariantHash data;
} QueuedTcpServerResponse;
explicit QueuedTcpServerThread(int socketDescriptor, QObject *parent);
explicit QueuedTcpServerThread(int socketDescriptor, const int timeout,
QObject *parent);
virtual ~QueuedTcpServerThread();
static QByteArrayList defaultResponse(const int code,
const QVariantHash &json);
@ -56,12 +57,12 @@ public:
void run() override;
private slots:
void disconnected();
void readyRead();
private:
QTcpSocket *m_socket = nullptr;
int m_socketDescriptor;
int m_timeout = -1;
};