mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
added event emmiter
This commit is contained in:
@ -3,55 +3,65 @@ mDNS-based peer discovery for py-libp2p.
|
||||
Conforms to https://github.com/libp2p/specs/blob/master/discovery/mdns.md
|
||||
Uses zeroconf for mDNS broadcast/listen. Async operations use trio.
|
||||
"""
|
||||
import trio
|
||||
from typing import Callable, Optional
|
||||
from zeroconf import ServiceInfo, Zeroconf, ServiceBrowser, ServiceStateChange
|
||||
from .utils import (
|
||||
stringGen
|
||||
|
||||
from zeroconf import (
|
||||
Zeroconf,
|
||||
)
|
||||
|
||||
from libp2p.abc import (
|
||||
INetworkService
|
||||
INetworkService,
|
||||
)
|
||||
|
||||
from .broadcaster import (
|
||||
PeerBroadcaster,
|
||||
)
|
||||
from .listener import (
|
||||
PeerListener,
|
||||
)
|
||||
from .utils import (
|
||||
stringGen,
|
||||
)
|
||||
from .listener import PeerListener
|
||||
from .broadcaster import PeerBroadcaster
|
||||
from libp2p.peer.peerinfo import PeerInfo
|
||||
|
||||
SERVICE_TYPE = "_p2p._udp.local."
|
||||
MCAST_PORT = 5353
|
||||
MCAST_ADDR = "224.0.0.251"
|
||||
|
||||
|
||||
class MDNSDiscovery:
|
||||
"""
|
||||
mDNS-based peer discovery for py-libp2p, using zeroconf.
|
||||
Conforms to the libp2p mDNS discovery spec.
|
||||
"""
|
||||
def __init__(self, swarm: INetworkService, port: int = 8000, on_peer_discovery=None):
|
||||
|
||||
def __init__(self, swarm: INetworkService, port: int = 8000):
|
||||
self.peer_id = str(swarm.get_peer_id())
|
||||
self.port = port
|
||||
self.on_peer_discovery = on_peer_discovery
|
||||
self.zeroconf = Zeroconf()
|
||||
self.serviceName = f"{stringGen()}.{SERVICE_TYPE}"
|
||||
self.peerstore = swarm.peerstore
|
||||
self.swarm = swarm
|
||||
self.broadcaster = PeerBroadcaster(
|
||||
zeroconf=self.zeroconf,
|
||||
service_type=SERVICE_TYPE,
|
||||
service_name=self.serviceName,
|
||||
peer_id = self.peer_id,
|
||||
port = self.port
|
||||
peer_id=self.peer_id,
|
||||
port=self.port,
|
||||
)
|
||||
self.listener = PeerListener(
|
||||
zeroconf=self.zeroconf,
|
||||
peerstore=self.peerstore,
|
||||
service_type=SERVICE_TYPE,
|
||||
service_name=self.serviceName,
|
||||
on_peer_discovery=self.on_peer_discovery
|
||||
)
|
||||
|
||||
def start(self):
|
||||
def start(self) -> None:
|
||||
"""Register this peer and start listening for others."""
|
||||
print(f"Starting mDNS discovery for peer {self.peer_id} on port {self.port}")
|
||||
print("host is listening on", self.swarm.listeners)
|
||||
self.broadcaster.register()
|
||||
# Listener is started in constructor
|
||||
|
||||
def stop(self):
|
||||
def stop(self) -> None:
|
||||
"""Unregister this peer and clean up zeroconf resources."""
|
||||
self.broadcaster.unregister()
|
||||
self.zeroconf.close()
|
||||
self.zeroconf.close()
|
||||
|
||||
Reference in New Issue
Block a user