Refactored event bus to lazy_static.
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
use crate::event::Event;
|
||||
use crate::simplebus::Bus;
|
||||
use std::sync::Mutex;
|
||||
use lazy_static::lazy_static;
|
||||
use uuid::Uuid;
|
||||
|
||||
lazy_static! {
|
||||
static ref STATIC_BUS: Mutex<Bus<Event>> = Mutex::new(Bus::new());
|
||||
}
|
||||
|
||||
pub fn register<F>(closure: F) -> Uuid where F: FnMut(&Uuid, Event) -> bool + Send + Sync + 'static {
|
||||
STATIC_BUS.lock().unwrap().register(Box::new(closure))
|
||||
}
|
||||
|
||||
pub fn unregister(uuid: &Uuid) {
|
||||
STATIC_BUS.lock().unwrap().unregister(uuid);
|
||||
}
|
||||
|
||||
pub fn post(event: Event) {
|
||||
STATIC_BUS.lock().unwrap().post(event);
|
||||
}
|
||||
@@ -11,6 +11,8 @@ pub use constants::*;
|
||||
use crate::dns::protocol::DnsRecord;
|
||||
|
||||
pub mod constants;
|
||||
pub mod simplebus;
|
||||
pub mod eventbus;
|
||||
|
||||
/// Convert bytes array to HEX format
|
||||
pub fn to_hex(buf: &[u8]) -> String {
|
||||
@@ -161,6 +163,7 @@ pub fn setup_miner_thread(cpu: u32) {
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::net::IpAddr;
|
||||
|
||||
use crate::{check_domain, is_yggdrasil};
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
use uuid::Uuid;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct Bus<T> {
|
||||
listeners: HashMap<Uuid, Box<dyn FnMut(&Uuid, T) -> bool + Send + Sync>>
|
||||
}
|
||||
|
||||
impl<T: Clone> Bus<T> {
|
||||
pub fn new() -> Self {
|
||||
Bus { listeners: HashMap::new() }
|
||||
}
|
||||
|
||||
pub fn register<F>(&mut self, closure: F) -> Uuid where F: FnMut(&Uuid, T) -> bool + Send + Sync + 'static {
|
||||
let uuid = Uuid::new_v4();
|
||||
self.listeners.insert(uuid.clone(), Box::new(closure));
|
||||
uuid
|
||||
}
|
||||
|
||||
pub fn unregister(&mut self, uuid: &Uuid) {
|
||||
self.listeners.remove(&uuid);
|
||||
}
|
||||
|
||||
pub fn post(&mut self, event: T) {
|
||||
self.listeners.retain(|uuid, closure| {
|
||||
closure(uuid, event.clone())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::Bus;
|
||||
use crate::event::Event;
|
||||
|
||||
#[test]
|
||||
fn test1() {
|
||||
let string = Arc::new(Mutex::new(String::from("start")));
|
||||
let bus = Arc::new(Mutex::new(Bus::new()));
|
||||
let string_copy = string.clone();
|
||||
{
|
||||
bus.lock().unwrap().register(move |_uuid, e| {
|
||||
println!("Event {:?} received!", e);
|
||||
let mut copy = string_copy.lock().unwrap();
|
||||
copy.clear();
|
||||
copy.push_str("from thread");
|
||||
false
|
||||
});
|
||||
}
|
||||
let bus2 = bus.clone();
|
||||
thread::spawn(move || {
|
||||
bus2.lock().unwrap().post(Event::BlockchainChanged { index: 1 });
|
||||
});
|
||||
|
||||
let guard = string.lock().unwrap();
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
println!("string = {}", &guard);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user