Globals/GLOBALS

This commit is contained in:
Mike Dilger 2022-12-20 18:42:28 +13:00
parent 52a94eb32c
commit 6449757005
2 changed files with 39 additions and 0 deletions

35
src/globals.rs Normal file
View File

@ -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<BusMessage>,
/// This is a mpsc channel. The Overlord listens on it.
/// To create a sender, just clone() it.
pub to_overlord: mpsc::UnboundedSender<BusMessage>,
/// This is ephemeral. It is filled during lazy_static initialization,
/// and stolen away when the Overlord is created.
pub from_minions: Mutex<Option<mpsc::UnboundedReceiver<BusMessage>>>,
}
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)),
}
};
}

View File

@ -1,5 +1,9 @@
#[macro_use]
extern crate lazy_static;
mod comms; mod comms;
mod error; mod error;
mod globals;
fn main() { fn main() {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();