diff options
author | adrien <adrien@luxeylab.net> | 2021-09-10 18:41:39 +0200 |
---|---|---|
committer | adrien <adrien@luxeylab.net> | 2021-09-10 18:41:39 +0200 |
commit | 4d76c3d78ade04038593aeef867294c9eee2a4b8 (patch) | |
tree | 8fac4937beafb831b7354bb2afa37bb5bea99e00 /src/acme_actor.rs | |
parent | 195aec2cfe738f4025ea540d2591f876e1d209b9 (diff) | |
download | diplonat-4d76c3d78ade04038593aeef867294c9eee2a4b8.tar.gz diplonat-4d76c3d78ade04038593aeef867294c9eee2a4b8.zip |
wrote the skeleton of ACME. Involved solving the cosmetic warnings about CamelCase for enums (without changing the API).
Diffstat (limited to 'src/acme_actor.rs')
-rw-r--r-- | src/acme_actor.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/acme_actor.rs b/src/acme_actor.rs new file mode 100644 index 0000000..3b6aa50 --- /dev/null +++ b/src/acme_actor.rs @@ -0,0 +1,70 @@ +use anyhow::Result; +use log::*; +use tokio::{ + select, + sync::watch, + time::{ + Duration, + self, +}}; + +use crate::config::RuntimeConfigAcme; +use crate::messages; + +pub struct AcmeActor { + email: String, + last_ports: messages::PublicExposedPorts, + refresh: Duration, + + rx_ports: watch::Receiver<messages::PublicExposedPorts>, +} + +impl AcmeActor { + pub async fn new( + config: Option<RuntimeConfigAcme>, + rxp: &watch::Receiver<messages::PublicExposedPorts> + ) -> Result<Option<Self>> { + + if config.is_none() { + return Ok(None); + } + let config = config.unwrap(); + + let ctx = Self { + email: config.email, + last_ports: messages::PublicExposedPorts::new(), + refresh: config.refresh_time, + rx_ports: rxp.clone(), + }; + + Ok(Some(ctx)) + } + + pub async fn listen(&mut self) -> Result<()> { + let mut interval = time::interval(self.refresh); + loop { + // 1. Wait for an event + let new_ports = select! { + Some(ports) = self.rx_ports.recv() => Some(ports), + _ = interval.tick() => None, + else => return Ok(()) // Sender dropped, terminate loop. + }; + + // 2. Update last ports if needed + if let Some(p) = new_ports { self.last_ports = p; } + + // 3. Flush IGD requests + match self.do_acme().await { + Ok(()) => debug!("Successfully updated ACME"), + Err(e) => error!("An error occured while updating ACME. {}", e), + } + } + } + + pub async fn do_acme(&self) -> Result<()> { + debug!("Doing ACME!!!"); + debug!("{:#?}", self.last_ports); + + Ok(()) + } +}
\ No newline at end of file |