use std::{net::SocketAddr, path::PathBuf, time::Duration};
#[cfg(feature = "rerun-web-viewer")]
use re_web_viewer_server::WebViewerServerPort;
#[cfg(feature = "rerun-web-viewer")]
use re_ws_comms::RerunServerPort;
#[cfg(feature = "rerun-web-viewer")]
use rerun::MemoryLimit;
use rerun::{
ApplicationId, AsComponents, EntityPath, RecordingStream, RecordingStreamBuilder,
RecordingStreamError,
};
use ncomm_core::Node;
use ncomm_publishers_and_subscribers::rerun::{RerunPublisher, RerunTimestampedPublisher};
pub struct RerunNode<
Id: PartialEq + Clone + Send + 'static,
Path: Into<PathBuf> + Clone + Send + 'static,
> {
id: Id,
stream: RecordingStream,
output_path: Option<Path>,
}
impl<Id: PartialEq + Clone + Send + 'static, Path: Into<PathBuf> + Clone + Send + 'static>
RerunNode<Id, Path>
{
pub fn new(
application_id: impl Into<ApplicationId>,
output_path: Path,
id: Id,
) -> Result<Self, RecordingStreamError> {
let stream = RecordingStreamBuilder::new(application_id).save(output_path)?;
Ok(Self {
id,
stream,
output_path: None,
})
}
pub fn new_remote_default(
application_id: impl Into<ApplicationId>,
output_path: Option<Path>,
id: Id,
) -> Result<Self, RecordingStreamError> {
let stream = RecordingStreamBuilder::new(application_id).connect()?;
Ok(Self {
id,
stream,
output_path,
})
}
pub fn new_remote(
application_id: impl Into<ApplicationId>,
address: SocketAddr,
flush_timeout: Option<Duration>,
output_path: Option<Path>,
id: Id,
) -> Result<Self, RecordingStreamError> {
let stream =
RecordingStreamBuilder::new(application_id).connect_opts(address, flush_timeout)?;
Ok(Self {
id,
stream,
output_path,
})
}
pub fn new_rerun_spawn(
application_id: impl Into<ApplicationId>,
output_path: Option<Path>,
id: Id,
) -> Result<Self, RecordingStreamError> {
let stream = RecordingStreamBuilder::new(application_id).spawn()?;
Ok(Self {
id,
stream,
output_path,
})
}
#[cfg(feature = "rerun-web-viewer")]
pub fn new_rerun_server(
application_id: impl Into<ApplicationId>,
bind_ip: &str,
web_port: WebViewerServerPort,
ws_port: RerunServerPort,
server_memory_limit: MemoryLimit,
open_browser: bool,
output_path: Option<Path>,
id: Id,
) -> Result<Self, RecordingStreamError> {
let stream = RecordingStreamBuilder::new(application_id).serve(
bind_ip,
web_port,
ws_port,
server_memory_limit,
open_browser,
)?;
Ok(Self {
id,
stream,
output_path,
})
}
pub fn create_rerun_publisher<LogPath: Into<EntityPath> + Clone, Arch: AsComponents>(
&mut self,
path: LogPath,
) -> RerunPublisher<LogPath, Arch> {
RerunPublisher::new(self.stream.clone(), path)
}
pub fn create_rerun_timestamped_publisher<
LogPath: Into<EntityPath> + Clone,
Arch: AsComponents,
>(
&mut self,
path: LogPath,
) -> RerunTimestampedPublisher<LogPath, Arch> {
RerunTimestampedPublisher::new(self.stream.clone(), path)
}
}
impl<Id: PartialEq + Clone + Send + 'static, Path: Into<PathBuf> + Clone + Send + 'static> Node<Id>
for RerunNode<Id, Path>
{
fn get_id(&self) -> Id {
self.id.clone()
}
fn get_update_delay_us(&self) -> u128 {
10_000_000
}
fn start(&mut self) {
self.stream.connect();
}
fn shutdown(&mut self) {
if let Some(path) = self.output_path.as_ref() {
let _ = self.stream.save(path.clone());
}
self.stream.disconnect();
}
}