PeerNet.Discovery behaviour (PeerNet v0.1.0)

Copy Markdown View Source

Behaviour for discovering peers on a network.

Implementations announce this node's presence (own pubkey + listen address) and emit {:peer_discovered, ...} / {:peer_lost, ...} notifications to the PeerNet.Registry when other peers appear or disappear.

Implementations shipped

  • PeerNet.Discovery.Manual — does nothing automatically. Tests and apps that want full control over connection lifetime use this. Discovery events are pushed via Manual.announce_peer/3 and Manual.lose_peer/2.
  • PeerNet.Discovery.MDNS (planned, M3) — mdns_lite-backed announcement and discovery on the local network. Cross-platform interop with iOS Bonjour and Android NSD.

Why a Behaviour

Different deployment environments need different discovery strategies: Bluetooth on phones with no WiFi, mDNS on local networks, manual peer lists in CI / scripted setups. The Behaviour lets PeerNet stay agnostic and lets users compose what they need.

Notifications

Implementations send these messages to the Registry process:

{:peer_discovered, pubkey, %{ip: ip, port: port, source: :mdns}}
{:peer_lost, pubkey, %{source: :mdns}}

The Registry decides what to do. Default: if the pubkey is in the trust list, dial it. Otherwise log + drop.

Summary

Types

Address record handed to the Registry. Currently IP+port; future extensions could include transport hints (Bluetooth, WebRTC, etc.).

Where the discovery event came from. Used for logging.

Callbacks

Update the discovery's view of the local listen address. Called once the Acceptor has resolved an OS-assigned port.

Start the discovery process for one PeerNet instance.

Stop discovery cleanly.

Types

address()

@type address() :: %{
  ip: :inet.ip_address(),
  port: :inet.port_number(),
  source: source()
}

Address record handed to the Registry. Currently IP+port; future extensions could include transport hints (Bluetooth, WebRTC, etc.).

source()

@type source() :: atom()

Where the discovery event came from. Used for logging.

Callbacks

announce_self(server, port_number)

@callback announce_self(server :: GenServer.server(), :inet.port_number()) :: :ok

Update the discovery's view of the local listen address. Called once the Acceptor has resolved an OS-assigned port.

start_link(opts)

@callback start_link(opts :: keyword()) :: GenServer.on_start()

Start the discovery process for one PeerNet instance.

Receives the per-instance config (own identity, listen port, registry pid to notify). Returns a :gen_statem / GenServer.on_start tuple.

stop(server)

@callback stop(server :: GenServer.server()) :: :ok

Stop discovery cleanly.