chat_core/protocol/
hello.rs

1use crate::network::{HelloMessage, NetworkNode, PeerId};
2
3/// Interval for periodic hello messages that advertise our dialable addresses.
4pub const HELLO_INTERVAL_SECS: u64 = 10;
5
6/// Build a `HelloMessage` from the node's current dialable addresses.
7pub fn build_hello_message<N: NetworkNode>(node: &N, local_peer_id: &PeerId) -> Option<Vec<u8>> {
8    let addrs = node.dial_addrs();
9    let circuit_address = addrs.iter().find(|a| a.as_str().contains("p2p-circuit")).map(|a| a.as_str().to_string());
10    let web_rtc_address = addrs.iter().find(|a| a.as_str().contains("webrtc")).map(|a| a.as_str().to_string());
11
12    if circuit_address.is_none() && web_rtc_address.is_none() {
13        return None;
14    }
15
16    let hello = HelloMessage {
17        peer_id: local_peer_id.as_str().to_string(),
18        circuit_address,
19        web_rtc_address,
20    };
21    serde_json::to_vec(&hello).ok()
22}