|
| 1 | +//# |
| 2 | +//# Copyright (C) 2022-2022 QuasarApp. |
| 3 | +//# Distributed under the lgplv3 software license, see the accompanying |
| 4 | +//# Everyone is permitted to copy and distribute verbatim copies |
| 5 | +//# of this license document, but changing it is not allowed. |
| 6 | +//# |
| 7 | + |
| 8 | +#include "abstractnodeparser_old.h" |
| 9 | +#include "params.h" |
| 10 | +#include "abstractnode.h" |
| 11 | +#include "qaglobalutils.h" |
| 12 | + |
| 13 | +#include <badrequest.h> |
| 14 | +#include <bigdatarequest.h> |
| 15 | +#include <closeconnection.h> |
| 16 | +#include <ping.h> |
| 17 | + |
| 18 | +namespace QH { |
| 19 | + |
| 20 | +AbstractNodeParserOld::AbstractNodeParserOld(AbstractNode* parentNode): iParser(parentNode) { |
| 21 | + debug_assert(parentNode, "Node object can't be null!"); |
| 22 | + |
| 23 | + registerPackageTypeOld<PKG::Ping>(); |
| 24 | + registerPackageTypeOld<PKG::BadRequest>(); |
| 25 | + registerPackageTypeOld<PKG::CloseConnection>(); |
| 26 | +} |
| 27 | + |
| 28 | +AbstractNodeParserOld::~AbstractNodeParserOld() { |
| 29 | +} |
| 30 | + |
| 31 | +ParserResult AbstractNodeParserOld::parsePackage(const QSharedPointer<PKG::AbstractData> &pkg, |
| 32 | + const Header &pkgHeader, |
| 33 | + AbstractNodeInfo *sender) { |
| 34 | + auto nodePtr = node(); |
| 35 | + if (!nodePtr) { |
| 36 | + return ParserResult::NotProcessed; |
| 37 | + } |
| 38 | + |
| 39 | + if (!(sender)) { |
| 40 | + QuasarAppUtils::Params::log("sender socket is not valid!", |
| 41 | + QuasarAppUtils::Error); |
| 42 | + return ParserResult::Error; |
| 43 | + } |
| 44 | + |
| 45 | + if (!pkg->isValid()) { |
| 46 | + QuasarAppUtils::Params::log("incomming package is not valid!", |
| 47 | + QuasarAppUtils::Error); |
| 48 | + nodePtr->changeTrust(sender->networkAddress(), CRITICAL_ERROOR); |
| 49 | + return ParserResult::Error; |
| 50 | + } |
| 51 | + |
| 52 | + if (PKG::Ping::commandOld() == pkg->cmd()) { |
| 53 | + auto cmd = pkg.staticCast<PKG::Ping>(); |
| 54 | + if (!cmd->ansver()) { |
| 55 | + cmd->setAnsver(true); |
| 56 | + nodePtr->sendData(cmd.data(), sender, &pkgHeader); |
| 57 | + } |
| 58 | + |
| 59 | + emit sigPingReceived(cmd); |
| 60 | + |
| 61 | + return ParserResult::Processed; |
| 62 | + } else if (PKG::BadRequest::commandOld() == pkg->cmd()) { |
| 63 | + auto cmd = static_cast<PKG::BadRequest *>(pkg.data()); |
| 64 | + |
| 65 | + emit nodePtr->requestError(cmd->errCode(), cmd->err()); |
| 66 | + |
| 67 | + return ParserResult::Processed; |
| 68 | + |
| 69 | + } else if (PKG::CloseConnection::commandOld() == pkg->cmd()) { |
| 70 | + if (sender->isLocal()) { |
| 71 | + nodePtr->removeNode(sender->networkAddress()); |
| 72 | + } |
| 73 | + return ParserResult::Processed; |
| 74 | + } |
| 75 | + |
| 76 | + return ParserResult::NotProcessed; |
| 77 | +} |
| 78 | + |
| 79 | +int AbstractNodeParserOld::version() const { |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +QString AbstractNodeParserOld::parserId() const { |
| 84 | + return "HeartLibAbstractAPI"; |
| 85 | +} |
| 86 | +} |
0 commit comments