mirror of
https://github.com/QuasarApp/Heart.git
synced 2025-05-08 23:49:40 +00:00
dev 15
This commit is contained in:
parent
3b5a5e98e4
commit
131e1b4080
@ -42,30 +42,26 @@ include($$PWD/../QuasarAppLib/QuasarLib.pri)
|
||||
|
||||
|
||||
SOURCES += \
|
||||
DataPacakages/abstract.cpp \
|
||||
DataPacakages/abstractdata.cpp \
|
||||
DataPacakages/badrequest.cpp \
|
||||
abstractheader.cpp \
|
||||
abstractnode.cpp \
|
||||
abstractnodeinfo.cpp \
|
||||
abstractpackage.cpp \
|
||||
baseheader.cpp \
|
||||
basenodeinfo.cpp \
|
||||
basepackage.cpp \
|
||||
clientprotocol.cpp
|
||||
clientprotocol.cpp \
|
||||
header.cpp \
|
||||
package.cpp
|
||||
|
||||
|
||||
HEADERS += \
|
||||
DataPacakages/abstract.h \
|
||||
DataPacakages/abstractdata.h \
|
||||
DataPacakages/badrequest.h \
|
||||
abstractheader.h \
|
||||
abstractnode.h \
|
||||
abstractnodeinfo.h \
|
||||
abstractpackage.h \
|
||||
baseheader.h \
|
||||
basenodeinfo.h \
|
||||
basepackage.h \
|
||||
clientprotocol.h \
|
||||
clientprotocol_global.h \
|
||||
config.h
|
||||
config.h \
|
||||
header.h \
|
||||
package.h
|
||||
|
||||
include($$PWD/ClientProtocolIncludes.pri)
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "abstract.h"
|
||||
#include "abstractdata.h"
|
||||
#include <QDataStream>
|
||||
#include <QMap>
|
||||
#include <typeinfo>
|
||||
@ -6,31 +6,25 @@
|
||||
|
||||
namespace ClientProtocol {
|
||||
|
||||
namespace Data {
|
||||
|
||||
|
||||
static QMap<size_t, unsigned char> commandTable = {};
|
||||
|
||||
|
||||
unsigned char Abstract::cmd() const {
|
||||
unsigned char AbstractData::cmd() const {
|
||||
return _cmd;
|
||||
}
|
||||
|
||||
Abstract::Abstract() {
|
||||
size_t hash = typeid(*this).hash_code();
|
||||
if (!commandTable.contains(hash)) {
|
||||
commandTable[hash] = static_cast<unsigned char>(commandTable.size());
|
||||
}
|
||||
_cmd = commandTable[hash];
|
||||
AbstractData::AbstractData() {
|
||||
_cmd = static_cast<unsigned char>(generateId());
|
||||
}
|
||||
|
||||
Abstract::Abstract(const ClientProtocol::BasePackage &package):
|
||||
Abstract() {
|
||||
AbstractData::AbstractData(const ClientProtocol::Package &package):
|
||||
AbstractData() {
|
||||
|
||||
fromBytes(package.data);
|
||||
}
|
||||
|
||||
bool Abstract::fromBytes(const QByteArray &data) {
|
||||
bool AbstractData::fromBytes(const QByteArray &data) {
|
||||
|
||||
if (data.isEmpty())
|
||||
return false;
|
||||
@ -40,15 +34,19 @@ bool Abstract::fromBytes(const QByteArray &data) {
|
||||
return true;
|
||||
}
|
||||
|
||||
QByteArray Abstract::toBytes() const {
|
||||
int AbstractData::generateId() {
|
||||
return typeid(*this).hash_code() % 0xFF;
|
||||
}
|
||||
|
||||
QByteArray AbstractData::toBytes() const {
|
||||
QByteArray res;
|
||||
QDataStream stream(&res, QIODevice::WriteOnly);
|
||||
toStream(stream);
|
||||
return res;
|
||||
}
|
||||
|
||||
bool Abstract::toPackage(BasePackage &package,
|
||||
unsigned char trigeredCommand) const {
|
||||
bool AbstractData::toPackage(Package &package,
|
||||
unsigned char trigeredCommand) const {
|
||||
|
||||
if (!isValid()) {
|
||||
return false;
|
||||
@ -63,20 +61,23 @@ bool Abstract::toPackage(BasePackage &package,
|
||||
return package.isValid();
|
||||
}
|
||||
|
||||
QDataStream &Abstract::fromStream(QDataStream &stream) {
|
||||
QDataStream &AbstractData::fromStream(QDataStream &stream) {
|
||||
stream >> _cmd;
|
||||
return stream;
|
||||
}
|
||||
|
||||
QDataStream &Abstract::toStream(QDataStream &stream) const {
|
||||
QDataStream &AbstractData::toStream(QDataStream &stream) const {
|
||||
stream << _cmd;
|
||||
return stream;
|
||||
}
|
||||
|
||||
bool Abstract::isValid() const {
|
||||
bool AbstractData::isValid() const {
|
||||
return _cmd;
|
||||
}
|
||||
|
||||
}
|
||||
AbstractData::~AbstractData() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,16 +1,14 @@
|
||||
#ifndef ABSTRACTDATA_H
|
||||
#define ABSTRACTDATA_H
|
||||
#include "clientprotocol.h"
|
||||
#include "package.h"
|
||||
|
||||
namespace ClientProtocol {
|
||||
|
||||
namespace Data {
|
||||
|
||||
/**
|
||||
* @brief The AbstractData class
|
||||
* all data packages inherited this class.
|
||||
*/
|
||||
class CLIENTPROTOCOLSHARED_EXPORT Abstract
|
||||
class CLIENTPROTOCOLSHARED_EXPORT AbstractData
|
||||
{
|
||||
private:
|
||||
/**
|
||||
@ -22,13 +20,13 @@ protected:
|
||||
/**
|
||||
* @brief AbstractData
|
||||
*/
|
||||
explicit Abstract();
|
||||
explicit AbstractData();
|
||||
|
||||
/**
|
||||
* @brief AbstractData
|
||||
* @param package
|
||||
*/
|
||||
explicit Abstract(const BasePackage& package);
|
||||
explicit AbstractData(const Package& package);
|
||||
|
||||
/**
|
||||
* @brief fromBytes - private initialisation of object from byte array
|
||||
@ -36,6 +34,12 @@ protected:
|
||||
*/
|
||||
bool fromBytes(const QByteArray&);
|
||||
|
||||
/**
|
||||
* @brief generateId
|
||||
* @return generate cmd function
|
||||
*/
|
||||
virtual int generateId();
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
@ -57,7 +61,7 @@ public:
|
||||
* @param trigeredCommand - old cmd
|
||||
* @return retorn package object created from this object.
|
||||
*/
|
||||
bool toPackage(BasePackage &package, unsigned char trigeredCommand = 0) const;
|
||||
bool toPackage(Package &package, unsigned char trigeredCommand = 0) const;
|
||||
|
||||
/**
|
||||
* @brief fromStream
|
||||
@ -79,11 +83,13 @@ public:
|
||||
*/
|
||||
virtual bool isValid() const;
|
||||
|
||||
virtual ~Abstract();
|
||||
virtual ~AbstractData();
|
||||
|
||||
friend class testSankeServer;
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,10 @@
|
||||
#include "badrequest.h"
|
||||
|
||||
BadRequest::BadRequest()
|
||||
{
|
||||
ClientProtocol::BadRequest::BadRequest():AbstractData() {
|
||||
|
||||
}
|
||||
|
||||
ClientProtocol::BadRequest::BadRequest(const ClientProtocol::Package &package):
|
||||
AbstractData(package) {
|
||||
|
||||
}
|
||||
|
@ -1,16 +1,18 @@
|
||||
#ifndef BADREQUEST_H
|
||||
#define BADREQUEST_H
|
||||
|
||||
#include "abstract.h"
|
||||
#include "abstractdata.h"
|
||||
|
||||
namespace ClientProtocol{
|
||||
|
||||
}
|
||||
|
||||
class BadRequest : public Abstract
|
||||
class BadRequest : public AbstractData
|
||||
{
|
||||
public:
|
||||
BadRequest();
|
||||
explicit BadRequest();
|
||||
explicit BadRequest(const Package& package);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // BADREQUEST_H
|
||||
|
@ -1,9 +1,9 @@
|
||||
#include "abstract.h"
|
||||
#include "abstractnode.h"
|
||||
#include <QSslCertificate>
|
||||
#include <QSslKey>
|
||||
#include <QSslKey>
|
||||
#include <QSslSocket>
|
||||
#include <badrequest.h>
|
||||
#include <quasarapp.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/x509.h>
|
||||
@ -248,7 +248,7 @@ bool AbstractNode::registerSocket(QAbstractSocket *socket) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AbstractNode::parsePackage(const BasePackage &pkg, AbstractNodeInfo *sender) {
|
||||
bool AbstractNode::parsePackage(const Package &pkg, AbstractNodeInfo *sender) {
|
||||
|
||||
if (!pkg.isValid()) {
|
||||
QuasarAppUtils::Params::verboseLog("incomming package is not valid!",
|
||||
@ -269,7 +269,7 @@ bool AbstractNode::parsePackage(const BasePackage &pkg, AbstractNodeInfo *sender
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AbstractNode::sendPackage(const BasePackage &pkg, QAbstractSocket *target) {
|
||||
bool AbstractNode::sendPackage(const Package &pkg, QAbstractSocket *target) {
|
||||
if (!pkg.isValid()) {
|
||||
return false;
|
||||
}
|
||||
@ -292,7 +292,8 @@ bool AbstractNode::sendPackage(const BasePackage &pkg, QAbstractSocket *target)
|
||||
return sendet;
|
||||
}
|
||||
|
||||
bool AbstractNode::sendResponse(const Abstract &resp, quint32 id, const BaseHeader *req) {
|
||||
bool AbstractNode::sendResponse(const AbstractData &resp, quint32 id,
|
||||
const Header *req) {
|
||||
auto client = getInfoPtr(id);
|
||||
|
||||
if (!client) {
|
||||
@ -301,13 +302,21 @@ bool AbstractNode::sendResponse(const Abstract &resp, quint32 id, const BaseHead
|
||||
return false;
|
||||
}
|
||||
|
||||
BasePackage pkg = ;
|
||||
if (!resp.toPackage(pkg, req->command)) {
|
||||
Package pkg;
|
||||
bool convert = false;
|
||||
if (req) {
|
||||
convert = resp.toPackage(pkg, req->command);
|
||||
} else {
|
||||
convert = resp.toPackage(pkg);
|
||||
}
|
||||
|
||||
if (convert) {
|
||||
QuasarAppUtils::Params::verboseLog("Response not sent because dont create package from object",
|
||||
QuasarAppUtils::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (!sendPackage(pkg, client->sct())) {
|
||||
QuasarAppUtils::Params::verboseLog("Response not sent!",
|
||||
QuasarAppUtils::Error);
|
||||
@ -317,7 +326,7 @@ bool AbstractNode::sendResponse(const Abstract &resp, quint32 id, const BaseHead
|
||||
return true;
|
||||
}
|
||||
|
||||
void AbstractNode::badRequest(quint32 address, const BaseHeader &req) {
|
||||
void AbstractNode::badRequest(quint32 address, const Header &req) {
|
||||
auto client = getInfoPtr(address);
|
||||
|
||||
if (!client) {
|
||||
@ -331,20 +340,22 @@ void AbstractNode::badRequest(quint32 address, const BaseHeader &req) {
|
||||
if (!changeTrust(address, REQUEST_ERROR)) {
|
||||
|
||||
QuasarAppUtils::Params::verboseLog("Bad request detected, bud responce command not sendet!"
|
||||
" because karma not changed",
|
||||
" because trust not changed",
|
||||
QuasarAppUtils::Error);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
BasePackage pcg;
|
||||
if (!(pcg.create(Command::BadRequest, Type::Responke, &req))) {
|
||||
auto bad = BadRequest();
|
||||
Package pcg;
|
||||
|
||||
if (!bad.toPackage(pcg, req.command)) {
|
||||
QuasarAppUtils::Params::verboseLog("Bad request detected, bud responce command not sendet!"
|
||||
" because package not created",
|
||||
QuasarAppUtils::Error);
|
||||
};
|
||||
}
|
||||
|
||||
if (!sendPackage(pcg, client->getSct())) {
|
||||
if (!sendPackage(pcg, client->sct())) {
|
||||
|
||||
QuasarAppUtils::Params::verboseLog("Bad request detected, bud responce command not sendet!"
|
||||
" because karma not changed",
|
||||
@ -353,7 +364,7 @@ void AbstractNode::badRequest(quint32 address, const BaseHeader &req) {
|
||||
}
|
||||
|
||||
QuasarAppUtils::Params::verboseLog("Bad request sendet to adderess: " +
|
||||
client->getSct()->peerAddress().toString(),
|
||||
client->sct()->peerAddress().toString(),
|
||||
QuasarAppUtils::Info);
|
||||
}
|
||||
|
||||
@ -502,9 +513,9 @@ void AbstractNode::avelableBytes() {
|
||||
val.pkg.reset();
|
||||
|
||||
memcpy(&val.pkg.hdr,
|
||||
array.data(), sizeof(BaseHeader));
|
||||
array.data(), sizeof(Header));
|
||||
|
||||
val.pkg.data.append(array.mid(sizeof(BaseHeader)));
|
||||
val.pkg.data.append(array.mid(sizeof(Header)));
|
||||
}
|
||||
|
||||
if (val.pkg.isValid()) {
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <QAbstractSocket>
|
||||
#include <QSslConfiguration>
|
||||
#include <QTcpServer>
|
||||
#include "abstractdata.h"
|
||||
|
||||
class QSslCertificate;
|
||||
class QSslKey;
|
||||
@ -26,7 +27,7 @@ enum class SslMode {
|
||||
*/
|
||||
struct NodeInfoData {
|
||||
AbstractNodeInfo info;
|
||||
BasePackage pkg;
|
||||
Package pkg;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -142,7 +143,7 @@ public:
|
||||
SslMode getMode() const;
|
||||
|
||||
signals:
|
||||
void incomingReques(BasePackage pkg, quint32 sender);
|
||||
void incomingReques(Package pkg, quint32 sender);
|
||||
|
||||
|
||||
protected:
|
||||
@ -182,7 +183,7 @@ protected:
|
||||
* @param sender
|
||||
* @return
|
||||
*/
|
||||
virtual bool parsePackage(const BasePackage &pkg, AbstractNodeInfo *sender);
|
||||
virtual bool parsePackage(const Package &pkg, AbstractNodeInfo *sender);
|
||||
|
||||
/**
|
||||
* @brief sendPackage
|
||||
@ -190,7 +191,7 @@ protected:
|
||||
* @param target
|
||||
* @return
|
||||
*/
|
||||
virtual bool sendPackage(const BasePackage &pkg, QAbstractSocket *target);
|
||||
virtual bool sendPackage(const Package &pkg, QAbstractSocket *target);
|
||||
|
||||
/**
|
||||
* @brief sendResponse
|
||||
@ -199,15 +200,15 @@ protected:
|
||||
* @param req
|
||||
* @return
|
||||
*/
|
||||
virtual bool sendResponse(const Abstract& resp, quint32 address,
|
||||
const BaseHeader *req = nullptr);
|
||||
virtual bool sendResponse(const AbstractData& resp, quint32 address,
|
||||
const Header *req = nullptr);
|
||||
|
||||
/**
|
||||
* @brief badRequest
|
||||
* @param address
|
||||
* @param req
|
||||
*/
|
||||
virtual void badRequest(quint32 address, const BaseHeader &req);
|
||||
virtual void badRequest(quint32 address, const Header &req);
|
||||
|
||||
/**
|
||||
* @brief getWorkState
|
||||
|
@ -1,10 +0,0 @@
|
||||
#include "baseheader.h"
|
||||
|
||||
|
||||
namespace ClientProtocol {
|
||||
|
||||
BaseHeader::BaseHeader():AbstractHeader() {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
#ifndef BASEHEADER_H
|
||||
#define BASEHEADER_H
|
||||
|
||||
#include "abstractheader.h"
|
||||
|
||||
|
||||
namespace ClientProtocol {
|
||||
struct CLIENTPROTOCOLSHARED_EXPORT BaseHeader : public AbstractHeader {
|
||||
/**
|
||||
* @brief Header default constructor
|
||||
*/
|
||||
BaseHeader();
|
||||
~BaseHeader() = default;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif // BASEHEADER_H
|
@ -1,9 +0,0 @@
|
||||
#include "basepackage.h"
|
||||
|
||||
namespace ClientProtocol {
|
||||
|
||||
BasePackage::BasePackage():AbstractPackage(){
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
#ifndef BASEPACKAGE_H
|
||||
#define BASEPACKAGE_H
|
||||
|
||||
#include "abstractpackage.h"
|
||||
|
||||
namespace ClientProtocol {
|
||||
/**
|
||||
* @brief The Package struct
|
||||
*/
|
||||
struct CLIENTPROTOCOLSHARED_EXPORT BasePackage: public AbstractPackage {
|
||||
|
||||
BasePackage();
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif // BASEPACKAGE_H
|
@ -1,8 +1,7 @@
|
||||
#ifndef CLIENTPROTOCOL_H
|
||||
#define CLIENTPROTOCOL_H
|
||||
|
||||
#include "baseheader.h"
|
||||
#include "basepackage.h"
|
||||
#include "package.h"
|
||||
#include "config.h"
|
||||
#include "abstractnode.h"
|
||||
#include "abstractnodeinfo.h"
|
||||
|
@ -1,11 +1,11 @@
|
||||
#include "abstractheader.h"
|
||||
#include "header.h"
|
||||
|
||||
namespace ClientProtocol {
|
||||
AbstractHeader::AbstractHeader() {
|
||||
Header::Header() {
|
||||
reset();
|
||||
}
|
||||
|
||||
bool AbstractHeader::isValid() const {
|
||||
bool Header::isValid() const {
|
||||
|
||||
if (sizeof (*this) != 4) {
|
||||
return false;
|
||||
@ -14,7 +14,7 @@ bool AbstractHeader::isValid() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void AbstractHeader::reset() {
|
||||
void Header::reset() {
|
||||
size = 0;
|
||||
command = 0;
|
||||
triggerCommnad = 0;
|
@ -7,28 +7,28 @@ namespace ClientProtocol {
|
||||
* @brief The Header struct 4 byte
|
||||
*/
|
||||
#pragma pack(push, 1)
|
||||
struct CLIENTPROTOCOLSHARED_EXPORT AbstractHeader {
|
||||
struct CLIENTPROTOCOLSHARED_EXPORT Header {
|
||||
/**
|
||||
* @brief size - size of package data (not header)
|
||||
*/
|
||||
unsigned short size;
|
||||
unsigned int size;
|
||||
|
||||
/**
|
||||
* @brief command of pacage
|
||||
*/
|
||||
quint8 command;
|
||||
unsigned int command;
|
||||
|
||||
/**
|
||||
* @brief command of pacage see Command (rquest from client)
|
||||
* the server should write to which command it responds
|
||||
*/
|
||||
quint8 triggerCommnad;
|
||||
unsigned int triggerCommnad;
|
||||
|
||||
/**
|
||||
* @brief Header default constructor
|
||||
*/
|
||||
AbstractHeader();
|
||||
~AbstractHeader() = default;
|
||||
Header();
|
||||
~Header() = default;
|
||||
|
||||
/**
|
||||
* @brief isValid
|
@ -1,12 +1,13 @@
|
||||
#include "abstract.h"
|
||||
#include "abstractpackage.h"
|
||||
#include "abstractdata.h"
|
||||
#include "package.h"
|
||||
|
||||
namespace ClientProtocol {
|
||||
|
||||
AbstractPackage::AbstractPackage() {
|
||||
Package::Package() {
|
||||
reset();
|
||||
}
|
||||
|
||||
bool AbstractPackage::isValid() const {
|
||||
bool Package::isValid() const {
|
||||
if (!hdr.isValid()) {
|
||||
return false;
|
||||
}
|
||||
@ -18,16 +19,16 @@ bool AbstractPackage::isValid() const {
|
||||
return hdr.size == static_cast<unsigned int> (data.size());
|
||||
}
|
||||
|
||||
QByteArray AbstractPackage::toBytes() const {
|
||||
QByteArray Package::toBytes() const {
|
||||
QByteArray res;
|
||||
res.append(reinterpret_cast<char*>(const_cast<AbstractHeader*>(&hdr)),
|
||||
res.append(reinterpret_cast<char*>(const_cast<Header*>(&hdr)),
|
||||
sizeof (hdr));
|
||||
|
||||
res.append(data);
|
||||
return res;
|
||||
}
|
||||
|
||||
void AbstractPackage::reset() {
|
||||
void Package::reset() {
|
||||
hdr.reset();
|
||||
data.clear();
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
#ifndef ABSTRACTPACKAGE_H
|
||||
#define ABSTRACTPACKAGE_H
|
||||
#include "abstractheader.h"
|
||||
#include "header.h"
|
||||
#include "clientprotocol_global.h"
|
||||
|
||||
#include <QByteArray>
|
||||
@ -11,17 +11,17 @@ class Abstract;
|
||||
/**
|
||||
* @brief The Package struct
|
||||
*/
|
||||
struct CLIENTPROTOCOLSHARED_EXPORT AbstractPackage {
|
||||
struct CLIENTPROTOCOLSHARED_EXPORT Package {
|
||||
/**
|
||||
* @brief hdr - header of package
|
||||
*/
|
||||
AbstractHeader hdr;
|
||||
Header hdr;
|
||||
/**
|
||||
* @brief data - source data of package
|
||||
*/
|
||||
QByteArray data;
|
||||
|
||||
AbstractPackage();
|
||||
Package();
|
||||
|
||||
/**
|
||||
* @brief isValid
|
||||
@ -40,7 +40,7 @@ struct CLIENTPROTOCOLSHARED_EXPORT AbstractPackage {
|
||||
*/
|
||||
virtual void reset();
|
||||
|
||||
virtual ~AbstractPackage() = default;
|
||||
virtual ~Package() = default;
|
||||
|
||||
};
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QCryptographicHash>
|
||||
#include <clientprotocol.h>
|
||||
#include <abstractdata.h>
|
||||
#include <badrequest.h>
|
||||
|
||||
#include "testutils.h"
|
||||
|
||||
@ -25,6 +27,7 @@ public:
|
||||
|
||||
private slots:
|
||||
void initTestCase();
|
||||
void testPakageData();
|
||||
void testAbstractNode();
|
||||
|
||||
};
|
||||
@ -42,6 +45,15 @@ void testSankeServer::initTestCase() {
|
||||
// ClientProtocol::initClientProtockol();
|
||||
}
|
||||
|
||||
void testSankeServer::testPakageData() {
|
||||
ClientProtocol::BadRequest bad;
|
||||
ClientProtocol::BadRequest bad1;
|
||||
ClientProtocol::BadRequest bad2;
|
||||
|
||||
QVERIFY(bad.cmd() == bad1.cmd() && bad.cmd() == bad2.cmd());
|
||||
|
||||
}
|
||||
|
||||
void testSankeServer::testAbstractNode() {
|
||||
ClientProtocol::AbstractNode node;
|
||||
node.run(TEST_LOCAL_HOST, TEST_PORT);
|
||||
|
Loading…
x
Reference in New Issue
Block a user