Heart 1.4.984.b4712811
Heart is base back end library for your c++ Qt projects.
Loading...
Searching...
No Matches
apiversionparser.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2022-2027 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 "apiversionparser.h"
9#include "abstractnodeinfo.h"
10#include "distversion.h"
11
12#include <apiversion.h>
13#include <versionisreceived.h>
14#include <abstractnode.h>
15#include <qaglobalutils.h>
16#include <apiversion.h>
17
18namespace QH {
19
23
24ParserResult APIVersionParser::parsePackage(const QSharedPointer<PKG::AbstractData> &pkg,
25 const Header &pkgHeader,
26 AbstractNodeInfo *sender) {
27
28 // here node must be receive version of connected application.
29 // if not use the default version (0)APIVersion
30 auto parentResult = commandHandler<PKG::APIVersion>(this, &APIVersionParser::processAppVersion,
31 pkg, sender, pkgHeader);
32 if (parentResult != QH::ParserResult::NotProcessed) {
33 return parentResult;
34 }
35
36 // here node must be receive response that version is delivered.
37 // when node receive this message then node status are confirmed
38 parentResult = commandHandler<PKG::VersionIsReceived>(this, &APIVersionParser::versionDeliveredSuccessful,
39 pkg, sender, pkgHeader);
40 if (parentResult != QH::ParserResult::NotProcessed) {
41 return parentResult;
42 }
43
44 const auto parser = selectParser(pkg->cmd(), sender);
45
46 if (!parser) {
47 qWarning () << "Can't found required parser for versions";
49 }
50
51 auto perserResult = parser->parsePackage(pkg, pkgHeader, sender);
52 if (perserResult != QH::ParserResult::NotProcessed) {
53 return perserResult;
54 }
55
57}
58
60 return 0;
61}
62
64 return "APIVersionParser";
65}
66
68 QString message = " supports parsers:\n";
69 message += iParser::toString();
70
71 for (const auto& parser: _apiParsers) {
72 for (auto it = parser.begin(); it != parser.end(); ++it) {
73 message += "ver: " + QString::number(it.key()) + " " + it.value()->toString();
74 }
75 }
76
77 return message;
78}
79
80QSharedPointer<PKG::AbstractData>
82 AbstractNodeInfo *sender) const {
83 if (!sender)
84 return nullptr;
85
86 if (cmd == PKG::APIVersion::command()) {
87 return QSharedPointer<PKG::APIVersion>::create();
88 } else if (cmd == PKG::VersionIsReceived::command()) {
89 return QSharedPointer<PKG::VersionIsReceived>::create();
90 }
91
92 auto distVersion = sender->version();
93 const auto parsers = selectParser(distVersion);
94
95 if (parsers.isEmpty()) {
96 return nullptr;
97 }
98
99 for (const auto& parser: parsers) {
100 if (!parser) {
101 qCritical() << "Internal Error with selection parasers.";
102 continue;
103 }
104
105 if (auto package = parser->genPackage(cmd))
106 return package;
107 }
108
109 return nullptr;
110}
111
112QSharedPointer<iParser>
114 AbstractNodeInfo *node) const {
115 return selectParser(node->version()).value(apiKey, nullptr);
116}
117
118bool QH::APIVersionParser::commandsValidation(const QSharedPointer<iParser> &parserObject) {
119#ifdef QT_DEBUG
120 auto types = QSet<unsigned short>{parserObject->registeredTypes().keyBegin(),
121 parserObject->registeredTypes().keyEnd()};
122 int typesSize = types.size();
123
124 for (const auto &parsersMap : std::as_const(_apiParsers)) {
125 for (const auto &parser: parsersMap) {
126 if (parser->parserId() == parserObject->parserId()) {
127 continue;
128 }
129
130 auto localTypes = QSet<unsigned short>{parser->registeredTypes().keyBegin(),
131 parser->registeredTypes().keyEnd()};
132 types -= localTypes;
133
134 if (types.size() != typesSize) {
135 auto err = QString("Parser object can't be added. "
136 "Because commands of the %0_v%1 parser "
137 "already registered in the %2_v%3 parser. "
138 "Use the iParser::initSupportedCommands method to "
139 "fix this issue. "
140 "See our documentation for get more information. "
141 "https://quasarapp.ddns.net:3031/docs/QuasarApp/Heart/latest/classQH_1_1iParser.html").
142 arg(parserObject->parserId()).
143 arg(parserObject->version()).
144 arg(parser->parserId()).
145 arg(parser->version());
146
147 qCritical() << err;
148
149 return false;
150 }
151 }
152 }
153#endif
154
155 return true;
156}
157
158const QSharedPointer<QH::iParser>& APIVersionParser::addApiParser(const QSharedPointer<iParser> &parserObject) {
159 debug_assert(parserObject, "Parser object should not be nullptr");
160
161 parserObject->initSupportedCommands();
162
163 if (!commandsValidation(parserObject)) {
164 debug_assert(false, "Parser object can't be added. "
165 "Because its commands already registered "
166 "in the another parsers.");
167 }
168
169 _apiParsers[parserObject->parserId()][parserObject->version()] = parserObject;
170 return _apiParsers[parserObject->parserId()][parserObject->version()];
171}
172
173QHash<QString, QSharedPointer<iParser>>
175 QHash<QString, QSharedPointer<iParser>> result;
176
177 for (auto it = distVersion.begin(); it != distVersion.end(); ++it) {
178 for (int version = it->max(); version >= it->min(); --version) {
179 auto parser = _apiParsers[it.key()].value(version, nullptr);
180 if (parser) {
181 result[it.key()] = parser;
182 break;
183 }
184 }
185 }
186
187 return result;
188}
189
191 return _apiParsers.size();
192}
193
194QSharedPointer<iParser> APIVersionParser::selectParser(unsigned short cmd,
195 AbstractNodeInfo *sender) const{
196 auto parser = sender->getParser(cmd);
197 if (!parser) {
198 parser = selectParserImpl(cmd, sender);
199 }
200
201 return parser;
202}
203
204QSharedPointer<iParser> APIVersionParser::selectParser(const QString &parserKey,
205 AbstractNodeInfo *sender) const {
206 if (!sender)
207 return nullptr;
208
209 auto parser = sender->getParser(parserKey);
210 if (!parser) {
211 parser = selectParserImpl(parserKey, sender);
212 }
213
214 return parser;
215}
216
217QSharedPointer<iParser> APIVersionParser::selectParser(const QString &parserKey,
218 unsigned short version) const {
219
220 DistVersion versionData;
221 versionData.setMin(version);
222 versionData.setMax(version);
223
224 VersionData versions;
225 versions.insert(parserKey, versionData);
226
227 return selectParser(versions).value(parserKey);
228}
229
230QSharedPointer<iParser> APIVersionParser::selectParserImpl(unsigned short cmd,
231 AbstractNodeInfo *sender) const {
232 auto version = sender->version();
233 const auto availableParser = selectParser(version);
234 for (const auto& parser: availableParser) {
235 if (parser->checkCommand(cmd)) {
236 sender->addParser(cmd, parser);
237 return parser;
238 }
239 }
240
241 return nullptr;
242}
243
244QSharedPointer<iParser> APIVersionParser::selectParserImpl(const QString &key,
245 AbstractNodeInfo *sender) const{
246 auto version = sender->version();
247 auto parser = selectParser(version).value(key);
248 sender->addParser(parser);
249
250 return parser;
251}
252
253unsigned short APIVersionParser::maximumApiVersion(const QString &apiKey) const {
254
255 auto availableVersion = _apiParsers.value(apiKey, {});
256 if (availableVersion.size()) {
257 return availableVersion.lastKey();
258 }
259
260 return 0;
261}
262
263unsigned short APIVersionParser::minimumApiVersion(const QString &apiKey) const {
264 auto availableVersion = _apiParsers.value(apiKey, {});
265 if (availableVersion.size()) {
266 return availableVersion.firstKey();
267 }
268
269 return 0;
270}
271
273 VersionData supportedAPIs;
275
276 for (auto it = _apiParsers.begin(); it != _apiParsers.end(); ++it) {
277 DistVersion supportVersions;
278
279 for (auto api = it->begin(); api != it->end(); ++api) {
280 const auto packages = api.value()->multiVersionPackages();
281 for (auto pkg = packages.begin(); pkg != packages.end(); ++pkg) {
282 multiVersionPackages[pkg.key()] = pkg.value();
283 }
284 }
285
286 supportVersions.setMax(it->lastKey());
287 supportVersions.setMin(it->firstKey());
288
289 supportedAPIs.insert(it.key(), supportVersions);
290
291 }
292
293 if (supportedAPIs.isEmpty())
294 return false;
295
296 PKG::APIVersion versionInformationPkg;
297 versionInformationPkg.setApisVersions(supportedAPIs);
298 versionInformationPkg.setPackagesVersions(multiVersionPackages);
299
300 return sendData(&versionInformationPkg, dist);
301}
302
303bool APIVersionParser::processAppVersion(const QSharedPointer<PKG::APIVersion> &message,
304 QH::AbstractNodeInfo *sender,
305 const QH::Header &) {
306
307 auto distVersion = message->apisVersions();
308 sender->setVersion(distVersion);
309 sender->setMultiVersionPackages(message->packagesVersions());
310
311 auto parser = selectParser(distVersion);
312
313 for (auto parserKey = distVersion.keyBegin(); parserKey != distVersion.keyEnd(); ++parserKey) {
314 if (!parser.contains(*parserKey)) {
315 auto requiredApi = distVersion.value(*parserKey);
316
317 qCritical() << "Can't found required parser for versions: " << *parserKey << requiredApi.min() << requiredApi.max();
318
319 unsigned short distMinVersion = sender->version().value(*parserKey).min();
320
321 if (distMinVersion > maximumApiVersion(*parserKey)) {
322 emit sigNoLongerSupport(*parserKey, distMinVersion);
323 }
324
325 return false;
326 }
327 }
328
329 sender->setFVersionReceived(true);
330
331 PKG::VersionIsReceived result;
332 return node()->sendData(&result, sender);
333}
334
335bool APIVersionParser::versionDeliveredSuccessful(const QSharedPointer<PKG::VersionIsReceived> &,
336 AbstractNodeInfo *sender,
337 const QH::Header &) {
338 sender->setFVersionDelivered(true);
339 return true;
340}
341
342}
int version() const override
version This method return version of parser object
QSharedPointer< PKG::AbstractData > searchPackage(unsigned short cmd, AbstractNodeInfo *sender) const
searchPackage This method search package recursive in all registered pararsers. Searching will be in ...
unsigned short minimumApiVersion(const QString &apiKey) const
minimumApiVersion This method return minimum supported api version of this node.
QHash< QString, QSharedPointer< QH::iParser > > selectParser(const VersionData &distVersion) const
selectParser This method select api parser betwin nodes.
unsigned short maximumApiVersion(const QString &apiKey) const
maximumApiVersion This method return maximum supported api version of this node.
const QSharedPointer< QH::iParser > & addApiParser(const QSharedPointer< QH::iParser > &parserObject)
addApiParser This method add new Api parser for this node.
ParserResult parsePackage(const QSharedPointer< PKG::AbstractData > &pkg, const Header &pkgHeader, AbstractNodeInfo *sender) override
parsePackage This is main method of all children classes of an AbstractNode class....
APIVersionParser(AbstractNode *node)
void sigNoLongerSupport(const QString &ApiKey, unsigned short version)
sigNoLongerSupport This signal will be emit when node receive incomplite versions.
QSharedPointer< iParser > getSelectedApiParser(const QString &apiKey, QH::AbstractNodeInfo *node) const
getSelectedApiParser This method return apiParser for selected node
unsigned int parsersTypedCount() const
parsersTypedCount This method return count of the parsers types.
QString toString() const override
toString This method show all supported commands and them names.
QString parserId() const override
parserId This is id of the parsers. All parser will be synced betwin nodes by ids.
bool sendSupportedAPI(AbstractNodeInfo *dist) const
sendSupportedAPI This method sents all ainformation about supported api.
The AbstractNodeInfo class contains information about client or server connection and tcp socket of n...
void setVersion(const VersionData &newVersion)
setVersion This method sets new version data structure.
void setFVersionReceived(bool newFVersionReceived)
setFVersionReceived This method change the fVersionReceived flag.
void setMultiVersionPackages(const PackagesVersionData &newMultiVersionPackages)
setMultiVersionPackages This method sets new list of multi-version packages.
void addParser(unsigned short cmd, QSharedPointer< QH::iParser > parser)
addParser This method add to cache new parser for command .
const VersionData & version() const
version This method return version data structure;
QSharedPointer< QH::iParser > getParser(unsigned short cmd)
getParser This method return parser of chosen command.
virtual unsigned int sendData(const PKG::AbstractData *resp, const HostAddress &address, const Header *req=nullptr)
sendData This method send data object another to node
The DistVersion class This is information of supported versions of the destinations api.
Definition distversion.h:21
void setMax(unsigned short newMax)
void setMin(unsigned short newMin)
The APIVersion class This is base package class that send information about supported api version on ...
Definition apiversion.h:24
void setPackagesVersions(const PackagesVersionData &newPackagesVersions)
static unsigned short command()
Definition apiversion.h:29
void setApisVersions(const VersionData &newApisVersions)
static unsigned short command()
QString toString() const override
Definition iparser.cpp:68
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
const PackagesVersionData & multiVersionPackages() const
multiVersionPackages return list of the supported multiversions packages.
Definition iparser.cpp:62
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 packages in the AbstractNode::parsePac...
Definition iparser.h:167
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
QHash< QString, DistVersion > VersionData
VersionData This is array of all available apis and supported its versions.
Definition distversion.h:78
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