PyBitmessage-2021-04-27/src/network/stats.py

78 lines
2.6 KiB
Python

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from __future__ import print_function
from future import standard_library
standard_library.install_aliases()
from builtins import *
from past.utils import old_div
import time
from . import asyncore_pollchoose as asyncore
from .fix_circular_imports import BMConnectionPool, missingObjects
lastReceivedTimestamp = time.time()
lastReceivedBytes = 0
currentReceivedSpeed = 0
lastSentTimestamp = time.time()
lastSentBytes = 0
currentSentSpeed = 0
def connectedHostsList():
retval = []
for i in list(BMConnectionPool().inboundConnections.values()) + \
list(BMConnectionPool().outboundConnections.values()):
if not i.fullyEstablished:
continue
try:
retval.append(i)
except AttributeError:
pass
return retval
def sentBytes():
return asyncore.sentBytes
def uploadSpeed():
global lastSentTimestamp, lastSentBytes, currentSentSpeed
currentTimestamp = time.time()
if int(lastSentTimestamp) < int(currentTimestamp):
currentSentBytes = asyncore.sentBytes
currentSentSpeed = int(old_div((currentSentBytes - lastSentBytes), (currentTimestamp - lastSentTimestamp)))
lastSentBytes = currentSentBytes
lastSentTimestamp = currentTimestamp
return currentSentSpeed
def receivedBytes():
return asyncore.receivedBytes
def downloadSpeed():
global lastReceivedTimestamp, lastReceivedBytes, currentReceivedSpeed
currentTimestamp = time.time()
if int(lastReceivedTimestamp) < int(currentTimestamp):
currentReceivedBytes = asyncore.receivedBytes
currentReceivedSpeed = int(old_div((currentReceivedBytes - lastReceivedBytes),
(currentTimestamp - lastReceivedTimestamp)))
lastReceivedBytes = currentReceivedBytes
lastReceivedTimestamp = currentTimestamp
return currentReceivedSpeed
def pendingDownload():
return len(missingObjects)
#tmp = {}
#for connection in BMConnectionPool().inboundConnections.values() + \
# BMConnectionPool().outboundConnections.values():
# for k in connection.objectsNewToMe.keys():
# tmp[k] = True
#return len(tmp)
def pendingUpload():
#tmp = {}
#for connection in BMConnectionPool().inboundConnections.values() + \
# BMConnectionPool().outboundConnections.values():
# for k in connection.objectsNewToThem.keys():
# tmp[k] = True
#This probably isn't the correct logic so it's disabled
#return len(tmp)
return 0