diff --git a/Makefile b/Makefile index a45ec9f..6383dc1 100644 --- a/Makefile +++ b/Makefile @@ -2,11 +2,13 @@ INSTALL_DIR = $(HOME)/.local/bin +export PATH := target/debug:$(PATH) + build: @cargo build run: build - @PATH=$(PWD)/target/debug:$(PATH) ./ts-control + ./ts-control install: mkdir -p "$(INSTALL_DIR)" diff --git a/src/command_utils/events.rs b/src/command_utils/events.rs index 6689a3f..9e6f35e 100644 --- a/src/command_utils/events.rs +++ b/src/command_utils/events.rs @@ -27,9 +27,19 @@ pub fn handle_event_response(connection: &mut Telnet, event: Event, known_client } } -pub fn loop_response_reader(connection: &mut Telnet, self_clid: i32) { +fn try_get_self_clid(connection: &mut Telnet) -> Option { + wrappers::get_self_clid(connection) + .unwrap_or_default() + .parse() + .map(Some) + .unwrap_or(None) +} + +pub fn loop_response_reader(connection: &mut Telnet) { let mut known_clients = wrappers::get_clients(connection).unwrap_or_else(|_| Vec::new()); - + + let mut self_clid: Option = try_get_self_clid(connection); + loop { match commands::read_response(connection, true, String::new()) { Ok(response) => { @@ -37,7 +47,10 @@ pub fn loop_response_reader(connection: &mut Telnet, self_clid: i32) { let event = Event::new(connection, event_type, params, &known_clients); if let Some(client) = &event.client { - if client.clid == self_clid { + if self_clid.is_none() { + self_clid = try_get_self_clid(connection); + } + if Some(client.clid) == self_clid { continue; } } diff --git a/src/main.rs b/src/main.rs index 5ada48b..ac3fdf4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,8 @@ use std::io::{Error, ErrorKind}; -use std::num::ParseIntError; use telnet::Telnet; use crate::cli::Commands; -use crate::models::Channel; -use crate::models::Client; mod wrappers; mod commands; @@ -16,14 +13,10 @@ mod models; mod response; mod command_utils; -fn channel_or_error(channel_res: Result, String>) -> Result { - channel_res.map_err(|err| make_action_error("find channel", err))? - .ok_or_else(|| make_action_error("find channel", String::from("Not Found."))) -} - -fn client_or_error(client_res: Result, String>) -> Result { - client_res.map_err(|err| make_action_error("find client", err))? - .ok_or_else(|| make_action_error("find client", String::from("Not Found."))) +fn result_or_error(res: Result, String>, result_type: &str) -> Result { + let error_action = format!("find {}", result_type); + res.map_err(|err| make_action_error(&error_action, err))? + .ok_or_else(|| make_action_error(&error_action, String::from("Not Found."))) } fn connect() -> Result { @@ -89,7 +82,7 @@ fn main() -> Result<(), Error> { } if args.want_client() { - let client = client_or_error(args.client(&mut connection))?; + let client = result_or_error(args.client(&mut connection), "client")?; match wrappers::fetch_client(&mut connection, &[client]) { Ok(_) => println!("Successfully fetched client."), @@ -100,7 +93,7 @@ fn main() -> Result<(), Error> { } if args.want_channel() { - let channel = channel_or_error(args.channel(&mut connection))?; + let channel = result_or_error(args.channel(&mut connection), "channel")?; match wrappers::fetch_channel(&mut connection, channel) { Ok(_) => println!("Successfully fetched channel."), @@ -124,8 +117,8 @@ fn main() -> Result<(), Error> { } Commands::Move(args) => { - let channel = channel_or_error(args.channel(&mut connection))?; - let client = client_or_error(args.client(&mut connection))?; + let channel = result_or_error(args.channel(&mut connection), "channel")?; + let client = result_or_error(args.client(&mut connection), "client")?; match wrappers::move_client(&mut connection, &channel, &[client]) { Ok(resp) => println!("Successfully moved client: {}", resp), @@ -149,12 +142,7 @@ fn main() -> Result<(), Error> { command_utils::events::register_events(&mut connection, args.event.clone()) .map_err(to_other_error)?; - let self_clid: i32 = wrappers::get_self_clid(&mut connection) - .map_err(|msg| make_action_error("get self clid", msg))? - .parse() - .map_err(|err: ParseIntError| make_action_error("parse clid", err.to_string()))?; - - command_utils::events::loop_response_reader(&mut connection, self_clid); + command_utils::events::loop_response_reader(&mut connection); // loop_response_reader failed. Let's try to reconnect after 1 second. std::thread::sleep(std::time::Duration::from_secs(1));