Adding response FIFO.

This commit is contained in:
Micheal Smith
2025-11-12 05:56:07 -06:00
committed by Micheal Smith
parent f880795b44
commit 21d9c3f002
3 changed files with 47 additions and 5 deletions

13
Cargo.lock generated
View File

@@ -1401,6 +1401,18 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "nix"
version = "0.30.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6"
dependencies = [
"bitflags",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]] [[package]]
name = "nom" name = "nom"
version = "7.1.3" version = "7.1.3"
@@ -1973,6 +1985,7 @@ dependencies = [
"genai", "genai",
"human-panic", "human-panic",
"irc", "irc",
"nix",
"rstest", "rstest",
"serde", "serde",
"serde_json", "serde_json",

View File

@@ -15,6 +15,10 @@ serde_json = "1.0"
tracing = "0.1" tracing = "0.1"
tracing-subscriber = "0.3" tracing-subscriber = "0.3"
[dependencies.nix]
version = "0.30.1"
features = [ "fs" ]
[dependencies.clap] [dependencies.clap]
version = "4.5" version = "4.5"
features = [ "derive" ] features = [ "derive" ]

View File

@@ -1,10 +1,13 @@
use std::{collections::VecDeque, path::Path, sync::Arc}; use std::{collections::VecDeque, path::Path, sync::Arc};
use color_eyre::Result; use color_eyre::Result;
use nix::{NixPath, sys::stat, unistd::mkfifo};
use tokio::{ use tokio::{
io::AsyncWriteExt, io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
net::{UnixListener, UnixStream}, net::{
sync::{RwLock, broadcast}, unix::pipe, UnixListener, UnixStream
},
sync::{broadcast, RwLock},
}; };
use tracing::{error, info}; use tracing::{error, info};
@@ -46,8 +49,30 @@ impl EventManager {
Ok(()) Ok(())
} }
pub async fn start_listening(self: Arc<Self>, path: impl AsRef<Path>) { // NB: This assumes it has exclusive control of the FIFO.
let listener = UnixListener::bind(path).unwrap(); pub async fn start_fifo<P>(path: &P) -> Result<()>
where
P: AsRef<Path> + NixPath + ?Sized,
{
// Overwrite, or create the FIFO.
let _ = std::fs::remove_file(path);
mkfifo(path, stat::Mode::S_IRWXU)?;
loop {
let rx = pipe::OpenOptions::new().open_receiver(path)?;
let mut reader = BufReader::new(rx);
let mut line = String::new();
while reader.read_line(&mut line).await? > 0 {
// Now handle the command.
line.clear();
}
}
}
pub async fn start_listening(self: Arc<Self>, broadcast_path: impl AsRef<Path>) {
let listener = UnixListener::bind(broadcast_path).unwrap();
loop { loop {
match listener.accept().await { match listener.accept().await {