aboutsummaryrefslogtreecommitdiff
path: root/src/igd_actor.rs
blob: 4e2f4b6523fafc07241c0f58636f91df6e7a68d0 (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
use igd::aio::*;
use log::*;
use anyhow::{Result, Context};
use tokio::sync::watch;
use crate::messages;

pub struct IgdActor {
  rx_ports: watch::Receiver<messages::PublicExposedPorts>,
  gateway: Gateway,
}

impl IgdActor {
  pub async fn new(rxp: &watch::Receiver<messages::PublicExposedPorts>) -> Result<Self> {
    let gw = search_gateway(Default::default())
              .await
              .context("Failed to find gateway")?;
    info!("Gateway: {}", gw);

    let ctx = Self { 
      gateway: gw,
      rx_ports: rxp.clone()
    };

    return Ok(ctx);
  }

  pub async fn listen(&mut self) -> Result<()> {
    while let Some(ports) = self.rx_ports.recv().await {
      println!("received = {:#?}", ports);
    }
    return Ok(());
  }
}