2017-05-29 00:24:07 +02:00
|
|
|
import time
|
|
|
|
|
2017-05-25 14:59:18 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
|
|
|
from network.connectionpool import BMConnectionPool
|
2017-05-29 00:24:07 +02:00
|
|
|
from inventory import PendingDownloadQueue, PendingUpload
|
2017-05-25 14:59:18 +02:00
|
|
|
import asyncore_pollchoose as asyncore
|
|
|
|
import shared
|
|
|
|
import throttle
|
|
|
|
|
2017-05-29 00:24:07 +02:00
|
|
|
lastReceivedTimestamp = time.time()
|
|
|
|
lastReceivedBytes = 0
|
|
|
|
currentReceivedSpeed = 0
|
|
|
|
lastSentTimestamp = time.time()
|
|
|
|
lastSentBytes = 0
|
|
|
|
currentSentSpeed = 0
|
|
|
|
|
2017-05-25 14:59:18 +02:00
|
|
|
def connectedHostsList():
|
2017-05-29 03:16:14 +02:00
|
|
|
if BMConfigParser().get("network", "asyncore"):
|
2017-05-25 14:59:18 +02:00
|
|
|
retval = []
|
|
|
|
for i in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values():
|
|
|
|
if not i.connected:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
retval.append((i.destination, i.streams[0]))
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
return retval
|
|
|
|
else:
|
|
|
|
return shared.connectedHostsList.items()
|
|
|
|
|
|
|
|
def sentBytes():
|
2017-05-29 03:16:14 +02:00
|
|
|
if BMConfigParser().get("network", "asyncore"):
|
2017-05-25 14:59:18 +02:00
|
|
|
return asyncore.sentBytes
|
|
|
|
else:
|
|
|
|
return throttle.SendThrottle().total
|
|
|
|
|
|
|
|
def uploadSpeed():
|
2017-05-29 00:24:07 +02:00
|
|
|
global lastSentTimestamp, lastSentBytes, currentSentSpeed
|
2017-05-29 03:16:14 +02:00
|
|
|
if BMConfigParser().get("network", "asyncore"):
|
2017-05-29 00:24:07 +02:00
|
|
|
currentTimestamp = time.time()
|
|
|
|
if int(lastSentTimestamp) < int(currentTimestamp):
|
|
|
|
currentSentBytes = asyncore.sentBytes
|
|
|
|
currentSentSpeed = int((currentSentBytes - lastSentBytes) / (currentTimestamp - lastSentTimestamp))
|
|
|
|
lastSentBytes = currentSentBytes
|
|
|
|
lastSentTimestamp = currentTimestamp
|
|
|
|
return currentSentSpeed
|
2017-05-25 14:59:18 +02:00
|
|
|
else:
|
|
|
|
return throttle.sendThrottle().getSpeed()
|
|
|
|
|
|
|
|
def receivedBytes():
|
2017-05-29 03:16:14 +02:00
|
|
|
if BMConfigParser().get("network", "asyncore"):
|
2017-05-25 14:59:18 +02:00
|
|
|
return asyncore.receivedBytes
|
|
|
|
else:
|
|
|
|
return throttle.ReceiveThrottle().total
|
|
|
|
|
|
|
|
def downloadSpeed():
|
2017-05-29 00:24:07 +02:00
|
|
|
global lastReceivedTimestamp, lastReceivedBytes, currentReceivedSpeed
|
2017-05-29 03:16:14 +02:00
|
|
|
if BMConfigParser().get("network", "asyncore"):
|
2017-05-29 00:24:07 +02:00
|
|
|
currentTimestamp = time.time()
|
|
|
|
if int(lastReceivedTimestamp) < int(currentTimestamp):
|
|
|
|
currentReceivedBytes = asyncore.receivedBytes
|
|
|
|
currentReceivedSpeed = int((currentReceivedBytes - lastReceivedBytes) / (currentTimestamp - lastReceivedTimestamp))
|
|
|
|
lastReceivedBytes = currentReceivedBytes
|
|
|
|
lastReceivedTimestamp = currentTimestamp
|
|
|
|
return currentReceivedSpeed
|
2017-05-25 14:59:18 +02:00
|
|
|
else:
|
|
|
|
return throttle.ReceiveThrottle().getSpeed()
|
2017-05-29 00:24:07 +02:00
|
|
|
|
|
|
|
def pendingDownload():
|
2017-05-29 03:16:14 +02:00
|
|
|
if BMConfigParser().get("network", "asyncore"):
|
2017-05-29 00:24:07 +02:00
|
|
|
tmp = {}
|
|
|
|
for connection in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values():
|
|
|
|
for k in connection.objectsNewToMe.keys():
|
|
|
|
tmp[k] = True
|
|
|
|
return len(tmp)
|
|
|
|
else:
|
|
|
|
return PendingDownloadQueue.totalSize()
|
|
|
|
|
|
|
|
def pendingUpload():
|
2017-05-29 03:16:14 +02:00
|
|
|
if BMConfigParser().get("network", "asyncore"):
|
2017-05-29 00:24:07 +02:00
|
|
|
return 0
|
|
|
|
tmp = {}
|
|
|
|
for connection in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values():
|
|
|
|
for k in connection.objectsNewToThem.keys():
|
|
|
|
tmp[k] = True
|
|
|
|
return len(tmp)
|
|
|
|
else:
|
|
|
|
return PendingUpload().len()
|