2020-05-09 14:19:09 +00:00
|
|
|
use anyhow::{Result, Context};
|
2020-05-21 15:51:30 +00:00
|
|
|
use tokio::sync::broadcast;
|
|
|
|
use futures::future::try_join_all;
|
2020-05-21 20:25:33 +00:00
|
|
|
use log::*;
|
|
|
|
use std::cell::Cell;
|
|
|
|
|
|
|
|
use crate::environment_adapter::*;
|
|
|
|
use crate::igd_adapter::*;
|
|
|
|
use crate::node_state::*;
|
2020-05-09 13:53:22 +00:00
|
|
|
|
2020-05-21 15:51:30 +00:00
|
|
|
pub struct Diplonat<'a> {
|
|
|
|
pub notif: broadcast::Sender<()>,
|
2020-05-21 20:25:33 +00:00
|
|
|
pub state: Cell<NodeState>,
|
|
|
|
|
|
|
|
env: EnvironmentAdapter,
|
|
|
|
igd: IgdAdapter<'a>,
|
2020-05-09 13:53:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 15:51:30 +00:00
|
|
|
impl<'a> Diplonat<'a> {
|
|
|
|
pub async fn new() -> Result<Diplonat<'a>> {
|
|
|
|
let (tx, _) = broadcast::channel(1);
|
2020-05-21 20:25:33 +00:00
|
|
|
let ns = Cell::new(NodeState::new());
|
|
|
|
|
|
|
|
// we deliberately choose to init one after another
|
2020-05-21 15:51:30 +00:00
|
|
|
let ctx = Diplonat {
|
|
|
|
notif: tx,
|
2020-05-21 20:25:33 +00:00
|
|
|
state: ns,
|
|
|
|
|
|
|
|
env: EnvironmentAdapter::new(&ns, &tx).await?,
|
|
|
|
igd: IgdAdapter::new(&ns, &tx).await?
|
2020-05-09 14:50:38 +00:00
|
|
|
};
|
2020-05-09 14:27:54 +00:00
|
|
|
|
2020-05-21 20:25:33 +00:00
|
|
|
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);
|
|
|
|
|
2020-05-09 14:50:38 +00:00
|
|
|
return Ok(ctx);
|
|
|
|
}
|
2020-05-09 13:53:22 +00:00
|
|
|
|
2020-05-21 13:22:45 +00:00
|
|
|
pub async fn listen(&self) -> Result<()> {
|
|
|
|
return Ok(());
|
2020-05-09 14:50:38 +00:00
|
|
|
}
|
2020-05-09 13:53:22 +00:00
|
|
|
}
|