Heart 1.3.848.aa44c26
Heart is base back end library for your c++ Qt projects.
abstractnodeinfo.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-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#include "abstractnodeinfo.h"
9#include <hostaddress.h>
10#include <QAbstractSocket>
11#include <QDataStream>
12#include <QHostInfo>
13#include <QMetaObject>
14#include <quasarapp.h>
15#include <iparser.h>
16
17namespace QH {
18
20 const HostAddress *address) {
21 setSct(sct);
22 if (address)
23 setNetworkAddress(*address);
24
25}
26
30
31QAbstractSocket *AbstractNodeInfo::sct() const {
32 return _sct;
33}
34
36 if (_sct) {
37 auto socketPtr = _sct;
38 _sct = nullptr;
39
40 socketPtr->disconnect();
41
42 // We invoke the delate later method on another thread (thread of the socket.)
43 // because all network sockets must be open adn closed in the one thread.
44
45 QMetaObject::invokeMethod(socketPtr,
46 "handleDisckonnetFromHost",
47 Qt::QueuedConnection);
48
49 QMetaObject::invokeMethod(socketPtr,
50 "deleteLater",
51 Qt::QueuedConnection);
52
54 emit sigDisconnected(this);
55
56 }
57}
58
60 _trust = static_cast<int>(TrustNode::Baned);
62}
63
65 return _trust < 1;
66}
67
69 _trust = static_cast<int>(TrustNode::Restore);
70}
71
72void AbstractNodeInfo::setSct(QAbstractSocket *sct) {
73
75
76 _sct = sct;
77
78 if (!_sct)
79 return;
80
81
82 if (!_sct->peerAddress().isNull()) {
83 setNetworkAddress(HostAddress{_sct->peerAddress(), _sct->peerPort()});
84 }
85
86 auto connectedF = [this] () {
87
89 emit sigConnected(this);
90 };
91
92 connect(_sct, &QAbstractSocket::connected,
93 this, connectedF, Qt::DirectConnection);
94
95 connect(_sct, &QAbstractSocket::disconnected,
96 this, &AbstractNodeInfo::removeSocket, Qt::QueuedConnection);
97
98 connect(_sct, &QAbstractSocket::errorOccurred,
99 this, [this] (QAbstractSocket::SocketError err){
100 emit sigErrorOccurred(this, err, _sct->errorString());
101 }, Qt::DirectConnection);
102
103 connect(_sct, &QAbstractSocket::readyRead,
104 this, [this] (){ emit sigReadyRead(this);},
105 Qt::DirectConnection);
106
107 if (_sct->state() == QAbstractSocket::ConnectedState) {
108 connectedF();
109 }
110}
111
113 _isLocal = isLocal;
114}
115
117 return _status;
118}
119
121 if (status == _status) {
122 return;
123 }
124
125 _status = status;
126
127 emit statusChaned(this, _status);
128}
129
133
135 return _multiVersionPackages;
136}
137
139 _multiVersionPackages = newMultiVersionPackages;
140}
141
143 return _fVersionDelivered;
144}
145
146void AbstractNodeInfo::setFVersionDelivered(bool newFVersionDelivered) {
147 _fVersionDelivered = newFVersionDelivered;
148}
149
151 return _fVersionReceived;
152}
153
154void AbstractNodeInfo::setFVersionReceived(bool newFVersionReceived) {
155 _fVersionReceived = newFVersionReceived;
156}
157
159 return _version;
160}
161
163 _version = newVersion;
164}
165
170
172 return _isLocal;
173}
174
176 return _networkAddress;
177}
178
180
181 if (!networkAddress.isNull()) {
182 _networkAddress = networkAddress;
183
184 QHostInfo::lookupHost(_networkAddress.toString(), [this] (QHostInfo info){
185 if (dynamic_cast<AbstractNodeInfo*>(this)) {
186 setInfo(info);
187 }
188 });
189 }
190}
191
192void AbstractNodeInfo::setInfo(const QHostInfo &info) {
193 if (!_info)
194 _info = new QHostInfo();
195
196 *_info = info;
197}
198
199QHostInfo *AbstractNodeInfo::info() const {
200 return _info;
201}
202
203int AbstractNodeInfo::trust() const {
204 return _trust;
205}
206
207void AbstractNodeInfo::setTrust(int trust) {
208 _trust = trust;
209
210 if (isBanned() && _sct) {
211 qDebug() << "The node" << _sct->peerAddress().toString() << "is banned!";
212
213 removeSocket();
214 }
215}
216
217bool AbstractNodeInfo::isValid() const {
218 return _sct;
219}
220
221bool AbstractNodeInfo::isConnected() const {
222 return isValid() && status() != NodeCoonectionStatus::NotConnected;
223}
224
225void AbstractNodeInfo::reset() {
226 setSct(nullptr);
227 setNetworkAddress(HostAddress{});
228 setTrust(static_cast<int>(TrustNode::Default));
229 setStatus(NodeCoonectionStatus::NotConnected);
230 setIsLocal(false);
231
232 QMutexLocker lock(&_parsersListMutex);
233 _parsersMap.clear();
234
235}
236
237QSharedPointer<QH::iParser> AbstractNodeInfo::getParser(unsigned short cmd) {
238 QMutexLocker lock(&_parsersListMutex);
239
240 return _parsersMap.value(cmd, nullptr);
241}
242
243QSharedPointer<iParser> AbstractNodeInfo::getParser(const QString &type) {
244 QMutexLocker lock(&_parsersListMutex);
245
246 return _parsersKeysMap.value(type, nullptr);
247}
248
249void QH::AbstractNodeInfo::addParser(unsigned short cmd,
250 QSharedPointer<QH::iParser> parser) {
251 if (!parser)
252 return;
253
254 QMutexLocker lock(&_parsersListMutex);
255
256 _parsersMap[cmd] = parser;
257 _parsersKeysMap[parser->parserId()] = parser;
258}
259
260void AbstractNodeInfo::addParser(QSharedPointer<iParser> parser) {
261 if (!parser)
262 return;
263
264 QMutexLocker lock(&_parsersListMutex);
265
266 _parsersKeysMap[parser->parserId()] = parser;
267}
268
270 return static_cast<uint>(status);
271}
272
273}
const PackagesVersionData & multiVersionPackages() const
multiVersionPackages This is list of packages of one api package tah support multiple versions.
bool isLocal() const
isLocal return true if connection opened on this node.
void setStatus(const NodeCoonectionStatus &status)
setStatus This method Sets new value of status node.
void setVersion(const VersionData &newVersion)
setVersion This method sets new version data structure.
AbstractNodeInfo(QAbstractSocket *sct=nullptr, const HostAddress *address=nullptr)
AbstractNodeInfo.
NodeCoonectionStatus status() const
status This method return status of the node connection.
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 setSct(QAbstractSocket *sct)
setSct This method sets a new socket for this node or client.
bool fVersionDelivered() const
fVersionDelivered This method return true if this node delivere own version to the distanation node.
void sigReadyRead(QH::AbstractNodeInfo *thisNode)
sigReadyRead This is wrapper signal for the QAbstractSocket::readyRead signal.
HostAddress networkAddress() const
networkAddress This method return network address of current node or client.
void setNetworkAddress(const HostAddress &networkAddress)
setNetworkAddress This method update network address of the current node.
void addParser(unsigned short cmd, QSharedPointer< QH::iParser > parser)
addParser This method add to cache new parser for command .
void statusChaned(QH::AbstractNodeInfo *thisNode, QH::NodeCoonectionStatus status)
statusChaned This signal emitted when nodes status is changed.
virtual bool confirmData() const
confirmData This method check all data of node and return true. If node is confirmed.
virtual void removeSocket()
removeSocket This method use for remove socket. You can override this method for handle this event.
void setFVersionDelivered(bool newFVersionDelivered)
setFVersionDelivered This method change the fVersionDelivered flag.
virtual ~AbstractNodeInfo()
~AbstractNodeInfo
QHostInfo * info() const
info - This method return Host domain information. For more information see the Qt Documentation abou...
const VersionData & version() const
version This method return version data structure;
virtual void unBan()
unBan - Set trust value of node to TrustNode::Restore. See TrustNode enum for more information.
virtual void ban()
ban This node, set trust value to 0.
void updateConfirmStatus()
updateConfirmStatus This method invoked after process a received package and update confirm status of...
QAbstractSocket * sct() const
sct This method return socket of connection.
virtual bool isBanned() const
isBanned - check node which banned.
void setIsLocal(bool isLocal)
setIsLocal This method sets local status for this Node.
void sigConnected(QH::AbstractNodeInfo *thisNode)
sigConnected This is wrapper signal for the QAbstractSocket::connetced signal.
void sigErrorOccurred(QH::AbstractNodeInfo *thisNode, QAbstractSocket::SocketError socketError, QString message)
sigErrorOccurred This is wrapper signal for the QAbstractSocket::errorOccurred signal.
bool fVersionReceived() const
fVersionReceived This method return true if this node receive version information.
void sigDisconnected(QH::AbstractNodeInfo *thisNode)
sigDisconnected This is wrapper signal for the QAbstractSocket::disconnected signal.
The Host Address class this is wrapper of QHostAddress. Contains the NetworkAddress and network port.
Definition hostaddress.h:22
QString toString() const
toString this method convert the Host Address value to string value.
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13
uint qHash(NodeCoonectionStatus status)
qHash - Simple hash function of NodeCoonectionStatus
NodeCoonectionStatus
The AbstractNodeState enum - This is status of the known nodes or clients.
@ Connected
The node with this status has socket status is connected.
@ NotConnected
This node not sent data about its envirement and status of node socket is disconnected.
QHash< QString, DistVersion > VersionData
VersionData This is array of all available apis and supported its versions.
Definition distversion.h:73
@ Baned
Node with this trust value is forbidden.
QHash< unsigned short, DistVersion > PackagesVersionData
PackagesVersionData This is some as VersionData but for int commands.
Definition distversion.h:78