rebuilt to use message router.

This commit is contained in:
2024-11-06 21:05:52 -05:00
parent d4ce2ab03b
commit 52b6506088
9 changed files with 640 additions and 209 deletions

View File

@ -1,20 +1,48 @@
mod cache;
mod messages;
mod client;
mod message;
mod router;
mod session;
use cache::Cache;
use messages::{ReceiveMsg, SendMsg, SessionRequest};
//use client::ClientMsg;
//use router::Router;
//use session::{Session, SessionFilter, SessionMsg};
use client::{Client, ClientMsg};
use message::{Message, MsgData};
use router::Router;
use session::{Session, SessionData, SessionMsg};
use std::{
sync::mpsc::{channel, Sender},
thread::spawn,
};
use uuid::Uuid;
/// Application connection to the database
/// Support functions for Messages.
pub trait Msg {
fn to_msgdata(&self) -> MsgData;
}
#[cfg(test)]
mod test_message {
use super::*;
pub enum Tester {
Test1,
Test2,
}
impl Msg for Tester {
fn to_msgdata(&self) -> MsgData {
match self {
Tester::Test1 => MsgData::Test1,
Tester::Test2 => MsgData::Test2,
}
}
}
}
/// Application client to MoreThanText
#[derive(Clone)]
pub struct MoreThanText {
id: Option<Uuid>,
tx: Sender<SendMsg>,
nodes: Vec<String>
session: Option<SessionData>,
tx: Sender<Message>,
}
impl MoreThanText {
@ -25,38 +53,48 @@ impl MoreThanText {
/// ```
/// use morethantext::MoreThanText;
///
/// MoreThanText::new(Vec::new());
/// MoreThanText::new();
/// ```
pub fn new(_nodes: Vec<String>) -> Self {
pub fn new() -> Self {
let (tx, rx) = channel();
spawn(move || {
let mut cache = Cache::new(rx);
cache.listen();
});
Self { id: None, tx: tx, nodes: Vec::new() }
let mut senders = Vec::new();
senders.push(Client::start(tx.clone()));
senders.push(Session::start(tx.clone()));
Router::start(senders, rx);
Self {
session: None,
tx: tx,
}
}
/// Opens an existing or new session with the database.
///
/// If the string is None, incorrect, or expired,
/// a new session will be created.
/// a new session will be created.
///
/// Example:
///
/// ```
/// use morethantext::MoreThanText;
///
/// let mut mtt = MoreThanText::new(Vec::new());
/// let mut mtt = MoreThanText::new();
/// mtt.open_session(None);
/// mtt.open_session(Some("7b1ff340-7dfa-4f29-b144-601384e54423".to_string()));
/// ```
pub fn open_session(&mut self, id: Option<String>) {
let (tx, rx) = channel();
let request = SessionRequest { id: id, tx: tx };
self.tx.send(SendMsg::OpenSession(request)).unwrap();
match rx.recv().unwrap() {
ReceiveMsg::Session(sid) => self.id = Some(sid),
}
let req = ClientMsg::OpenSession(id, tx);
let msg = Message::new(&req);
self.tx.send(msg).unwrap();
match rx.recv().unwrap().get_message() {
MsgData::Session(data) => {
match data {
SessionMsg::Opened(sess) => self.session = Some(sess.clone()),
_ => {},
}
},
_ => {},
};
}
/// Get the session id
@ -66,13 +104,13 @@ impl MoreThanText {
/// ```
/// use morethantext::MoreThanText;
///
/// let mut mtt = MoreThanText::new(Vec::new());
/// mtt.get_id();
/// let mut mtt = MoreThanText::new();
/// let id = mtt.get_id();
/// ```
pub fn get_id(&self) -> String {
match self.id {
Some(sid) => sid.to_string(),
None => "none".to_string(),
match &self.session {
Some(id) => id.to_string(),
None => "".to_string(),
}
}
}
@ -82,40 +120,24 @@ mod mtt_client {
use super::*;
#[test]
fn uniques_ids() {
let mut mtt = MoreThanText::new(Vec::new());
let mut ids: Vec<Uuid> = Vec::new();
for _ in 1..10 {
mtt.open_session(None);
let id = mtt.id.clone().unwrap();
if ids.contains(&id) {
assert!(false, "{} is a duplicate id", id);
}
ids.push(id);
}
fn default_values() {
let mtt = MoreThanText::new();
assert!(mtt.session.is_none());
}
#[test]
fn existing_ids_are_reused() {
let mut mtt = MoreThanText::new(Vec::new());
fn new_session() {
let mut mtt = MoreThanText::new();
mtt.open_session(None);
let holder = mtt.id.clone().unwrap().to_string();
mtt.open_session(Some(holder.clone()));
assert_eq!(mtt.id.clone().unwrap().to_string(), holder);
assert!(mtt.session.is_some());
}
#[test]
fn bad_ids_generate_new_ones() {
let mut mtt = MoreThanText::new(Vec::new());
mtt.open_session(Some("bad test string".to_string()));
assert!(mtt.id.is_some());
}
#[test]
fn incorrect_ids_generate_new_ones() {
let mut mtt = MoreThanText::new(Vec::new());
let holder = Uuid::new_v4();
mtt.open_session(Some(holder.clone().to_string()));
assert_ne!(mtt.id, Some(holder));
fn session_ids_are_unique() {
let mut mtt = MoreThanText::new();
mtt.open_session(None);
let id1 = mtt.get_id();
mtt.open_session(None);
assert_ne!(mtt.get_id(), id1);
}
}