linked client to session.

This commit is contained in:
2025-04-07 00:41:28 -04:00
parent d8623f9691
commit f9f64dae55
6 changed files with 75 additions and 44 deletions

View File

@ -12,6 +12,8 @@ use std::{
};
use uuid::Uuid;
const RESPONS_TO: [MsgType; 1] = [MsgType::Session];
pub struct Request;
impl Request {
@ -29,11 +31,17 @@ mod requests {
}
}
pub struct Reply;
pub struct Reply {
sess_id: Uuid,
}
impl Reply {
pub fn get_session(&self) -> String {
"id".to_string()
fn new(sess_id: Uuid) -> Self {
Self { sess_id: sess_id }
}
pub fn get_session(&self) -> Uuid {
self.sess_id.clone()
}
pub fn get_content(&self) -> String {
@ -46,7 +54,16 @@ mod replies {
use super::*;
pub fn create_reply() -> Reply {
Reply {}
Reply {
sess_id: Uuid::new_v4(),
}
}
#[test]
fn create_new_reply() {
let sess_id = Uuid::new_v4();
let reply = Reply::new(sess_id);
assert_eq!(reply.get_session(), sess_id);
}
}
@ -83,15 +100,15 @@ impl ClientRegistry {
fn send(&mut self, id: &Uuid, msg: Reply) {
let mut reg = self.registry.lock().unwrap();
let tx = reg.get(id).unwrap();
let tx = reg.remove(id).unwrap();
tx.send(msg).unwrap();
reg.remove(id).unwrap();
}
}
#[cfg(test)]
mod clientregistries {
use super::*;
use crate::client::replies::create_reply;
use std::{
sync::mpsc::{channel, Receiver},
time::Duration,
@ -118,7 +135,7 @@ mod clientregistries {
}
assert_eq!(rxs.len(), count, "should have been {} receivers", count);
for (id, rx) in rxs.iter() {
let msg = Reply {};
let msg = create_reply();
reg.send(id, msg);
rx.recv_timeout(TIMEOUT).unwrap();
}
@ -129,7 +146,7 @@ mod clientregistries {
#[test]
fn prevent_duplicates() {
let mut reg = ClientRegistry::new();
let (tx, rx) = channel::<Reply>();
let (tx, _rx) = channel::<Reply>();
let existing = reg.add(tx);
let expected = Uuid::new_v4();
let ids = [existing.clone(), expected.clone()];
@ -166,6 +183,7 @@ impl ClientLink {
#[cfg(test)]
mod clientlinks {
use super::*;
use crate::client::replies::create_reply;
use std::time::Duration;
static TIMEOUT: Duration = Duration::from_millis(500);
@ -182,10 +200,10 @@ mod clientlinks {
MsgType::ClientRequest => {}
_ => unreachable!("should have been a client request"),
}
match msg.get_data().get("tx_id") {
match msg.get_data("tx_id") {
Some(result) => {
let id = result.to_uuid().unwrap();
registry.send(&id, Reply {});
registry.send(&id, create_reply());
rx_client.recv().unwrap();
}
None => unreachable!("should have had a seender id"),
@ -196,6 +214,7 @@ mod clientlinks {
pub struct Client {
queue: Queue,
registry: ClientRegistry,
return_to: HashMap<Uuid, Uuid>,
rx: Receiver<Message>,
}
@ -204,12 +223,14 @@ impl Client {
Self {
queue: queue,
registry: ClientRegistry::new(),
return_to: HashMap::new(),
rx: rx,
}
}
pub fn start(queue: Queue) -> ClientLink {
let (tx, rx) = channel();
queue.add(tx.clone(), RESPONS_TO.to_vec());
let mut client = Client::new(rx, queue);
let link = ClientLink::new(tx, client.get_registry());
spawn(move || {
@ -221,10 +242,19 @@ impl Client {
fn listen(&mut self) {
loop {
let msg = self.rx.recv().unwrap();
//self.queue.send(Message::new(MsgType::SessionValidate));
let id = msg.get_data().get("tx_id").unwrap().to_uuid().unwrap();
let reply = Reply {};
self.registry.send(&id, reply);
match msg.get_class() {
MsgType::ClientRequest => {
let tx_id = msg.get_data("tx_id").unwrap().to_uuid().unwrap();
self.return_to.insert(msg.get_id(), tx_id);
self.queue.send(msg.reply(MsgType::SessionValidate));
}
MsgType::Session => {
let rx_id = self.return_to.remove(&msg.get_id()).unwrap();
let sess_id = msg.get_data("sess_id").unwrap().to_uuid().unwrap();
self.registry.send(&rx_id, Reply::new(sess_id));
}
_ => unreachable!("Received message it did not understand"),
}
}
}
@ -244,17 +274,21 @@ mod clients {
#[test]
fn start_client() {
let (tx, rx) = channel();
let mut queue = Queue::new();
let queue = Queue::new();
queue.add(tx, [MsgType::SessionValidate].to_vec());
let mut link = Client::start(queue.clone());
let req = create_request();
link.send(req);
/*
let reply_rx = link.send(req);
let sess = rx.recv_timeout(TIMEOUT).unwrap();
match sess.get_class() {
MsgType::SessionValidate => {},
MsgType::SessionValidate => {}
_ => unreachable!("should request session validation"),
}
*/
let sess_id = Uuid::new_v4();
let mut sess_res = sess.reply(MsgType::Session);
sess_res.add_data("sess_id", sess_id.clone());
queue.send(sess_res);
let reply = reply_rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(reply.get_session(), sess_id);
}
}