2020-05-22 16:41:13 +00:00
|
|
|
use anyhow::Result;
|
2020-05-21 20:25:33 +00:00
|
|
|
use log::*;
|
2020-05-22 17:21:11 +00:00
|
|
|
use tokio::try_join;
|
2020-05-22 16:41:13 +00:00
|
|
|
use crate::consul_actor::ConsulActor;
|
2020-05-22 17:21:11 +00:00
|
|
|
use crate::igd_actor::IgdActor;
|
2020-05-22 16:41:13 +00:00
|
|
|
use crate::environment::Environment;
|
2020-05-21 20:25:33 +00:00
|
|
|
|
2020-05-22 16:41:13 +00:00
|
|
|
pub struct Diplonat {
|
2020-05-22 17:21:11 +00:00
|
|
|
consul: ConsulActor,
|
|
|
|
igd: IgdActor
|
2020-05-09 13:53:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 16:41:13 +00:00
|
|
|
impl Diplonat {
|
|
|
|
pub async fn new() -> Result<Self> {
|
|
|
|
let env = Environment::new()?;
|
2020-05-22 17:21:11 +00:00
|
|
|
let ca = ConsulActor::new(&env.consul_url, &env.consul_node_name);
|
2020-05-23 13:38:08 +00:00
|
|
|
let ia = IgdActor::new(
|
|
|
|
&env.private_ip,
|
|
|
|
env.refresh_time,
|
|
|
|
env.expiration_time,
|
|
|
|
&ca.rx_open_ports
|
|
|
|
).await?;
|
2020-05-21 20:25:33 +00:00
|
|
|
|
2020-05-22 16:41:13 +00:00
|
|
|
let ctx = Self {
|
2020-05-22 17:21:11 +00:00
|
|
|
consul: ca,
|
|
|
|
igd: ia
|
2020-05-09 14:50:38 +00:00
|
|
|
};
|
2020-05-09 14:27:54 +00:00
|
|
|
|
2020-05-09 14:50:38 +00:00
|
|
|
return Ok(ctx);
|
|
|
|
}
|
2020-05-09 13:53:22 +00:00
|
|
|
|
2020-05-22 16:41:13 +00:00
|
|
|
pub async fn listen(&mut self) -> Result<()> {
|
2020-05-22 17:21:11 +00:00
|
|
|
try_join!(
|
|
|
|
self.consul.listen(),
|
|
|
|
self.igd.listen()
|
|
|
|
)?;
|
2020-05-22 16:41:13 +00:00
|
|
|
|
2020-05-21 13:22:45 +00:00
|
|
|
return Ok(());
|
2020-05-09 14:50:38 +00:00
|
|
|
}
|
2020-05-09 13:53:22 +00:00
|
|
|
}
|