Completed the trip to client and back.
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
use crate::{queue::Message, utils::GenID};
|
||||
use crate::{queue::{Message, MsgType}, utils::GenID};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{
|
||||
@ -142,52 +142,94 @@ mod clientregistries {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ClientLink;
|
||||
pub struct ClientLink {
|
||||
tx: Sender<Message>,
|
||||
registry: ClientRegistry,
|
||||
}
|
||||
|
||||
impl ClientLink {
|
||||
fn new() -> Self {
|
||||
Self {}
|
||||
fn new(tx: Sender<Message>, registry: ClientRegistry) -> Self {
|
||||
Self {
|
||||
tx: tx,
|
||||
registry: registry,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn forward(&self, req: Request) -> Reply {
|
||||
Reply {}
|
||||
pub fn send(&mut self, req: Request) -> Receiver<Reply> {
|
||||
let (tx, rx) = channel();
|
||||
let mut msg = Message::new(MsgType::ClientRequest);
|
||||
let id = self.registry.add(tx);
|
||||
msg.add_data("tx_id", id);
|
||||
self.tx.send(msg).unwrap();
|
||||
rx
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod clientlinks {
|
||||
use super::*;
|
||||
use std::time::Duration;
|
||||
|
||||
static TIMEOUT: Duration = Duration::from_millis(500);
|
||||
|
||||
#[test]
|
||||
fn create_client_link() {
|
||||
ClientLink::new();
|
||||
let (tx, rx) = channel();
|
||||
let mut registry = ClientRegistry::new();
|
||||
let mut link = ClientLink::new(tx, registry.clone());
|
||||
let req = Request::new();
|
||||
let rx_client = link.send(req);
|
||||
let msg = rx.recv_timeout(TIMEOUT).unwrap();
|
||||
match msg.get_class() {
|
||||
MsgType::ClientRequest => {},
|
||||
_ => unreachable!("should have been a client request"),
|
||||
}
|
||||
match msg.get_data().get("tx_id") {
|
||||
Some(result) => {
|
||||
let id = result.to_uuid().unwrap();
|
||||
registry.send(&id, Reply {});
|
||||
rx_client.recv().unwrap();
|
||||
},
|
||||
None => unreachable!("should have had a seender id"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Client {
|
||||
registry: ClientRegistry,
|
||||
rx: Receiver<Message>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
fn new(rx: Receiver<Message>) -> Self {
|
||||
Self { rx: rx }
|
||||
Self {
|
||||
registry: ClientRegistry::new(),
|
||||
rx: rx
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start() -> ClientLink {
|
||||
let (tx, rx) = channel();
|
||||
let mut client = Client::new(rx);
|
||||
let link = ClientLink::new(tx, client.get_registry());
|
||||
spawn(move || {
|
||||
let client = Client::new(rx);
|
||||
client.listen();
|
||||
});
|
||||
ClientLink::new()
|
||||
link
|
||||
}
|
||||
|
||||
fn listen(&self) {
|
||||
fn listen(&mut self) {
|
||||
loop {
|
||||
let req = self.rx.recv().unwrap();
|
||||
//req.get_sender().send(Reply {}).unwrap();
|
||||
let msg = self.rx.recv().unwrap();
|
||||
let id = msg.get_data().get("tx_id").unwrap().to_uuid().unwrap();
|
||||
let reply = Reply {};
|
||||
self.registry.send(&id, reply);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_registry(&self) -> ClientRegistry {
|
||||
self.registry.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -197,8 +239,8 @@ mod clients {
|
||||
|
||||
#[test]
|
||||
fn start_client() {
|
||||
let link = Client::start();
|
||||
let mut link = Client::start();
|
||||
let req = create_request();
|
||||
link.forward(req);
|
||||
link.send(req);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user