From 6449757005637a8a733040f0a028dbfe6c958269 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Tue, 20 Dec 2022 18:42:28 +1300 Subject: [PATCH] Globals/GLOBALS --- src/globals.rs | 35 +++++++++++++++++++++++++++++++++++ src/main.rs | 4 ++++ 2 files changed, 39 insertions(+) create mode 100644 src/globals.rs diff --git a/src/globals.rs b/src/globals.rs new file mode 100644 index 00000000..884f9f21 --- /dev/null +++ b/src/globals.rs @@ -0,0 +1,35 @@ +use crate::comms::BusMessage; +use tokio::sync::{broadcast, mpsc, Mutex}; + +/// Only one of these is ever created, via lazy_static!, and represents +/// global state for the rust application +pub struct Globals { + /// This is a broadcast channel. All Minions should listen on it. + /// To create a receiver, just run .subscribe() on it. + pub to_minions: broadcast::Sender, + + /// This is a mpsc channel. The Overlord listens on it. + /// To create a sender, just clone() it. + pub to_overlord: mpsc::UnboundedSender, + + /// This is ephemeral. It is filled during lazy_static initialization, + /// and stolen away when the Overlord is created. + pub from_minions: Mutex>>, +} + +lazy_static! { + pub static ref GLOBALS: Globals = { + + // Setup a communications channel from the Overlord to the Minions. + let (to_minions, _) = broadcast::channel(16); + + // Setup a communications channel from the Minions to the Overlord. + let (to_overlord, from_minions) = mpsc::unbounded_channel(); + + Globals { + to_minions, + to_overlord, + from_minions: Mutex::new(Some(from_minions)), + } + }; +} diff --git a/src/main.rs b/src/main.rs index 46e8166a..7fb960e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,9 @@ +#[macro_use] +extern crate lazy_static; + mod comms; mod error; +mod globals; fn main() { tracing_subscriber::fmt::init();