From 03b06ce14f293d7ab69ff9e437e1bf5ee97d39f0 Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Sat, 30 May 2020 19:45:43 +0000 Subject: [PATCH] Basic tile communication working --- Cargo.lock | 380 ++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 +- src/main.rs | 26 +++- src/tile.rs | 46 +++--- src/tiles/time.rs | 57 +++++-- 5 files changed, 477 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a612a0..fab98e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,24 +6,154 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b585a98a234c46fc563103e9278c9391fde1f4e6850334da895d27edb9580f62" +[[package]] +name = "async-std" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a45cee2749d880d7066e328a7e161c7470ced883b2fd000ca4643e9f1dd5083a" +dependencies = [ + "async-task", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-timer", + "kv-log-macro", + "log", + "memchr", + "num_cpus", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "smol", + "wasm-bindgen-futures", +] + +[[package]] +name = "async-task" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17772156ef2829aadc587461c7753af20b7e8db1529bc66855add962a3b35d3" + +[[package]] +name = "autocfg" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" + [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "bumpalo" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5356f1d23ee24a1f785a56d1d1a5f0fd5b0f6a0c0fb2412ce11da71649ab78f6" + [[package]] name = "bytes" version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1" +[[package]] +name = "cc" +version = "1.0.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bbb73db36c1246e9034e307d0fba23f9a2e251faa47ade70c1bd252220c8311" + [[package]] name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "chrono" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2" +dependencies = [ + "num-integer", + "num-traits", + "time", +] + +[[package]] +name = "crossbeam" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69323bff1fb41c635347b8ead484a5ca6c3f11914d784170b158d8449ab07f8e" +dependencies = [ + "cfg-if", + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-queue", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-channel" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cced8691919c02aac3cb0a1bc2e9b73d89e832bf9a06fc579d4e71b68a2da061" +dependencies = [ + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-deque" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "lazy_static", + "maybe-uninit", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-queue" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab6bffe714b6bb07e42f201352c34f51fefd355ace793f9e638ebd52d23f98d2" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + [[package]] name = "dbus" version = "0.8.3" @@ -143,6 +273,16 @@ dependencies = [ "once_cell", ] +[[package]] +name = "futures-timer" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" +dependencies = [ + "gloo-timers", + "send_wrapper", +] + [[package]] name = "futures-util" version = "0.3.5" @@ -174,6 +314,19 @@ dependencies = [ "wasi", ] +[[package]] +name = "gloo-timers" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "hermit-abi" version = "0.1.13" @@ -198,6 +351,15 @@ version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e" +[[package]] +name = "js-sys" +version = "0.3.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce10c23ad2ea25ceca0093bd3192229da4c5b3c0f2de499c1ecac0d98d452177" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -208,6 +370,15 @@ dependencies = [ "winapi-build", ] +[[package]] +name = "kv-log-macro" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ff57d6d215f7ca7eb35a9a64d656ba4d9d2bef114d741dc08048e75e2f5d418" +dependencies = [ + "log", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -238,12 +409,27 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "maybe-uninit" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" + [[package]] name = "memchr" version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" +[[package]] +name = "memoffset" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4fc2c02a7e374099d4ee95a193111f72d2110197fe200272371758f6c3643d8" +dependencies = [ + "autocfg", +] + [[package]] name = "mio" version = "0.6.22" @@ -319,6 +505,38 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "nix" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363" +dependencies = [ + "bitflags", + "cc", + "cfg-if", + "libc", + "void", +] + +[[package]] +name = "num-integer" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.13.0" @@ -367,6 +585,18 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "piper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b0deb65f46e873ba8aa7c6a8dbe3f23cb1bf59c339a81a1d56361dde4d66ac8" +dependencies = [ + "crossbeam-utils", + "futures-io", + "futures-sink", + "futures-util", +] + [[package]] name = "pkg-config" version = "0.3.17" @@ -460,6 +690,8 @@ checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" name = "rustybar" version = "0.1.0" dependencies = [ + "async-std", + "chrono", "dbus", "dbus-tokio", "serde", @@ -474,6 +706,24 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed3d612bc64430efeb3f7ee6ef26d590dce0c43249217bddc62112540c7941e1" +[[package]] +name = "scoped-tls-hkt" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2e9d7eaddb227e8fbaaa71136ae0e1e913ca159b86c7da82f3e8f0044ad3a63" + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "send_wrapper" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f638d531eccd6e23b980caf34876660d38e265409d8e99b397ab71eb3612fad0" + [[package]] name = "serde" version = "1.0.110" @@ -521,6 +771,25 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" +[[package]] +name = "smol" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "686c634ad1873fffef6aed20f180eede424fbf3bb31802394c90fd7335a661b7" +dependencies = [ + "async-task", + "crossbeam", + "futures-io", + "futures-util", + "nix", + "once_cell", + "piper", + "scoped-tls-hkt", + "slab", + "socket2", + "wepoll-binding", +] + [[package]] name = "socket2" version = "0.3.12" @@ -544,6 +813,16 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi 0.3.8", +] + [[package]] name = "tokio" version = "0.2.21" @@ -594,12 +873,113 @@ dependencies = [ "rand", ] +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +[[package]] +name = "wasm-bindgen" +version = "0.2.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c2dc4aa152834bc334f506c1a06b866416a8b6697d5c9f75b9a689c8486def0" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ded84f06e0ed21499f6184df0e0cb3494727b0c5da89534e0fcc55c51d812101" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64487204d863f109eb77e8462189d111f27cb5712cc9fdb3461297a76963a2f6" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "838e423688dac18d73e31edce74ddfac468e37b1506ad163ffaf0a46f703ffe3" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3156052d8ec77142051a533cdd686cba889537b213f948cd1d20869926e68e92" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9ba19973a58daf4db6f352eda73dc0e289493cd29fb2632eb172085b6521acd" + +[[package]] +name = "web-sys" +version = "0.3.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b72fe77fd39e4bd3eaa4412fd299a0be6b3dfe9d2597e2f1c20beb968f41d17" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wepoll-binding" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "374fff4ff9701ff8b6ad0d14bacd3156c44063632d8c136186ff5967d48999a7" +dependencies = [ + "bitflags", + "wepoll-sys", +] + +[[package]] +name = "wepoll-sys" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9082a777aed991f6769e2b654aa0cb29f1c3d615daf009829b07b66c7aff6a24" +dependencies = [ + "cc", +] + [[package]] name = "winapi" version = "0.2.8" diff --git a/Cargo.toml b/Cargo.toml index dcf86c7..ceeb366 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,11 +6,11 @@ edition = "2018" [dependencies] +async-std = "1.6" +chrono = "0.4" +dbus = "0.8" +dbus-tokio = "0.5" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" - -uuid = { version = "0.8", features = [ "v4" ] } - tokio = { version = "0.2", features = ["full"] } -dbus-tokio = "0.5" -dbus = "0.8" +uuid = { version = "0.8", features = [ "v4" ] } diff --git a/src/main.rs b/src/main.rs index 94390f9..2fde63b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use std::sync::Arc; use tile::Tile; use tokio; use tokio::sync::mpsc::channel; +use uuid::Uuid; #[tokio::main] async fn main() -> Result<(), Box> { @@ -18,13 +19,30 @@ async fn main() -> Result<(), Box> { panic!("Lost connection to D-Bus: {}", err); }); - let (sender, receiver) = channel(1024); + let (mut sender, mut receiver) = channel(1024); - let tiles: Vec> = vec![Arc::new(tiles::Time::new(0, sender.clone()))]; + let instance = Uuid::new_v4().to_string().into_boxed_str(); + let tiles: Vec> = vec![Arc::new(tiles::Time::new(0, sender.clone(), instance))]; - for tile in tiles { - tile.spawn(); + for tile in &tiles { + tile.clone().spawn(); } + let num_tiles = tiles.len(); + tokio::spawn(async move { + let mut blocks = Vec::new(); + blocks.resize_with(num_tiles, Default::default); + loop { + let message = receiver.recv().await.unwrap(); + if message.sender_id < num_tiles { + blocks[message.sender_id] = Some(message.block); + } else { + eprintln!("Invalid message with sender id {}", message.sender_id); + continue; + } + eprintln!("Current state: {:?}", blocks); + } + }); + loop {} } diff --git a/src/tile.rs b/src/tile.rs index 50ea1ac..ad63592 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -2,7 +2,7 @@ use serde::Serialize; use std::sync::Arc; use tokio::task::JoinHandle; -#[derive(Copy, Clone, Serialize)] +#[derive(Copy, Clone, Debug, Serialize)] #[serde(rename_all = "lowercase")] pub enum Alignment { Left, @@ -16,7 +16,7 @@ impl Default for Alignment { } } -#[derive(Copy, Clone, Serialize)] +#[derive(Copy, Clone, Debug, Serialize)] #[serde(rename_all = "lowercase")] pub enum Markup { None, @@ -29,33 +29,33 @@ impl Default for Markup { } } -#[derive(Clone, Serialize, Default)] +#[derive(Clone, Serialize, Default, Debug)] pub struct Block { - full_text: Box, - short_text: Option>, - color: Option>, + pub full_text: Box, + pub short_text: Option>, + pub color: Option>, #[serde(rename = "background")] - background_color: Option>, + pub background_color: Option>, #[serde(rename = "border")] - border_color: Option>, - border_top: Option, - border_right: Option, - border_bottom: Option, - border_left: Option, - min_width: Option, - align: Option, - name: Box, - instance: Box, - urgent: Option, - separator: Option, - separator_block_width: Option, - markup: Option, + pub border_color: Option>, + pub border_top: Option, + pub border_right: Option, + pub border_bottom: Option, + pub border_left: Option, + pub min_width: Option, + pub align: Option, + pub name: Box, + pub instance: Box, + pub urgent: Option, + pub separator: Option, + pub separator_block_width: Option, + pub markup: Option, } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct TileData { - sender_id: usize, - block: Block, + pub sender_id: usize, + pub block: Block, } pub trait Tile: Send { diff --git a/src/tiles/time.rs b/src/tiles/time.rs index d298722..601ec83 100644 --- a/src/tiles/time.rs +++ b/src/tiles/time.rs @@ -1,34 +1,71 @@ use crate::tile::{Block, Tile, TileData}; +use async_std::sync::RwLock; +use chrono::prelude::*; +use chrono::DateTime; use std::sync::Arc; -use tokio::sync::mpsc::Sender; +use std::time::Duration; +use tokio::sync::mpsc::{error::SendError, Sender}; use tokio::task::JoinHandle; -use tokio::time; -use uuid::Uuid; +use tokio::time::delay_for; pub struct Time { sender_id: usize, - sender: Sender, + sender: RwLock>, instance: Box, + format: Box, + short_format: Box, } impl Time { - pub fn new(sender_id: usize, sender: Sender) -> Time { - let instance = Uuid::new_v4().to_string().into_boxed_str(); + pub fn new(sender_id: usize, sender: Sender, instance: Box) -> Time { Time { sender_id, - sender, + sender: RwLock::new(sender), instance, + format: "%Y-%m-%d %H:%M:%S".into(), + short_format: "%H:%M:%S".into(), } } - async fn run(&self) {} + async fn send(&self, data: TileData) -> Result<(), SendError> { + let mut sender = self.sender.write().await; + sender.send(data).await + } + + async fn send_time(&self, time: DateTime) -> Result<(), SendError> { + let block = Block { + full_text: time.format(&self.format).to_string().into(), + short_text: Some(time.format(&self.short_format).to_string().into()), + instance: self.instance.clone(), + name: "time".into(), + ..Default::default() + }; + let data = TileData { + sender_id: self.sender_id, + block, + }; + self.send(data).await + } + + async fn run(&self) { + let mut time = Local::now(); + loop { + self.send_time(time).await; + time = Local::now(); + let millis_part = time.naive_local().timestamp_subsec_millis() as u64; + let delay_ms = 1000u64 - millis_part % 1000; // Don't crash if we hit a leap second + delay_for(Duration::from_millis(delay_ms)).await; + } + } } impl Tile for Time { fn spawn(self: Arc) -> JoinHandle<()> { - tokio::spawn(async { + tokio::spawn(async move { let instance = self; - instance.run().await + loop { + instance.run().await + } }) } }