Connecting elements

This commit is contained in:
Quentin 2020-05-22 19:21:11 +02:00
parent e1d0eadb9d
commit c8742b1f14
3 changed files with 27 additions and 18 deletions

View file

@ -1,28 +1,34 @@
use anyhow::Result; use anyhow::Result;
use futures::future::try_join_all;
use log::*; use log::*;
use tokio::try_join;
use crate::consul_actor::ConsulActor; use crate::consul_actor::ConsulActor;
use crate::igd_actor::IgdActor;
use crate::environment::Environment; use crate::environment::Environment;
pub struct Diplonat { pub struct Diplonat {
consul: ConsulActor consul: ConsulActor,
igd: IgdActor
} }
impl Diplonat { impl Diplonat {
pub async fn new() -> Result<Self> { pub async fn new() -> Result<Self> {
let env = Environment::new()?; let env = Environment::new()?;
let ca = ConsulActor::new(&env.consul_url, &env.consul_node_name);
let ia = IgdActor::new(&ca.rx_open_ports).await?;
let ctx = Self { let ctx = Self {
consul: ConsulActor::new(&env.consul_url, &env.consul_node_name) consul: ca,
igd: ia
}; };
return Ok(ctx); return Ok(ctx);
} }
pub async fn listen(&mut self) -> Result<()> { pub async fn listen(&mut self) -> Result<()> {
try_join_all(vec![ try_join!(
self.consul.listen() self.consul.listen(),
]).await?; self.igd.listen()
)?;
return Ok(()); return Ok(());
} }

View file

@ -1,31 +1,33 @@
use igd::aio::*; use igd::aio::*;
use log::*; use log::*;
use tokio::sync::broadcast;
use anyhow::{Result, Context}; use anyhow::{Result, Context};
use std::cell::Cell; use tokio::sync::watch;
use crate::messages;
use crate::diplonat::*; pub struct IgdActor {
use crate::node_state::*; rx_ports: watch::Receiver<messages::PublicExposedPorts>,
pub struct IgdAdapter<'a> {
state: &'a Cell<NodeState>,
gateway: Gateway, gateway: Gateway,
} }
impl<'a> IgdAdapter<'a> {
pub async fn new(ns: &'a Cell<NodeState>, send: &broadcast::Sender<()>) -> Result<IgdAdapter<'a>> { impl IgdActor {
pub async fn new(rxp: &watch::Receiver<messages::PublicExposedPorts>) -> Result<Self> {
let gw = search_gateway(Default::default()) let gw = search_gateway(Default::default())
.await .await
.context("Failed to find gateway")?; .context("Failed to find gateway")?;
info!("Gateway: {}", gw); info!("Gateway: {}", gw);
let ctx = Self { let ctx = Self {
state: ns, gateway: gw,
gateway: gw rx_ports: rxp.clone()
}; };
return Ok(ctx); return Ok(ctx);
} }
fn run(&self) -> Result<()> { pub async fn listen(&mut self) -> Result<()> {
while let Some(ports) = self.rx_ports.recv().await {
println!("received = {:#?}", ports);
}
return Ok(()); return Ok(());
} }
} }

View file

@ -2,6 +2,7 @@ mod messages;
mod environment; mod environment;
mod consul; mod consul;
mod consul_actor; mod consul_actor;
mod igd_actor;
mod diplonat; mod diplonat;
//use std::net::SocketAddrV4; //use std::net::SocketAddrV4;