2018-10-10 12:49:48 +02:00
|
|
|
"""
|
2018-11-20 13:11:18 +01:00
|
|
|
Network status tab widget definition.
|
2018-10-10 12:49:48 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-25 18:17:02 +02:00
|
|
|
import time
|
2017-03-19 22:08:00 +01:00
|
|
|
|
2018-02-15 18:28:01 +01:00
|
|
|
from qtpy import QtCore, QtGui, QtWidgets
|
2018-10-10 12:49:48 +02:00
|
|
|
|
2016-03-16 19:27:12 +01:00
|
|
|
import l10n
|
2017-05-25 14:59:18 +02:00
|
|
|
import network.stats
|
2019-11-05 17:54:04 +01:00
|
|
|
import state
|
2016-03-16 19:27:12 +01:00
|
|
|
import widgets
|
2018-10-10 12:49:48 +02:00
|
|
|
from inventory import Inventory
|
2020-07-16 16:05:32 +02:00
|
|
|
from network import BMConnectionPool, knownnodes
|
2018-10-10 12:49:48 +02:00
|
|
|
from retranslateui import RetranslateMixin
|
|
|
|
from tr import _translate
|
|
|
|
from uisignaler import UISignaler
|
2016-03-16 19:27:12 +01:00
|
|
|
|
2017-10-24 13:02:15 +02:00
|
|
|
|
2018-02-15 18:28:01 +01:00
|
|
|
class NetworkStatus(QtWidgets.QWidget, RetranslateMixin):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Network status tab"""
|
2016-03-16 19:27:12 +01:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
super(NetworkStatus, self).__init__(parent)
|
|
|
|
widgets.load('networkstatus.ui', self)
|
|
|
|
|
2017-10-24 13:02:15 +02:00
|
|
|
header = self.tableWidgetConnectionCount.horizontalHeader()
|
2018-02-22 11:33:16 +01:00
|
|
|
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
|
|
|
|
header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
|
|
|
|
header.setSectionResizeMode(2, QtWidgets.QHeaderView.Stretch)
|
2017-10-24 13:02:15 +02:00
|
|
|
|
|
|
|
# Somehow this value was 5 when I tested
|
|
|
|
if header.sortIndicatorSection() > 4:
|
|
|
|
header.setSortIndicator(0, QtCore.Qt.AscendingOrder)
|
2017-06-24 12:16:12 +02:00
|
|
|
|
2016-04-25 18:17:02 +02:00
|
|
|
self.startup = time.localtime()
|
2018-10-10 12:49:48 +02:00
|
|
|
|
2016-03-16 19:27:12 +01:00
|
|
|
self.UISignalThread = UISignaler.get()
|
2018-02-15 18:28:01 +01:00
|
|
|
self.UISignalThread.updateNumberOfMessagesProcessed.connect(
|
|
|
|
self.updateNumberOfMessagesProcessed)
|
|
|
|
self.UISignalThread.updateNumberOfPubkeysProcessed.connect(
|
|
|
|
self.updateNumberOfPubkeysProcessed)
|
|
|
|
self.UISignalThread.updateNumberOfBroadcastsProcessed.connect(
|
|
|
|
self.updateNumberOfBroadcastsProcessed)
|
|
|
|
self.UISignalThread.updateNetworkStatusTab.connect(
|
|
|
|
self.updateNetworkStatusTab)
|
2018-02-05 13:39:32 +01:00
|
|
|
|
2016-03-16 19:27:12 +01:00
|
|
|
self.timer = QtCore.QTimer()
|
2018-02-15 18:28:01 +01:00
|
|
|
self.timer.timeout.connect(self.runEveryTwoSeconds)
|
2018-02-05 13:39:32 +01:00
|
|
|
|
|
|
|
def startUpdate(self):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Start a timer to update counters every 2 seconds"""
|
2018-02-06 16:33:19 +01:00
|
|
|
Inventory().numberOfInventoryLookupsPerformed = 0
|
2018-02-05 13:39:32 +01:00
|
|
|
self.runEveryTwoSeconds()
|
|
|
|
self.timer.start(2000) # milliseconds
|
|
|
|
|
|
|
|
def stopUpdate(self):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Stop counter update timer"""
|
2018-02-05 13:39:32 +01:00
|
|
|
self.timer.stop()
|
2016-03-16 19:27:12 +01:00
|
|
|
|
2018-02-15 18:28:01 +01:00
|
|
|
@staticmethod
|
|
|
|
def formatBytes(num):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Format bytes nicely (SI prefixes)"""
|
2018-02-15 18:28:01 +01:00
|
|
|
for x in (
|
|
|
|
_translate("networkstatus", "byte(s)", None, num),
|
|
|
|
"kB", "MB", "GB"
|
|
|
|
):
|
2016-03-17 19:03:17 +01:00
|
|
|
if num < 1000.0:
|
|
|
|
return "%3.0f %s" % (num, x)
|
|
|
|
num /= 1000.0
|
2018-02-15 18:28:01 +01:00
|
|
|
return "%3.0f %s" % (num, "TB")
|
2016-03-17 19:03:17 +01:00
|
|
|
|
2018-02-15 18:28:01 +01:00
|
|
|
@staticmethod
|
|
|
|
def formatByteRate(num):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Format transfer speed in kB/s"""
|
2016-03-17 19:03:17 +01:00
|
|
|
num /= 1000
|
2016-04-25 18:17:02 +02:00
|
|
|
return "%4.0f kB" % num
|
2018-10-10 12:49:48 +02:00
|
|
|
|
2016-04-27 09:48:53 +02:00
|
|
|
def updateNumberOfObjectsToBeSynced(self):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Update the counter for number of objects to be synced"""
|
2018-02-15 18:28:01 +01:00
|
|
|
self.labelSyncStatus.setText(_translate(
|
|
|
|
"networkstatus", "Object(s) to be synced: %n", None,
|
|
|
|
network.stats.pendingDownload() + network.stats.pendingUpload()))
|
2016-03-17 19:03:17 +01:00
|
|
|
|
2016-03-16 19:27:12 +01:00
|
|
|
def updateNumberOfMessagesProcessed(self):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Update the counter for number of processed messages"""
|
2017-10-19 08:39:09 +02:00
|
|
|
self.updateNumberOfObjectsToBeSynced()
|
2018-02-15 18:28:01 +01:00
|
|
|
self.labelMessageCount.setText(_translate(
|
|
|
|
"networkstatus", "Processed %n person-to-person message(s).",
|
|
|
|
None, state.numberOfMessagesProcessed))
|
2016-03-16 19:27:12 +01:00
|
|
|
|
|
|
|
def updateNumberOfBroadcastsProcessed(self):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Update the counter for the number of processed broadcasts"""
|
2017-10-19 08:39:09 +02:00
|
|
|
self.updateNumberOfObjectsToBeSynced()
|
2018-02-15 18:28:01 +01:00
|
|
|
self.labelBroadcastCount.setText(_translate(
|
|
|
|
"networkstatus", "Processed %n broadcast message(s).", None,
|
|
|
|
state.numberOfBroadcastsProcessed))
|
2016-03-16 19:27:12 +01:00
|
|
|
|
|
|
|
def updateNumberOfPubkeysProcessed(self):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Update the counter for the number of processed pubkeys"""
|
2017-10-19 08:39:09 +02:00
|
|
|
self.updateNumberOfObjectsToBeSynced()
|
2018-02-15 18:28:01 +01:00
|
|
|
self.labelPubkeyCount.setText(_translate(
|
|
|
|
"networkstatus", "Processed %n public key(s).", None,
|
|
|
|
state.numberOfPubkeysProcessed))
|
2016-03-16 19:27:12 +01:00
|
|
|
|
|
|
|
def updateNumberOfBytes(self):
|
|
|
|
"""
|
2018-02-15 18:28:01 +01:00
|
|
|
This function is run every two seconds, so we divide the rate
|
|
|
|
of bytes sent and received by 2.
|
2016-03-16 19:27:12 +01:00
|
|
|
"""
|
2018-02-15 18:28:01 +01:00
|
|
|
self.labelBytesRecvCount.setText(_translate(
|
|
|
|
"networkstatus", "Down: {0}/s Total: {1}").format(
|
|
|
|
self.formatByteRate(network.stats.downloadSpeed()),
|
|
|
|
self.formatBytes(network.stats.receivedBytes())
|
|
|
|
))
|
|
|
|
self.labelBytesSentCount.setText(_translate(
|
|
|
|
"networkstatus", "Up: {0}/s Total: {1}").format(
|
|
|
|
self.formatByteRate(network.stats.uploadSpeed()),
|
|
|
|
self.formatBytes(network.stats.sentBytes())
|
|
|
|
))
|
2016-03-16 19:27:12 +01:00
|
|
|
|
2017-10-19 08:39:09 +02:00
|
|
|
def updateNetworkStatusTab(self, outbound, add, destination):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Add or remove an entry to the list of connected peers"""
|
|
|
|
# pylint: disable=too-many-branches,undefined-variable
|
2017-10-19 08:39:09 +02:00
|
|
|
if outbound:
|
|
|
|
try:
|
|
|
|
c = BMConnectionPool().outboundConnections[destination]
|
|
|
|
except KeyError:
|
|
|
|
if add:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
c = BMConnectionPool().inboundConnections[destination]
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
c = BMConnectionPool().inboundConnections[destination.host]
|
|
|
|
except KeyError:
|
|
|
|
if add:
|
|
|
|
return
|
2016-03-16 19:27:12 +01:00
|
|
|
|
2017-06-24 12:16:12 +02:00
|
|
|
self.tableWidgetConnectionCount.setUpdatesEnabled(False)
|
2017-10-19 08:39:09 +02:00
|
|
|
self.tableWidgetConnectionCount.setSortingEnabled(False)
|
2018-10-10 12:49:48 +02:00
|
|
|
|
2017-10-19 08:39:09 +02:00
|
|
|
if add:
|
2016-03-16 19:27:12 +01:00
|
|
|
self.tableWidgetConnectionCount.insertRow(0)
|
2018-10-10 12:49:48 +02:00
|
|
|
self.tableWidgetConnectionCount.setItem(
|
2018-02-15 18:28:01 +01:00
|
|
|
0, 0, QtWidgets.QTableWidgetItem(
|
|
|
|
"%s:%i" % (destination.host, destination.port)))
|
2018-10-10 12:49:48 +02:00
|
|
|
self.tableWidgetConnectionCount.setItem(
|
2018-02-15 18:28:01 +01:00
|
|
|
0, 2, QtWidgets.QTableWidgetItem("%s" % (c.userAgent)))
|
2018-10-10 12:49:48 +02:00
|
|
|
self.tableWidgetConnectionCount.setItem(
|
2018-02-15 18:28:01 +01:00
|
|
|
0, 3, QtWidgets.QTableWidgetItem("%s" % (c.tlsVersion)))
|
2018-10-10 12:49:48 +02:00
|
|
|
self.tableWidgetConnectionCount.setItem(
|
2018-02-15 18:28:01 +01:00
|
|
|
0, 4, QtWidgets.QTableWidgetItem(
|
|
|
|
"%s" % ",".join(map(str, c.streams))))
|
2017-07-05 09:17:01 +02:00
|
|
|
try:
|
2018-10-10 12:49:48 +02:00
|
|
|
# .. todo:: FIXME: hard coded stream no
|
2018-02-15 18:28:01 +01:00
|
|
|
rating = "%.1f" % knownnodes.knownNodes[1][destination]['rating']
|
2017-07-05 09:17:01 +02:00
|
|
|
except KeyError:
|
|
|
|
rating = "-"
|
2018-10-10 12:49:48 +02:00
|
|
|
self.tableWidgetConnectionCount.setItem(
|
2018-02-15 18:28:01 +01:00
|
|
|
0, 1, QtWidgets.QTableWidgetItem("%s" % (rating)))
|
2017-10-19 08:39:09 +02:00
|
|
|
if outbound:
|
2018-02-15 18:28:01 +01:00
|
|
|
brush = QtGui.QBrush(
|
|
|
|
QtGui.QColor("yellow"), QtCore.Qt.SolidPattern)
|
2016-03-16 19:27:12 +01:00
|
|
|
else:
|
2018-02-15 18:28:01 +01:00
|
|
|
brush = QtGui.QBrush(
|
|
|
|
QtGui.QColor("green"), QtCore.Qt.SolidPattern)
|
2018-10-10 12:49:48 +02:00
|
|
|
for j in range(1):
|
2017-06-24 12:16:12 +02:00
|
|
|
self.tableWidgetConnectionCount.item(0, j).setBackground(brush)
|
2018-02-15 18:28:01 +01:00
|
|
|
self.tableWidgetConnectionCount.item(0, 0).setData(
|
|
|
|
QtCore.Qt.UserRole, destination)
|
|
|
|
self.tableWidgetConnectionCount.item(0, 1).setData(
|
|
|
|
QtCore.Qt.UserRole, outbound)
|
2017-10-19 08:39:09 +02:00
|
|
|
else:
|
2020-06-05 15:14:45 +02:00
|
|
|
if not BMConnectionPool().inboundConnections:
|
2019-06-07 13:54:00 +02:00
|
|
|
self.window().setStatusIcon('yellow')
|
2017-10-19 08:39:09 +02:00
|
|
|
for i in range(self.tableWidgetConnectionCount.rowCount()):
|
2018-02-15 18:28:01 +01:00
|
|
|
if self.tableWidgetConnectionCount.item(i, 0).data(
|
|
|
|
QtCore.Qt.UserRole) != destination:
|
2017-10-19 08:39:09 +02:00
|
|
|
continue
|
2018-02-15 18:28:01 +01:00
|
|
|
if self.tableWidgetConnectionCount.item(i, 1).data(
|
|
|
|
QtCore.Qt.UserRole) == outbound:
|
2017-10-19 08:39:09 +02:00
|
|
|
self.tableWidgetConnectionCount.removeRow(i)
|
|
|
|
break
|
2018-10-10 12:49:48 +02:00
|
|
|
|
2017-06-24 12:16:12 +02:00
|
|
|
self.tableWidgetConnectionCount.setUpdatesEnabled(True)
|
2017-10-19 08:39:09 +02:00
|
|
|
self.tableWidgetConnectionCount.setSortingEnabled(True)
|
2018-02-15 18:28:01 +01:00
|
|
|
self.labelTotalConnections.setText(_translate(
|
|
|
|
"networkstatus", "Total Connections: {0}").format(
|
|
|
|
self.tableWidgetConnectionCount.rowCount()
|
|
|
|
))
|
|
|
|
# FYI: The 'singlelistener' thread sets the icon color to green
|
|
|
|
# when it receives an incoming connection, meaning that the user's
|
|
|
|
# firewall is configured correctly.
|
|
|
|
if self.tableWidgetConnectionCount.rowCount():
|
|
|
|
if state.statusIconColor == 'red':
|
|
|
|
self.window().setStatusIcon('yellow')
|
|
|
|
elif state.statusIconColor != 'red':
|
2016-03-16 19:27:12 +01:00
|
|
|
self.window().setStatusIcon('red')
|
|
|
|
|
|
|
|
# timer driven
|
|
|
|
def runEveryTwoSeconds(self):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Updates counters, runs every 2 seconds if the timer is running"""
|
2018-02-15 18:28:01 +01:00
|
|
|
self.labelLookupsPerSecond.setText(_translate(
|
|
|
|
"networkstatus", "Inventory lookups per second: {0}"
|
|
|
|
).format(Inventory().numberOfInventoryLookupsPerformed / 2))
|
2017-01-10 21:15:35 +01:00
|
|
|
Inventory().numberOfInventoryLookupsPerformed = 0
|
2016-03-16 19:27:12 +01:00
|
|
|
self.updateNumberOfBytes()
|
2016-04-27 12:16:21 +02:00
|
|
|
self.updateNumberOfObjectsToBeSynced()
|
2016-04-25 18:17:02 +02:00
|
|
|
|
|
|
|
def retranslateUi(self):
|
2018-11-20 13:11:18 +01:00
|
|
|
"""Conventional Qt Designer method for dynamic l10n"""
|
2017-06-24 12:16:12 +02:00
|
|
|
super(NetworkStatus, self).retranslateUi()
|
2018-02-15 18:28:01 +01:00
|
|
|
self.labelTotalConnections.setText(_translate(
|
|
|
|
"networkstatus", "Total Connections: {0}"
|
|
|
|
).format(self.tableWidgetConnectionCount.rowCount()))
|
2018-11-20 13:11:18 +01:00
|
|
|
self.labelStartupTime.setText(_translate(
|
2018-02-15 18:28:01 +01:00
|
|
|
"networkstatus", "Since startup on {0}"
|
|
|
|
).format(l10n.formatTimestamp(self.startup)))
|
2018-11-20 13:11:18 +01:00
|
|
|
self.updateNumberOfMessagesProcessed()
|
|
|
|
self.updateNumberOfBroadcastsProcessed()
|
|
|
|
self.updateNumberOfPubkeysProcessed()
|