2025-04-03 08:24:08 -04:00
|
|
|
use crate::{
|
2025-04-08 09:30:04 -04:00
|
|
|
field::Field,
|
2025-04-03 21:48:16 -04:00
|
|
|
queue::{Message, MsgType, Queue},
|
2025-04-03 08:24:08 -04:00
|
|
|
utils::GenID,
|
|
|
|
};
|
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-08 09:30:04 -04:00
|
|
|
pub struct Request {
|
|
|
|
pub session: Option<Field>,
|
|
|
|
}
|
2025-03-27 07:18:19 -04:00
|
|
|
|
|
|
|
impl Request {
|
2025-04-08 09:30:04 -04:00
|
|
|
pub fn new(session: Option<Field>) -> Self {
|
|
|
|
Self { session: session }
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2025-04-08 09:30:04 -04:00
|
|
|
pub mod requests {
|
2025-03-27 07:18:19 -04:00
|
|
|
use super::*;
|
|
|
|
|
2025-04-08 09:30:04 -04:00
|
|
|
pub fn get_root_document() -> Request {
|
|
|
|
Request::new(None)
|
|
|
|
}
|
|
|
|
|
2025-04-15 18:37:02 -04:00
|
|
|
pub fn get_root_with_session(sess_id: &Uuid) -> Request {
|
|
|
|
Request::new(Some(sess_id.clone().into()))
|
|
|
|
}
|
|
|
|
|
2025-04-08 09:30:04 -04:00
|
|
|
pub fn get_root_document_eith_session<F>(id: F) -> Request
|
|
|
|
where
|
|
|
|
F: Into<Field>,
|
|
|
|
{
|
|
|
|
Request::new(Some(id.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_request_no_session() {
|
|
|
|
let sess: Option<Field> = None;
|
|
|
|
let req = Request::new(sess);
|
|
|
|
assert!(req.session.is_none(), "should not have a session")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_request_with_session() {
|
|
|
|
let id = Uuid::new_v4();
|
|
|
|
let req = Request::new(Some(id.into()));
|
|
|
|
match req.session {
|
|
|
|
Some(result) => assert_eq!(result.to_uuid().unwrap(), id),
|
|
|
|
None => unreachable!("should contain a session"),
|
|
|
|
}
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-07 00:41:28 -04:00
|
|
|
pub struct Reply {
|
|
|
|
sess_id: Uuid,
|
2025-04-15 18:37:02 -04:00
|
|
|
content: String,
|
2025-04-07 00:41:28 -04:00
|
|
|
}
|
2025-03-27 07:18:19 -04:00
|
|
|
|
|
|
|
impl Reply {
|
2025-04-15 18:37:02 -04:00
|
|
|
fn new(sess_id: Uuid, content: String) -> Self {
|
|
|
|
Self {
|
|
|
|
sess_id: sess_id,
|
|
|
|
content: content,
|
|
|
|
}
|
2025-04-07 00:41:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_session(&self) -> Uuid {
|
|
|
|
self.sess_id.clone()
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_content(&self) -> String {
|
2025-04-15 18:37:02 -04:00
|
|
|
self.content.clone()
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod replies {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub fn create_reply() -> Reply {
|
2025-04-07 00:41:28 -04:00
|
|
|
Reply {
|
|
|
|
sess_id: Uuid::new_v4(),
|
2025-04-15 18:37:02 -04:00
|
|
|
content: "some text".to_string(),
|
2025-04-07 00:41:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_new_reply() {
|
|
|
|
let sess_id = Uuid::new_v4();
|
2025-04-15 18:37:02 -04:00
|
|
|
let txt = Uuid::new_v4().to_string();
|
|
|
|
let reply = Reply::new(sess_id, txt.clone());
|
2025-04-07 00:41:28 -04:00
|
|
|
assert_eq!(reply.get_session(), sess_id);
|
2025-04-15 18:37:02 -04:00
|
|
|
assert_eq!(reply.get_content(), txt);
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-02 14:26:09 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ClientRegistry {
|
2025-04-19 07:57:16 -04:00
|
|
|
registry: Arc<Mutex<HashMap<Uuid, Sender<Message>>>>,
|
2025-04-02 14:26:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientRegistry {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
registry: Arc::new(Mutex::new(HashMap::new())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_id<'a>(
|
|
|
|
gen: &mut impl Iterator<Item = Uuid>,
|
2025-04-19 07:57:16 -04:00
|
|
|
data: &HashMap<Uuid, Sender<Message>>,
|
2025-04-02 14:26:09 -04:00
|
|
|
) -> Uuid {
|
|
|
|
let mut id = gen.next().unwrap();
|
|
|
|
while data.contains_key(&id) {
|
|
|
|
id = gen.next().unwrap();
|
|
|
|
}
|
|
|
|
id.clone()
|
|
|
|
}
|
|
|
|
|
2025-04-19 07:57:16 -04:00
|
|
|
pub fn add(&mut self, tx: Sender<Message>) -> Uuid {
|
2025-04-02 14:26:09 -04:00
|
|
|
let mut reg = self.registry.lock().unwrap();
|
|
|
|
let mut gen_id = GenID::new();
|
|
|
|
let id = ClientRegistry::get_id(&mut gen_id, ®);
|
|
|
|
reg.insert(id.clone(), tx);
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
2025-04-19 07:57:16 -04:00
|
|
|
fn send(&mut self, id: &Uuid, msg: Message) {
|
2025-04-02 14:26:09 -04:00
|
|
|
let mut reg = self.registry.lock().unwrap();
|
2025-04-07 00:41:28 -04:00
|
|
|
let tx = reg.remove(id).unwrap();
|
2025-04-02 14:26:09 -04:00
|
|
|
tx.send(msg).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod clientregistries {
|
|
|
|
use super::*;
|
2025-04-07 00:41:28 -04:00
|
|
|
use crate::client::replies::create_reply;
|
2025-04-02 14:26:09 -04:00
|
|
|
use std::{
|
|
|
|
sync::mpsc::{channel, Receiver},
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
|
|
|
static TIMEOUT: Duration = Duration::from_millis(500);
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_client_registry() {
|
|
|
|
let reg = ClientRegistry::new();
|
|
|
|
let data = reg.registry.lock().unwrap();
|
|
|
|
assert!(data.is_empty(), "needs to create an empty hashmap");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn send_from_client() {
|
|
|
|
let mut reg = ClientRegistry::new();
|
|
|
|
let count = 10;
|
2025-04-19 07:57:16 -04:00
|
|
|
let mut rxs: HashMap<Uuid, Receiver<Message>> = HashMap::new();
|
2025-04-02 14:26:09 -04:00
|
|
|
for _ in 0..count {
|
2025-04-19 07:57:16 -04:00
|
|
|
let (tx, rx) = channel::<Message>();
|
2025-04-02 14:26:09 -04:00
|
|
|
let id = reg.add(tx);
|
|
|
|
rxs.insert(id, rx);
|
|
|
|
}
|
|
|
|
assert_eq!(rxs.len(), count, "should have been {} receivers", count);
|
|
|
|
for (id, rx) in rxs.iter() {
|
2025-04-19 07:57:16 -04:00
|
|
|
let msg = Message::new(MsgType::Document);
|
2025-04-02 14:26:09 -04:00
|
|
|
reg.send(id, msg);
|
|
|
|
rx.recv_timeout(TIMEOUT).unwrap();
|
|
|
|
}
|
|
|
|
let data = reg.registry.lock().unwrap();
|
|
|
|
assert!(data.is_empty(), "should remove sender after sending");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prevent_duplicates() {
|
|
|
|
let mut reg = ClientRegistry::new();
|
2025-04-19 07:57:16 -04:00
|
|
|
let (tx, _rx) = channel::<Message>();
|
2025-04-02 14:26:09 -04:00
|
|
|
let existing = reg.add(tx);
|
|
|
|
let expected = Uuid::new_v4();
|
|
|
|
let ids = [existing.clone(), expected.clone()];
|
|
|
|
let data = reg.registry.lock().unwrap();
|
|
|
|
let result = ClientRegistry::get_id(&mut ids.into_iter(), &data);
|
|
|
|
assert_eq!(result, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2025-04-02 22:10:16 -04:00
|
|
|
pub struct ClientLink {
|
|
|
|
tx: Sender<Message>,
|
|
|
|
registry: ClientRegistry,
|
|
|
|
}
|
2025-04-02 14:26:09 -04:00
|
|
|
|
|
|
|
impl ClientLink {
|
2025-04-02 22:10:16 -04:00
|
|
|
fn new(tx: Sender<Message>, registry: ClientRegistry) -> Self {
|
|
|
|
Self {
|
|
|
|
tx: tx,
|
|
|
|
registry: registry,
|
|
|
|
}
|
2025-04-02 14:26:09 -04:00
|
|
|
}
|
|
|
|
|
2025-04-19 07:57:16 -04:00
|
|
|
pub fn send(&mut self, mut req: Message) -> Receiver<Message> {
|
2025-04-02 22:10:16 -04:00
|
|
|
let (tx, rx) = channel();
|
2025-04-19 07:57:16 -04:00
|
|
|
//let mut msg: Message = req.into();
|
2025-04-02 22:10:16 -04:00
|
|
|
let id = self.registry.add(tx);
|
2025-04-19 07:57:16 -04:00
|
|
|
req.add_data("tx_id", id);
|
|
|
|
self.tx.send(req).unwrap();
|
2025-04-02 22:10:16 -04:00
|
|
|
rx
|
2025-04-02 14:26:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod clientlinks {
|
|
|
|
use super::*;
|
2025-04-07 00:41:28 -04:00
|
|
|
use crate::client::replies::create_reply;
|
2025-04-02 22:10:16 -04:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
static TIMEOUT: Duration = Duration::from_millis(500);
|
2025-04-02 14:26:09 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_client_link() {
|
2025-04-02 22:10:16 -04:00
|
|
|
let (tx, rx) = channel();
|
|
|
|
let mut registry = ClientRegistry::new();
|
|
|
|
let mut link = ClientLink::new(tx, registry.clone());
|
2025-04-08 09:30:04 -04:00
|
|
|
let req = Request::new(None);
|
2025-04-19 07:57:16 -04:00
|
|
|
let rx_client = link.send(req.into());
|
2025-04-02 22:10:16 -04:00
|
|
|
let msg = rx.recv_timeout(TIMEOUT).unwrap();
|
2025-04-15 09:53:38 -04:00
|
|
|
match msg.get_msg_type() {
|
2025-04-03 08:24:08 -04:00
|
|
|
MsgType::ClientRequest => {}
|
2025-04-02 22:10:16 -04:00
|
|
|
_ => unreachable!("should have been a client request"),
|
|
|
|
}
|
2025-04-07 00:41:28 -04:00
|
|
|
match msg.get_data("tx_id") {
|
2025-04-02 22:10:16 -04:00
|
|
|
Some(result) => {
|
|
|
|
let id = result.to_uuid().unwrap();
|
2025-04-19 07:57:16 -04:00
|
|
|
registry.send(&id, msg.reply(MsgType::Document));
|
2025-04-02 22:10:16 -04:00
|
|
|
rx_client.recv().unwrap();
|
2025-04-03 08:24:08 -04:00
|
|
|
}
|
2025-04-02 22:10:16 -04:00
|
|
|
None => unreachable!("should have had a seender id"),
|
|
|
|
}
|
2025-04-02 14:26:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-27 07:18:19 -04:00
|
|
|
pub struct Client {
|
2025-04-03 22:28:47 -04:00
|
|
|
queue: Queue,
|
2025-04-02 22:10:16 -04:00
|
|
|
registry: ClientRegistry,
|
2025-04-15 18:37:02 -04:00
|
|
|
return_to: HashMap<Uuid, Message>,
|
2025-04-02 14:26:09 -04:00
|
|
|
rx: Receiver<Message>,
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2025-04-03 22:28:47 -04:00
|
|
|
fn new(rx: Receiver<Message>, queue: Queue) -> Self {
|
2025-04-02 22:10:16 -04:00
|
|
|
Self {
|
2025-04-03 22:28:47 -04:00
|
|
|
queue: queue,
|
2025-04-02 22:10:16 -04:00
|
|
|
registry: ClientRegistry::new(),
|
2025-04-07 00:41:28 -04:00
|
|
|
return_to: HashMap::new(),
|
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
|
|
|
}
|
|
|
|
|
2025-04-03 22:28:47 -04:00
|
|
|
pub fn start(queue: Queue) -> ClientLink {
|
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());
|
2025-04-03 22:28:47 -04:00
|
|
|
let mut client = Client::new(rx, queue);
|
2025-04-02 22:10:16 -04:00
|
|
|
let link = ClientLink::new(tx, client.get_registry());
|
2025-03-27 07:18:19 -04:00
|
|
|
spawn(move || {
|
|
|
|
client.listen();
|
|
|
|
});
|
2025-04-02 22:10:16 -04:00
|
|
|
link
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
|
|
|
|
2025-04-02 22:10:16 -04:00
|
|
|
fn listen(&mut self) {
|
2025-03-27 07:18:19 -04:00
|
|
|
loop {
|
2025-04-02 22:10:16 -04:00
|
|
|
let msg = self.rx.recv().unwrap();
|
2025-04-15 09:53:38 -04:00
|
|
|
match msg.get_msg_type() {
|
|
|
|
MsgType::ClientRequest => self.client_request(msg),
|
2025-04-15 18:37:02 -04:00
|
|
|
MsgType::Document => self.document(msg),
|
|
|
|
MsgType::SessionValidated => self.session(msg),
|
2025-04-07 00:41:28 -04:00
|
|
|
_ => unreachable!("Received message it did not understand"),
|
|
|
|
}
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
|
|
|
}
|
2025-04-02 22:10:16 -04:00
|
|
|
|
|
|
|
fn get_registry(&self) -> ClientRegistry {
|
|
|
|
self.registry.clone()
|
|
|
|
}
|
2025-04-15 09:53:38 -04:00
|
|
|
|
|
|
|
fn client_request(&mut self, msg: Message) {
|
2025-04-15 18:37:02 -04:00
|
|
|
self.return_to.insert(msg.get_id(), msg.clone());
|
|
|
|
let mut reply = msg.reply(MsgType::SessionValidate);
|
|
|
|
match msg.get_data("sess_id") {
|
|
|
|
Some(sess_id) => reply.add_data("sess_id", sess_id.clone()),
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
self.queue.send(reply).unwrap();
|
2025-04-15 09:53:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn session(&mut self, msg: Message) {
|
2025-04-15 18:37:02 -04:00
|
|
|
let initial_msg = self.return_to.get_mut(&msg.get_id()).unwrap();
|
|
|
|
let mut reply = msg.reply(MsgType::DocumentRequest);
|
|
|
|
match msg.get_data("sess_id") {
|
|
|
|
Some(sess_id) => {
|
|
|
|
initial_msg.add_data("sess_id", sess_id.clone());
|
|
|
|
reply.add_data("sess_id", sess_id.clone());
|
|
|
|
}
|
|
|
|
None => unreachable!("validated should always have an id"),
|
|
|
|
}
|
|
|
|
self.queue.send(reply).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn document(&mut self, msg: Message) {
|
|
|
|
let initial_msg = self.return_to.remove(&msg.get_id()).unwrap();
|
|
|
|
let tx_id = initial_msg.get_data("tx_id").unwrap().to_uuid().unwrap();
|
2025-04-19 07:57:16 -04:00
|
|
|
/*
|
2025-04-15 18:37:02 -04:00
|
|
|
let reply = Reply::new(
|
|
|
|
initial_msg.get_data("sess_id").unwrap().to_uuid().unwrap(),
|
|
|
|
msg.get_data("doc").unwrap().to_string(),
|
2025-04-19 07:57:16 -04:00
|
|
|
)s
|
|
|
|
*/
|
2025-04-20 10:15:55 -04:00
|
|
|
self.registry
|
|
|
|
.send(&tx_id, initial_msg.reply(MsgType::Document));
|
2025-04-15 09:53:38 -04:00
|
|
|
}
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod clients {
|
|
|
|
use super::*;
|
2025-04-15 18:37:02 -04:00
|
|
|
use requests::get_root_with_session;
|
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
|
|
|
|
2025-04-19 07:57:16 -04:00
|
|
|
/*
|
2025-03-27 07:18:19 -04:00
|
|
|
#[test]
|
|
|
|
fn start_client() {
|
2025-04-15 18:37:02 -04:00
|
|
|
let sess_id1 = Uuid::new_v4();
|
|
|
|
let sess_id2 = Uuid::new_v4();
|
|
|
|
let doc = Uuid::new_v4().to_string();
|
2025-04-03 22:28:47 -04:00
|
|
|
let (tx, rx) = channel();
|
2025-04-07 00:41:28 -04:00
|
|
|
let queue = Queue::new();
|
2025-04-15 09:53:38 -04:00
|
|
|
queue.add(
|
|
|
|
tx,
|
|
|
|
[MsgType::SessionValidate, MsgType::DocumentRequest].to_vec(),
|
|
|
|
);
|
2025-04-03 21:48:16 -04:00
|
|
|
let mut link = Client::start(queue.clone());
|
2025-04-15 18:37:02 -04:00
|
|
|
let req = get_root_with_session(&sess_id1);
|
2025-04-19 07:57:16 -04:00
|
|
|
let reply_rx = link.send(req.into());
|
2025-04-15 18:37:02 -04:00
|
|
|
let send1 = rx.recv_timeout(TIMEOUT).unwrap();
|
|
|
|
match send1.get_msg_type() {
|
2025-04-07 00:41:28 -04:00
|
|
|
MsgType::SessionValidate => {}
|
2025-04-03 22:28:47 -04:00
|
|
|
_ => unreachable!("should request session validation"),
|
|
|
|
}
|
2025-04-15 18:37:02 -04:00
|
|
|
assert_eq!(
|
|
|
|
send1.get_data("sess_id").unwrap().to_uuid().unwrap(),
|
|
|
|
sess_id1
|
|
|
|
);
|
|
|
|
assert!(send1.get_data("tx_id").is_none());
|
|
|
|
let mut response = send1.reply_with_data(MsgType::SessionValidated);
|
|
|
|
response.add_data("sess_id", sess_id2);
|
|
|
|
queue.send(response).unwrap();
|
|
|
|
let send2 = rx.recv_timeout(TIMEOUT).unwrap();
|
|
|
|
assert_eq!(send2.get_id(), send1.get_id());
|
|
|
|
match send2.get_msg_type() {
|
|
|
|
MsgType::DocumentRequest => {}
|
|
|
|
_ => unreachable!("should request session validation"),
|
|
|
|
}
|
|
|
|
assert_eq!(
|
|
|
|
send2.get_data("sess_id").unwrap().to_uuid().unwrap(),
|
|
|
|
sess_id2
|
|
|
|
);
|
|
|
|
let mut document = send2.reply(MsgType::Document);
|
|
|
|
document.add_data("doc", doc.clone());
|
|
|
|
queue.send(document).unwrap();
|
2025-04-07 00:41:28 -04:00
|
|
|
let reply = reply_rx.recv_timeout(TIMEOUT).unwrap();
|
2025-04-19 07:57:16 -04:00
|
|
|
assert_eq!(reply.get_data("sess_id").unwrap().to_uuid().unwrap(), sess_id2);
|
|
|
|
assert_eq!(reply.get_data("doc").unwrap().to_string(), doc);
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
2025-04-19 07:57:16 -04:00
|
|
|
*/
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|