Skip to content

Commit

Permalink
braid-run: simplify control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw committed Dec 12, 2023
1 parent 70f1ed2 commit 02d20fa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
36 changes: 20 additions & 16 deletions braid/braid-run/src/mainbrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,6 @@ pub async fn run(phase1: StartupPhase1) -> Result<()> {

// We run this closure for each incoming packet.

// TODO: evaluate if we can reduce or eliminate cloning of http
// session handler below. That seems not necessary.

// Let's be sure about the type of our input.
let r: std::result::Result<
(flydra_types::FlydraRawUdpPacket, std::net::SocketAddr),
Expand Down Expand Up @@ -1132,24 +1129,31 @@ pub async fn run(phase1: StartupPhase1) -> Result<()> {
}
};

// Create closure which is called only if there is a new frame offset
// (which occurs upon synchronization).
let send_new_frame_offset = |frame| {
let http_session_handler = http_session_handler2.clone();
let cam_name = ros_cam_name.clone();
let fut_no_err = async move {
match http_session_handler
.send_frame_offset(&cam_name, frame)
.await
{
Ok(_) => {}
Err(e) => {
error!("Error sending frame offset: {}", e);
}
};
};
rt_handle.spawn(fut_no_err);
};

let synced_frame = cam_manager2.got_new_frame_live(
&packet,
&sync_pulse_pause_started_arc,
sync_time_min,
std::time::Duration::from_secs(SYNCHRONIZE_DURATION_SEC as u64 + 2),
|name, frame| {
let name2 = name.clone();
let http_session_handler3 = http_session_handler2.clone();
let fut_no_err = async move {
match http_session_handler3.send_frame_offset(&name2, frame).await {
Ok(_) => {}
Err(e) => {
error!("Error sending frame offset: {}", e);
}
};
};
rt_handle.spawn(fut_no_err);
},
send_new_frame_offset,
);

let cam_num = cam_manager.cam_num(&ros_cam_name);
Expand Down
4 changes: 2 additions & 2 deletions flydra2/src/connected_camera_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl ConnectedCamerasManager {
mut send_new_frame_offset: F,
) -> Option<SyncFno>
where
F: FnMut(&RosCamName, u64),
F: FnMut(u64),
{
assert!(packet.framenumber >= 0);

Expand Down Expand Up @@ -466,7 +466,7 @@ impl ConnectedCamerasManager {
self.notify_cam_changed_listeners();

// Do notifications associated with synchronization.
send_new_frame_offset(&ros_cam_name, frame0);
send_new_frame_offset(frame0);
info!(
"cam {} synchronized with frame offset: {}",
ros_cam_name.as_str(),
Expand Down

0 comments on commit 02d20fa

Please sign in to comment.