macula_ping_pong (macula v0.14.3)

View Source

Macula Ping/Pong - Direct P2P Async RPC Demo

A peer-to-peer communication module that demonstrates bidirectional messaging across NAT boundaries using Macula's async RPC with direct P2P delivery (NATS-style request/reply).

Each node: - Registers local "ping.handler" RPC handler - Periodically sends PING RPCs to random peers (direct P2P) - Receives PONGs via callback - Measures Round-Trip Time (RTT) for each exchange - Tracks statistics per peer and per NAT type

Architecture: - Bootstrap: DHT only (no pub/sub routing) - Communication: Direct P2P via QUIC - Discovery: DHT for service registration

Flow: 1. fc01 registers local "ping.handler" (handles incoming PINGs) 2. fc01 calls macula_rpc_handler:request_to/5 to ping rc05 directly 3. Request goes directly to rc05 via QUIC (NAT-aware, no DHT lookup) 4. rc05's "ping.handler" executes, returns PONG 5. fc01 receives PONG via callback, measures RTT

Summary

Functions

Get statistics for a specific peer

Get all statistics

Send a PING to a specific peer

Reset all statistics

Start with default settings

Start with options Options: - interval: milliseconds between PINGs (default: 5000) - timeout: milliseconds to wait for PONG (default: 3000) - node_id: custom node identifier (default: NODE_ID env var)

Functions

get_peer_stats(PeerId)

-spec get_peer_stats(binary()) -> map() | not_found.

Get statistics for a specific peer

get_stats()

-spec get_stats() -> map().

Get all statistics

handle_call(Request, From, State)

handle_cast(Msg, State)

handle_info(Info, State)

init(Opts)

ping(PeerId)

-spec ping(binary()) -> {ok, integer()} | {error, term()}.

Send a PING to a specific peer

reset_stats()

-spec reset_stats() -> ok.

Reset all statistics

start_link()

-spec start_link() -> {ok, pid()} | {error, term()}.

Start with default settings

start_link(Opts)

-spec start_link(map()) -> {ok, pid()} | {error, term()}.

Start with options Options: - interval: milliseconds between PINGs (default: 5000) - timeout: milliseconds to wait for PONG (default: 3000) - node_id: custom node identifier (default: NODE_ID env var)

terminate(Reason, State)