Scaffolding before implementing peer exchange logic
This commit is contained in:
parent
850468dc39
commit
df3568fc15
@ -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
|
@ -8,3 +8,6 @@ class GossipCommands(IntEnum):
|
|||||||
PUT_BLOCKS = auto()
|
PUT_BLOCKS = auto()
|
||||||
CLOSE = auto()
|
CLOSE = auto()
|
||||||
|
|
||||||
|
|
||||||
|
def command_to_byte(cmd: GossipCommands):
|
||||||
|
return int(cmd).to_bytes(1, 'big')
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
from typing import Protocol
|
from typing import TYPE_CHECKING, Protocol
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
import socket
|
||||||
|
|
||||||
|
|
||||||
class Peer(Protocol):
|
class Peer(Protocol):
|
||||||
@ -9,7 +11,7 @@ class Peer(Protocol):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
return
|
return
|
||||||
def get_socket(self):
|
def get_socket(self) -> 'socket.socket':
|
||||||
return
|
return
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user