Make swssion pass on any additional information.
This commit is contained in:
71
src/queue.rs
71
src/queue.rs
@ -18,7 +18,7 @@ pub enum MsgType {
|
||||
#[derive(Clone)]
|
||||
pub struct Message {
|
||||
id: Uuid,
|
||||
class: MsgType,
|
||||
msg_type: MsgType,
|
||||
data: HashMap<String, Field>,
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ impl Message {
|
||||
pub fn new(msg_type: MsgType) -> Self {
|
||||
Self {
|
||||
id: Uuid::new_v4(),
|
||||
class: msg_type,
|
||||
msg_type: msg_type,
|
||||
data: HashMap::new(),
|
||||
}
|
||||
}
|
||||
@ -34,13 +34,21 @@ impl Message {
|
||||
pub fn reply(&self, data: MsgType) -> Message {
|
||||
Self {
|
||||
id: self.id.clone(),
|
||||
class: data,
|
||||
msg_type: data,
|
||||
data: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_class(&self) -> &MsgType {
|
||||
&self.class
|
||||
pub fn reply_with_data(&self, msg_type: MsgType) -> Message {
|
||||
Self {
|
||||
id: self.id.clone(),
|
||||
msg_type: msg_type,
|
||||
data: self.data.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_msg_type(&self) -> &MsgType {
|
||||
&self.msg_type
|
||||
}
|
||||
|
||||
pub fn add_data<S, F>(&mut self, name: S, data: F)
|
||||
@ -51,8 +59,9 @@ impl Message {
|
||||
self.data.insert(name.into(), data.into());
|
||||
}
|
||||
|
||||
pub fn get_data(&self, name: &str) -> Option<&Field> {
|
||||
self.data.get(name)
|
||||
pub fn get_data<S>(&self, name: S) -> Option<&Field> where S: Into<String> {
|
||||
let field_name = name.into();
|
||||
self.data.get(&field_name)
|
||||
}
|
||||
|
||||
pub fn get_id(&self) -> Uuid {
|
||||
@ -79,7 +88,7 @@ mod messages {
|
||||
#[test]
|
||||
fn new_message() {
|
||||
let msg = Message::new(MsgType::SessionValidate);
|
||||
match msg.class {
|
||||
match msg.msg_type {
|
||||
MsgType::SessionValidate => (),
|
||||
_ => unreachable!("new defaults to noop"),
|
||||
}
|
||||
@ -106,7 +115,7 @@ mod messages {
|
||||
let data = MsgType::ClientRequest;
|
||||
let result = msg.reply(data);
|
||||
assert_eq!(result.id, id);
|
||||
match result.class {
|
||||
match result.msg_type {
|
||||
MsgType::ClientRequest => {}
|
||||
_ => unreachable!("should have been a registration request"),
|
||||
}
|
||||
@ -116,7 +125,7 @@ mod messages {
|
||||
#[test]
|
||||
fn get_message_type() {
|
||||
let msg = Message::new(MsgType::SessionValidate);
|
||||
match msg.get_class() {
|
||||
match msg.get_msg_type() {
|
||||
MsgType::SessionValidate => {}
|
||||
_ => unreachable!("should have bneen noopn"),
|
||||
}
|
||||
@ -133,6 +142,32 @@ mod messages {
|
||||
assert_eq!(msg.get_data(&two).unwrap().to_string(), two);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_data_into_string() {
|
||||
let id = Uuid::new_v4();
|
||||
let mut msg = Message::new(MsgType::SessionValidate);
|
||||
msg.add_data(id, id);
|
||||
assert_eq!(msg.get_data(id).unwrap().to_uuid().unwrap(), id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_data_with_reply() {
|
||||
let id = Uuid::new_v4();
|
||||
let reply_type = MsgType::Session;
|
||||
let mut msg = Message::new(MsgType::SessionValidate);
|
||||
msg.add_data(id, id);
|
||||
let reply = msg.reply_with_data(reply_type.clone());
|
||||
assert_eq!(reply.id, msg.id);
|
||||
match reply.get_msg_type() {
|
||||
MsgType::Session => {},
|
||||
_ => unreachable!("Got {:?} should have been {:?}", msg.get_msg_type(), reply_type),
|
||||
}
|
||||
assert_eq!(reply.data.len(), msg.data.len());
|
||||
let output = reply.get_data(&id.to_string()).unwrap().to_uuid().unwrap();
|
||||
let expected = msg.get_data(&id.to_string()).unwrap().to_uuid().unwrap();
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_message_id() {
|
||||
let msg = Message::new(MsgType::Session);
|
||||
@ -186,14 +221,14 @@ impl Queue {
|
||||
|
||||
pub fn send(&self, msg: Message) -> Result<(), String> {
|
||||
let store = self.store.read().unwrap();
|
||||
match store.get(&msg.get_class()) {
|
||||
match store.get(&msg.get_msg_type()) {
|
||||
Some(senders) => {
|
||||
for sender in senders.into_iter() {
|
||||
sender.send(msg.clone()).unwrap();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
None => Err(format!("no listeners for {:?}", msg.get_class())),
|
||||
None => Err(format!("no listeners for {:?}", msg.get_msg_type())),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -229,11 +264,11 @@ mod queues {
|
||||
queue.add(tx2, [MsgType::Session].to_vec());
|
||||
queue.send(Message::new(MsgType::SessionValidate)).unwrap();
|
||||
let result = rx1.recv().unwrap();
|
||||
match result.get_class() {
|
||||
match result.get_msg_type() {
|
||||
MsgType::SessionValidate => {}
|
||||
_ => unreachable!(
|
||||
"received {:?}, should have been session vvalidate",
|
||||
result.get_class()
|
||||
result.get_msg_type()
|
||||
),
|
||||
}
|
||||
match rx2.recv_timeout(TIMEOUT) {
|
||||
@ -245,11 +280,11 @@ mod queues {
|
||||
}
|
||||
queue.send(Message::new(MsgType::Session)).unwrap();
|
||||
let result = rx2.recv().unwrap();
|
||||
match result.get_class() {
|
||||
match result.get_msg_type() {
|
||||
MsgType::Session => {}
|
||||
_ => unreachable!(
|
||||
"received {:?}, should have been session vvalidate",
|
||||
result.get_class()
|
||||
result.get_msg_type()
|
||||
),
|
||||
}
|
||||
match rx1.recv_timeout(TIMEOUT) {
|
||||
@ -268,10 +303,10 @@ mod queues {
|
||||
queue.add(tx, [MsgType::Session, MsgType::SessionValidate].to_vec());
|
||||
queue.send(Message::new(MsgType::SessionValidate)).unwrap();
|
||||
let msg = rx.recv().unwrap();
|
||||
assert_eq!(msg.get_class(), &MsgType::SessionValidate);
|
||||
assert_eq!(msg.get_msg_type(), &MsgType::SessionValidate);
|
||||
queue.send(Message::new(MsgType::Session)).unwrap();
|
||||
let msg = rx.recv().unwrap();
|
||||
assert_eq!(msg.get_class(), &MsgType::Session);
|
||||
assert_eq!(msg.get_msg_type(), &MsgType::Session);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Reference in New Issue
Block a user