Added backend.
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				MoreThanText/morethantext/pipeline/head This commit looks good
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	MoreThanText/morethantext/pipeline/head This commit looks good
				
			This commit is contained in:
		
							
								
								
									
										50
									
								
								src/backend.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								src/backend.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
			
		||||
use crate::queue::Message;
 | 
			
		||||
use std::{
 | 
			
		||||
    sync::mpsc::{channel, Receiver, Sender},
 | 
			
		||||
    thread::spawn,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct BackEnd {
 | 
			
		||||
    tx: Sender<Message>,
 | 
			
		||||
    rx: Receiver<Message>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl BackEnd {
 | 
			
		||||
    fn new(tx: Sender<Message>, rx: Receiver<Message>) -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            tx: tx,
 | 
			
		||||
            rx: rx,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn start(queue_tx: Sender<Message>) {
 | 
			
		||||
        spawn(move || {
 | 
			
		||||
            let (tx, rx) = channel();
 | 
			
		||||
            let service = Self::new(queue_tx.clone(), rx);
 | 
			
		||||
            queue_tx.send(tx.clone().into()).unwrap();
 | 
			
		||||
            service.listen();
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn listen(&self) {
 | 
			
		||||
        loop {
 | 
			
		||||
            self.rx.recv().unwrap();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod backends {
 | 
			
		||||
    use std::time::Duration;
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn get_registered() {
 | 
			
		||||
        let (tx, rx) = channel();
 | 
			
		||||
        BackEnd::start(tx);
 | 
			
		||||
        match rx.recv_timeout(Duration::from_millis(500)).unwrap() {
 | 
			
		||||
            Message::Register(_) => {},
 | 
			
		||||
            _ => unreachable!("should register the service"),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,4 @@
 | 
			
		||||
use crate::{
 | 
			
		||||
    Field,
 | 
			
		||||
    Request,
 | 
			
		||||
    queue::Message,
 | 
			
		||||
    session2::SessionMessage,
 | 
			
		||||
};
 | 
			
		||||
use crate::{queue::Message, session2::SessionMessage, Field, Request};
 | 
			
		||||
use std::{
 | 
			
		||||
    sync::mpsc::{channel, Receiver, Sender},
 | 
			
		||||
    thread::spawn,
 | 
			
		||||
@@ -16,10 +11,7 @@ struct FrontEnd {
 | 
			
		||||
 | 
			
		||||
impl FrontEnd {
 | 
			
		||||
    fn new(tx: Sender<Message>, rx: Receiver<Message>) -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            tx: tx,
 | 
			
		||||
            rx: rx,
 | 
			
		||||
        }
 | 
			
		||||
        Self { tx: tx, rx: rx }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn start(queue_tx: Sender<Message>) {
 | 
			
		||||
@@ -43,9 +35,11 @@ impl FrontEnd {
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod frontends {
 | 
			
		||||
    use crate::create_request::empty_request;
 | 
			
		||||
    use std::time::Duration;
 | 
			
		||||
    use super::*;
 | 
			
		||||
    use crate::{
 | 
			
		||||
        create_request::empty_request, session2::create_session_message::create_session_new,
 | 
			
		||||
    };
 | 
			
		||||
    use std::time::Duration;
 | 
			
		||||
 | 
			
		||||
    fn run_service() -> (Sender<Message>, Receiver<Message>) {
 | 
			
		||||
        let (tx, rx) = channel();
 | 
			
		||||
@@ -63,8 +57,7 @@ mod frontends {
 | 
			
		||||
        let (tx, rx) = channel();
 | 
			
		||||
        FrontEnd::start(tx);
 | 
			
		||||
        match rx.recv().unwrap() {
 | 
			
		||||
            Message::Register(_) => {
 | 
			
		||||
            },
 | 
			
		||||
            Message::Register(_) => {}
 | 
			
		||||
            _ => unreachable!("should register the service"),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -75,13 +68,14 @@ mod frontends {
 | 
			
		||||
        let (req, _) = empty_request();
 | 
			
		||||
        tx.send(req.into()).unwrap();
 | 
			
		||||
        match rx.recv_timeout(Duration::from_millis(500)).unwrap() {
 | 
			
		||||
            Message::SessMsg(output) => {
 | 
			
		||||
                match output {
 | 
			
		||||
                    SessionMessage::Validate(result) => assert!(result.is_none()),
 | 
			
		||||
                    _ => unreachable!("Should have sent a val9idate"),
 | 
			
		||||
                }
 | 
			
		||||
            Message::SessMsg(output) => match output {
 | 
			
		||||
                SessionMessage::Validate(result) => assert!(result.is_none()),
 | 
			
		||||
                _ => unreachable!("Should have sent a val9idate"),
 | 
			
		||||
            },
 | 
			
		||||
            _ => unreachable!("Should have generated a session message"),
 | 
			
		||||
        }
 | 
			
		||||
        let (new_sess, sess_id) = create_session_new();
 | 
			
		||||
        tx.send(new_sess.into()).unwrap();
 | 
			
		||||
        // generate backend request.
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,4 @@
 | 
			
		||||
mod backend;
 | 
			
		||||
mod client;
 | 
			
		||||
mod data;
 | 
			
		||||
mod error;
 | 
			
		||||
@@ -468,7 +469,7 @@ impl MoreThanText {
 | 
			
		||||
         * tx = Queue::start();
 | 
			
		||||
         * tx.send(Session::start().into()).unwrap();
 | 
			
		||||
         * Self { tx: tx }
 | 
			
		||||
        */
 | 
			
		||||
         */
 | 
			
		||||
        let (tx, rx) = channel();
 | 
			
		||||
        let mut senders = Vec::new();
 | 
			
		||||
        senders.push(Client::start(tx.clone()));
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										18
									
								
								src/queue.rs
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								src/queue.rs
									
									
									
									
									
								
							@@ -1,7 +1,4 @@
 | 
			
		||||
use crate::{
 | 
			
		||||
    Request,
 | 
			
		||||
    session2::SessionMessage,
 | 
			
		||||
};
 | 
			
		||||
use crate::{session2::SessionMessage, Request};
 | 
			
		||||
use std::{
 | 
			
		||||
    sync::mpsc::{channel, Receiver, Sender},
 | 
			
		||||
    thread::spawn,
 | 
			
		||||
@@ -35,10 +32,7 @@ impl From<SessionMessage> for Message {
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod messages {
 | 
			
		||||
    use super::*;
 | 
			
		||||
    use crate::{
 | 
			
		||||
        create_request::empty_request,
 | 
			
		||||
        Field,
 | 
			
		||||
    };
 | 
			
		||||
    use crate::{create_request::empty_request, Field};
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn from_request() {
 | 
			
		||||
@@ -70,11 +64,9 @@ mod messages {
 | 
			
		||||
        let id: Option<Field> = None;
 | 
			
		||||
        let sess_msg: SessionMessage = id.into();
 | 
			
		||||
        match sess_msg.into() {
 | 
			
		||||
            Message::SessMsg(result) => {
 | 
			
		||||
                match result {
 | 
			
		||||
                    SessionMessage::Validate(data) => assert!(data.is_none()),
 | 
			
		||||
                    _ => unreachable!("should have been a validate"),
 | 
			
		||||
                }
 | 
			
		||||
            Message::SessMsg(result) => match result {
 | 
			
		||||
                SessionMessage::Validate(data) => assert!(data.is_none()),
 | 
			
		||||
                _ => unreachable!("should have been a validate"),
 | 
			
		||||
            },
 | 
			
		||||
            _ => unreachable!("should have been a session message"),
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,4 @@
 | 
			
		||||
use crate::{
 | 
			
		||||
    Field,
 | 
			
		||||
    queue::Message,
 | 
			
		||||
};
 | 
			
		||||
use crate::{queue::Message, Field};
 | 
			
		||||
use std::{
 | 
			
		||||
    sync::mpsc::{channel, Receiver, Sender},
 | 
			
		||||
    thread::spawn,
 | 
			
		||||
@@ -10,7 +7,7 @@ use uuid::Uuid;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone)]
 | 
			
		||||
pub enum SessionMessage {
 | 
			
		||||
    New(Field), 
 | 
			
		||||
    New(Field),
 | 
			
		||||
    Validate(Option<Field>),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -21,11 +18,12 @@ impl From<Option<Field>> for SessionMessage {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
pub mod crete_session_message {
 | 
			
		||||
pub mod create_session_message {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    fn create_session_new() -> SessionMessage {
 | 
			
		||||
        SessionMessage::New(Uuid::new_v4().into())
 | 
			
		||||
    pub fn create_session_new() -> (SessionMessage, Uuid) {
 | 
			
		||||
        let id = Uuid::new_v4();
 | 
			
		||||
        (SessionMessage::New(id.into()), id)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -38,16 +36,12 @@ mod sessionmessages {
 | 
			
		||||
        let text = "afield";
 | 
			
		||||
        let id = Some(text.into());
 | 
			
		||||
        match id.into() {
 | 
			
		||||
            SessionMessage::Validate(result) => {
 | 
			
		||||
                match result {
 | 
			
		||||
                    Some(data) => {
 | 
			
		||||
                        match data {
 | 
			
		||||
                            Field::Static(output) => assert_eq!(output, text),
 | 
			
		||||
                            _ => unreachable!("should have returned static text"),
 | 
			
		||||
                        }
 | 
			
		||||
                    },
 | 
			
		||||
                    None => unreachable!("shoulf have returned data"),
 | 
			
		||||
                }
 | 
			
		||||
            SessionMessage::Validate(result) => match result {
 | 
			
		||||
                Some(data) => match data {
 | 
			
		||||
                    Field::Static(output) => assert_eq!(output, text),
 | 
			
		||||
                    _ => unreachable!("should have returned static text"),
 | 
			
		||||
                },
 | 
			
		||||
                None => unreachable!("shoulf have returned data"),
 | 
			
		||||
            },
 | 
			
		||||
            _ => unreachable!("should have been a vaqlidate"),
 | 
			
		||||
        }
 | 
			
		||||
@@ -61,10 +55,7 @@ struct Session {
 | 
			
		||||
 | 
			
		||||
impl Session {
 | 
			
		||||
    fn new(tx: Sender<Message>, rx: Receiver<Message>) -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            tx: tx,
 | 
			
		||||
            rx: rx,
 | 
			
		||||
        }
 | 
			
		||||
        Self { tx: tx, rx: rx }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn start(queue_tx: Sender<Message>) {
 | 
			
		||||
@@ -87,15 +78,15 @@ impl Session {
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod sessions {
 | 
			
		||||
    use std::time::Duration;
 | 
			
		||||
    use super::*;
 | 
			
		||||
    use std::time::Duration;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn gets_registered() {
 | 
			
		||||
        let (tx, rx) = channel();
 | 
			
		||||
        Session::start(tx);
 | 
			
		||||
        match rx.recv().unwrap() {
 | 
			
		||||
            Message::Register(_) => {},
 | 
			
		||||
            Message::Register(_) => {}
 | 
			
		||||
            _ => unreachable!("should register the service"),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -113,11 +104,9 @@ mod sessions {
 | 
			
		||||
        let req: SessionMessage = data.into();
 | 
			
		||||
        sender.send(req.into()).unwrap();
 | 
			
		||||
        match rx.recv_timeout(Duration::from_millis(500)).unwrap() {
 | 
			
		||||
            Message::SessMsg(data) => {
 | 
			
		||||
                match data {
 | 
			
		||||
                    SessionMessage::New(_) => {},
 | 
			
		||||
                    _ => unreachable!("should have been a new session"),
 | 
			
		||||
                }
 | 
			
		||||
            Message::SessMsg(data) => match data {
 | 
			
		||||
                SessionMessage::New(_) => {}
 | 
			
		||||
                _ => unreachable!("should have been a new session"),
 | 
			
		||||
            },
 | 
			
		||||
            _ => unreachable!("should have been a session message response"),
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user