rewrite hostname_stream to use dbus

This commit is contained in:
Skye Jensen 2020-06-19 16:06:24 -04:00
parent 9dfa11f0cf
commit 9ae994f637
3 changed files with 25 additions and 16 deletions

View file

@ -1,5 +1,6 @@
use crate::tile::Block; use crate::tile::Block;
use crate::tiles; use crate::tiles;
use dbus::nonblock::SyncConnection;
use futures::{stream::BoxStream, Stream}; use futures::{stream::BoxStream, Stream};
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use smart_default::SmartDefault; use smart_default::SmartDefault;
@ -7,6 +8,7 @@ use std::env::var;
use std::error::Error; use std::error::Error;
use std::io; use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc;
use structopt::StructOpt; use structopt::StructOpt;
use tokio::fs::File; use tokio::fs::File;
use tokio::prelude::*; use tokio::prelude::*;
@ -113,11 +115,12 @@ pub async fn read_config() -> Result<Config, Box<dyn std::error::Error>> {
pub fn process_tile( pub fn process_tile(
tile: &TileConfig, tile: &TileConfig,
connection: &Arc<SyncConnection>,
) -> BoxStream<'static, Result<Block, Box<dyn std::error::Error + Send + Sync>>> { ) -> BoxStream<'static, Result<Block, Box<dyn std::error::Error + Send + Sync>>> {
let five_secs = Duration::from_secs(5); let five_secs = Duration::from_secs(5);
match &tile.config_type { match &tile.config_type {
TileConfigType::Battery => wrap(tiles::battery_stream(), tile.update.or(Some(five_secs))), TileConfigType::Battery => wrap(tiles::battery_stream(), tile.update.or(Some(five_secs))),
TileConfigType::Hostname => wrap(tiles::hostname_stream(), tile.update), TileConfigType::Hostname => wrap(tiles::hostname_stream(connection.clone()), tile.update),
TileConfigType::Load => wrap(tiles::load_stream(), tile.update.or(Some(five_secs))), TileConfigType::Load => wrap(tiles::load_stream(), tile.update.or(Some(five_secs))),
TileConfigType::Memory => wrap(tiles::memory_stream(), tile.update.or(Some(five_secs))), TileConfigType::Memory => wrap(tiles::memory_stream(), tile.update.or(Some(five_secs))),
TileConfigType::Time(c) => wrap(tiles::time_stream(c.clone()), tile.update), TileConfigType::Time(c) => wrap(tiles::time_stream(c.clone()), tile.update),

View file

@ -4,7 +4,7 @@ mod output;
mod tile; mod tile;
mod tiles; mod tiles;
use dbus_tokio::connection::new_session_sync; use dbus_tokio::connection::new_system_sync;
use futures::channel::mpsc::{channel, Sender}; use futures::channel::mpsc::{channel, Sender};
use futures::{stream::BoxStream, StreamExt}; use futures::{stream::BoxStream, StreamExt};
use std::fmt::Debug; use std::fmt::Debug;
@ -16,7 +16,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = config::read_config().await?; let config = config::read_config().await?;
// We can't do much until we have a D-Bus connection so just do it synchronously // We can't do much until we have a D-Bus connection so just do it synchronously
let (resource, _conn) = new_session_sync()?; let (resource, conn) = new_system_sync()?;
// Now start listening on our D-Bus connection // Now start listening on our D-Bus connection
tokio::spawn(async { tokio::spawn(async {
@ -29,7 +29,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let tiles: Vec<_> = config let tiles: Vec<_> = config
.tile .tile
.iter() .iter()
.map(config::process_tile) .map(|tile| config::process_tile(tile, &conn))
.enumerate() .enumerate()
.map(|(index, stream)| spawn_stream(index, stream, sender.clone())) .map(|(index, stream)| spawn_stream(index, stream, sender.clone()))
.collect(); .collect();

View file

@ -1,22 +1,28 @@
use crate::tile::Block; use crate::tile::Block;
use futures::stream; use dbus::nonblock::stdintf::org_freedesktop_dbus::Properties;
use futures::Stream; use dbus::nonblock::{Proxy, SyncConnection};
use tokio::fs::File; use futures::{FutureExt, Stream};
use tokio::prelude::*; use std::sync::Arc;
use std::time::Duration;
pub fn hostname_stream( pub fn hostname_stream(
connection: Arc<SyncConnection>,
) -> impl Stream<Item = Result<Block, Box<dyn std::error::Error + Send + Sync>>> { ) -> impl Stream<Item = Result<Block, Box<dyn std::error::Error + Send + Sync>>> {
stream::once(async { let proxy = Proxy::new(
let mut raw = String::new(); "org.freedesktop.hostname1",
File::open("/proc/sys/kernel/hostname") "/org/freedesktop/hostname1",
.await? Duration::from_secs(5),
.read_to_string(&mut raw) connection,
.await?; );
let reply = proxy.get("org.freedesktop.hostname1", "Hostname");
async {
let hostname: String = reply.await?;
let block = Block { let block = Block {
full_text: raw.trim_end_matches('\n').into(), full_text: hostname.into(),
name: "hostname".into(), name: "hostname".into(),
..Default::default() ..Default::default()
}; };
Ok(block) Ok(block)
}) }
.into_stream()
} }