broker reconnect working

This commit is contained in:
Evan Feenstra
2022-06-09 19:23:40 -07:00
parent 937ff0d8de
commit f1e8e13dda
3 changed files with 34 additions and 58 deletions

View File

@@ -8,6 +8,7 @@ use std::sync::Arc;
use std::thread;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::time::timeout;
const SUB_TOPIC: &str = "sphinx-return";
const PUB_TOPIC: &str = "sphinx";
@@ -48,85 +49,61 @@ pub fn start_broker(
let console: Arc<ConsoleLink> = Arc::new(ConsoleLink::new(config, router_tx));
loop {
let metrics = consolelink::request_metrics(console.clone(), client_id.clone());
match metrics.tracker() {
let changed: Option<bool> = match metrics.tracker() {
Some(t) => {
// wait for subscription to be sure
if t.concrete_subscriptions_len() > 0 {
if !client_connected {
println!("CLIENT CONNECTED!");
client_connected = true;
status_sender
.send(true)
.await
.expect("couldnt send true statu");
Some(true) // changed to true
} else {
None
}
} else {
None
}
}
None => {
if client_connected {
println!("CLIENT DIsCONNECTED!");
client_connected = false;
status_sender
.send(false)
.await
.expect("couldnt send false status");
Some(false)
} else {
None
}
}
};
if let Some(c) = changed {
client_connected = c;
status_sender
.send(c)
.await
.expect("couldnt send connection status");
}
tokio::time::sleep(Duration::from_millis(850)).await;
tokio::time::sleep(Duration::from_millis(800)).await;
}
});
let sub_task = tokio::spawn(async move {
// ready message loop
// let ready_tx_ = ready_tx.clone();
// loop {
// wait for CONNECTED
// loop {
// let status = status_rx.recv().await.unwrap();
// if status {
// break;
// }
// }
// now wait for READY
// loop {
// let message = rx.recv().await.unwrap();
// if let Some(payload) = message.payload.get(0) {
// let content = String::from_utf8_lossy(&payload[..]);
// log::info!("received message content: {}", content);
// if content == "READY" {
// // ready_tx.send(true).expect("could not send ready");
// break;
// }
// }
// }
// now start parsing... or break for DISCONNECT
// println!("OK START PARSING!");
loop {
let message = rx.recv().await.unwrap();
println!("T = {}, P = {:?}", message.topic, message.payload.len());
// println!("count {}", message.payload.len());
while let Ok(message) = rx.recv().await {
for payload in message.payload {
if let Err(e) = msg_tx.send(payload.to_vec()).await {
println!("pub err {:?}", e);
log::warn!("pub err {:?}", e);
}
}
}
// }
});
let relay_task = tokio::spawn(async move {
loop {
let msg = receiver.recv().await.unwrap();
while let Some(msg) = receiver.recv().await {
tx.publish(PUB_TOPIC, false, msg.message)
.await
.expect("could not mqtt pub");
let reply = msg_rx.recv().await.expect("could not unwrap msg_rx.recv()");
if let Err(_) = msg.reply_tx.send(ChannelReply { reply }) {
log::warn!("could not send on reply_tx");
if let Ok(reply) = timeout(Duration::from_millis(1000), msg_rx.recv()).await {
if let Err(_) = msg.reply_tx.send(ChannelReply {
reply: reply.unwrap(),
}) {
log::warn!("could not send on reply_tx");
}
}
}
// println!("ABORT! relay task finished <<<<<<<<<<<<<<<");
});
servers.await;