Fix the watch mask

This commit is contained in:
Michael Zhang 2021-02-20 19:13:10 -06:00
parent 8b7888d985
commit f6de40251b
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
2 changed files with 22 additions and 17 deletions

View file

@ -19,6 +19,7 @@ use crate::command::Command;
use crate::response::Response; use crate::response::Response;
pub type BoxedFunc = Box<dyn Fn()>; pub type BoxedFunc = Box<dyn Fn()>;
pub const TAG_PREFIX: &str = "panorama";
/// The private Client struct, that is shared by all of the exported structs in the state machine. /// The private Client struct, that is shared by all of the exported structs in the state machine.
pub struct Client<C> { pub struct Client<C> {
@ -28,7 +29,6 @@ pub struct Client<C> {
id: usize, id: usize,
results: ResultMap, results: ResultMap,
/// Cached capabilities that shouldn't change between
caps: Vec<StringEntry>, caps: Vec<StringEntry>,
handle: JoinHandle<Result<()>>, handle: JoinHandle<Result<()>>,
} }
@ -63,7 +63,7 @@ where
handlers.insert(id, (None, None)); handlers.insert(id, (None, None));
} }
let cmd_str = format!("pano{} {}\n", id, cmd); let cmd_str = format!("{}{} {}\r\n", TAG_PREFIX, id, cmd);
debug!("[{}] writing to socket: {:?}", id, cmd_str); debug!("[{}] writing to socket: {:?}", id, cmd_str);
self.conn.write_all(cmd_str.as_bytes()).await?; self.conn.write_all(cmd_str.as_bytes()).await?;
debug!("[{}] written.", id); debug!("[{}] written.", id);
@ -92,7 +92,7 @@ impl<'a, C> Future for ExecHandle<'a, C> {
type Output = (); type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let mut handlers = self.0.results.write(); let mut handlers = self.0.results.write();
let mut state = handlers.get_mut(&self.1); let state = handlers.get_mut(&self.1);
// TODO: handle the None case here // TODO: handle the None case here
debug!("f[{}] {:?}", self.1, state); debug!("f[{}] {:?}", self.1, state);
@ -117,18 +117,21 @@ async fn listen(conn: impl AsyncRead + Unpin, results: ResultMap) -> Result<()>
loop { loop {
let mut next_line = String::new(); let mut next_line = String::new();
reader.read_line(&mut next_line).await?; reader.read_line(&mut next_line).await?;
let next_line = next_line.trim_end_matches('\r');
// debug!("line: {:?}", next_line); // debug!("line: {:?}", next_line);
let parts = next_line.split(" ").collect::<Vec<_>>(); let mut parts = next_line.split(" ");
let tag = parts[0]; let tag = parts.next().unwrap();
let rest = parts.collect::<Vec<_>>().join(" ");
if tag == "*" { if tag == "*" {
debug!("UNTAGGED {:?}", next_line); debug!("UNTAGGED {:?}", rest);
} else if tag.starts_with("pano") { } else if tag.starts_with(TAG_PREFIX) {
let id = tag.trim_start_matches("pano").parse::<usize>()?; let id = tag.trim_start_matches(TAG_PREFIX).parse::<usize>()?;
debug!("set {} to {:?}", id, next_line); debug!("set {} to {:?}", id, rest);
let mut results = results.write(); let mut results = results.write();
if let Some((c, w)) = results.get_mut(&id) { if let Some((c, w)) = results.get_mut(&id) {
*c = Some(next_line); *c = Some(rest.to_string());
let w = w.take().unwrap(); let w = w.take().unwrap();
w.wake(); w.wake();
} }

View file

@ -96,8 +96,10 @@ async fn start_inotify_stream(
config_tx.send(config)?; config_tx.send(config)?;
} }
debug!("listening for inotify events");
while let Some(v) = event_stream.next().await { while let Some(v) = event_stream.next().await {
let event = v.context("event")?; let event = v.context("event")?;
debug!("inotify event: {:?}", event);
if let Some(name) = event.name { if let Some(name) = event.name {
let path = PathBuf::from(name); let path = PathBuf::from(name);
@ -139,15 +141,15 @@ pub fn spawn_config_watcher_system() -> Result<(JoinHandle<()>, ConfigWatcher)>
fs::create_dir_all(&config_home)?; fs::create_dir_all(&config_home)?;
} }
inotify inotify
.add_watch(&config_home, WatchMask::all()) .add_watch(&config_home, WatchMask::CLOSE_WRITE)
.context("adding watch for config home")?; .context("adding watch for config home")?;
let config_file_path = config_home.join("panorama.toml"); // let config_file_path = config_home.join("panorama.toml");
if config_file_path.exists() { // if config_file_path.exists() {
inotify // inotify
.add_watch(config_file_path, WatchMask::CLOSE) // .add_watch(config_file_path, WatchMask::ALL_EVENTS)
.context("adding watch for config file")?; // .context("adding watch for config file")?;
} // }
debug!("watching {:?}", config_home); debug!("watching {:?}", config_home);
let (config_tx, config_update) = watch::channel(Config::default()); let (config_tx, config_update) = watch::channel(Config::default());