Scaffolding before implementing peer exchange logic

This commit is contained in:
Kevin F 2022-02-24 01:03:50 -06:00
parent 850468dc39
commit df3568fc15
3 changed files with 44 additions and 2 deletions

View File

@ -0,0 +1,37 @@
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .. import Peer
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):
sock = announce_peer.get_socket()
sock.send(
command_to_byte(GossipCommands.ANNOUNCE) + our_transport_address)
if sock.dup
per_transport = 3
peer_types = {}
count_for_peer = 0
for peer in peer_set:
try:
count_for_peer = peer_types[peer.__name__]
except KeyError:
peer_types[peer.__name__] = 0
continue
if count_for_peer == per_transport:
continue
onionrplugins.events.event(
'get_our_transport',
data={'callback': _announce, 'peer': peer})
peer_types[peer.__name__] += 1

View File

@ -8,3 +8,6 @@ class GossipCommands(IntEnum):
PUT_BLOCKS = auto()
CLOSE = auto()
def command_to_byte(cmd: GossipCommands):
return int(cmd).to_bytes(1, 'big')

View File

@ -1,4 +1,6 @@
from typing import Protocol
from typing import TYPE_CHECKING, Protocol
if TYPE_CHECKING:
import socket
class Peer(Protocol):
@ -9,7 +11,7 @@ class Peer(Protocol):
def __init__(self):
return
def get_socket(self):
def get_socket(self) -> 'socket.socket':
return
def disconnect(self):