WIP: I'm lost with Boxes and opaque types
Some checks failed
continuous-integration/drone/pr Build is failing

This commit is contained in:
adrien 2021-09-22 15:52:50 +02:00
parent 006f84e3b0
commit a464aabe25

View file

@ -1,4 +1,7 @@
use std::pin::Pin;
use anyhow::{anyhow, Result};
use futures::future::{try_join_all, Future};
use tokio::try_join;
use crate::{
@ -6,10 +9,7 @@ use crate::{
};
pub struct Diplonat {
consul: ConsulActor,
firewall: Option<FirewallActor>,
igd: Option<IgdActor>,
actors: Vec<Pin<Box<dyn Future<Output = Result<()>>>>>,
}
impl Diplonat {
@ -18,46 +18,26 @@ impl Diplonat {
println!("{:#?}", config);
let consul_actor = ConsulActor::new(config.consul);
let mut actors = vec![Box::pin(consul_actor.listen())];
let firewall_actor = FirewallActor::new(config.firewall, &consul_actor.rx_open_ports)?;
if let Some(actor) = FirewallActor::new(config.firewall, &consul_actor.rx_open_ports)? {
actors.push(Box::pin(actor.listen()));
}
if let Some(actor) = IgdActor::new(config.igd, &consul_actor.rx_open_ports).await? {
actors.push(Box::pin(actor.listen()));
}
let igd_actor = IgdActor::new(config.igd, &consul_actor.rx_open_ports).await?;
if firewall_actor.is_none() && igd_actor.is_none() {
if actors.len() == 1 {
return Err(anyhow!(
"At least enable *one* module, otherwise it's boring!"
))
}
let ctx = Self {
consul: consul_actor,
firewall: firewall_actor,
igd: igd_actor,
};
return Ok(ctx)
Ok(Self { actors })
}
pub async fn listen(&mut self) -> Result<()> {
let firewall = &mut self.firewall;
let igd = &mut self.igd;
try_join!(
self.consul.listen(),
async {
match firewall {
Some(x) => x.listen().await,
None => Ok(()),
}
},
async {
match igd {
Some(x) => x.listen().await,
None => Ok(()),
}
},
)?;
return Ok(())
pub async fn listen(&self) -> Result<()> {
try_join_all(self.actors);
Ok(())
}
}