Heart 1.4.984.b4712811
Heart is base back end library for your c++ Qt projects.
Loading...
Searching...
No Matches
abstractnodeinfo.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-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 "abstractnodeinfo.h"
9#include <hostaddress.h>
10#include <QAbstractSocket>
11#include <QDataStream>
12#include <QHostInfo>
13#include <QMetaObject>
14#include <iparser.h>
15
16namespace QH {
17
19 const HostAddress *address) {
20 setSct(sct);
21 if (address)
22 setNetworkAddress(*address);
23
24}
25
29
30QAbstractSocket *AbstractNodeInfo::sct() const {
31 return _sct;
32}
33
35 if (_sct) {
36 auto socketPtr = _sct;
37 _sct = nullptr;
38
39 socketPtr->disconnect();
40
41 // We invoke the delete later method on another thread (thread of the socket.)
42 // because all network sockets must be open and closed in the one thread.
43
44 QMetaObject::invokeMethod(socketPtr,
45 "handleDisconnectFromHost",
46 Qt::QueuedConnection);
47
48 QMetaObject::invokeMethod(socketPtr,
49 "deleteLater",
50 Qt::QueuedConnection);
51
53 emit sigDisconnected(this);
54
55 }
56}
57
59 _trust = static_cast<int>(TrustNode::Baned);
61}
62
64 return _trust < 1;
65}
66
68 _trust = static_cast<int>(TrustNode::Restore);
69}
70
71void AbstractNodeInfo::setSct(QAbstractSocket *sct) {
72
74
75 _sct = sct;
76
77 if (!_sct)
78 return;
79
80
81 if (!_sct->peerAddress().isNull()) {
82 setNetworkAddress(HostAddress{_sct->peerAddress(), _sct->peerPort()});
83 }
84
85 auto connectedF = [this] () {
86
88 emit sigConnected(this);
89 };
90
91 connect(_sct, &QAbstractSocket::connected,
92 this, connectedF, Qt::DirectConnection);
93
94 connect(_sct, &QAbstractSocket::disconnected,
95 this, &AbstractNodeInfo::removeSocket, Qt::QueuedConnection);
96
97 connect(_sct, &QAbstractSocket::errorOccurred,
98 this, [this] (QAbstractSocket::SocketError err){
99 emit sigErrorOccurred(this, err, _sct->errorString());
100 }, Qt::DirectConnection);
101
102 connect(_sct, &QAbstractSocket::readyRead,
103 this, [this] (){ emit sigReadyRead(this);},
104 Qt::DirectConnection);
105
106 if (_sct->state() == QAbstractSocket::ConnectedState) {
107 connectedF();
108 }
109}
110
112 _isLocal = isLocal;
113}
114
116 return _status;
117}
118
120 if (status == _status) {
121 return;
122 }
123
124 _status = status;
125
126 emit statusChanged(this, _status);
127}
128
132
134 return _multiVersionPackages;
135}
136
138 _multiVersionPackages = newMultiVersionPackages;
139}
140
142 return _fVersionDelivered;
143}
144
145void AbstractNodeInfo::setFVersionDelivered(bool newFVersionDelivered) {
146 _fVersionDelivered = newFVersionDelivered;
147}
148
150 return _fVersionReceived;
151}
152
153void AbstractNodeInfo::setFVersionReceived(bool newFVersionReceived) {
154 _fVersionReceived = newFVersionReceived;
155}
156
158 return _version;
159}
160
162 _version = newVersion;
163}
164
169
171 return _isLocal;
172}
173
175 return _networkAddress;
176}
177
179
180 if (!networkAddress.isNull()) {
181 _networkAddress = networkAddress;
182
183 QHostInfo::lookupHost(_networkAddress.toString(), [this] (QHostInfo info){
184 if (dynamic_cast<AbstractNodeInfo*>(this)) {
185 setInfo(info);
186 }
187 });
188 }
189}
190
191void AbstractNodeInfo::setInfo(const QHostInfo &info) {
192 if (!_info)
193 _info = new QHostInfo();
194
195 *_info = info;
196}
197
198QHostInfo *AbstractNodeInfo::info() const {
199 return _info;
200}
201
203 return _trust;
204}
205
207 _trust = trust;
208
209 if (isBanned() && _sct) {
210 qDebug() << "The node" << _sct->peerAddress().toString() << "is banned!";
211
212 removeSocket();
213 }
214}
215
217 return _sct;
218}
219
223
225 setSct(nullptr);
227 setTrust(static_cast<int>(TrustNode::Default));
229 setIsLocal(false);
230
231 QMutexLocker lock(&_parsersListMutex);
232 _parsersMap.clear();
233
234}
235
236QSharedPointer<QH::iParser> AbstractNodeInfo::getParser(unsigned short cmd) {
237 QMutexLocker lock(&_parsersListMutex);
238
239 return _parsersMap.value(cmd, nullptr);
240}
241
242QSharedPointer<iParser> AbstractNodeInfo::getParser(const QString &type) {
243 QMutexLocker lock(&_parsersListMutex);
244
245 return _parsersKeysMap.value(type, nullptr);
246}
247
248void QH::AbstractNodeInfo::addParser(unsigned short cmd,
249 QSharedPointer<QH::iParser> parser) {
250 if (!parser)
251 return;
252
253 QMutexLocker lock(&_parsersListMutex);
254
255 _parsersMap[cmd] = parser;
256 _parsersKeysMap[parser->parserId()] = parser;
257}
258
259void AbstractNodeInfo::addParser(QSharedPointer<iParser> parser) {
260 if (!parser)
261 return;
262
263 QMutexLocker lock(&_parsersListMutex);
264
265 _parsersKeysMap[parser->parserId()] = parser;
266}
267
269 return static_cast<uint>(status);
270}
271
272}
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.
virtual void reset()
reset This method reset all fields of the nodeInfo.
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.
virtual int trust() const
trust - Show current value of trust level node or client.
void setInfo(const QHostInfo &info)
setInfo - Set new host info for this node.
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 delivered own version to the destination 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 .
virtual void setTrust(int trust)
setTrust This method set manually value of this node trust.
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...
QSharedPointer< QH::iParser > getParser(unsigned short cmd)
getParser This method return parser of chosen command.
virtual bool isValid() const
isValid - Check valid of node. This method check connect status of socket.
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::connected signal.
void statusChanged(QH::AbstractNodeInfo *thisNode, QH::NodeCoonectionStatus status)
statusChanged This signal emitted when nodes status is changed.
void sigErrorOccurred(QH::AbstractNodeInfo *thisNode, QAbstractSocket::SocketError socketError, QString message)
sigErrorOccurred This is wrapper signal for the QAbstractSocket::errorOccurred signal.
virtual bool isConnected() const
isConnected - Check of node connect status.
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
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:78
@ Default
General trust of the node or client.
@ 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:83