Created
August 12, 2017 14:26
-
-
Save superlou/cd13c9175eb354b6a92fdb810087d998 to your computer and use it in GitHub Desktop.
Getting main thread info into Rocket route
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use ::BackendMsg; | |
use std::io; | |
use std::sync::mpsc::SyncSender; | |
pub fn console_task(sender: SyncSender<BackendMsg>) { | |
let run = true; | |
while run { | |
let mut input = String::new(); | |
io::stdin().read_line(&mut input).unwrap(); | |
let input = input.trim(); | |
let command = match input { | |
"q" | "quit" => Some(BackendMsg::Quit), | |
"" => Some(BackendMsg::Take), | |
"a" => Some(BackendMsg::AutoTransition{secs: 0.25}), | |
input => { | |
match input.parse::<i32>() { | |
Ok(id) => Some(BackendMsg::SetPreview{id: id}), | |
Err(_) => None, | |
} | |
} | |
}; | |
match command { | |
Some(x) => sender.send(x).unwrap(), | |
_ => {}, | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::path::PathBuf; | |
use std::thread; | |
use std::sync::mpsc; | |
use std::sync::mpsc::SyncSender; | |
use manager::Manager; | |
use console::console_task; | |
use rocket::State; | |
pub enum BackendMsg { | |
Quit, | |
Take, | |
AutoTransition {secs: f32}, | |
SetPreview {id: i32}, | |
SetProgram {id: i32}, | |
} | |
pub enum BackendResponse { | |
OK | |
} | |
#[get("/")] | |
fn index(sender: State<SyncSender<BackendMsg>>) -> &'static str { | |
sender.send(BackendMsg::Take).unwrap(); | |
"Here" | |
} | |
fn main() { | |
let mut manager = Manager::new("127.0.0.1:9999"); | |
manager.start(); | |
let (sender, receiver) = mpsc::sync_channel::<BackendMsg>(256); | |
let sender2 = sender.clone(); | |
thread::spawn(move || { | |
rocket::ignite().manage(sender) | |
.mount("/", routes![index]) | |
.launch(); | |
}); | |
thread::spawn(move || { | |
console_task(sender2); | |
}); | |
let mut run = true; | |
while run { | |
let response = receiver.recv().unwrap(); | |
match response { | |
BackendMsg::Quit => run = false, | |
BackendMsg::Take => manager.take(), | |
BackendMsg::AutoTransition{secs: x} => manager.transition(x), | |
_ => {}, | |
}; | |
} | |
manager.quit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment