Heart 1.3.842.34c2ab5
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-2024 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 QuasarAppUtils::Params::log(QString("The node %0 is banned!").
212 arg(_sct->peerAddress().toString()));
213
214 removeSocket();
215 }
216}
217
218bool AbstractNodeInfo::isValid() const {
219 return _sct;
220}
221
222bool AbstractNodeInfo::isConnected() const {
223 return isValid() && status() != NodeCoonectionStatus::NotConnected;
224}
225
226void AbstractNodeInfo::reset() {
227 setSct(nullptr);
228 setNetworkAddress(HostAddress{});
229 setTrust(static_cast<int>(TrustNode::Default));
230 setStatus(NodeCoonectionStatus::NotConnected);
231 setIsLocal(false);
232
233 QMutexLocker lock(&_parsersListMutex);
234 _parsersMap.clear();
235
236}
237
238QSharedPointer<QH::iParser> AbstractNodeInfo::getParser(unsigned short cmd) {
239 QMutexLocker lock(&_parsersListMutex);
240
241 return _parsersMap.value(cmd, nullptr);
242}
243
244QSharedPointer<iParser> AbstractNodeInfo::getParser(const QString &type) {
245 QMutexLocker lock(&_parsersListMutex);
246
247 return _parsersKeysMap.value(type, nullptr);
248}
249
250void QH::AbstractNodeInfo::addParser(unsigned short cmd,
251 QSharedPointer<QH::iParser> parser) {
252 if (!parser)
253 return;
254
255 QMutexLocker lock(&_parsersListMutex);
256
257 _parsersMap[cmd] = parser;
258 _parsersKeysMap[parser->parserId()] = parser;
259}
260
261void AbstractNodeInfo::addParser(QSharedPointer<iParser> parser) {
262 if (!parser)
263 return;
264
265 QMutexLocker lock(&_parsersListMutex);
266
267 _parsersKeysMap[parser->parserId()] = parser;
268}
269
271 return static_cast<uint>(status);
272}
273
274}
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