morethantext/src/client.rs

163 lines
4.7 KiB
Rust
Raw Normal View History

2025-04-21 22:29:15 -04:00
use crate::queue::{Message, MsgType, Queue};
2025-03-27 07:18:19 -04:00
use std::{
2025-04-02 14:26:09 -04:00
collections::HashMap,
sync::{
mpsc::{channel, Receiver, Sender},
Arc, Mutex,
},
2025-03-27 07:18:19 -04:00
thread::spawn,
};
2025-04-02 14:26:09 -04:00
use uuid::Uuid;
2025-03-27 07:18:19 -04:00
2025-04-15 18:37:02 -04:00
const RESPONS_TO: [MsgType; 2] = [MsgType::Document, MsgType::SessionValidated];
2025-04-07 00:41:28 -04:00
2025-04-02 14:26:09 -04:00
#[derive(Clone)]
pub struct ClientChannel {
queue: Queue,
registry: Arc<Mutex<HashMap<Uuid, Sender<Message>>>>,
2025-04-02 14:26:09 -04:00
}
impl ClientChannel {
fn new(queue: Queue) -> Self {
2025-04-02 14:26:09 -04:00
Self {
queue: queue,
2025-04-02 14:26:09 -04:00
registry: Arc::new(Mutex::new(HashMap::new())),
}
}
pub fn send(&self, mut msg: Message) -> Receiver<Message> {
2025-04-02 14:26:09 -04:00
let mut reg = self.registry.lock().unwrap();
if reg.contains_key(&msg.get_id()) {
let mut id = Uuid::new_v4();
while reg.contains_key(&id) {
id = Uuid::new_v4();
}
msg.reset_id(id);
}
let (tx, rx) = channel();
reg.insert(msg.get_id(), tx);
2025-04-21 22:29:15 -04:00
self.queue.send(msg).unwrap();
rx
2025-04-02 14:26:09 -04:00
}
fn reply(&self, msg: Message) {
2025-04-02 14:26:09 -04:00
let mut reg = self.registry.lock().unwrap();
match reg.remove(&msg.get_id()) {
Some(tx) => tx.send(msg).unwrap(),
None => {}
}
2025-04-02 14:26:09 -04:00
}
}
#[cfg(test)]
mod client_channels {
2025-04-02 14:26:09 -04:00
use super::*;
use std::time::Duration;
2025-04-02 14:26:09 -04:00
static TIMEOUT: Duration = Duration::from_millis(500);
#[test]
fn fowards_message() {
let msg_type = MsgType::Document;
let reply_type = MsgType::Time;
let queue = Queue::new();
let (tx, rx) = channel();
queue.add(tx, [msg_type.clone()].to_vec());
let chan = ClientChannel::new(queue);
let msg = Message::new(msg_type.clone());
let client_rx = chan.send(msg.clone());
let reply = rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(reply.get_id(), msg.get_id());
assert_eq!(reply.get_msg_type().clone(), msg_type);
let client_reply = reply.reply(MsgType::Time);
chan.reply(client_reply);
let client_msg = client_rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(client_msg.get_id(), msg.get_id());
assert_eq!(client_msg.get_msg_type().clone(), reply_type);
2025-04-02 14:26:09 -04:00
}
#[test]
fn no_duplicate_ids() {
2025-04-02 22:10:16 -04:00
let (tx, rx) = channel();
let queue = Queue::new();
queue.add(tx, [MsgType::Time].to_vec());
let chan = ClientChannel::new(queue);
let msg1 = Message::new(MsgType::Time);
let msg2 = msg1.reply(MsgType::Time);
let rx1 = chan.send(msg1);
let rx2 = chan.send(msg2);
let queue1 = rx.recv_timeout(TIMEOUT).unwrap();
let queue2 = rx.recv_timeout(TIMEOUT).unwrap();
assert_ne!(queue1.get_id(), queue2.get_id());
chan.reply(queue1.reply(MsgType::Document));
chan.reply(queue2.reply(MsgType::Document));
let reply1 = rx1.recv_timeout(TIMEOUT).unwrap();
let reply2 = rx2.recv_timeout(TIMEOUT).unwrap();
assert_eq!(reply1.get_id(), queue1.get_id());
assert_eq!(reply2.get_id(), queue2.get_id());
2025-04-02 14:26:09 -04:00
}
#[test]
fn ignore_unrequested() {
let queue = Queue::new();
let chan = ClientChannel::new(queue);
chan.reply(Message::new(MsgType::Document));
2025-04-02 14:26:09 -04:00
}
}
2025-03-27 07:18:19 -04:00
pub struct Client {
channel: ClientChannel,
2025-04-02 14:26:09 -04:00
rx: Receiver<Message>,
2025-03-27 07:18:19 -04:00
}
impl Client {
2025-04-21 22:29:15 -04:00
fn new(chan: ClientChannel, rx: Receiver<Message>) -> Self {
2025-04-02 22:10:16 -04:00
Self {
channel: chan,
2025-04-03 08:24:08 -04:00
rx: rx,
2025-04-02 22:10:16 -04:00
}
2025-03-27 07:18:19 -04:00
}
pub fn start(queue: Queue) -> ClientChannel {
2025-03-27 07:18:19 -04:00
let (tx, rx) = channel();
2025-04-07 00:41:28 -04:00
queue.add(tx.clone(), RESPONS_TO.to_vec());
let chan = ClientChannel::new(queue.clone());
2025-04-21 22:29:15 -04:00
let client = Client::new(chan.clone(), rx);
2025-03-27 07:18:19 -04:00
spawn(move || {
client.listen();
});
chan
2025-03-27 07:18:19 -04:00
}
fn listen(&self) {
2025-03-27 07:18:19 -04:00
loop {
2025-04-02 22:10:16 -04:00
let msg = self.rx.recv().unwrap();
self.channel.reply(msg);
2025-03-27 07:18:19 -04:00
}
}
}
#[cfg(test)]
mod clients {
use super::*;
use crate::session::sessions::create_validated_reply;
2025-04-03 22:28:47 -04:00
use std::time::Duration;
static TIMEOUT: Duration = Duration::from_millis(500);
2025-03-27 07:18:19 -04:00
#[test]
fn session_validated() {
2025-04-07 00:41:28 -04:00
let queue = Queue::new();
let (queue_tx, queue_rx) = channel();
queue.add(queue_tx, [MsgType::SessionValidate].to_vec());
let chan = Client::start(queue.clone());
let chan_rx = chan.send(Message::new(MsgType::SessionValidate));
let msg = queue_rx.recv_timeout(TIMEOUT).unwrap();
let expected = create_validated_reply(msg);
2025-04-21 22:29:15 -04:00
queue.send(expected.clone()).unwrap();
let result = chan_rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(result.get_id(), expected.get_id());
assert_eq!(result.get_msg_type(), expected.get_msg_type());
2025-03-27 07:18:19 -04:00
}
}