Clean up fifo command sending shit.
This commit is contained in:
parent
ba740808f1
commit
5fd205a1fb
3 changed files with 50 additions and 27 deletions
|
@ -4,7 +4,7 @@ use deerwm::commands;
|
|||
use nanoserde::SerBin;
|
||||
|
||||
fn main() {
|
||||
let mut cmd_fifo = unix_named_pipe::open_write(commands::DEFAULT_PIPE)
|
||||
let mut cmd_fifo = unix_named_pipe::open_write(commands::DEFAULT_FIFO)
|
||||
.expect("failed to open fifo for writing");
|
||||
|
||||
let quit_cmd = commands::Command::Quit;
|
||||
|
|
|
@ -5,32 +5,14 @@ use std::io::Read;
|
|||
use deerwm::commands;
|
||||
|
||||
fn main() {
|
||||
if let Err(e) = unix_named_pipe::create(commands::DEFAULT_PIPE, None) {
|
||||
match e.kind() {
|
||||
std::io::ErrorKind::AlreadyExists => {}
|
||||
_ => panic!("{}", e),
|
||||
}
|
||||
}
|
||||
let mut cmd_fifo =
|
||||
unix_named_pipe::open_read(commands::DEFAULT_PIPE).expect("failed to open fifo");
|
||||
let mut cmd_buffer = [0; 256];
|
||||
let cmd_rx = commands::command_reciever(commands::DEFAULT_FIFO);
|
||||
|
||||
'run: loop {
|
||||
if let Ok(n) = cmd_fifo.read(&mut cmd_buffer) {
|
||||
if n > 0 {
|
||||
println!("{:?}", &cmd_buffer[..n]);
|
||||
|
||||
if let Ok(cmd) = Command::de_bin(&mut 0, &cmd_buffer[..n]) {
|
||||
match cmd {
|
||||
Command::Quit => {
|
||||
println!("Recieved quit command!");
|
||||
break 'run;
|
||||
},
|
||||
}
|
||||
}
|
||||
while let Ok(cmd) = cmd_rx.try_recv() {
|
||||
match cmd {
|
||||
Command::Quit => break 'run,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::fs::remove_file(commands::DEFAULT_PIPE).expect("failed to create fifo");
|
||||
}
|
||||
|
|
|
@ -1,8 +1,49 @@
|
|||
use nanoserde::{DeBin, SerBin};
|
||||
use std::{io::Read, path::Path, sync::mpsc};
|
||||
|
||||
pub const DEFAULT_PIPE: &str = "/tmp/deerwm";
|
||||
pub const DEFAULT_FIFO: &str = "/tmp/deerwm";
|
||||
|
||||
#[derive(Clone, Debug, DeBin, SerBin)]
|
||||
#[derive(PartialEq, Clone, Debug, DeBin, SerBin)]
|
||||
pub enum Command {
|
||||
Quit
|
||||
Nothing,
|
||||
Quit,
|
||||
}
|
||||
|
||||
/// Starts a thread that recieves commands from the fifo specified.
|
||||
pub fn command_reciever(fifo_path: &str) -> mpsc::Receiver<Command> {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
// Open FIFO to listen for commands on
|
||||
if Path::new(fifo_path).exists() {
|
||||
std::fs::remove_file(fifo_path).unwrap();
|
||||
}
|
||||
unix_named_pipe::create(fifo_path, None).expect("failed to create fifo");
|
||||
let mut cmd_fifo =
|
||||
unix_named_pipe::open_read(fifo_path).expect("failed to open fifo for reading");
|
||||
|
||||
// Spawn the recieving thread
|
||||
std::thread::spawn(move || {
|
||||
let mut cmd_buf = [0; 256];
|
||||
let mut quit = false;
|
||||
while !quit {
|
||||
// Read data from fifo
|
||||
if let Ok(n) = cmd_fifo.read(&mut cmd_buf) {
|
||||
// Do not bother trying to parse if there is no data
|
||||
if n > 0 {
|
||||
// Try to parse bytes into `Command`
|
||||
// TODO: try to handle the case of more than one command being in the buffer???
|
||||
if let Ok(cmd) = Command::de_bin(&mut 0, &cmd_buf[..n]) {
|
||||
println!("Received: {:?}", cmd);
|
||||
if cmd == Command::Quit {
|
||||
quit = true;
|
||||
}
|
||||
|
||||
tx.send(cmd).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
rx
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue