diplonat/src/igd_actor.rs

34 lines
749 B
Rust
Raw Normal View History

2020-05-21 20:25:33 +00:00
use igd::aio::*;
use log::*;
use anyhow::{Result, Context};
2020-05-22 17:21:11 +00:00
use tokio::sync::watch;
use crate::messages;
2020-05-21 15:51:30 +00:00
2020-05-22 17:21:11 +00:00
pub struct IgdActor {
rx_ports: watch::Receiver<messages::PublicExposedPorts>,
2020-05-21 20:25:33 +00:00
gateway: Gateway,
}
2020-05-22 17:21:11 +00:00
impl IgdActor {
pub async fn new(rxp: &watch::Receiver<messages::PublicExposedPorts>) -> Result<Self> {
2020-05-21 20:25:33 +00:00
let gw = search_gateway(Default::default())
.await
.context("Failed to find gateway")?;
info!("Gateway: {}", gw);
let ctx = Self {
2020-05-22 17:21:11 +00:00
gateway: gw,
rx_ports: rxp.clone()
2020-05-21 20:25:33 +00:00
};
2020-05-22 17:21:11 +00:00
2020-05-21 20:25:33 +00:00
return Ok(ctx);
2020-05-21 15:51:30 +00:00
}
2020-05-21 20:25:33 +00:00
2020-05-22 17:21:11 +00:00
pub async fn listen(&mut self) -> Result<()> {
while let Some(ports) = self.rx_ports.recv().await {
println!("received = {:#?}", ports);
}
2020-05-21 15:51:30 +00:00
return Ok(());
}
}