2025-04-02 14:26:09 -04:00
|
|
|
use crate::{client::Request, 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-02 14:26:09 -04:00
|
|
|
ClientRequest,
|
2025-04-13 08:26:18 -04:00
|
|
|
Document,
|
2025-04-11 22:06:36 -04:00
|
|
|
DocumentRequest,
|
2025-04-04 13:26:28 -04:00
|
|
|
Session,
|
2025-04-13 08:26:18 -04:00
|
|
|
SessionValidate,
|
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,
|
|
|
|
|
class: MsgType,
|
|
|
|
|
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-02 22:10:16 -04:00
|
|
|
class: 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(),
|
|
|
|
|
class: data,
|
2025-04-03 21:48:16 -04:00
|
|
|
data: HashMap::new(),
|
2025-03-29 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 22:10:16 -04:00
|
|
|
pub fn get_class(&self) -> &MsgType {
|
2025-03-29 09:22:53 -04:00
|
|
|
&self.class
|
|
|
|
|
}
|
|
|
|
|
|
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-07 00:41:28 -04:00
|
|
|
pub fn get_data(&self, name: &str) -> Option<&Field> {
|
|
|
|
|
self.data.get(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-02 14:26:09 -04:00
|
|
|
impl From<Request> for Message {
|
2025-04-08 09:30:04 -04:00
|
|
|
fn from(value: Request) -> Self {
|
|
|
|
|
let mut msg = Message::new(MsgType::ClientRequest);
|
|
|
|
|
match value.session {
|
|
|
|
|
Some(id) => msg.add_data("sess_id", id),
|
|
|
|
|
None => {}
|
|
|
|
|
}
|
|
|
|
|
msg
|
2025-04-02 14:26:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-29 09:22:53 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod messages {
|
|
|
|
|
use super::*;
|
2025-04-08 09:30:04 -04:00
|
|
|
use crate::client::requests::{get_root_document, get_root_document_eith_session};
|
2025-03-29 09:22:53 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn new_message() {
|
2025-04-07 00:41:28 -04:00
|
|
|
let msg = Message::new(MsgType::SessionValidate);
|
2025-03-29 09:22:53 -04:00
|
|
|
match msg.class {
|
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");
|
|
|
|
|
let data = MsgType::ClientRequest;
|
2025-03-29 09:22:53 -04:00
|
|
|
let result = msg.reply(data);
|
|
|
|
|
assert_eq!(result.id, id);
|
|
|
|
|
match result.class {
|
2025-04-03 21:48:16 -04:00
|
|
|
MsgType::ClientRequest => {}
|
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-03-29 09:22:53 -04:00
|
|
|
match msg.get_class() {
|
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
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn get_message_id() {
|
|
|
|
|
let msg = Message::new(MsgType::Session);
|
|
|
|
|
assert_eq!(msg.get_id(), msg.id);
|
|
|
|
|
}
|
2025-04-08 09:30:04 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn from_request_no_session() {
|
|
|
|
|
let req = get_root_document();
|
|
|
|
|
let msg: Message = req.into();
|
|
|
|
|
assert!(
|
|
|
|
|
msg.get_data("sess_id").is_none(),
|
|
|
|
|
"should not have a session id"
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn from_request_with_session() {
|
|
|
|
|
let id = Uuid::new_v4();
|
|
|
|
|
let req = get_root_document_eith_session(id.clone());
|
|
|
|
|
let msg: Message = req.into();
|
|
|
|
|
match msg.get_data("sess_id") {
|
|
|
|
|
Some(result) => assert_eq!(result.to_uuid().unwrap(), id),
|
|
|
|
|
None => unreachable!("should return an id"),
|
|
|
|
|
}
|
|
|
|
|
}
|
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-05 09:50:54 -04:00
|
|
|
match store.get(&msg.get_class()) {
|
|
|
|
|
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-09 08:40:23 -04:00
|
|
|
None => Err(format!("no listeners for {:?}", msg.get_class())),
|
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());
|
|
|
|
|
queue.add(tx2, [MsgType::Session].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();
|
|
|
|
|
match result.get_class() {
|
|
|
|
|
MsgType::SessionValidate => {}
|
|
|
|
|
_ => unreachable!(
|
|
|
|
|
"received {:?}, should have been session vvalidate",
|
|
|
|
|
result.get_class()
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
match rx2.recv_timeout(TIMEOUT) {
|
|
|
|
|
Ok(_) => unreachable!("should not have received anything"),
|
|
|
|
|
Err(err) => match err {
|
|
|
|
|
RecvTimeoutError::Timeout => {}
|
|
|
|
|
_ => unreachable!("{:?}", err),
|
|
|
|
|
},
|
|
|
|
|
}
|
2025-04-09 08:40:23 -04:00
|
|
|
queue.send(Message::new(MsgType::Session)).unwrap();
|
2025-04-04 13:26:28 -04:00
|
|
|
let result = rx2.recv().unwrap();
|
|
|
|
|
match result.get_class() {
|
|
|
|
|
MsgType::Session => {}
|
|
|
|
|
_ => unreachable!(
|
|
|
|
|
"received {:?}, should have been session vvalidate",
|
|
|
|
|
result.get_class()
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
queue.add(tx, [MsgType::Session, 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();
|
|
|
|
|
assert_eq!(msg.get_class(), &MsgType::SessionValidate);
|
2025-04-09 08:40:23 -04:00
|
|
|
queue.send(Message::new(MsgType::Session)).unwrap();
|
2025-04-04 13:26:28 -04:00
|
|
|
let msg = rx.recv().unwrap();
|
|
|
|
|
assert_eq!(msg.get_class(), &MsgType::Session);
|
|
|
|
|
}
|
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-09 08:40:23 -04:00
|
|
|
match queue.send(Message::new(MsgType::Session)) {
|
|
|
|
|
Ok(_) => unreachable!("should return error"),
|
|
|
|
|
Err(_) => {}
|
|
|
|
|
}
|
2025-04-05 09:50:54 -04:00
|
|
|
}
|
2025-04-03 08:24:08 -04:00
|
|
|
}
|