diff --git a/src/globals.rs b/src/globals.rs index 6fd95aa2..2ad40649 100644 --- a/src/globals.rs +++ b/src/globals.rs @@ -28,7 +28,15 @@ pub struct Globals { /// This is ephemeral. It is filled during lazy_static initialization, /// and stolen away when the Overlord is created. - pub from_minions: Mutex>>, + pub tmp_overlord_receiver: Mutex>>, + + /// This is an mpsc channel. The Syncer listens on it. + /// To create a sender, just clone() it. + pub to_syncer: mpsc::UnboundedSender, + + /// This is ephemeral. It is filled during lazy_static initializtion, + /// and stolen away when the Syncer is created. + pub tmp_syncer_receiver: Mutex>>, /// All nostr events, keyed by the event Id pub events: RwLock>, @@ -80,13 +88,18 @@ lazy_static! { let (to_minions, _) = broadcast::channel(256); // Setup a communications channel from the Minions to the Overlord. - let (to_overlord, from_minions) = mpsc::unbounded_channel(); + let (to_overlord, tmp_overlord_receiver) = mpsc::unbounded_channel(); + + // Setup a communications channel from the UI (or anybody else) to the Syncer. + let (to_syncer, tmp_syncer_receiver) = mpsc::unbounded_channel(); Globals { db: Mutex::new(None), to_minions, to_overlord, - from_minions: Mutex::new(Some(from_minions)), + tmp_overlord_receiver: Mutex::new(Some(tmp_overlord_receiver)), + to_syncer, + tmp_syncer_receiver: Mutex::new(Some(tmp_syncer_receiver)), events: RwLock::new(HashMap::new()), incoming_events: RwLock::new(Vec::new()), relationships: RwLock::new(HashMap::new()), diff --git a/src/main.rs b/src/main.rs index 4796e046..143e0157 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ mod process; mod relationship; mod settings; mod signer; +mod syncer; mod ui; use crate::comms::BusMessage; @@ -67,15 +68,29 @@ fn main() -> Result<(), Error> { } async fn tokio_main() { - // Steal `from_minions` from the GLOBALS, and give it to a new Overlord - let from_minions = { - let mut mutex_option = GLOBALS.from_minions.lock().await; + // note: we don't get a spawn handle here, we don't signal this thread when we are exiting, + // we just let it die when tokio_main() exits. I think that is ok. + tokio::task::spawn(async move { + // Steal `tmp_syncer_receiver` from the GLOBALS, and give it to a new Syncer + let syncer_receiver = { + let mut mutex_option = GLOBALS.tmp_syncer_receiver.lock().await; + mem::replace(mutex_option.deref_mut(), None) + } + .unwrap(); + + let mut syncer = crate::syncer::Syncer::new(syncer_receiver); + syncer.run().await; + }); + + // Steal `tmp_overlord_receiver` from the GLOBALS, and give it to a new Overlord + let overlord_receiver = { + let mut mutex_option = GLOBALS.tmp_overlord_receiver.lock().await; mem::replace(mutex_option.deref_mut(), None) } .unwrap(); // Run the overlord - let mut overlord = crate::overlord::Overlord::new(from_minions); + let mut overlord = crate::overlord::Overlord::new(overlord_receiver); overlord.run().await; } diff --git a/src/overlord/mod.rs b/src/overlord/mod.rs index 9c583667..f4448e73 100644 --- a/src/overlord/mod.rs +++ b/src/overlord/mod.rs @@ -114,6 +114,9 @@ impl Overlord { .insert(Url::new(&relay.url), relay.clone()); } + // Test the syncer + let _ = GLOBALS.to_syncer.send("test".to_owned()); + // Load people from the database { let mut dbpeople = DbPerson::fetch(None).await?; diff --git a/src/syncer.rs b/src/syncer.rs new file mode 100644 index 00000000..d67fa9d1 --- /dev/null +++ b/src/syncer.rs @@ -0,0 +1,32 @@ +use tokio::sync::mpsc; + +pub struct Syncer { + incoming: mpsc::UnboundedReceiver, +} + +impl Syncer { + pub fn new(incoming: mpsc::UnboundedReceiver) -> Syncer { + Syncer { incoming } + } + + pub async fn run(&mut self) { + loop { + let message = self.incoming.recv().await; + + if message.is_none() { + return; + } + + let message = message.unwrap(); + + match &*message { + "test" => { + tracing::debug!("Syncer received test message."); + } + _ => { + tracing::debug!("Syncer received unknown message: {}", message); + } + } + } + } +}