aboutsummaryrefslogtreecommitdiff
path: root/src/diplonat.rs
blob: 01bd99411f52136111bdf55120cc85235e619b85 (plain) (blame)
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
use anyhow::{Result, Context};
use tokio::sync::broadcast;
use futures::future::try_join_all;
use log::*;
use std::cell::Cell;

use crate::environment_adapter::*;
use crate::igd_adapter::*;
use crate::node_state::*;

pub struct Diplonat<'a> {
  pub notif: broadcast::Sender<()>,
  pub state: Cell<NodeState>,

  env: EnvironmentAdapter,
  igd: IgdAdapter<'a>,
}

impl<'a> Diplonat<'a> {
  pub async fn new() -> Result<Diplonat<'a>> {
    let (tx, _) = broadcast::channel(1);
    let ns = Cell::new(NodeState::new());

    // we deliberately choose to init one after another
    let ctx = Diplonat {
      notif: tx,
      state: ns,

      env: EnvironmentAdapter::new(&ns, &tx).await?,
      igd: IgdAdapter::new(&ns, &tx).await?
    };

    info!("Consul URL: {:#?}", ns.consul_url);
    info!("Consul node name: {:#?}", ns.consul_node_name);
    info!("Private IP address: {:#?}", ns.private_ip);
    info!("Refresh time: {:#?} seconds", ns.refresh_time);
    info!("Expiration time: {:#?} seconds", ns.expiration_time);

    return Ok(ctx);
  }

  pub async fn listen(&self) -> Result<()> {
    return Ok(());
  }
}