use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use nym_bandwidth_controller::config::BandwidthControllerConfig;
use nym_bandwidth_controller::BandwidthTicketProvider;
use nym_network_defaults::NymNetworkDetails;
#[derive(Clone, Copy, Debug)]
pub struct RestockPolicy {
pub restock_below_tickets: u64,
pub readiness_min_tickets: u64,
pub check_interval: Duration,
pub soon_expiry: Duration,
}
impl Default for RestockPolicy {
fn default() -> Self {
Self {
restock_below_tickets: 20,
readiness_min_tickets: 5,
check_interval: Duration::from_secs(3 * 3600),
soon_expiry: Duration::from_secs(12 * 3600),
}
}
}
impl From<RestockPolicy> for BandwidthControllerConfig {
fn from(p: RestockPolicy) -> Self {
BandwidthControllerConfig {
topup_interval: p.check_interval,
soon_expiry_threshold: p.soon_expiry,
nb_ticket_restock: p.restock_below_tickets,
min_nb_ticket_needed: p.readiness_min_tickets,
..Default::default()
}
}
}
pub struct SessionConfig {
pub mnemonic: bip39::Mnemonic,
pub network: NymNetworkDetails,
pub credential_store_path: Option<PathBuf>,
pub data_path: PathBuf,
pub dvpn_directory_url: Option<String>,
pub automatic_topups: Option<RestockPolicy>,
pub bandwidth_provider: Option<Arc<dyn BandwidthTicketProvider>>,
pub reuse_registrations: bool,
}
impl SessionConfig {
pub fn new(mnemonic: bip39::Mnemonic, network: NymNetworkDetails, data_path: PathBuf) -> Self {
Self {
mnemonic,
network,
credential_store_path: None,
data_path,
dvpn_directory_url: None,
automatic_topups: None,
bandwidth_provider: None,
reuse_registrations: true,
}
}
#[must_use]
pub fn with_automatic_topups(mut self, policy: RestockPolicy) -> Self {
self.automatic_topups = Some(policy);
self
}
#[must_use]
pub fn with_bandwidth_provider(mut self, provider: Arc<dyn BandwidthTicketProvider>) -> Self {
self.bandwidth_provider = Some(provider);
self
}
#[must_use]
pub fn with_dvpn_directory_url(mut self, url: impl Into<String>) -> Self {
self.dvpn_directory_url = Some(url.into());
self
}
#[must_use]
pub fn with_credential_store_path(mut self, path: impl Into<PathBuf>) -> Self {
self.credential_store_path = Some(path.into());
self
}
}