2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2011-2020 The Bitcoin Core developers
|
2014-12-13 12:09:33 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-05-23 12:09:59 -05:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <qt/peertablemodel.h>
|
2014-05-23 12:09:59 -05:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <qt/guiconstants.h>
|
|
|
|
#include <qt/guiutil.h>
|
2014-05-23 12:09:59 -05:00
|
|
|
|
2018-04-07 03:42:02 -04:00
|
|
|
#include <interfaces/node.h>
|
2014-05-23 12:09:59 -05:00
|
|
|
|
2019-11-29 21:18:24 +02:00
|
|
|
#include <utility>
|
2019-08-21 09:42:29 +08:00
|
|
|
|
2014-05-23 12:09:59 -05: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 03:42:02 -04:00
|
|
|
void refreshPeers(interfaces::Node& node)
|
2014-06-04 12:06:18 +02:00
|
|
|
{
|
2014-05-23 12:09:59 -05:00
|
|
|
cachedNodeStats.clear();
|
2017-04-17 15:57:19 -04:00
|
|
|
|
2018-04-07 03:42:02 -04:00
|
|
|
interfaces::Node::NodesStats nodes_stats;
|
2017-04-17 15:57:19 -04:00
|
|
|
node.getNodesStats(nodes_stats);
|
|
|
|
cachedNodeStats.reserve(nodes_stats.size());
|
2018-06-18 07:58:28 +02:00
|
|
|
for (const auto& node_stats : nodes_stats)
|
2014-05-23 12:09:59 -05:00
|
|
|
{
|
|
|
|
CNodeCombinedStats stats;
|
2017-04-17 15:57:19 -04: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 12:09:59 -05:00
|
|
|
cachedNodeStats.append(stats);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 13:07:08 +02:00
|
|
|
int size() const
|
2014-05-23 12:09:59 -05:00
|
|
|
{
|
|
|
|
return cachedNodeStats.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
CNodeCombinedStats *index(int idx)
|
|
|
|
{
|
2015-06-21 13:07:08 +02:00
|
|
|
if (idx >= 0 && idx < cachedNodeStats.size())
|
2014-05-23 12:09:59 -05:00
|
|
|
return &cachedNodeStats[idx];
|
2015-06-21 13:07:08 +02:00
|
|
|
|
2018-07-31 14:02:34 -04:00
|
|
|
return nullptr;
|
2014-05-23 12:09:59 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-03 14:48:27 +00:00
|
|
|
PeerTableModel::PeerTableModel(interfaces::Node& node, QObject* parent) :
|
2014-06-04 12:06:18 +02:00
|
|
|
QAbstractTableModel(parent),
|
2017-04-17 15:57:19 -04:00
|
|
|
m_node(node),
|
2018-07-31 14:02:34 -04:00
|
|
|
timer(nullptr)
|
2014-05-23 12:09:59 -05:00
|
|
|
{
|
2016-11-18 15:47:20 +01:00
|
|
|
priv.reset(new PeerTablePriv());
|
2014-05-23 12:09:59 -05:00
|
|
|
|
|
|
|
// set up timer for auto refresh
|
2016-11-18 15:47:20 +01:00
|
|
|
timer = new QTimer(this);
|
2018-06-24 16:18:22 +01:00
|
|
|
connect(timer, &QTimer::timeout, this, &PeerTableModel::refresh);
|
2014-06-04 12:06:18 +02:00
|
|
|
timer->setInterval(MODEL_UPDATE_DELAY);
|
2014-05-23 12:09:59 -05:00
|
|
|
|
|
|
|
// load initial data
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2016-11-18 15:47:20 +01:00
|
|
|
PeerTableModel::~PeerTableModel()
|
|
|
|
{
|
|
|
|
// Intentionally left empty
|
|
|
|
}
|
|
|
|
|
2014-06-04 12:06:18 +02:00
|
|
|
void PeerTableModel::startAutoRefresh()
|
2014-05-23 12:09:59 -05:00
|
|
|
{
|
|
|
|
timer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerTableModel::stopAutoRefresh()
|
|
|
|
{
|
|
|
|
timer->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
int PeerTableModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2021-01-02 21:40:53 +02:00
|
|
|
if (parent.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-23 12:09:59 -05:00
|
|
|
return priv->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int PeerTableModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2021-01-02 21:40:53 +02:00
|
|
|
if (parent.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-12-29 22:42:27 -05:00
|
|
|
return columns.length();
|
2014-05-23 12:09:59 -05: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 22:29:15 +02:00
|
|
|
const auto column = static_cast<ColumnIndex>(index.column());
|
2014-08-04 16:25:21 +02:00
|
|
|
if (role == Qt::DisplayRole) {
|
2020-12-25 22:29:15 +02:00
|
|
|
switch (column) {
|
2016-08-19 15:38:04 -04:00
|
|
|
case NetNodeId:
|
2017-04-10 15:00:23 -04:00
|
|
|
return (qint64)rec->nodeStats.nodeid;
|
2014-05-23 12:09:59 -05:00
|
|
|
case Address:
|
2018-06-24 22:09:01 +02: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 20:10:12 +01:00
|
|
|
case ConnectionType:
|
|
|
|
return GUIUtil::ConnectionTypeToQString(rec->nodeStats.m_conn_type, /* prepend_direction */ false);
|
2020-12-25 14:46:03 +01:00
|
|
|
case Network:
|
|
|
|
return GUIUtil::NetworkToQString(rec->nodeStats.m_network);
|
2014-06-04 12:06:18 +02:00
|
|
|
case Ping:
|
2020-09-29 19:11:53 -07:00
|
|
|
return GUIUtil::formatPingTime(rec->nodeStats.m_min_ping_time);
|
2017-10-14 16:06:54 -07:00
|
|
|
case Sent:
|
|
|
|
return GUIUtil::formatBytes(rec->nodeStats.nSendBytes);
|
|
|
|
case Received:
|
|
|
|
return GUIUtil::formatBytes(rec->nodeStats.nRecvBytes);
|
2020-12-25 14:46:03 +01:00
|
|
|
case Subversion:
|
|
|
|
return QString::fromStdString(rec->nodeStats.cleanSubVer);
|
2020-12-25 22:29:15 +02:00
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
assert(false);
|
2014-08-04 16:25:21 +02:00
|
|
|
} else if (role == Qt::TextAlignmentRole) {
|
2020-12-25 22:29:15 +02: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 12:15:57 +02:00
|
|
|
} else if (role == StatsRole) {
|
2021-02-22 09:51:21 +02:00
|
|
|
return QVariant::fromValue(rec);
|
2014-05-23 12:09:59 -05:00
|
|
|
}
|
2014-08-04 16:25:21 +02:00
|
|
|
|
2014-05-23 12:09:59 -05: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 12:09:59 -05: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 13:07:08 +02:00
|
|
|
return QModelIndex();
|
2014-05-23 12:09:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerTableModel::refresh()
|
|
|
|
{
|
2015-07-14 13:59:05 +02:00
|
|
|
Q_EMIT layoutAboutToBeChanged();
|
2017-04-17 15:57:19 -04:00
|
|
|
priv->refreshPeers(m_node);
|
2015-07-14 13:59:05 +02:00
|
|
|
Q_EMIT layoutChanged();
|
2014-05-23 12:09:59 -05:00
|
|
|
}
|