Added service redistry,

This commit is contained in:
2025-04-03 08:24:08 -04:00
parent 311a3293cd
commit 29f0062cac
3 changed files with 52 additions and 6 deletions

View File

@ -3,12 +3,13 @@ use std::{
collections::HashMap,
sync::{
mpsc::{channel, Receiver, Sender},
Arc, RwLock,
Arc, Mutex, RwLock,
},
thread::spawn,
};
use uuid::Uuid;
#[derive(Clone)]
pub enum MsgType {
ClientMessage,
ClientRequest,
@ -16,6 +17,7 @@ pub enum MsgType {
NoOp,
}
#[derive(Clone)]
pub struct Message {
id: Uuid,
class: MsgType,
@ -114,6 +116,47 @@ mod messages {
}
}
struct ServiceRegistry {
store: Arc<Mutex<Vec<Sender<Message>>>>,
}
impl ServiceRegistry {
fn new() -> Self {
Self {
store: Arc::new(Mutex::new(Vec::new())),
}
}
fn add(&self, tx: Sender<Message>) {
let mut store = self.store.lock().unwrap();
store.push(tx);
}
fn send(&self, msg: Message) {
let mut store = self.store.lock().unwrap();
for sender in store.iter() {
sender.send(msg.clone()).unwrap();
}
}
}
#[cfg(test)]
mod serviceredistries {
use super::*;
#[test]
fn create_registry() {
let reg = ServiceRegistry::new();
let (tx1, rx1) = channel();
let (tx2, rx2) = channel();
reg.add(tx1);
reg.add(tx2);
reg.send(Message::new(MsgType::NoOp));
rx1.recv().unwrap();
rx2.recv().unwrap();
}
}
struct Queue {
registry: Arc<RwLock<Vec<Sender<Message>>>>,
rx: Receiver<Message>,