MiNode/minode/advertiser.py
2017-06-09 20:41:33 +02:00

45 lines
1.5 KiB
Python

import logging
import threading
import time
import message
import shared
class Advertiser(threading.Thread):
def __init__(self):
super().__init__(name='Advertiser')
def run(self):
while True:
time.sleep(0.4)
if shared.shutting_down:
logging.debug('Shutting down Advertiser')
break
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():
if c.status == 'fully_established':
c.send_queue.put(message.Inv(vectors_to_advertise))
@staticmethod
def _advertise_addresses():
addresses_to_advertise = set()
while not shared.address_advertise_queue.empty():
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)
if len(addresses_to_advertise) > 0:
for c in shared.connections.copy():
if c.status == 'fully_established':
c.send_queue.put(message.Addr(addresses_to_advertise))