2016-10-15 16:12:09 +02:00
|
|
|
import logging
|
2016-06-30 10:11:33 +02:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
|
|
|
import message
|
|
|
|
import shared
|
|
|
|
|
|
|
|
|
|
|
|
class Advertiser(threading.Thread):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(name='Advertiser')
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while True:
|
2016-07-19 09:23:52 +02:00
|
|
|
time.sleep(0.4)
|
2016-10-15 16:12:09 +02:00
|
|
|
if shared.shutting_down:
|
|
|
|
logging.debug('Shutting down Advertiser')
|
|
|
|
break
|
2016-06-30 10:11:33 +02:00
|
|
|
self._advertise_vectors()
|
|
|
|
self._advertise_addresses()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _advertise_vectors():
|
|
|
|
vectors_to_advertise = set()
|
|
|
|
while not shared.vector_advertise_queue.empty():
|
|
|
|
vectors_to_advertise.add(shared.vector_advertise_queue.get())
|
|
|
|
if len(vectors_to_advertise) > 0:
|
|
|
|
for c in shared.connections.copy():
|
2016-07-19 11:53:24 +02:00
|
|
|
if c.status == 'fully_established':
|
2016-06-30 10:11:33 +02:00
|
|
|
c.send_queue.put(message.Inv(vectors_to_advertise))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _advertise_addresses():
|
|
|
|
addresses_to_advertise = set()
|
|
|
|
while not shared.address_advertise_queue.empty():
|
2017-06-09 20:41:33 +02:00
|
|
|
addr = shared.address_advertise_queue.get()
|
|
|
|
if addr.port == 'i2p':
|
|
|
|
# We should not try to construct Addr messages with I2P destinations (yet)
|
|
|
|
continue
|
|
|
|
addresses_to_advertise.add(addr)
|
2016-06-30 10:11:33 +02:00
|
|
|
if len(addresses_to_advertise) > 0:
|
|
|
|
for c in shared.connections.copy():
|
2016-07-19 11:53:24 +02:00
|
|
|
if c.status == 'fully_established':
|
2016-06-30 10:11:33 +02:00
|
|
|
c.send_queue.put(message.Addr(addresses_to_advertise))
|