You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

peertablemodel.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright (c) 2011-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "peertablemodel.h"
  5. #include "clientmodel.h"
  6. #include "guiconstants.h"
  7. #include "guiutil.h"
  8. #include "validation.h" // for cs_main
  9. #include "sync.h"
  10. #include <QDebug>
  11. #include <QList>
  12. #include <QTimer>
  13. bool NodeLessThan::operator()(const CNodeCombinedStats &left, const CNodeCombinedStats &right) const
  14. {
  15. const CNodeStats *pLeft = &(left.nodeStats);
  16. const CNodeStats *pRight = &(right.nodeStats);
  17. if (order == Qt::DescendingOrder)
  18. std::swap(pLeft, pRight);
  19. switch(column)
  20. {
  21. case PeerTableModel::NetNodeId:
  22. return pLeft->nodeid < pRight->nodeid;
  23. case PeerTableModel::Address:
  24. return pLeft->addrName.compare(pRight->addrName) < 0;
  25. case PeerTableModel::Subversion:
  26. return pLeft->cleanSubVer.compare(pRight->cleanSubVer) < 0;
  27. case PeerTableModel::Ping:
  28. return pLeft->dMinPing < pRight->dMinPing;
  29. }
  30. return false;
  31. }
  32. // private implementation
  33. class PeerTablePriv
  34. {
  35. public:
  36. /** Local cache of peer information */
  37. QList<CNodeCombinedStats> cachedNodeStats;
  38. /** Column to sort nodes by */
  39. int sortColumn;
  40. /** Order (ascending or descending) to sort nodes by */
  41. Qt::SortOrder sortOrder;
  42. /** Index of rows by node ID */
  43. std::map<NodeId, int> mapNodeRows;
  44. /** Pull a full list of peers from vNodes into our cache */
  45. void refreshPeers()
  46. {
  47. {
  48. cachedNodeStats.clear();
  49. std::vector<CNodeStats> vstats;
  50. if(g_connman)
  51. g_connman->GetNodeStats(vstats);
  52. #if QT_VERSION >= 0x040700
  53. cachedNodeStats.reserve(vstats.size());
  54. #endif
  55. for (const CNodeStats& nodestats : vstats)
  56. {
  57. CNodeCombinedStats stats;
  58. stats.nodeStateStats.nMisbehavior = 0;
  59. stats.nodeStateStats.nSyncHeight = -1;
  60. stats.nodeStateStats.nCommonHeight = -1;
  61. stats.fNodeStateStatsAvailable = false;
  62. stats.nodeStats = nodestats;
  63. cachedNodeStats.append(stats);
  64. }
  65. }
  66. // Try to retrieve the CNodeStateStats for each node.
  67. {
  68. TRY_LOCK(cs_main, lockMain);
  69. if (lockMain)
  70. {
  71. for (CNodeCombinedStats &stats : cachedNodeStats)
  72. stats.fNodeStateStatsAvailable = GetNodeStateStats(stats.nodeStats.nodeid, stats.nodeStateStats);
  73. }
  74. }
  75. if (sortColumn >= 0)
  76. // sort cacheNodeStats (use stable sort to prevent rows jumping around unnecessarily)
  77. qStableSort(cachedNodeStats.begin(), cachedNodeStats.end(), NodeLessThan(sortColumn, sortOrder));
  78. // build index map
  79. mapNodeRows.clear();
  80. int row = 0;
  81. for (const CNodeCombinedStats& stats : cachedNodeStats)
  82. mapNodeRows.insert(std::pair<NodeId, int>(stats.nodeStats.nodeid, row++));
  83. }
  84. int size() const
  85. {
  86. return cachedNodeStats.size();
  87. }
  88. CNodeCombinedStats *index(int idx)
  89. {
  90. if (idx >= 0 && idx < cachedNodeStats.size())
  91. return &cachedNodeStats[idx];
  92. return 0;
  93. }
  94. };
  95. PeerTableModel::PeerTableModel(ClientModel *parent) :
  96. QAbstractTableModel(parent),
  97. clientModel(parent),
  98. timer(0)
  99. {
  100. columns << tr("NodeId") << tr("Node/Service") << tr("User Agent") << tr("Ping");
  101. priv.reset(new PeerTablePriv());
  102. // default to unsorted
  103. priv->sortColumn = -1;
  104. // set up timer for auto refresh
  105. timer = new QTimer(this);
  106. connect(timer, SIGNAL(timeout()), SLOT(refresh()));
  107. timer->setInterval(MODEL_UPDATE_DELAY);
  108. // load initial data
  109. refresh();
  110. }
  111. PeerTableModel::~PeerTableModel()
  112. {
  113. // Intentionally left empty
  114. }
  115. void PeerTableModel::startAutoRefresh()
  116. {
  117. timer->start();
  118. }
  119. void PeerTableModel::stopAutoRefresh()
  120. {
  121. timer->stop();
  122. }
  123. int PeerTableModel::rowCount(const QModelIndex &parent) const
  124. {
  125. Q_UNUSED(parent);
  126. return priv->size();
  127. }
  128. int PeerTableModel::columnCount(const QModelIndex &parent) const
  129. {
  130. Q_UNUSED(parent);
  131. return columns.length();
  132. }
  133. QVariant PeerTableModel::data(const QModelIndex &index, int role) const
  134. {
  135. if(!index.isValid())
  136. return QVariant();
  137. CNodeCombinedStats *rec = static_cast<CNodeCombinedStats*>(index.internalPointer());
  138. if (role == Qt::DisplayRole) {
  139. switch(index.column())
  140. {
  141. case NetNodeId:
  142. return (qint64)rec->nodeStats.nodeid;
  143. case Address:
  144. return QString::fromStdString(rec->nodeStats.addrName);
  145. case Subversion:
  146. return QString::fromStdString(rec->nodeStats.cleanSubVer);
  147. case Ping:
  148. return GUIUtil::formatPingTime(rec->nodeStats.dMinPing);
  149. }
  150. } else if (role == Qt::TextAlignmentRole) {
  151. if (index.column() == Ping)
  152. return (QVariant)(Qt::AlignRight | Qt::AlignVCenter);
  153. }
  154. return QVariant();
  155. }
  156. QVariant PeerTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  157. {
  158. if(orientation == Qt::Horizontal)
  159. {
  160. if(role == Qt::DisplayRole && section < columns.size())
  161. {
  162. return columns[section];
  163. }
  164. }
  165. return QVariant();
  166. }
  167. Qt::ItemFlags PeerTableModel::flags(const QModelIndex &index) const
  168. {
  169. if(!index.isValid())
  170. return 0;
  171. Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  172. return retval;
  173. }
  174. QModelIndex PeerTableModel::index(int row, int column, const QModelIndex &parent) const
  175. {
  176. Q_UNUSED(parent);
  177. CNodeCombinedStats *data = priv->index(row);
  178. if (data)
  179. return createIndex(row, column, data);
  180. return QModelIndex();
  181. }
  182. const CNodeCombinedStats *PeerTableModel::getNodeStats(int idx)
  183. {
  184. return priv->index(idx);
  185. }
  186. void PeerTableModel::refresh()
  187. {
  188. Q_EMIT layoutAboutToBeChanged();
  189. priv->refreshPeers();
  190. Q_EMIT layoutChanged();
  191. }
  192. int PeerTableModel::getRowByNodeId(NodeId nodeid)
  193. {
  194. std::map<NodeId, int>::iterator it = priv->mapNodeRows.find(nodeid);
  195. if (it == priv->mapNodeRows.end())
  196. return -1;
  197. return it->second;
  198. }
  199. void PeerTableModel::sort(int column, Qt::SortOrder order)
  200. {
  201. priv->sortColumn = column;
  202. priv->sortOrder = order;
  203. refresh();
  204. }