Peter Surda
0cc8589b27
- should prevent the same object being re-requested indefinitely - locking for object tracking - move SSL-specific error handling to TLSDispatcher - observe maximum connection limit when accepting a new connection - stack depth test (for debugging purposes) - separate download thread - connection pool init moved to main thread
49 lines
2.0 KiB
Python
49 lines
2.0 KiB
Python
import Queue
|
|
import threading
|
|
|
|
import addresses
|
|
#from bmconfigparser import BMConfigParser
|
|
from debug import logger
|
|
from helper_threading import StoppableThread
|
|
#from inventory import Inventory
|
|
from network.connectionpool import BMConnectionPool
|
|
import protocol
|
|
|
|
class DownloadThread(threading.Thread, StoppableThread):
|
|
maxPending = 500
|
|
requestChunk = 1000
|
|
|
|
def __init__(self):
|
|
threading.Thread.__init__(self, name="DownloadThread")
|
|
self.initStop()
|
|
self.name = "DownloadThread"
|
|
logger.info("init download thread")
|
|
|
|
def run(self):
|
|
while not self._stopped:
|
|
requested = 0
|
|
for i in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values():
|
|
# this may take a while, but it needs a consistency so I think it's better
|
|
with i.objectsNewToMeLock:
|
|
downloadPending = len(list((k for k, v in i.objectsNewToMe.iteritems() if not v)))
|
|
if downloadPending >= DownloadThread.maxPending:
|
|
continue
|
|
# keys with True values in the dict
|
|
request = list((k for k, v in i.objectsNewToMe.iteritems() if v))
|
|
if len(request) == 0:
|
|
continue
|
|
if len(request) > DownloadThread.requestChunk - downloadPending:
|
|
request = request[:DownloadThread.requestChunk - downloadPending]
|
|
# mark them as pending
|
|
for k in request:
|
|
i.objectsNewToMe[k] = False
|
|
|
|
payload = addresses.encodeVarint(len(request)) + ''.join(request)
|
|
i.writeQueue.put(protocol.CreatePacket('getdata', payload))
|
|
logger.debug("%s:%i Requesting %i objects", i.destination.host, i.destination.port, len(request))
|
|
requested += len(request)
|
|
self.stop.wait(1)
|
|
|
|
def stopThread(self):
|
|
super(DownloadThread, self).stopThread()
|