Add hostname module

This commit is contained in:
Artemis Tosini 2020-05-31 18:43:43 +00:00
parent 224739a7ec
commit f0e71ab4e1
Signed by: artemist
GPG key ID: EE5227935FE3FF18
4 changed files with 72 additions and 4 deletions

View file

@ -27,8 +27,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
sender.clone(), sender.clone(),
Uuid::new_v4().to_string().into_boxed_str(), Uuid::new_v4().to_string().into_boxed_str(),
)), )),
Arc::new(tiles::Time::new( Arc::new(tiles::Hostname::new(
1, 1,
sender.clone(),
Uuid::new_v4().to_string().into_boxed_str(),
)),
Arc::new(tiles::Time::new(
2,
sender, sender,
Uuid::new_v4().to_string().into_boxed_str(), Uuid::new_v4().to_string().into_boxed_str(),
)), )),

58
src/tiles/hostname.rs Normal file
View file

@ -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<Sender<TileData>>,
instance: Box<str>,
}
impl Hostname {
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Box<str>) -> Hostname {
Hostname {
sender_id,
sender: RwLock::new(sender),
instance,
}
}
async fn send(&self, data: TileData) -> Result<(), SendError<TileData>> {
let mut sender = self.sender.write().await;
sender.send(data).await
}
async fn run(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
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<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> {
tokio::spawn(async move {
let instance = self;
instance.run().await
})
}
}

View file

@ -1,9 +1,9 @@
use crate::tile::{Block, Tile, TileData}; use crate::tile::{Block, Tile, TileData};
use async_std::sync::RwLock; use async_std::sync::RwLock;
use std::fs::File;
use std::io::Read;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tokio::fs::File;
use tokio::prelude::*;
use tokio::sync::mpsc::{error::SendError, Sender}; use tokio::sync::mpsc::{error::SendError, Sender};
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tokio::time::interval; use tokio::time::interval;
@ -33,7 +33,10 @@ impl Load {
let mut raw = String::new(); let mut raw = String::new();
loop { loop {
timer.tick().await; 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 (load, _rest) = raw.split_at(raw.find(' ').unwrap_or(0));
let block = Block { let block = Block {
full_text: load.into(), full_text: load.into(),

View file

@ -1,4 +1,6 @@
pub mod hostname;
pub mod load; pub mod load;
pub mod time; pub mod time;
pub use hostname::Hostname;
pub use load::Load; pub use load::Load;
pub use time::Time; pub use time::Time;