MiNode/minode/advertiser.py

49 lines
1.6 KiB
Python
Raw Normal View History

2022-09-23 00:54:12 +02:00
"""
Advertiser thread advertises new addresses and objects among all connections
"""
2016-10-15 16:12:09 +02:00
import logging
2016-06-30 10:11:33 +02:00
import threading
import time
2021-03-09 15:40:59 +01:00
from . import message, shared
2016-06-30 10:11:33 +02:00
class Advertiser(threading.Thread):
2022-09-23 00:54:12 +02:00
"""The advertiser thread"""
2016-06-30 10:11:33 +02:00
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':
2021-03-08 16:06:07 +01:00
# We should not try to construct Addr messages
# with I2P destinations (yet)
2017-06-09 20:41:33 +02:00
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))