From 16d64dda283c04985524abbd37ee27f03420f66a Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Sat, 21 Feb 2015 16:56:48 +0100 Subject: [PATCH] Network table showing current network status --- res/ui/network_tab.ui | 12 +++++ src/cutecoin/core/community.py | 6 +++ src/cutecoin/core/network/__init__.py | 0 src/cutecoin/core/network/node.py | 50 +++++++++++++++++++++ src/cutecoin/gui/network_tab.py | 2 +- src/cutecoin/models/network.py | 64 +++++++++++++++++++-------- 6 files changed, 115 insertions(+), 19 deletions(-) create mode 100644 src/cutecoin/core/network/__init__.py create mode 100644 src/cutecoin/core/network/node.py diff --git a/res/ui/network_tab.ui b/res/ui/network_tab.ui index 62812626..ff6022e1 100644 --- a/res/ui/network_tab.ui +++ b/res/ui/network_tab.ui @@ -18,6 +18,18 @@ <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QTableView" name="table_network"> + <property name="horizontalScrollBarPolicy"> + <enum>Qt::ScrollBarAlwaysOff</enum> + </property> + <property name="sizeAdjustPolicy"> + <enum>QAbstractScrollArea::AdjustToContents</enum> + </property> + <property name="selectionBehavior"> + <enum>QAbstractItemView::SelectRows</enum> + </property> + <property name="sortingEnabled"> + <bool>true</bool> + </property> <attribute name="horizontalHeaderShowSortIndicator" stdset="0"> <bool>true</bool> </attribute> diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py index 1c0bffb4..6242a98e 100644 --- a/src/cutecoin/core/community.py +++ b/src/cutecoin/core/community.py @@ -9,6 +9,7 @@ from ucoinpy import PROTOCOL_VERSION from ucoinpy.documents.peer import Peer, Endpoint, BMAEndpoint from ucoinpy.documents.block import Block from ..tools.exceptions import NoPeerAvailable +from .network.node import Node import logging import inspect import hashlib @@ -80,6 +81,7 @@ class Community(object): ''' self.currency = currency self.peers = [p for p in peers if p.currency == currency] + self._nodes = [Node.from_peer(p) for p in peers] self._cache = Cache(self) self._cache.refresh() @@ -243,6 +245,10 @@ class Community(object): logging.debug("Peers found : {0}".format(peers)) return peers + @property + def nodes(self): + return self._nodes + def get_block(self, number=None): if number is None: data = self.request(bma.blockchain.Current) diff --git a/src/cutecoin/core/network/__init__.py b/src/cutecoin/core/network/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/cutecoin/core/network/node.py b/src/cutecoin/core/network/node.py new file mode 100644 index 00000000..10e6f391 --- /dev/null +++ b/src/cutecoin/core/network/node.py @@ -0,0 +1,50 @@ +''' +Created on 21 févr. 2015 + +@author: inso +''' + +from ucoinpy.documents.peer import Peer, BMAEndpoint +from ucoinpy.api import bma +from ...core.person import Person +from ...tools.exceptions import PersonNotFoundError + + +class Node(object): + ''' + classdocs + ''' + + def __init__(self, endpoint, pubkey, block): + ''' + Constructor + ''' + self._endpoint = endpoint + self._pubkey = pubkey + self._block = block + + @classmethod + def from_peer(cls, peer): + e = next((e for e in peer.endpoints if type(e) is BMAEndpoint)) + informations = bma.network.Peering(e.conn_handler()).get() + try: + block = bma.blockchain.Current(e.conn_handler()).get() + block_number = block["number"] + except ValueError as e: + if '404' in e: + block_number = 0 + node_pubkey = informations["pubkey"] + + return cls(e, node_pubkey, block_number) + + @property + def pubkey(self): + return self._pubkey + + @property + def endpoint(self): + return self._endpoint + + @property + def block(self): + return self._block diff --git a/src/cutecoin/gui/network_tab.py b/src/cutecoin/gui/network_tab.py index b1da42f0..6783e33b 100644 --- a/src/cutecoin/gui/network_tab.py +++ b/src/cutecoin/gui/network_tab.py @@ -21,7 +21,7 @@ class NetworkTabWidget(QWidget, Ui_NetworkTabWidget): super().__init__() self.setupUi(self) model = NetworkTableModel(community) - proxy = NetworkFilterProxyModel() + proxy = NetworkFilterProxyModel(self) proxy.setSourceModel(model) self.table_network.setModel(proxy) diff --git a/src/cutecoin/models/network.py b/src/cutecoin/models/network.py index d0b00066..dc72f09a 100644 --- a/src/cutecoin/models/network.py +++ b/src/cutecoin/models/network.py @@ -5,14 +5,10 @@ Created on 5 févr. 2014 ''' import logging -from ..core.transfer import Transfer, Received from ..core.person import Person from ..tools.exceptions import PersonNotFoundError -from ucoinpy.documents.peer import BMAEndpoint from ucoinpy.api import bma -from PyQt5.QtCore import QAbstractTableModel, Qt, QVariant, QSortFilterProxyModel, \ - QDateTime -from PyQt5.QtGui import QFont, QColor +from PyQt5.QtCore import QAbstractTableModel, Qt, QVariant, QSortFilterProxyModel class NetworkFilterProxyModel(QSortFilterProxyModel): @@ -35,6 +31,19 @@ class NetworkFilterProxyModel(QSortFilterProxyModel): right_data = self.sourceModel().data(right, Qt.DisplayRole) return (left_data < right_data) + def headerData(self, section, orientation, role): + if role != Qt.DisplayRole: + return QVariant() + + header_names = {'pubkey': 'Pubkey', + 'is_member': 'Membre', + 'uid': 'UID', + 'address': 'Address', + 'port': 'Port', + 'current_block': 'Block'} + type = self.sourceModel().headerData(section, orientation, role) + return header_names[type] + def data(self, index, role): source_index = self.mapToSource(index) source_data = self.sourceModel().data(source_index, role) @@ -54,30 +63,49 @@ class NetworkTableModel(QAbstractTableModel): super().__init__(parent) self.community = community self.column_types = ( + 'pubkey', + 'is_member', + 'uid', 'address', - 'port' + 'port', + 'current_block' ) @property - def peers(self): - return self.community.peers + def nodes(self): + return self.community.nodes def rowCount(self, parent): - return len(self.peers) + return len(self.nodes) def columnCount(self, parent): return len(self.column_types) def headerData(self, section, orientation, role): + if role != Qt.DisplayRole: + return QVariant() + return self.column_types[section] - def data_peer(self, peer): - e = next((e for e in peer.endpoints if type(e) is BMAEndpoint)) - informations = bma.network.peering.Peers(e.conn_handler()).get() - if e.server: - address = e.server - elif e.ipv4: - address = e.port + def data_node(self, node): + try: + person = Person.lookup(node.pubkey, self.community) + uid = person.name + except PersonNotFoundError: + uid = "" + + is_member = node.pubkey in self.community.members_pubkeys() + + address = "" + if node.endpoint.server: + address = node.endpoint.server + elif node.endpoint.ipv4: + address = node.endpoint.ipv4 + elif node.endpoint.ipv6: + address = node.endpoint.ipv6 + port = node.endpoint.port + + return (node.pubkey, is_member, uid, address, port, node.block) def data(self, index, role): row = index.row() @@ -86,9 +114,9 @@ class NetworkTableModel(QAbstractTableModel): if not index.isValid(): return QVariant() - peer = self.peers[row] + node = self.nodes[row] if role == Qt.DisplayRole: - return self.data_peer(peer)[col] + return self.data_node(node)[col] def flags(self, index): return Qt.ItemIsSelectable | Qt.ItemIsEnabled -- GitLab