addrthread.py.bak
1 """ 2 Announce addresses as they are received from other hosts 3 """ 4 import random 5 from six.moves import queue 6 7 # magic imports! 8 import connectionpool 9 from protocol import assembleAddrMessage 10 from network import addrQueue # FIXME: init with queue 11 12 from threads import StoppableThread 13 14 15 class AddrThread(StoppableThread): 16 """(Node) address broadcasting thread""" 17 name = "AddrBroadcaster" 18 19 def run(self): 20 while not self._stopped: 21 chunk = [] 22 while True: 23 try: 24 data = addrQueue.get(False) 25 chunk.append(data) 26 except queue.Empty: 27 break 28 29 if chunk: 30 # Choose peers randomly 31 connections = connectionpool.pool.establishedConnections() 32 random.shuffle(connections) 33 for i in connections: 34 random.shuffle(chunk) 35 filtered = [] 36 for stream, peer, seen, destination in chunk: 37 # peer's own address or address received from peer 38 if i.destination in (peer, destination): 39 continue 40 if stream not in i.streams: 41 continue 42 filtered.append((stream, peer, seen)) 43 if filtered: 44 i.append_write_buf(assembleAddrMessage(filtered)) 45 46 addrQueue.iterate() 47 for i in range(len(chunk)): 48 addrQueue.task_done() 49 self.stop.wait(1)