Skip to main content

truffle_core/proxy/
discovery.rs

1//! Mesh discovery helpers for the reverse proxy subsystem.
2//!
3//! Uses [`SyncedStore`] to announce local proxies and discover remote proxies
4//! across the mesh. The store is created at the binding layer (NAPI/Tauri)
5//! because [`SyncedStore::new`] requires `Arc<Node>`.
6
7use crate::proxy::types::{ProxyAnnouncement, ProxyInfo, RemoteProxy};
8use crate::synced_store::SyncedStore;
9
10/// Build a list of announcements from locally active proxies.
11pub fn build_announcements(proxies: &[ProxyInfo]) -> Vec<ProxyAnnouncement> {
12    proxies
13        .iter()
14        .map(|p| ProxyAnnouncement {
15            id: p.id.clone(),
16            name: p.name.clone(),
17            listen_port: p.listen_port,
18            url: p.url.clone(),
19        })
20        .collect()
21}
22
23/// Extract remote proxies from a SyncedStore, excluding the local device's own entries.
24pub async fn extract_remote_proxies(
25    store: &SyncedStore<Vec<ProxyAnnouncement>>,
26    local_device_id: &str,
27) -> Vec<RemoteProxy> {
28    store
29        .all()
30        .await
31        .into_iter()
32        .filter(|(device_id, _)| device_id != local_device_id)
33        .flat_map(|(device_id, slice)| {
34            slice.data.into_iter().map(move |a| RemoteProxy {
35                peer_id: device_id.clone(),
36                peer_name: String::new(), // Resolved at the binding layer
37                id: a.id,
38                name: a.name,
39                url: a.url,
40                listen_port: a.listen_port,
41            })
42        })
43        .collect()
44}