2019-11-27 06:47:04 +01:00
|
|
|
"""
|
|
|
|
Announce addresses as they are received from other hosts
|
|
|
|
"""
|
2017-07-05 08:57:44 +02:00
|
|
|
import Queue
|
|
|
|
|
2019-08-06 13:04:33 +02:00
|
|
|
import state
|
2019-11-27 06:47:04 +01:00
|
|
|
from helper_random import randomshuffle
|
|
|
|
from network.assemble import assemble_addr
|
2017-07-05 08:57:44 +02:00
|
|
|
from network.connectionpool import BMConnectionPool
|
|
|
|
from queues import addrQueue
|
2019-08-06 13:04:33 +02:00
|
|
|
from threads import StoppableThread
|
2017-07-05 08:57:44 +02:00
|
|
|
|
2019-08-01 13:37:26 +02:00
|
|
|
|
|
|
|
class AddrThread(StoppableThread):
|
|
|
|
name = "AddrBroadcaster"
|
2017-07-05 08:57:44 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while not state.shutdown:
|
|
|
|
chunk = []
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data = addrQueue.get(False)
|
2019-11-27 06:47:04 +01:00
|
|
|
chunk.append(data)
|
2017-07-05 08:57:44 +02:00
|
|
|
except Queue.Empty:
|
|
|
|
break
|
|
|
|
|
2019-11-27 06:47:04 +01:00
|
|
|
if chunk:
|
|
|
|
# Choose peers randomly
|
|
|
|
connections = BMConnectionPool().establishedConnections()
|
|
|
|
randomshuffle(connections)
|
|
|
|
for i in connections:
|
|
|
|
randomshuffle(chunk)
|
|
|
|
filtered = []
|
|
|
|
for stream, peer, seen, destination in chunk:
|
|
|
|
# peer's own address or address received from peer
|
|
|
|
if i.destination in (peer, destination):
|
|
|
|
continue
|
|
|
|
if stream not in i.streams:
|
|
|
|
continue
|
|
|
|
filtered.append((stream, peer, seen))
|
|
|
|
if filtered:
|
|
|
|
i.append_write_buf(assemble_addr(filtered))
|
2017-07-05 08:57:44 +02:00
|
|
|
|
|
|
|
addrQueue.iterate()
|
2017-10-19 08:56:48 +02:00
|
|
|
for i in range(len(chunk)):
|
|
|
|
addrQueue.task_done()
|
2017-07-05 08:57:44 +02:00
|
|
|
self.stop.wait(1)
|