Add hostname module
This commit is contained in:
parent
224739a7ec
commit
f0e71ab4e1
|
@ -27,8 +27,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
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(),
|
||||
)),
|
||||
|
|
58
src/tiles/hostname.rs
Normal file
58
src/tiles/hostname.rs
Normal 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
|
||||
})
|
||||
}
|
||||
}
|
|
@ -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(),
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue