From 8ec4f2860cdb3b37f64d63e1fc58ff9d69cdc0a0 Mon Sep 17 00:00:00 2001 From: Micheal Smith Date: Thu, 13 Nov 2025 04:35:38 -0600 Subject: [PATCH] Implementing fifo channel, and command structure. --- src/commands.rs | 18 ++++++++++++++++++ src/event_manager.rs | 15 ++++++++------- src/lib.rs | 1 + 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 src/commands.rs diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..5935fcc --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,18 @@ +use std::fmt::Display; + +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub enum Command { + SendMessage { channel: String, message: String }, +} + +impl Display for Command { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::SendMessage { channel, message } => { + write!(f, "[{channel}]: {message}") + } + } + } +} diff --git a/src/event_manager.rs b/src/event_manager.rs index 6a6dfcd..bfe0b1f 100644 --- a/src/event_manager.rs +++ b/src/event_manager.rs @@ -4,14 +4,12 @@ use color_eyre::Result; use nix::{NixPath, sys::stat, unistd::mkfifo}; use tokio::{ io::{AsyncBufReadExt, AsyncWriteExt, BufReader}, - net::{ - unix::pipe, UnixListener, UnixStream - }, - sync::{broadcast, RwLock}, + net::{UnixListener, UnixStream, unix::pipe}, + sync::{RwLock, broadcast, mpsc}, }; use tracing::{error, info}; -use crate::event::Event; +use crate::{commands::Command, event::Event}; // Hard coding for now. Maybe make this a parameter to new. const EVENT_BUF_MAX: usize = 1000; @@ -50,7 +48,7 @@ impl EventManager { } // NB: This assumes it has exclusive control of the FIFO. - pub async fn start_fifo

(path: &P) -> Result<()> + pub async fn start_fifo

(path: &P, command_tx: mpsc::Sender) -> Result<()> where P: AsRef + NixPath + ?Sized, { @@ -60,12 +58,15 @@ impl EventManager { 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. + let cmd: Command = serde_json::from_str(&line)?; + info!("Command received: {:?}.", cmd); + command_tx.send(cmd).await?; line.clear(); } } diff --git a/src/lib.rs b/src/lib.rs index 650af16..ac72eaf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ use tracing::{Level, info}; use tracing_subscriber::FmtSubscriber; pub mod chat; +pub mod commands; pub mod event; pub mod event_manager; pub mod ipc;