Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 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 console_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ chrono = { version = "0.4", features = ["serde"] }
csv = "1"
paste = "1"
pyo3 = { version = "0.13", features = ["extension-module"], optional = true }
sbp = { git = "https://github.com/swift-nav/libsbp.git", rev = "ce5e3d14ae4284c53982bc3fd79a2fa15976ff9f", features = [
sbp = { git = "https://github.com/swift-nav/libsbp.git", rev = "83110eaca01c4fb1ec1500d578a1cbbaa2b8c788", features = [
"json",
"swiftnav-rs",
] }
Expand Down
2 changes: 1 addition & 1 deletion console_backend/src/bin/fileio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main() -> Result<()> {
let run = move |rdr| {
let messages = sbp::iter_messages(rdr).log_errors(log::Level::Debug);
for msg in messages {
bc_source.send(&msg);
bc_source.send(&msg, None);
if done_rx.try_recv().is_ok() {
break;
}
Expand Down
66 changes: 37 additions & 29 deletions console_backend/src/broadcaster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ use std::{
};

use crossbeam::channel;
use sbp::messages::{ConcreteMessage, SBPMessage, SBP};
use sbp::{
messages::{ConcreteMessage, SBPMessage, SBP},
time::{GpsTime, GpsTimeError},
};
use slotmap::HopSlotMap;

type MaybeGpsTime = Option<Result<GpsTime, GpsTimeError>>;

pub struct Broadcaster {
channels: Arc<Mutex<HopSlotMap<KeyInner, Sender>>>,
}
Expand All @@ -22,12 +27,12 @@ impl Broadcaster {
}
}

pub fn send(&self, message: &SBP) {
pub fn send(&self, message: &SBP, gps_time: MaybeGpsTime) {
let msg_type = message.get_message_type();
let mut channels = self.channels.lock().expect(Self::CHANNELS_LOCK_FAILURE);
channels.retain(|_, chan| {
if chan.msg_types.iter().any(|ty| ty == &msg_type) {
chan.inner.send(message.clone()).is_ok()
chan.inner.send((message.clone(), gps_time.clone())).is_ok()
} else {
true
}
Expand Down Expand Up @@ -59,7 +64,7 @@ impl Broadcaster {
}

/// Wait once for a specific message. Returns an error if `dur` elapses.
pub fn wait<E>(&self, dur: Duration) -> Result<E, channel::RecvTimeoutError>
pub fn wait<E>(&self, dur: Duration) -> Result<(E, MaybeGpsTime), channel::RecvTimeoutError>
where
E: Event,
{
Expand All @@ -84,13 +89,13 @@ impl Default for Broadcaster {

/// A wrapper around a channel sender that knows what message types its receivers expect.
struct Sender {
inner: channel::Sender<SBP>,
inner: channel::Sender<(SBP, MaybeGpsTime)>,
msg_types: &'static [u16],
}

/// A wrapper around a channel receiver that converts to the appropriate event.
pub struct Receiver<E> {
inner: channel::Receiver<SBP>,
inner: channel::Receiver<(SBP, MaybeGpsTime)>,
marker: PhantomData<E>,
}

Expand All @@ -107,27 +112,30 @@ impl<E> Receiver<E>
where
E: Event,
{
pub fn recv(&self) -> Result<E, channel::RecvError> {
pub fn recv(&self) -> Result<(E, MaybeGpsTime), channel::RecvError> {
let msg = self.inner.recv()?;
Ok(E::from_sbp(msg))
Ok((E::from_sbp(msg.0), msg.1))
}

pub fn recv_timeout(&self, dur: Duration) -> Result<E, channel::RecvTimeoutError> {
pub fn recv_timeout(
&self,
dur: Duration,
) -> Result<(E, MaybeGpsTime), channel::RecvTimeoutError> {
let msg = self.inner.recv_timeout(dur)?;
Ok(E::from_sbp(msg))
Ok((E::from_sbp(msg.0), msg.1))
}

pub fn try_recv(&self) -> Result<E, channel::TryRecvError> {
pub fn try_recv(&self) -> Result<(E, MaybeGpsTime), channel::TryRecvError> {
let msg = self.inner.try_recv()?;
Ok(E::from_sbp(msg))
Ok((E::from_sbp(msg.0), msg.1))
}

pub fn iter(&self) -> impl Iterator<Item = E> + '_ {
self.inner.iter().map(E::from_sbp)
pub fn iter(&self) -> impl Iterator<Item = (E, MaybeGpsTime)> + '_ {
self.inner.iter().map(|msg| (E::from_sbp(msg.0), msg.1))
}

pub fn try_iter(&self) -> impl Iterator<Item = E> + '_ {
self.inner.try_iter().map(E::from_sbp)
pub fn try_iter(&self) -> impl Iterator<Item = (E, MaybeGpsTime)> + '_ {
self.inner.try_iter().map(|msg| (E::from_sbp(msg.0), msg.1))
}

// other channel methods as needed
Expand Down Expand Up @@ -171,8 +179,8 @@ mod tests {
let b = Broadcaster::new();
let (msg_obs, _) = b.subscribe::<MsgObs>();

b.send(&make_msg_obs());
b.send(&make_msg_obs_dep_a());
b.send(&make_msg_obs(), None);
b.send(&make_msg_obs_dep_a(), None);

assert!(msg_obs.try_recv().is_ok());
assert!(msg_obs.try_recv().is_err());
Expand All @@ -184,8 +192,8 @@ mod tests {
let (msg_obs1, _) = b.subscribe::<MsgObs>();
let (msg_obs2, _) = b.subscribe::<MsgObs>();

b.send(&make_msg_obs());
b.send(&make_msg_obs());
b.send(&make_msg_obs(), None);
b.send(&make_msg_obs(), None);

assert_eq!(msg_obs1.try_iter().count(), 2);
assert_eq!(msg_obs2.try_iter().count(), 2);
Expand All @@ -198,13 +206,13 @@ mod tests {
let (msg_obs1, key) = b.subscribe::<MsgObs>();
let (msg_obs2, _) = b.subscribe::<MsgObs>();

b.send(&make_msg_obs());
b.send(&make_msg_obs(), None);
assert_eq!(msg_obs1.try_iter().count(), 1);
assert_eq!(msg_obs2.try_iter().count(), 1);

b.unsubscribe(key);

b.send(&make_msg_obs());
b.send(&make_msg_obs(), None);
assert_eq!(msg_obs1.try_iter().count(), 0);
assert_eq!(msg_obs2.try_iter().count(), 1);
}
Expand All @@ -226,9 +234,9 @@ mod tests {

std::thread::sleep(Duration::from_secs(1));

b.send(&make_msg_obs());
b.send(&make_msg_obs_dep_a());
b.send(&make_msg_obs());
b.send(&make_msg_obs(), None);
b.send(&make_msg_obs_dep_a(), None);
b.send(&make_msg_obs(), None);

// msg_obs.iter() goes forever if you don't drop the channel
b.unsubscribe(key);
Expand All @@ -243,7 +251,7 @@ mod tests {
scope(|s| {
s.spawn(|_| {
std::thread::sleep(Duration::from_secs(1));
b.send(&make_msg_obs())
b.send(&make_msg_obs(), None)
});

assert!(b.wait::<MsgObs>(Duration::from_secs(2)).is_ok());
Expand All @@ -258,7 +266,7 @@ mod tests {
scope(|s| {
s.spawn(|_| {
std::thread::sleep(Duration::from_secs(2));
b.send(&make_msg_obs())
b.send(&make_msg_obs(), None)
});

assert!(b.wait::<MsgObs>(Duration::from_secs(1)).is_err());
Expand All @@ -273,8 +281,8 @@ mod tests {
let (obs_msg, _) = b.subscribe::<ObsMsg>();
let (msg_obs, _) = b.subscribe::<MsgObs>();

b.send(&make_msg_obs());
b.send(&make_msg_obs_dep_a());
b.send(&make_msg_obs(), None);
b.send(&make_msg_obs_dep_a(), None);

// ObsMsg should accept both MsgObs and MsgObsDepA
assert!(obs_msg.try_recv().is_ok());
Expand Down
14 changes: 7 additions & 7 deletions console_backend/src/fileio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ where
pending.insert(sequence, request);
},
recv(res_rx) -> msg => {
let msg = msg?;
let (msg, _) = msg?;
let req = match pending.remove(&msg.sequence) {
Some(req) => req,
None => continue,
Expand Down Expand Up @@ -243,7 +243,7 @@ where
pending.insert(req_state.sequence, (req_state, req));
},
recv(res_rx) -> msg => {
let msg = msg?;
let (msg, _) = msg?;
if pending.remove(&msg.sequence).is_none() {
continue
}
Expand Down Expand Up @@ -284,7 +284,7 @@ where
dirname: path.clone().into(),
}))?;

let reply = self
let (reply, _) = self
.broadcast
.wait::<MsgFileioReadDirResp>(READDIR_TIMEOUT)?;

Expand Down Expand Up @@ -341,7 +341,7 @@ where
let config = self
.broadcast
.wait::<MsgFileioConfigResp>(CONFIG_REQ_TIMEOUT)
.map_or_else(|_| Default::default(), Into::into);
.map_or_else(|_| Default::default(), |(msg, _)| FileioConfig::new(msg));

tx.send(true).unwrap();

Expand Down Expand Up @@ -487,9 +487,9 @@ struct FileioConfig {
batch_size: u32,
}

impl From<MsgFileioConfigResp> for FileioConfig {
fn from(msg: MsgFileioConfigResp) -> Self {
FileioConfig {
impl FileioConfig {
fn new(msg: MsgFileioConfigResp) -> Self {
Self {
window_size: msg.window_size,
batch_size: msg.batch_size,
}
Expand Down