mirror of
https://github.com/mikedilger/gossip.git
synced 2024-09-19 19:46:50 +00:00
Globals/GLOBALS
This commit is contained in:
parent
52a94eb32c
commit
6449757005
35
src/globals.rs
Normal file
35
src/globals.rs
Normal 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)),
|
||||
}
|
||||
};
|
||||
}
|
@ -1,5 +1,9 @@
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
mod comms;
|
||||
mod error;
|
||||
mod globals;
|
||||
|
||||
fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
Loading…
Reference in New Issue
Block a user