Initial Magpie infrastructure

This commit is contained in:
mars 2022-10-27 18:12:12 -06:00
parent 5459cff113
commit 825f4b28c8
5 changed files with 107 additions and 0 deletions

View File

@ -1,7 +1,9 @@
[workspace]
members = [
"apps/magpie",
"apps/music-player",
"apps/sandbox",
"crates/magpie-types",
"crates/script",
"crates/types",
"scripts/music-player",

8
apps/magpie/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "magpie"
version = "0.1.0"
edition = "2021"
[dependencies]
mio = { version = "0.8", features = ["net", "os-poll"] }
mio-signals = "0.2"

89
apps/magpie/src/main.rs Normal file
View File

@ -0,0 +1,89 @@
use mio::net::UnixListener;
use mio::{Events, Interest, Poll, Token};
use mio_signals::{Signal, Signals};
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
use std::time::Duration;
const SOCK_NAME: &str = "magpie.sock";
/// Wraps [mio::net::UnixListener] with automatic file deletion on drop.
pub struct Listener {
pub uds: UnixListener,
pub path: PathBuf,
}
impl Drop for Listener {
fn drop(&mut self) {
match std::fs::remove_file(&self.path) {
Ok(_) => {}
Err(e) => eprintln!("Could not delete UnixListener {:?}", e),
}
}
}
impl Deref for Listener {
type Target = UnixListener;
fn deref(&self) -> &UnixListener {
&self.uds
}
}
impl DerefMut for Listener {
fn deref_mut(&mut self) -> &mut UnixListener {
&mut self.uds
}
}
fn main() -> std::io::Result<()> {
let sock_dir = std::env::var("XDG_RUNTIME_DIR").expect("XDG_RUNTIME_DIR not set");
let sock_dir = Path::new(&sock_dir);
let sock_path = sock_dir.join(SOCK_NAME);
eprintln!("Making socket at: {:?}", sock_path);
let mut listener = Listener {
uds: UnixListener::bind(&sock_path)?,
path: sock_path.to_path_buf(),
};
let mut signals = Signals::new(Signal::Interrupt | Signal::Quit)?;
let mut poll = Poll::new()?;
let mut events = Events::with_capacity(128);
const LISTENER: Token = Token(0);
poll.registry()
.register(&mut listener.uds, LISTENER, Interest::READABLE)?;
const SIGNALS: Token = Token(1);
poll.registry()
.register(&mut signals, SIGNALS, Interest::READABLE)?;
let mut quit = false;
while !quit {
let wait = Duration::from_millis(100);
poll.poll(&mut events, Some(wait))?;
for event in events.iter() {
match event.token() {
LISTENER => loop {
match listener.accept() {
Ok((connection, address)) => {
println!("Got a connection from: {:?}", address);
}
Err(ref err) if err.kind() == std::io::ErrorKind::WouldBlock => break,
Err(err) => return Err(err),
}
},
SIGNALS => while let Some(received) = signals.receive()? {
eprintln!("Received {:?} signal; exiting...", received);
quit = true;
}
_ => panic!("Unrecognized event token: {:?}", event),
}
}
}
Ok(())
}

View File

@ -0,0 +1,7 @@
[package]
name = "magpie-types"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }

View File

@ -0,0 +1 @@
// protocol types go here