when the tool exits, exit the application

This commit is contained in:
Laila van Reenen 2025-02-01 14:03:02 +01:00
parent e7a46b82a5
commit d2d31b2bc1
Signed by: LailaTheElf
GPG Key ID: 8A3EF0226518C12D
3 changed files with 27 additions and 20 deletions

2
Cargo.lock generated
View File

@ -246,7 +246,7 @@ dependencies = [
[[package]]
name = "mqtt-client"
version = "2.0.1"
version = "2.0.2"
dependencies = [
"crossbeam",
"rumqttc",

View File

@ -1,6 +1,6 @@
[package]
name = "mqtt-client"
version = "2.0.1"
version = "2.0.2"
edition = "2021"
[dependencies]

View File

@ -25,6 +25,8 @@ pub mod mqtt_client {
let (tx_sender, tx_recever) = unbounded::<MqttMessage>();
let (rx_sender, rx_recever) = unbounded::<MqttMessage>();
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::<T>(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<MqttMessage>, 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"),