2017-05-29 00:24:07 +02:00
|
|
|
import Queue
|
|
|
|
import threading
|
|
|
|
|
|
|
|
import addresses
|
|
|
|
from helper_threading import StoppableThread
|
|
|
|
from network.connectionpool import BMConnectionPool
|
|
|
|
from queues import invQueue
|
|
|
|
import protocol
|
|
|
|
import state
|
|
|
|
|
|
|
|
class InvThread(threading.Thread, StoppableThread):
|
|
|
|
def __init__(self):
|
|
|
|
threading.Thread.__init__(self, name="InvThread")
|
|
|
|
self.initStop()
|
|
|
|
self.name = "InvThread"
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while not state.shutdown:
|
2017-06-27 13:25:12 +02:00
|
|
|
chunk = []
|
2017-05-29 00:24:07 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2017-05-29 14:52:31 +02:00
|
|
|
data = invQueue.get(False)
|
|
|
|
if len(data) == 2:
|
2017-06-24 12:13:35 +02:00
|
|
|
BMConnectionPool().handleReceivedObject(data[0], data[1])
|
2017-05-29 14:52:31 +02:00
|
|
|
else:
|
2017-06-24 12:13:35 +02:00
|
|
|
BMConnectionPool().handleReceivedObject(data[0], data[1], data[2])
|
2017-06-27 13:25:12 +02:00
|
|
|
chunk.append((data[0], data[1]))
|
2017-05-29 00:24:07 +02:00
|
|
|
except Queue.Empty:
|
|
|
|
break
|
|
|
|
|
2017-06-27 13:25:12 +02:00
|
|
|
if chunk:
|
|
|
|
for connection in BMConnectionPool().inboundConnections.values() + \
|
|
|
|
BMConnectionPool().outboundConnections.values():
|
2017-05-29 00:24:07 +02:00
|
|
|
hashes = []
|
2017-06-27 13:25:12 +02:00
|
|
|
for inv in chunk:
|
|
|
|
if inv[0] not in connection.streams:
|
|
|
|
continue
|
2017-05-29 00:24:07 +02:00
|
|
|
try:
|
2017-06-27 13:25:12 +02:00
|
|
|
with connection.objectsNewToThemLock:
|
|
|
|
del connection.objectsNewToThem[inv[1]]
|
|
|
|
hashes.append(inv[1])
|
2017-05-29 00:24:07 +02:00
|
|
|
except KeyError:
|
|
|
|
continue
|
2017-06-24 12:13:35 +02:00
|
|
|
if hashes:
|
2017-07-06 19:45:36 +02:00
|
|
|
connection.append_write_buf(protocol.CreatePacket('inv', \
|
2017-06-27 13:25:12 +02:00
|
|
|
addresses.encodeVarint(len(hashes)) + "".join(hashes)))
|
|
|
|
invQueue.iterate()
|
2017-05-29 00:24:07 +02:00
|
|
|
self.stop.wait(1)
|