Skip to content

Commit

Permalink
Add remote online flag
Browse files Browse the repository at this point in the history
  • Loading branch information
akiroz committed Feb 5, 2024
1 parent e749cd5 commit e557792
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zika"
version = "3.4.0"
version = "3.4.1"
license = "MIT"
description = "IP Tunneling over MQTT"
repository = "https://github.com/akiroz/zika"
Expand Down
22 changes: 21 additions & 1 deletion src/remote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::time::Duration;
use std::sync::Arc;
use std::sync::{atomic::{AtomicBool, Ordering}, Arc};
use std::ops::Range;

use log;
Expand Down Expand Up @@ -31,6 +31,7 @@ pub struct Remote {
clients: Vec<RemoteClient>,
subs: Arc<Mutex<Vec<String>>>,
pub on_event: broadcast::Receiver<(usize, Packet)>,
pub online: Arc<AtomicBool>,
}

impl Remote {
Expand All @@ -45,7 +46,9 @@ impl Remote {
clients: Vec::with_capacity(broker_opts.len()),
subs: subs.clone(),
on_event: evt_recv,
online: Arc::new(AtomicBool::new(false)),
};

for (idx, opt) in broker_opts.iter().enumerate() {
log::debug!("broker[{}] opts {:?}", idx, opt);
let (mqtt_client, mut event_loop) = mqtt::AsyncClient::new(opt.clone(), 128);
Expand Down Expand Up @@ -95,6 +98,23 @@ impl Remote {
});
remote.clients.push(remote_client);
}

// Online checker
let broker_len = remote.clients.len();
let mut chkr_recv = remote.on_event.resubscribe();
let chkr_online = remote.online.clone();
task::spawn(async move {
let mut broker_state: Vec<bool> = Vec::with_capacity(broker_len);
loop {
match chkr_recv.recv().await {
Ok((idx, Packet::ConnAck(_))) => broker_state[idx] = true,
Ok((idx, Packet::Disconnect(_))) => broker_state[idx] = false,
_ => {}
}
chkr_online.store(broker_state.iter().any(|up| *up), Ordering::Relaxed);
}
});

(remote, msg_recv)
}

Expand Down

0 comments on commit e557792

Please sign in to comment.