distrans_peer/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
mod error;
mod fetcher;
mod peer;
mod proto;
mod seeder;
pub mod veilid_config;

use std::{path::PathBuf, sync::Arc};

use tokio::sync::broadcast::{self, Receiver, Sender};
use veilid_core::{RoutingContext, Sequencing, VeilidUpdate};

pub use error::{Error, Result, Unexpected};
pub use fetcher::Fetcher;
pub(crate) use peer::{reset_backoff, retry_backoff};
pub use peer::{reset_with_backoff, Observable, Peer, PeerState, Veilid};
pub use seeder::Seeder;

#[cfg(test)]
pub mod tests;

const VEILID_UPDATE_CAPACITY: usize = 1024;

pub async fn new_routing_context(
    state_dir: &str,
) -> Result<(RoutingContext, Sender<VeilidUpdate>, Receiver<VeilidUpdate>)> {
    let state_path_buf = PathBuf::from(state_dir);

    let (cb_update_tx, update_rx): (Sender<VeilidUpdate>, Receiver<VeilidUpdate>) =
        broadcast::channel(VEILID_UPDATE_CAPACITY);
    let update_tx = cb_update_tx.clone();

    // Configure Veilid core
    let update_callback = Arc::new(move |change: VeilidUpdate| {
        let _ = cb_update_tx.send(change);
    });
    let config_state_path = Arc::new(state_path_buf);
    let config_callback = Arc::new(move |key| {
        veilid_config::callback(config_state_path.to_str().unwrap().to_string(), key)
    });

    // Start Veilid API
    let api: veilid_core::VeilidAPI =
        veilid_core::api_startup(update_callback, config_callback).await?;

    let routing_context = api
        .routing_context()?
        .with_sequencing(Sequencing::EnsureOrdered)
        .with_default_safety()?;
    Ok((routing_context, update_tx, update_rx))
}