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 nix::{NixPath, sys::stat, unistd::mkfifo};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
|
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
|
||||||
net::{
|
net::{UnixListener, UnixStream, unix::pipe},
|
||||||
unix::pipe, UnixListener, UnixStream
|
sync::{RwLock, broadcast, mpsc},
|
||||||
},
|
|
||||||
sync::{broadcast, RwLock},
|
|
||||||
};
|
};
|
||||||
use tracing::{error, info};
|
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.
|
// Hard coding for now. Maybe make this a parameter to new.
|
||||||
const EVENT_BUF_MAX: usize = 1000;
|
const EVENT_BUF_MAX: usize = 1000;
|
||||||
@@ -50,7 +48,7 @@ impl EventManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NB: This assumes it has exclusive control of the FIFO.
|
// 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
|
where
|
||||||
P: AsRef<Path> + NixPath + ?Sized,
|
P: AsRef<Path> + NixPath + ?Sized,
|
||||||
{
|
{
|
||||||
@@ -66,6 +64,9 @@ impl EventManager {
|
|||||||
|
|
||||||
while reader.read_line(&mut line).await? > 0 {
|
while reader.read_line(&mut line).await? > 0 {
|
||||||
// Now handle the command.
|
// Now handle the command.
|
||||||
|
let cmd: Command = serde_json::from_str(&line)?;
|
||||||
|
info!("Command received: {:?}.", cmd);
|
||||||
|
command_tx.send(cmd).await?;
|
||||||
line.clear();
|
line.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use tracing::{Level, info};
|
|||||||
use tracing_subscriber::FmtSubscriber;
|
use tracing_subscriber::FmtSubscriber;
|
||||||
|
|
||||||
pub mod chat;
|
pub mod chat;
|
||||||
|
pub mod commands;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod event_manager;
|
pub mod event_manager;
|
||||||
pub mod ipc;
|
pub mod ipc;
|
||||||
|
|||||||
Reference in New Issue
Block a user