Onionr/src/gossip/client/announce.py

55 lines
1.6 KiB
Python
Raw Normal View History

2022-02-25 07:02:04 +00:00
from time import sleep
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .. import Peer
2022-02-25 07:02:04 +00:00
import logger
import onionrplugins
from ..commands import GossipCommands, command_to_byte
from ..peerset import gossip_peer_set
def do_announce():
"Announce with N peers of each identified transport"
2022-07-11 15:27:13 +00:00
per_transport = 4
peer_types = {}
count_for_peer = 0
def _announce(announce_peer: 'Peer', our_transport_address: str):
2022-07-11 15:27:13 +00:00
assert our_transport_address
2022-02-25 07:02:04 +00:00
try:
2022-04-20 05:28:29 +00:00
our_transport_address = our_transport_address.encode('utf-8') + b"\n"
2022-02-25 07:02:04 +00:00
except AttributeError:
2022-07-11 15:27:13 +00:00
our_transport_address = our_transport_address + b'\n'
sock = announce_peer.get_socket(12)
2022-06-26 19:43:16 +00:00
sock.sendall(command_to_byte(GossipCommands.ANNOUNCE))
sock.sendall(our_transport_address)
2022-02-25 07:02:04 +00:00
if int.from_bytes(sock.recv(1), 'big') != 1:
logger.warn(
f"Could not announce with {announce_peer.transport_address}")
sock.close()
while not len(gossip_peer_set):
2022-02-25 07:02:04 +00:00
sleep(1)
2022-07-11 15:27:13 +00:00
for peer in gossip_peer_set:
try:
2022-02-25 07:02:04 +00:00
count_for_peer = peer_types[peer.__class__]
except KeyError:
2022-02-25 07:02:04 +00:00
count_for_peer = peer_types[peer.__class__] = 0
if count_for_peer == per_transport:
continue
2022-02-25 07:02:04 +00:00
# Plugin for the transport associated with the peer will call _announce
# with the peer and *our* transport address
onionrplugins.events.event(
'get_our_transport',
2022-02-25 07:02:04 +00:00
data={'callback': _announce, 'peer': peer},
threaded=True)
2022-02-25 07:02:04 +00:00
peer_types[peer.__class__] += 1