Heart 1.4.918.d927d9c
Heart is base back end library for your c++ Qt projects.
Loading...
Searching...
No Matches
iparser.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-2025 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
9#ifndef IPARSER_H
10#define IPARSER_H
11
12#include "distversion.h"
13#include "hostaddress.h"
14#include <QSharedPointer>
15#include <multiversiondata.h>
16
17namespace QH {
18
20class AbstractNode;
21
22
26using PacksMap = QHash<unsigned short, std::function<PKG::AbstractData *()>>;
27
28
35enum class ParserResult {
37 Error = 0,
42};
43
50class HEARTSHARED_EXPORT iParser: public QObject, public QuasarAppUtils::iHRO
51{
52 Q_OBJECT
53public:
54 iParser(AbstractNode* parentNode);
55 virtual ~iParser() = default;
56 template<class T>
63 _registeredTypes[T::command()] = [](){
64 return new T();
65 };
66
67 if constexpr(std::is_base_of_v<PKG::MultiversionData, T>) {
68 T tmp;
69 if (const DistVersion &distVersion = tmp.packageVersion()) {
70 _multiVersionPackages[tmp.cmd()] = distVersion;
71 }
72 }
73 };
74
124 virtual ParserResult parsePackage(const QSharedPointer<PKG::AbstractData> &pkg,
125 const Header& pkgHeader, AbstractNodeInfo* sender) = 0;
126
131 static QString pareseResultToString(const ParserResult& parseResult);
132
137 virtual int version() const = 0;
138
165 template<class PackageClass,class HandlerType, class HandlerMethod>
166
167 inline ParserResult commandHandler(HandlerType handlerObject, HandlerMethod method,
168 const QSharedPointer<QH::PKG::AbstractData> &pkg,
169 QH::AbstractNodeInfo *sender,
170 const QH::Header &pkgHeader) {
171
172 if (PackageClass::command() == pkg->cmd()) {
173 auto data = pkg.staticCast<PackageClass>();
174
175 if (!data->isValid()) {
177 }
178
179 if(!(handlerObject->*method)(data, sender, pkgHeader)) {
181 }
182
184 }
185
187 }
188
194 const PacksMap &registeredTypes() const;
195
204 QSharedPointer<PKG::AbstractData> genPackage(unsigned short cmd) const;
205
211 bool checkCommand(unsigned short cmd) const;
212
217 virtual QString parserId() const = 0;
218
223 virtual void initSupportedCommands();
224
225 QString toString() const override;
226
232protected:
233 AbstractNode *node() const;
234
243 virtual unsigned int sendData(const PKG::AbstractData *resp, const HostAddress& address,
244 const Header *req = nullptr) const;
245
254 virtual unsigned int sendData(const PKG::AbstractData *resp, const AbstractNodeInfo *node,
255 const Header *req = nullptr) const;
256
257private:
258 // command - {version - factory}
259 PacksMap _registeredTypes;
260 PackagesVersionData _multiVersionPackages;
261
262 AbstractNode *_node;
263
264 friend class BigDataParserOld;
266 friend class AbstractNode;
267
268};
269
270
271}
272#endif // IPARSER_H
The AbstractNodeInfo class contains information about client or server connection and tcp socket of n...
The AbstractNode class - Abstract implementation of node. this implementation have a methods for send...
The DistVersion class This is information of supported versions of the destinations api.
Definition distversion.h:21
The Host Address class this is wrapper of QHostAddress. Contains the NetworkAddress and network port.
Definition hostaddress.h:22
The AbstractData class is provide base functions for transport data by network For create you own pac...
QString toString() const override
Definition iparser.cpp:68
virtual ~iParser()=default
friend class BigDataParserOld
Definition iparser.h:264
virtual QString parserId() const =0
parserId This is id of the parsers. All parser will be synced betwin nodes by ids.
virtual unsigned int sendData(const PKG::AbstractData *resp, const HostAddress &address, const Header *req=nullptr) const
sendData This method send data object another to node
Definition iparser.cpp:50
friend class AbstractNodeParserOld
Definition iparser.h:265
void registerPackageType()
registerPackageType This method register package type T. This is need to prepare pacakge for parsing ...
Definition iparser.h:62
virtual void initSupportedCommands()
initSupportedCommands This method will be invoked before add a parser into parser's storage....
Definition iparser.cpp:66
static QString pareseResultToString(const ParserResult &parseResult)
pareseResultToString This method convert ParserResult value to string.
Definition iparser.cpp:22
const PackagesVersionData & multiVersionPackages() const
multiVersionPackages return list of the supported multiversions packages.
Definition iparser.cpp:62
virtual ParserResult parsePackage(const QSharedPointer< PKG::AbstractData > &pkg, const Header &pkgHeader, AbstractNodeInfo *sender)=0
parsePackage This is main method of all childs classes of an AbstractNode class. This method work on ...
AbstractNode * node() const
Definition iparser.cpp:46
iParser(AbstractNode *parentNode)
Definition iparser.cpp:17
friend class AbstractNode
Definition iparser.h:266
ParserResult commandHandler(HandlerType handlerObject, HandlerMethod method, const QSharedPointer< QH::PKG::AbstractData > &pkg, QH::AbstractNodeInfo *sender, const QH::Header &pkgHeader)
commandHandler This method it is simple wrapper for the handle pacakges in the AbstractNode::parsePac...
Definition iparser.h:167
virtual int version() const =0
version This method return version of parser object
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13
ParserResult
The ParserResult enum. Error - parser detect a errorob package. NotProcessed - the parser does not kn...
Definition iparser.h:35
@ NotProcessed
the parser does not know what to do with the package or has not finished processing it.
Definition iparser.h:39
@ Error
parser detect a errorob package.
Definition iparser.h:37
@ Processed
the parser finished processing correctly.
Definition iparser.h:41
QHash< unsigned short, std::function< PKG::AbstractData *()> > PacksMap
PacksMap This is hash map where id is command of package and value is factory function.
Definition iparser.h:26
QHash< unsigned short, DistVersion > PackagesVersionData
PackagesVersionData This is some as VersionData but for int commands.
Definition distversion.h:83
The Header struct 32 bytes.
Definition header.h:19