Added service redistry,
This commit is contained in:
parent
311a3293cd
commit
29f0062cac
@ -1,4 +1,7 @@
|
||||
use crate::{queue::{Message, MsgType}, utils::GenID};
|
||||
use crate::{
|
||||
queue::{Message, MsgType},
|
||||
utils::GenID,
|
||||
};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{
|
||||
@ -181,7 +184,7 @@ mod clientlinks {
|
||||
let rx_client = link.send(req);
|
||||
let msg = rx.recv_timeout(TIMEOUT).unwrap();
|
||||
match msg.get_class() {
|
||||
MsgType::ClientRequest => {},
|
||||
MsgType::ClientRequest => {}
|
||||
_ => unreachable!("should have been a client request"),
|
||||
}
|
||||
match msg.get_data().get("tx_id") {
|
||||
@ -189,7 +192,7 @@ mod clientlinks {
|
||||
let id = result.to_uuid().unwrap();
|
||||
registry.send(&id, Reply {});
|
||||
rx_client.recv().unwrap();
|
||||
},
|
||||
}
|
||||
None => unreachable!("should have had a seender id"),
|
||||
}
|
||||
}
|
||||
@ -204,7 +207,7 @@ impl Client {
|
||||
fn new(rx: Receiver<Message>) -> Self {
|
||||
Self {
|
||||
registry: ClientRegistry::new(),
|
||||
rx: rx
|
||||
rx: rx,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,7 +147,7 @@ mod fields {
|
||||
let field: Field = text.into();
|
||||
match field.to_uuid() {
|
||||
Ok(_) => unreachable!("should return an error"),
|
||||
Err(_) => {},
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
45
src/queue.rs
45
src/queue.rs
@ -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>,
|
||||
|
Loading…
x
Reference in New Issue
Block a user