2025-04-21 21:44:52 -04:00
|
|
|
use crate::field::Field;
|
2025-03-29 09:22:53 -04:00
|
|
|
use std::{
|
|
|
|
|
collections::HashMap,
|
2025-04-03 22:28:47 -04:00
|
|
|
sync::{mpsc::Sender, Arc, RwLock},
|
2025-03-29 09:22:53 -04:00
|
|
|
};
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
2025-04-04 13:26:28 -04:00
|
|
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
2025-04-02 22:10:16 -04:00
|
|
|
pub enum MsgType {
|
2025-04-13 08:26:18 -04:00
|
|
|
Document,
|
2025-04-11 22:06:36 -04:00
|
|
|
DocumentRequest,
|
2025-04-13 08:26:18 -04:00
|
|
|
SessionValidate,
|
2025-04-15 18:37:02 -04:00
|
|
|
SessionValidated,
|
2025-04-10 13:42:43 -04:00
|
|
|
Time,
|
2025-03-29 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-03 08:24:08 -04:00
|
|
|
#[derive(Clone)]
|
2025-03-29 09:22:53 -04:00
|
|
|
pub struct Message {
|
|
|
|
|
id: Uuid,
|
2025-04-15 09:53:38 -04:00
|
|
|
msg_type: MsgType,
|
2025-03-29 09:22:53 -04:00
|
|
|
data: HashMap<String, Field>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Message {
|
2025-04-02 22:10:16 -04:00
|
|
|
pub fn new(msg_type: MsgType) -> Self {
|
2025-03-29 09:22:53 -04:00
|
|
|
Self {
|
2025-04-03 21:48:16 -04:00
|
|
|
id: Uuid::new_v4(),
|
2025-04-15 09:53:38 -04:00
|
|
|
msg_type: msg_type,
|
2025-03-29 09:22:53 -04:00
|
|
|
data: HashMap::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-05 09:50:54 -04:00
|
|
|
pub fn reply(&self, data: MsgType) -> Message {
|
2025-03-29 09:22:53 -04:00
|
|
|
Self {
|
|
|
|
|
id: self.id.clone(),
|
2025-04-15 09:53:38 -04:00
|
|
|
msg_type: data,
|
2025-04-03 21:48:16 -04:00
|
|
|
data: HashMap::new(),
|
2025-03-29 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 09:53:38 -04:00
|
|
|
pub fn reply_with_data(&self, msg_type: MsgType) -> Message {
|
|
|
|
|
Self {
|
|
|
|
|
id: self.id.clone(),
|
|
|
|
|
msg_type: msg_type,
|
|
|
|
|
data: self.data.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_msg_type(&self) -> &MsgType {
|
|
|
|
|
&self.msg_type
|
2025-03-29 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-02 22:10:16 -04:00
|
|
|
pub fn add_data<S, F>(&mut self, name: S, data: F)
|
2025-03-30 11:38:41 -04:00
|
|
|
where
|
|
|
|
|
S: Into<String>,
|
|
|
|
|
F: Into<Field>,
|
|
|
|
|
{
|
2025-03-29 09:22:53 -04:00
|
|
|
self.data.insert(name.into(), data.into());
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 18:37:02 -04:00
|
|
|
pub fn get_data<S>(&self, name: S) -> Option<&Field>
|
|
|
|
|
where
|
|
|
|
|
S: Into<String>,
|
|
|
|
|
{
|
2025-04-15 09:53:38 -04:00
|
|
|
let field_name = name.into();
|
|
|
|
|
self.data.get(&field_name)
|
2025-03-29 09:22:53 -04:00
|
|
|
}
|
2025-04-05 09:50:54 -04:00
|
|
|
|
|
|
|
|
pub fn get_id(&self) -> Uuid {
|
|
|
|
|
self.id.clone()
|
|
|
|
|
}
|
2025-03-29 09:22:53 -04:00
|
|
|
|
2025-04-21 21:44:52 -04:00
|
|
|
pub fn reset_id(&mut self, id: Uuid) {
|
|
|
|
|
self.id = id;
|
2025-04-02 14:26:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-29 09:22:53 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod messages {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn new_message() {
|
2025-04-07 00:41:28 -04:00
|
|
|
let msg = Message::new(MsgType::SessionValidate);
|
2025-04-15 09:53:38 -04:00
|
|
|
match msg.msg_type {
|
2025-04-07 00:41:28 -04:00
|
|
|
MsgType::SessionValidate => (),
|
2025-03-29 09:22:53 -04:00
|
|
|
_ => unreachable!("new defaults to noop"),
|
|
|
|
|
}
|
|
|
|
|
assert!(msg.data.is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-03 21:48:16 -04:00
|
|
|
#[test]
|
|
|
|
|
fn message_ids_are_random() {
|
|
|
|
|
let mut ids: Vec<Uuid> = Vec::new();
|
|
|
|
|
for _ in 0..10 {
|
2025-04-07 00:41:28 -04:00
|
|
|
let msg = Message::new(MsgType::SessionValidate);
|
2025-04-03 21:48:16 -04:00
|
|
|
let id = msg.id.clone();
|
|
|
|
|
assert!(!ids.contains(&id), "{} is a duplicate", id);
|
|
|
|
|
ids.push(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-29 09:22:53 -04:00
|
|
|
#[test]
|
|
|
|
|
fn create_reply() {
|
|
|
|
|
let id = Uuid::new_v4();
|
2025-04-07 00:41:28 -04:00
|
|
|
let mut msg = Message::new(MsgType::SessionValidate);
|
2025-03-29 09:22:53 -04:00
|
|
|
msg.id = id.clone();
|
2025-04-03 21:48:16 -04:00
|
|
|
msg.add_data("test", "test");
|
2025-04-21 22:29:15 -04:00
|
|
|
let data = MsgType::SessionValidate;
|
2025-03-29 09:22:53 -04:00
|
|
|
let result = msg.reply(data);
|
|
|
|
|
assert_eq!(result.id, id);
|
2025-04-15 09:53:38 -04:00
|
|
|
match result.msg_type {
|
2025-04-21 22:29:15 -04:00
|
|
|
MsgType::SessionValidate => {}
|
2025-03-29 09:22:53 -04:00
|
|
|
_ => unreachable!("should have been a registration request"),
|
|
|
|
|
}
|
2025-04-03 21:48:16 -04:00
|
|
|
assert!(result.data.is_empty());
|
2025-03-29 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn get_message_type() {
|
2025-04-07 00:41:28 -04:00
|
|
|
let msg = Message::new(MsgType::SessionValidate);
|
2025-04-15 09:53:38 -04:00
|
|
|
match msg.get_msg_type() {
|
2025-04-07 00:41:28 -04:00
|
|
|
MsgType::SessionValidate => {}
|
2025-03-29 09:22:53 -04:00
|
|
|
_ => unreachable!("should have bneen noopn"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn add_data() {
|
2025-04-07 00:41:28 -04:00
|
|
|
let mut msg = Message::new(MsgType::SessionValidate);
|
2025-03-29 09:22:53 -04:00
|
|
|
let one = "one";
|
|
|
|
|
let two = "two".to_string();
|
|
|
|
|
msg.add_data(one, one);
|
|
|
|
|
msg.add_data(two.clone(), two.clone());
|
2025-04-07 00:41:28 -04:00
|
|
|
assert_eq!(msg.get_data(one).unwrap().to_string(), one);
|
|
|
|
|
assert_eq!(msg.get_data(&two).unwrap().to_string(), two);
|
2025-03-29 09:22:53 -04:00
|
|
|
}
|
2025-04-05 09:50:54 -04:00
|
|
|
|
2025-04-15 09:53:38 -04:00
|
|
|
#[test]
|
|
|
|
|
fn get_data_into_string() {
|
|
|
|
|
let id = Uuid::new_v4();
|
|
|
|
|
let mut msg = Message::new(MsgType::SessionValidate);
|
|
|
|
|
msg.add_data(id, id);
|
|
|
|
|
assert_eq!(msg.get_data(id).unwrap().to_uuid().unwrap(), id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn copy_data_with_reply() {
|
|
|
|
|
let id = Uuid::new_v4();
|
2025-04-15 18:37:02 -04:00
|
|
|
let reply_type = MsgType::SessionValidated;
|
2025-04-15 09:53:38 -04:00
|
|
|
let mut msg = Message::new(MsgType::SessionValidate);
|
|
|
|
|
msg.add_data(id, id);
|
|
|
|
|
let reply = msg.reply_with_data(reply_type.clone());
|
|
|
|
|
assert_eq!(reply.id, msg.id);
|
|
|
|
|
match reply.get_msg_type() {
|
2025-04-15 18:37:02 -04:00
|
|
|
MsgType::SessionValidated => {}
|
|
|
|
|
_ => unreachable!(
|
|
|
|
|
"Got {:?} should have been {:?}",
|
|
|
|
|
msg.get_msg_type(),
|
|
|
|
|
reply_type
|
|
|
|
|
),
|
2025-04-15 09:53:38 -04:00
|
|
|
}
|
|
|
|
|
assert_eq!(reply.data.len(), msg.data.len());
|
|
|
|
|
let output = reply.get_data(&id.to_string()).unwrap().to_uuid().unwrap();
|
|
|
|
|
let expected = msg.get_data(&id.to_string()).unwrap().to_uuid().unwrap();
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-05 09:50:54 -04:00
|
|
|
#[test]
|
|
|
|
|
fn get_message_id() {
|
2025-04-15 18:37:02 -04:00
|
|
|
let msg = Message::new(MsgType::SessionValidated);
|
2025-04-05 09:50:54 -04:00
|
|
|
assert_eq!(msg.get_id(), msg.id);
|
|
|
|
|
}
|
2025-04-08 09:30:04 -04:00
|
|
|
|
|
|
|
|
#[test]
|
2025-04-21 21:44:52 -04:00
|
|
|
fn reset_msg_id() {
|
|
|
|
|
let mut msg = Message::new(MsgType::Time);
|
|
|
|
|
msg.reset_id(Uuid::nil());
|
|
|
|
|
assert_eq!(msg.get_id(), Uuid::nil());
|
2025-04-08 09:30:04 -04:00
|
|
|
}
|
2025-03-29 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-03 21:48:16 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct Queue {
|
2025-04-04 13:26:28 -04:00
|
|
|
store: Arc<RwLock<HashMap<MsgType, Vec<Sender<Message>>>>>,
|
2025-04-03 08:24:08 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-03 21:48:16 -04:00
|
|
|
impl Queue {
|
|
|
|
|
pub fn new() -> Self {
|
2025-04-03 08:24:08 -04:00
|
|
|
Self {
|
2025-04-04 13:26:28 -04:00
|
|
|
store: Arc::new(RwLock::new(HashMap::new())),
|
2025-04-03 08:24:08 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 13:26:28 -04:00
|
|
|
pub fn add(&self, tx: Sender<Message>, msg_types: Vec<MsgType>) {
|
2025-04-03 21:48:16 -04:00
|
|
|
let mut store = self.store.write().unwrap();
|
2025-04-04 13:26:28 -04:00
|
|
|
for msg_type in msg_types.into_iter() {
|
|
|
|
|
if !store.contains_key(&msg_type) {
|
|
|
|
|
store.insert(msg_type.clone(), Vec::new());
|
|
|
|
|
}
|
|
|
|
|
let senders = store.get_mut(&msg_type).unwrap();
|
|
|
|
|
senders.push(tx.clone());
|
|
|
|
|
}
|
2025-04-03 08:24:08 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-09 08:40:23 -04:00
|
|
|
pub fn send(&self, msg: Message) -> Result<(), String> {
|
2025-04-03 21:48:16 -04:00
|
|
|
let store = self.store.read().unwrap();
|
2025-04-15 09:53:38 -04:00
|
|
|
match store.get(&msg.get_msg_type()) {
|
2025-04-05 09:50:54 -04:00
|
|
|
Some(senders) => {
|
|
|
|
|
for sender in senders.into_iter() {
|
|
|
|
|
sender.send(msg.clone()).unwrap();
|
|
|
|
|
}
|
2025-04-09 08:40:23 -04:00
|
|
|
Ok(())
|
2025-04-05 09:50:54 -04:00
|
|
|
}
|
2025-04-15 09:53:38 -04:00
|
|
|
None => Err(format!("no listeners for {:?}", msg.get_msg_type())),
|
2025-04-03 08:24:08 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2025-04-04 13:26:28 -04:00
|
|
|
mod queues {
|
2025-04-03 08:24:08 -04:00
|
|
|
use super::*;
|
2025-04-04 13:26:28 -04:00
|
|
|
use std::{
|
|
|
|
|
sync::mpsc::{channel, RecvTimeoutError},
|
|
|
|
|
time::Duration,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static TIMEOUT: Duration = Duration::from_millis(500);
|
2025-04-03 08:24:08 -04:00
|
|
|
|
|
|
|
|
#[test]
|
2025-04-03 21:48:16 -04:00
|
|
|
fn create_queue() {
|
|
|
|
|
let queue = Queue::new();
|
2025-04-03 08:24:08 -04:00
|
|
|
let (tx1, rx1) = channel();
|
|
|
|
|
let (tx2, rx2) = channel();
|
2025-04-07 00:41:28 -04:00
|
|
|
queue.add(tx1, [MsgType::SessionValidate].to_vec());
|
|
|
|
|
queue.add(tx2, [MsgType::SessionValidate].to_vec());
|
2025-04-09 08:40:23 -04:00
|
|
|
queue.send(Message::new(MsgType::SessionValidate)).unwrap();
|
2025-04-03 08:24:08 -04:00
|
|
|
rx1.recv().unwrap();
|
|
|
|
|
rx2.recv().unwrap();
|
|
|
|
|
}
|
2025-04-04 13:26:28 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn messages_are_routed() {
|
|
|
|
|
let queue = Queue::new();
|
|
|
|
|
let (tx1, rx1) = channel();
|
|
|
|
|
let (tx2, rx2) = channel();
|
|
|
|
|
queue.add(tx1, [MsgType::SessionValidate].to_vec());
|
2025-04-15 18:37:02 -04:00
|
|
|
queue.add(tx2, [MsgType::SessionValidated].to_vec());
|
2025-04-09 08:40:23 -04:00
|
|
|
queue.send(Message::new(MsgType::SessionValidate)).unwrap();
|
2025-04-04 13:26:28 -04:00
|
|
|
let result = rx1.recv().unwrap();
|
2025-04-15 09:53:38 -04:00
|
|
|
match result.get_msg_type() {
|
2025-04-04 13:26:28 -04:00
|
|
|
MsgType::SessionValidate => {}
|
|
|
|
|
_ => unreachable!(
|
|
|
|
|
"received {:?}, should have been session vvalidate",
|
2025-04-15 09:53:38 -04:00
|
|
|
result.get_msg_type()
|
2025-04-04 13:26:28 -04:00
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
match rx2.recv_timeout(TIMEOUT) {
|
|
|
|
|
Ok(_) => unreachable!("should not have received anything"),
|
|
|
|
|
Err(err) => match err {
|
|
|
|
|
RecvTimeoutError::Timeout => {}
|
|
|
|
|
_ => unreachable!("{:?}", err),
|
|
|
|
|
},
|
|
|
|
|
}
|
2025-04-15 18:37:02 -04:00
|
|
|
queue.send(Message::new(MsgType::SessionValidated)).unwrap();
|
2025-04-04 13:26:28 -04:00
|
|
|
let result = rx2.recv().unwrap();
|
2025-04-15 09:53:38 -04:00
|
|
|
match result.get_msg_type() {
|
2025-04-15 18:37:02 -04:00
|
|
|
MsgType::SessionValidated => {}
|
2025-04-04 13:26:28 -04:00
|
|
|
_ => unreachable!(
|
|
|
|
|
"received {:?}, should have been session vvalidate",
|
2025-04-15 09:53:38 -04:00
|
|
|
result.get_msg_type()
|
2025-04-04 13:26:28 -04:00
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
match rx1.recv_timeout(TIMEOUT) {
|
|
|
|
|
Ok(_) => unreachable!("should not have received anything"),
|
|
|
|
|
Err(err) => match err {
|
|
|
|
|
RecvTimeoutError::Timeout => {}
|
|
|
|
|
_ => unreachable!("{:?}", err),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn assign_sender_multiple_message_types() {
|
|
|
|
|
let queue = Queue::new();
|
|
|
|
|
let (tx, rx) = channel();
|
2025-04-15 18:37:02 -04:00
|
|
|
queue.add(
|
|
|
|
|
tx,
|
|
|
|
|
[MsgType::SessionValidated, MsgType::SessionValidate].to_vec(),
|
|
|
|
|
);
|
2025-04-09 08:40:23 -04:00
|
|
|
queue.send(Message::new(MsgType::SessionValidate)).unwrap();
|
2025-04-04 13:26:28 -04:00
|
|
|
let msg = rx.recv().unwrap();
|
2025-04-15 09:53:38 -04:00
|
|
|
assert_eq!(msg.get_msg_type(), &MsgType::SessionValidate);
|
2025-04-15 18:37:02 -04:00
|
|
|
queue.send(Message::new(MsgType::SessionValidated)).unwrap();
|
2025-04-04 13:26:28 -04:00
|
|
|
let msg = rx.recv().unwrap();
|
2025-04-15 18:37:02 -04:00
|
|
|
assert_eq!(msg.get_msg_type(), &MsgType::SessionValidated);
|
2025-04-04 13:26:28 -04:00
|
|
|
}
|
2025-04-05 09:50:54 -04:00
|
|
|
|
|
|
|
|
#[test]
|
2025-04-09 08:40:23 -04:00
|
|
|
fn unassigned_message_should_return_error() {
|
2025-04-05 09:50:54 -04:00
|
|
|
let queue = Queue::new();
|
2025-04-15 18:37:02 -04:00
|
|
|
match queue.send(Message::new(MsgType::SessionValidated)) {
|
2025-04-09 08:40:23 -04:00
|
|
|
Ok(_) => unreachable!("should return error"),
|
|
|
|
|
Err(_) => {}
|
|
|
|
|
}
|
2025-04-05 09:50:54 -04:00
|
|
|
}
|
2025-04-03 08:24:08 -04:00
|
|
|
}
|