From d2d31b2bc16b6d347f49a60a20290aa6880702ae Mon Sep 17 00:00:00 2001 From: LailaTheElf Date: Sat, 1 Feb 2025 14:03:02 +0100 Subject: [PATCH] when the tool exits, exit the application --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 43 +++++++++++++++++++++++++------------------ 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index feb7353..826ae32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,7 +246,7 @@ dependencies = [ [[package]] name = "mqtt-client" -version = "2.0.1" +version = "2.0.2" dependencies = [ "crossbeam", "rumqttc", diff --git a/Cargo.toml b/Cargo.toml index 7a472ad..575587e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mqtt-client" -version = "2.0.1" +version = "2.0.2" edition = "2021" [dependencies] diff --git a/src/lib.rs b/src/lib.rs index dcfa189..17ed006 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,8 @@ pub mod mqtt_client { let (tx_sender, tx_recever) = unbounded::(); let (rx_sender, rx_recever) = unbounded::(); + println!("INFO : mqtt client: run"); + let mut mqttoptions = MqttOptions::new(client, host, port); mqttoptions.set_keep_alive(Duration::from_secs(5)); mqttoptions.set_credentials(user, pass); @@ -39,19 +41,7 @@ pub mod mqtt_client { publisher(tx_recever, client_publisher); }); match publisher { - Err(_n) => println!("ERROR: main: failed to create publisher thread"), - Ok(_n) => {} - } - - // thread tool runner - let tool_runner = thread::Builder::new() - .name("tool runner".to_string()) - .spawn(move || { - let mut tool = T::new(client, tx_sender); - tool.run(rx_recever); - }); - match tool_runner { - Err(_n) => println!("ERROR: main: failed to create publisher thread"), + Err(_n) => println!("ERROR: mqtt client: failed to create publisher thread"), Ok(_n) => {} } @@ -62,19 +52,37 @@ pub mod mqtt_client { handeler::(connection, rx_sender); }); match mqtt { - Err(_n) => println!("ERROR: main: failed to create mqtt thread"), - Ok(join) => match join.join() { - Err(_) => println!("ERROR: main: failed to join mqtt thread"), + Err(_n) => println!("ERROR: mqtt client: failed to create mqtt thread"), + Ok(_) => {} + } + + // thread tool runner + let tool_runner = thread::Builder::new() + .name("tool runner".to_string()) + .spawn(move || { + let mut tool = T::new(client, tx_sender); + tool.run(rx_recever); + println!("WARN : rool_runner: tool has ended"); + }); + match tool_runner { + Err(_n) => println!("ERROR: mqtt client: failed to create publisher thread"), + Ok(runner) => match runner.join() { + Err(_) => println!("ERROR: mqtt client: failed to join tool runner thread"), Ok(_) => {} } } + println!("INFO : mqtt client: exit"); } pub(self) fn publisher(rx: Receiver, client: rumqttc::Client) { loop { let message = rx.recv(); match message { - Err(e) => println!("ERROR: publisher: failed to receve an message ({})", e), + Err(e) => { + println!("WARN : publisher: {}", e); + println!("INFO : publisher: exit, no one can send messages"); + return + }, Ok(msg) => { println!("INFO : publisher: topic={}; payload={}", msg.topic, msg.payload); match client.publish(msg.topic, msg.qos, msg.retain, msg.payload) { @@ -123,7 +131,6 @@ pub mod mqtt_client { Ok(event) => { match event { Event::Outgoing(n) => match n { - Outgoing::Publish(_n) => {}, //println!("INFO : mqtt_recive: out Publish"), Outgoing::Subscribe(_n) => {}, //println!("INFO : mqtt_recive: out Subscribe"), Outgoing::Unsubscribe(_n) => {}, //println!("INFO : mqtt_recive: out Unsubscribe"),