2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2011-2020 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-05-23 13:09:59 -04:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <qt/peertablemodel.h>
|
2014-05-23 13:09:59 -04:00
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <qt/guiconstants.h>
|
|
|
|
#include <qt/guiutil.h>
|
2014-05-23 13:09:59 -04:00
|
|
|
|
2018-04-07 04:42:02 -03:00
|
|
|
#include <interfaces/node.h>
|
2014-05-23 13:09:59 -04:00
|
|
|
|
2019-11-29 16:18:24 -03:00
|
|
|
#include <utility>
|
2019-08-20 21:42:29 -04:00
|
|
|
|
2014-05-23 13:09:59 -04:00
|
|
|
#include <QList>
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
// private implementation
|
|
|
|
class PeerTablePriv
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Local cache of peer information */
|
|
|
|
QList<CNodeCombinedStats> cachedNodeStats;
|
|
|
|
|
|
|
|
/** Pull a full list of peers from vNodes into our cache */
|
2018-04-07 04:42:02 -03:00
|
|
|
void refreshPeers(interfaces::Node& node)
|
2014-06-04 06:06:18 -04:00
|
|
|
{
|
2014-05-23 13:09:59 -04:00
|
|
|
cachedNodeStats.clear();
|
2017-04-17 16:57:19 -03:00
|
|
|
|
2018-04-07 04:42:02 -03:00
|
|
|
interfaces::Node::NodesStats nodes_stats;
|
2017-04-17 16:57:19 -03:00
|
|
|
node.getNodesStats(nodes_stats);
|
|
|
|
cachedNodeStats.reserve(nodes_stats.size());
|
2018-06-18 01:58:28 -04:00
|
|
|
for (const auto& node_stats : nodes_stats)
|
2014-05-23 13:09:59 -04:00
|
|
|
{
|
|
|
|
CNodeCombinedStats stats;
|
2017-04-17 16:57:19 -03:00
|
|
|
stats.nodeStats = std::get<0>(node_stats);
|
|
|
|
stats.fNodeStateStatsAvailable = std::get<1>(node_stats);
|
|
|
|
stats.nodeStateStats = std::get<2>(node_stats);
|
2014-05-23 13:09:59 -04:00
|
|
|
cachedNodeStats.append(stats);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 08:07:08 -03:00
|
|
|
int size() const
|
2014-05-23 13:09:59 -04:00
|
|
|
{
|
|
|
|
return cachedNodeStats.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
CNodeCombinedStats *index(int idx)
|
|
|
|
{
|
2015-06-21 08:07:08 -03:00
|
|
|
if (idx >= 0 && idx < cachedNodeStats.size())
|
2014-05-23 13:09:59 -04:00
|
|
|
return &cachedNodeStats[idx];
|
2015-06-21 08:07:08 -03:00
|
|
|
|
2018-07-31 14:02:34 -04:00
|
|
|
return nullptr;
|
2014-05-23 13:09:59 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-03 11:48:27 -03:00
|
|
|
PeerTableModel::PeerTableModel(interfaces::Node& node, QObject* parent) :
|
2014-06-04 06:06:18 -04:00
|
|
|
QAbstractTableModel(parent),
|
2017-04-17 16:57:19 -03:00
|
|
|
m_node(node),
|
2018-07-31 14:02:34 -04:00
|
|
|
timer(nullptr)
|
2014-05-23 13:09:59 -04:00
|
|
|
{
|
2016-11-18 11:47:20 -03:00
|
|
|
priv.reset(new PeerTablePriv());
|
2014-05-23 13:09:59 -04:00
|
|
|
|
|
|
|
// set up timer for auto refresh
|
2016-11-18 11:47:20 -03:00
|
|
|
timer = new QTimer(this);
|
2018-06-24 11:18:22 -04:00
|
|
|
connect(timer, &QTimer::timeout, this, &PeerTableModel::refresh);
|
2014-06-04 06:06:18 -04:00
|
|
|
timer->setInterval(MODEL_UPDATE_DELAY);
|
2014-05-23 13:09:59 -04:00
|
|
|
|
|
|
|
// load initial data
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2016-11-18 11:47:20 -03:00
|
|
|
PeerTableModel::~PeerTableModel()
|
|
|
|
{
|
|
|
|
// Intentionally left empty
|
|
|
|
}
|
|
|
|
|
2014-06-04 06:06:18 -04:00
|
|
|
void PeerTableModel::startAutoRefresh()
|
2014-05-23 13:09:59 -04:00
|
|
|
{
|
|
|
|
timer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerTableModel::stopAutoRefresh()
|
|
|
|
{
|
|
|
|
timer->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
int PeerTableModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2021-01-02 16:40:53 -03:00
|
|
|
if (parent.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-23 13:09:59 -04:00
|
|
|
return priv->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int PeerTableModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2021-01-02 16:40:53 -03:00
|
|
|
if (parent.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-12-30 00:42:27 -03:00
|
|
|
return columns.length();
|
2014-05-23 13:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant PeerTableModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if(!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
CNodeCombinedStats *rec = static_cast<CNodeCombinedStats*>(index.internalPointer());
|
|
|
|
|
2020-12-25 17:29:15 -03:00
|
|
|
const auto column = static_cast<ColumnIndex>(index.column());
|
2014-08-04 10:25:21 -04:00
|
|
|
if (role == Qt::DisplayRole) {
|
2020-12-25 17:29:15 -03:00
|
|
|
switch (column) {
|
2016-08-19 16:38:04 -03:00
|
|
|
case NetNodeId:
|
2017-04-10 16:00:23 -03:00
|
|
|
return (qint64)rec->nodeStats.nodeid;
|
2014-05-23 13:09:59 -04:00
|
|
|
case Address:
|
2018-06-24 16:09:01 -04:00
|
|
|
// prepend to peer address down-arrow symbol for inbound connection and up-arrow for outbound connection
|
|
|
|
return QString(rec->nodeStats.fInbound ? "↓ " : "↑ ") + QString::fromStdString(rec->nodeStats.addrName);
|
2021-01-09 16:10:12 -03:00
|
|
|
case ConnectionType:
|
|
|
|
return GUIUtil::ConnectionTypeToQString(rec->nodeStats.m_conn_type, /* prepend_direction */ false);
|
2020-12-25 10:46:03 -03:00
|
|
|
case Network:
|
|
|
|
return GUIUtil::NetworkToQString(rec->nodeStats.m_network);
|
2014-06-04 06:06:18 -04:00
|
|
|
case Ping:
|
2020-09-29 23:11:53 -03:00
|
|
|
return GUIUtil::formatPingTime(rec->nodeStats.m_min_ping_time);
|
2017-10-14 20:06:54 -03:00
|
|
|
case Sent:
|
|
|
|
return GUIUtil::formatBytes(rec->nodeStats.nSendBytes);
|
|
|
|
case Received:
|
|
|
|
return GUIUtil::formatBytes(rec->nodeStats.nRecvBytes);
|
2020-12-25 10:46:03 -03:00
|
|
|
case Subversion:
|
|
|
|
return QString::fromStdString(rec->nodeStats.cleanSubVer);
|
2020-12-25 17:29:15 -03:00
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
assert(false);
|
2014-08-04 10:25:21 -04:00
|
|
|
} else if (role == Qt::TextAlignmentRole) {
|
2020-12-25 17:29:15 -03:00
|
|
|
switch (column) {
|
|
|
|
case NetNodeId:
|
|
|
|
case Address:
|
|
|
|
return {};
|
|
|
|
case ConnectionType:
|
|
|
|
case Network:
|
|
|
|
return QVariant(Qt::AlignCenter);
|
|
|
|
case Ping:
|
|
|
|
case Sent:
|
|
|
|
case Received:
|
|
|
|
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
|
|
|
|
case Subversion:
|
|
|
|
return {};
|
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
assert(false);
|
2020-12-24 07:15:57 -03:00
|
|
|
} else if (role == StatsRole) {
|
2021-02-22 04:51:21 -03:00
|
|
|
return QVariant::fromValue(rec);
|
2014-05-23 13:09:59 -04:00
|
|
|
}
|
2014-08-04 10:25:21 -04:00
|
|
|
|
2014-05-23 13:09:59 -04:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant PeerTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if(orientation == Qt::Horizontal)
|
|
|
|
{
|
|
|
|
if(role == Qt::DisplayRole && section < columns.size())
|
|
|
|
{
|
|
|
|
return columns[section];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags PeerTableModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
2018-07-31 14:02:34 -04:00
|
|
|
if (!index.isValid()) return Qt::NoItemFlags;
|
2014-05-23 13:09:59 -04:00
|
|
|
|
|
|
|
Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex PeerTableModel::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
CNodeCombinedStats *data = priv->index(row);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
return createIndex(row, column, data);
|
2015-06-21 08:07:08 -03:00
|
|
|
return QModelIndex();
|
2014-05-23 13:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerTableModel::refresh()
|
|
|
|
{
|
2015-07-14 08:59:05 -03:00
|
|
|
Q_EMIT layoutAboutToBeChanged();
|
2017-04-17 16:57:19 -03:00
|
|
|
priv->refreshPeers(m_node);
|
2015-07-14 08:59:05 -03:00
|
|
|
Q_EMIT layoutChanged();
|
2014-05-23 13:09:59 -04:00
|
|
|
}
|