Skip to content

Commit

Permalink
networking overhaul
Browse files Browse the repository at this point in the history
- convert to axum
- rework strand/braid connections
- remove bui-backend
- logging cleanup, including tracing spans do not include all args
- simplify async code
- switch logging to tracing from log crate
  • Loading branch information
astraw committed Jan 28, 2024
1 parent 67b1b0c commit 46c21d0
Show file tree
Hide file tree
Showing 71 changed files with 2,888 additions and 2,967 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ members = [
"braidz-viewer",
"bui-backend-session",
"bui-backend-session/demo",
"bui-backend-session/types",
"build-util",
"camcal",
"led-box",
Expand All @@ -49,6 +50,7 @@ members = [
"enum-iter",
"env-tracing-logger",
"env-tracing-logger/env-tracing-logger-sample",
"event-stream-types",
"fastimage",
"fastfreeimage",
"fly-eye",
Expand Down Expand Up @@ -120,9 +122,7 @@ members = [
"zip-or-dir/dir2zip",
]

exclude = [
"led-box-firmware",
]
exclude = ["led-box-firmware"]

[profile.release]
debug = true
Expand Down
3 changes: 1 addition & 2 deletions ads-webasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ js-sys = "0.3"
gloo = "0.8.0"
gloo-file = "0.2"
wasm-bindgen = { version = "0.2.58" }
http = "0.2"
serde = "1.0"
serde_yaml = "0.9"
serde_derive = "1.0"
Expand All @@ -27,14 +26,14 @@ uuid = { version = "1.2.2", default-features = false, features = [
] } # add feature flag required for uuid crate
csv = { version = "1.1", optional = true }

bui-backend-types = "0.8"
yew-tincture = "0.1"

simple-obj-parse = { path = "../simple-obj-parse", optional = true }
textured-tri-mesh = { path = "../textured-tri-mesh", optional = true }
http-video-streaming-types = { path = "../http-video-streaming/http-video-streaming-types" }
enum-iter = { path = "../enum-iter" }
rust-cam-bui-types = { path = "../rust-cam-bui-types" }
bui-backend-session-types = { path = "../bui-backend-session/types" }

[dependencies.web-sys]
version = "0.3"
Expand Down
7 changes: 2 additions & 5 deletions ads-webasm/src/components/video_field.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cell::RefCell, rc::Rc};

use crate::video_data::VideoData;
use bui_backend_types;
use bui_backend_session_types;
use gloo::timers::callback::Timeout;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -29,8 +29,7 @@ pub struct ImData2 {
pub draw_shapes: Vec<CanvasDrawableShape>,
pub fno: u64,
pub ts_rfc3339: String, // timestamp in RFC3339 format
pub ck: bui_backend_types::ConnectionKey,
pub name: Option<String>,
pub ck: bui_backend_session_types::ConnectionKey,
}

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -134,7 +133,6 @@ impl Component for VideoField {
let fci = FirehoseCallbackInner {
ck: im_data.ck,
fno: im_data.fno as usize,
name: im_data.name.clone(),
ts_rfc3339: im_data.ts_rfc3339,
};

Expand Down Expand Up @@ -181,7 +179,6 @@ impl Component for VideoField {
ck: in_msg.ck,
fno: in_msg.fno,
found_points: in_msg.found_points.clone(),
name: in_msg.name.clone(),
ts_rfc3339: in_msg.ts_rfc3339,
draw_shapes: draw_shapes.into_iter().map(|s| s.into()).collect(),
};
Expand Down
20 changes: 6 additions & 14 deletions braid-config-data/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#![cfg_attr(
feature = "backtrace",
feature(error_generic_member_access)
)]
#![cfg_attr(feature = "backtrace", feature(error_generic_member_access))]

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -34,10 +31,6 @@ pub enum Error {

type Result<T> = std::result::Result<T, Error>;

fn default_lowlatency_camdata_udp_addr() -> String {
"127.0.0.1:0".to_string()
}

fn default_http_api_server_addr() -> String {
"127.0.0.1:0".to_string()
}
Expand Down Expand Up @@ -95,8 +88,7 @@ pub struct MainbrainConfig {
// /// Parameters to potentially raise the mainbrain thread priority.
// sched_policy_priority: Option<(i32, i32)>,
/// Address of UDP port to send low-latency detection data
#[serde(default = "default_lowlatency_camdata_udp_addr")]
pub lowlatency_camdata_udp_addr: String,
pub lowlatency_camdata_udp_addr: Option<String>,
/// Address of HTTP port for control API
#[serde(default = "default_http_api_server_addr")]
pub http_api_server_addr: String,
Expand All @@ -108,8 +100,8 @@ pub struct MainbrainConfig {
/// Save rows to data2d_distorted where nothing detected (saves timestamps)
#[serde(default = "default_true")]
pub save_empty_data2d: bool,
/// Secret to use for JWT auth on HTTP port for control API
pub jwt_secret: Option<String>,
/// Secret to use for signing HTTP cookies (base64 encoded)
pub secret_base64: Option<String>,
/// For debugging: filename to store captured packet data.
pub packet_capture_dump_fname: Option<std::path::PathBuf>,
/// Threshold duration before logging error (msec).
Expand All @@ -130,12 +122,12 @@ impl std::default::Default for MainbrainConfig {
tracking_params: flydra_types::default_tracking_params_full_3d(),
// Raising the mainbrain thread priority is currently disabled.
// sched_policy_priority: None,
lowlatency_camdata_udp_addr: default_lowlatency_camdata_udp_addr(),
lowlatency_camdata_udp_addr: None,
http_api_server_addr: default_http_api_server_addr(),
http_api_server_token: None,
model_server_addr: default_model_server_addr(),
save_empty_data2d: true,
jwt_secret: None,
secret_base64: None,
packet_capture_dump_fname: None,
acquisition_duration_allowed_imprecision_msec:
flydra_types::DEFAULT_ACQUISITION_DURATION_ALLOWED_IMPRECISION_MSEC,
Expand Down
13 changes: 6 additions & 7 deletions braid-http-session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
name = "braid-http-session"
version = "0.1.0"
edition = "2021"
rust-version="1.60"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
rust-version = "1.60"

[dependencies]
log = "0.4"
tracing = "0.1"
thiserror = "1"
hyper = "1.1"
serde = {version="1.0",features=["derive"]}
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
futures = "0.3"

bui-backend-session = {path="../bui-backend-session"}
flydra-types = { path = "../flydra-types", features=["with-dns"] }
bui-backend-session = { path = "../bui-backend-session" }
flydra-types = { path = "../flydra-types" }
http-body-util = "0.1.0"
bytes = "1.5.0"
axum = "0.7.4"
67 changes: 19 additions & 48 deletions braid-http-session/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ::bui_backend_session::{future_session, InsecureSession};
use log::{debug, error};
use tracing::{debug, error};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand All @@ -16,6 +16,7 @@ pub enum Error {
}

/// Create a `MainbrainSession` which has already made a request
#[tracing::instrument(level = "info")]
pub async fn mainbrain_future_session(
dest: flydra_types::MainbrainBuiLocation,
) -> Result<MainbrainSession, bui_backend_session::Error> {
Expand All @@ -26,54 +27,44 @@ pub async fn mainbrain_future_session(
Ok(MainbrainSession { inner })
}

type MyBody = http_body_util::combinators::BoxBody<bytes::Bytes, bui_backend_session::Error>;

fn body_from_buf(body_buf: &[u8]) -> MyBody {
let body = http_body_util::Full::new(bytes::Bytes::from(body_buf.to_vec()));
use http_body_util::BodyExt;
MyBody::new(body.map_err(|_: std::convert::Infallible| unreachable!()))
fn body_from_buf(body_buf: &[u8]) -> axum::body::Body {
axum::body::Body::new(http_body_util::Full::new(bytes::Bytes::from(
body_buf.to_vec(),
)))
}

/// This allows communicating with the Mainbrain over HTTP RPC.
///
/// This replaced the old ROS layer for camera -> mainbrain command and control
/// communication from flydra.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct MainbrainSession {
inner: InsecureSession,
}

impl MainbrainSession {
#[tracing::instrument(skip_all)]
async fn do_post(&mut self, bytes: Vec<u8>) -> Result<(), Error> {
let body = body_from_buf(&bytes);

let resp = self.inner.post("callback", body).await?;

debug!("called do_post and got response: {:?}", resp);
if !resp.status().is_success() {
error!(
"error: POST response was not a success {}:{}",
file!(),
line!()
);
// TODO: return Err(_)?
};
debug!("calling mainbrain callback handler");
let _resp = self.inner.post("callback", body).await?;
Ok(())
}

pub async fn get_remote_info(
&mut self,
orig_cam_name: &flydra_types::RawCamName,
raw_cam_name: &flydra_types::RawCamName,
) -> Result<flydra_types::RemoteCameraInfoResponse, Error> {
let path = format!(
"{}?camera={}",
flydra_types::REMOTE_CAMERA_INFO_PATH,
orig_cam_name.as_str()
"{}/{}",
flydra_types::braid_http::REMOTE_CAMERA_INFO_PATH,
flydra_types::braid_http::encode_cam_name(raw_cam_name)
);

debug!(
"Getting remote camera info for camera \"{}\".",
orig_cam_name.as_str()
raw_cam_name.as_str()
);

let resp = self.inner.get(&path).await?;
Expand All @@ -97,32 +88,12 @@ impl MainbrainSession {
>(&data)?)
}

pub async fn register_flydra_camnode(
&mut self,
msg: &flydra_types::RegisterNewCamera,
) -> Result<(), Error> {
debug!("register_flydra_camnode with message {:?}", msg);
let msg = flydra_types::HttpApiCallback::NewCamera(msg.clone());
Ok(self.send_message(msg).await?)
}

pub async fn update_image(
#[tracing::instrument(skip_all)]
pub async fn post_callback_message(
&mut self,
ros_cam_name: flydra_types::RosCamName,
current_image_png: flydra_types::PngImageData,
msg: flydra_types::BraidHttpApiCallback,
) -> Result<(), Error> {
let msg = flydra_types::PerCam {
ros_cam_name,
inner: flydra_types::UpdateImage { current_image_png },
};

debug!("update_image with message {:?}", msg);
let msg = flydra_types::HttpApiCallback::UpdateCurrentImage(msg);
Ok(self.send_message(msg).await?)
}

pub async fn send_message(&mut self, msg: flydra_types::HttpApiCallback) -> Result<(), Error> {
let bytes = serde_json::to_vec(&msg).unwrap();
Ok(self.do_post(bytes).await?)
self.do_post(bytes).await
}
}
Loading

0 comments on commit 46c21d0

Please sign in to comment.