From f0e71ab4e1fc1aba21e46885f491b4f169d5c82c Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Sun, 31 May 2020 18:43:43 +0000 Subject: [PATCH] Add hostname module --- src/main.rs | 7 +++++- src/tiles/hostname.rs | 58 +++++++++++++++++++++++++++++++++++++++++++ src/tiles/load.rs | 9 ++++--- src/tiles/mod.rs | 2 ++ 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 src/tiles/hostname.rs diff --git a/src/main.rs b/src/main.rs index ae29dc4..f565354 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,8 +27,13 @@ async fn main() -> Result<(), Box> { sender.clone(), Uuid::new_v4().to_string().into_boxed_str(), )), - Arc::new(tiles::Time::new( + Arc::new(tiles::Hostname::new( 1, + sender.clone(), + Uuid::new_v4().to_string().into_boxed_str(), + )), + Arc::new(tiles::Time::new( + 2, sender, Uuid::new_v4().to_string().into_boxed_str(), )), diff --git a/src/tiles/hostname.rs b/src/tiles/hostname.rs new file mode 100644 index 0000000..0f5e120 --- /dev/null +++ b/src/tiles/hostname.rs @@ -0,0 +1,58 @@ +use crate::tile::{Block, Tile, TileData}; +use async_std::sync::RwLock; +use std::sync::Arc; +use tokio::fs::File; +use tokio::prelude::*; +use tokio::sync::mpsc::{error::SendError, Sender}; +use tokio::task::JoinHandle; + +pub struct Hostname { + sender_id: usize, + sender: RwLock>, + instance: Box, +} + +impl Hostname { + pub fn new(sender_id: usize, sender: Sender, instance: Box) -> Hostname { + Hostname { + sender_id, + sender: RwLock::new(sender), + instance, + } + } + + async fn send(&self, data: TileData) -> Result<(), SendError> { + let mut sender = self.sender.write().await; + sender.send(data).await + } + + async fn run(&self) -> Result<(), Box> { + let mut raw = String::new(); + File::open("/proc/sys/kernel/hostname") + .await? + .read_to_string(&mut raw) + .await?; + let block = Block { + full_text: raw.trim_end_matches('\n').into(), + instance: self.instance.clone(), + name: "hostname".into(), + ..Default::default() + }; + let data = TileData { + block, + sender_id: self.sender_id, + }; + self.send(data).await?; + // What's the hostname gonna do? Change? + Ok(()) + } +} + +impl Tile for Hostname { + fn spawn(self: Arc) -> JoinHandle>> { + tokio::spawn(async move { + let instance = self; + instance.run().await + }) + } +} diff --git a/src/tiles/load.rs b/src/tiles/load.rs index b972b60..526d8f6 100644 --- a/src/tiles/load.rs +++ b/src/tiles/load.rs @@ -1,9 +1,9 @@ use crate::tile::{Block, Tile, TileData}; use async_std::sync::RwLock; -use std::fs::File; -use std::io::Read; use std::sync::Arc; use std::time::Duration; +use tokio::fs::File; +use tokio::prelude::*; use tokio::sync::mpsc::{error::SendError, Sender}; use tokio::task::JoinHandle; use tokio::time::interval; @@ -33,7 +33,10 @@ impl Load { let mut raw = String::new(); loop { timer.tick().await; - File::open("/proc/loadavg")?.read_to_string(&mut raw)?; + File::open("/proc/loadavg") + .await? + .read_to_string(&mut raw) + .await?; let (load, _rest) = raw.split_at(raw.find(' ').unwrap_or(0)); let block = Block { full_text: load.into(), diff --git a/src/tiles/mod.rs b/src/tiles/mod.rs index acafe19..0862d20 100644 --- a/src/tiles/mod.rs +++ b/src/tiles/mod.rs @@ -1,4 +1,6 @@ +pub mod hostname; pub mod load; pub mod time; +pub use hostname::Hostname; pub use load::Load; pub use time::Time;