Added service redistry,

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

View File

@ -1,4 +1,7 @@
use crate::{queue::{Message, MsgType}, utils::GenID}; use crate::{
queue::{Message, MsgType},
utils::GenID,
};
use std::{ use std::{
collections::HashMap, collections::HashMap,
sync::{ sync::{
@ -181,7 +184,7 @@ mod clientlinks {
let rx_client = link.send(req); let rx_client = link.send(req);
let msg = rx.recv_timeout(TIMEOUT).unwrap(); let msg = rx.recv_timeout(TIMEOUT).unwrap();
match msg.get_class() { match msg.get_class() {
MsgType::ClientRequest => {}, MsgType::ClientRequest => {}
_ => unreachable!("should have been a client request"), _ => unreachable!("should have been a client request"),
} }
match msg.get_data().get("tx_id") { match msg.get_data().get("tx_id") {
@ -189,7 +192,7 @@ mod clientlinks {
let id = result.to_uuid().unwrap(); let id = result.to_uuid().unwrap();
registry.send(&id, Reply {}); registry.send(&id, Reply {});
rx_client.recv().unwrap(); rx_client.recv().unwrap();
}, }
None => unreachable!("should have had a seender id"), None => unreachable!("should have had a seender id"),
} }
} }
@ -204,7 +207,7 @@ impl Client {
fn new(rx: Receiver<Message>) -> Self { fn new(rx: Receiver<Message>) -> Self {
Self { Self {
registry: ClientRegistry::new(), registry: ClientRegistry::new(),
rx: rx rx: rx,
} }
} }

View File

@ -147,7 +147,7 @@ mod fields {
let field: Field = text.into(); let field: Field = text.into();
match field.to_uuid() { match field.to_uuid() {
Ok(_) => unreachable!("should return an error"), Ok(_) => unreachable!("should return an error"),
Err(_) => {}, Err(_) => {}
} }
} }
} }

View File

@ -3,12 +3,13 @@ use std::{
collections::HashMap, collections::HashMap,
sync::{ sync::{
mpsc::{channel, Receiver, Sender}, mpsc::{channel, Receiver, Sender},
Arc, RwLock, Arc, Mutex, RwLock,
}, },
thread::spawn, thread::spawn,
}; };
use uuid::Uuid; use uuid::Uuid;
#[derive(Clone)]
pub enum MsgType { pub enum MsgType {
ClientMessage, ClientMessage,
ClientRequest, ClientRequest,
@ -16,6 +17,7 @@ pub enum MsgType {
NoOp, NoOp,
} }
#[derive(Clone)]
pub struct Message { pub struct Message {
id: Uuid, id: Uuid,
class: MsgType, 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 { struct Queue {
registry: Arc<RwLock<Vec<Sender<Message>>>>, registry: Arc<RwLock<Vec<Sender<Message>>>>,
rx: Receiver<Message>, rx: Receiver<Message>,