Onionr/src/gossip/client/announce.py

51 lines
1.5 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
from ..commands import GossipCommands, command_to_byte
import onionrplugins
def do_announce(peer_set):
"Announce with N peers of each identified transport"
def _announce(announce_peer: 'Peer', our_transport_address: str):
2022-02-25 07:02:04 +00:00
try:
our_transport_address = our_transport_address.encode('utf-8')
except AttributeError:
pass
sock = announce_peer.get_socket()
2022-03-13 06:35:22 +00:00
sock.sendall(
command_to_byte(GossipCommands.ANNOUNCE) + 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()
2022-02-25 07:02:04 +00:00
while not len(peer_set):
sleep(1)
per_transport = 3
peer_types = {}
count_for_peer = 0
for peer in 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