Implement peer discovery on LAN using mDNS (via `zeroconf`). A Peer ID is generated (via `libP2P`) per instance, and announced on backend startup. Meanwhile, other peers are discovered on LAN. A signal is send on peer found and another on peer lost. There a bridge methods to find already discovered peers, and to get infos on serverless (our own ID, adverised service name, and number of peers found). `Zeroconf` runs in background in its own thread. rel 479
82 lines
2.5 KiB
Python
82 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
|
|
# Libervia plugin for Serverless XMPP
|
|
# Copyright (C) 2009-2026 Jérôme Poisson (goffi@goffi.org)
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from collections.abc import Iterator
|
|
from typing import TypedDict
|
|
|
|
from pydantic import BaseModel, Field, RootModel
|
|
|
|
|
|
class DiscoverExtra(BaseModel):
|
|
# XXX: only a placeholder for now
|
|
pass
|
|
|
|
|
|
class DiscoveredPeerInfo(TypedDict):
|
|
"""Info stored for a discovered peer.
|
|
|
|
@ivar addrs: list of libp2p multiaddr strings, e.g. "/ip4/192.168.1.10/tcp/5299".
|
|
See https://github.com/multiformats/multiaddr
|
|
"""
|
|
|
|
name: str
|
|
device_id: str
|
|
addrs: list[str]
|
|
metadata: dict[str, str]
|
|
|
|
|
|
class ServerlessInfo(BaseModel):
|
|
"""Serverless device information."""
|
|
|
|
device_id: str = Field(description="ID of the device")
|
|
service_name: str = Field(description="mDNS service name")
|
|
peer_count: int = Field(description="Number of discovered peers")
|
|
|
|
|
|
class Node(BaseModel):
|
|
"""Discovered peer node.
|
|
|
|
@param device_id: Identifier for the discovered device.
|
|
@param name: Human-readable name of the peer.
|
|
@param addrs: List of addresses to reach the peer.
|
|
@param metadata: metadata extracted from the discovery protocol.
|
|
"""
|
|
|
|
device_id: str = Field(description="Identifier for the discovered device")
|
|
name: str = Field(description="Human-readable name of the peer")
|
|
addrs: list[str] = Field(
|
|
description="List of addresses (multiaddr format), see https://github.com/multiformats/multiaddr"
|
|
)
|
|
metadata: dict[str, str] = Field(
|
|
default_factory=dict, description="Key-value metadata from the discovery protocol"
|
|
)
|
|
|
|
|
|
class DiscoverNodes(RootModel):
|
|
"""List of discovered nodes."""
|
|
|
|
root: list[Node]
|
|
|
|
def __iter__(self) -> Iterator[Node]: # type: ignore
|
|
return iter(self.root)
|
|
|
|
def __len__(self) -> int:
|
|
return len(self.root)
|
|
|
|
def __getitem__(self, index: int) -> Node:
|
|
return self.root[index]
|