Implementing fifo channel, and command structure.
This commit is contained in:
18
src/commands.rs
Normal file
18
src/commands.rs
Normal file
@@ -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}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<P>(path: &P) -> Result<()>
|
||||
pub async fn start_fifo<P>(path: &P, command_tx: mpsc::Sender<Command>) -> Result<()>
|
||||
where
|
||||
P: AsRef<Path> + 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user