Skip to content

Commit

Permalink
add port check before spawning new viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
petertheprocess committed Sep 4, 2024
1 parent ef3748a commit 41bd4b6
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions crates/top/rerun/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ fn run_impl(
) -> anyhow::Result<()> {
#[cfg(feature = "native_viewer")]
let profiler = profiler(&args);
let mut is_another_viewer_running = false;

#[cfg(feature = "native_viewer")]
let startup_options = {
Expand Down Expand Up @@ -951,13 +952,26 @@ fn run_impl(

#[cfg(feature = "server")]
{
let server_options = re_sdk_comms::ServerOptions {
max_latency_sec: parse_max_latency(args.drop_at_latency.as_ref()),
quiet: false,
};
let tcp_listener: Receiver<LogMsg> =
re_sdk_comms::serve(&args.bind, args.port, server_options)?;
rxs.push(tcp_listener);
// Check if there is already a viewer running
// and if so, send the data to it.
use std::net::TcpStream;
let connect_addr = std::net::SocketAddr::new(args.bind.parse().unwrap(), args.port);
if TcpStream::connect_timeout(&connect_addr, std::time::Duration::from_secs(1)).is_ok()
{
re_log::info!(
addr = %connect_addr,
"A process is already listening at this address. Assuming it's a Rerun Viewer."
);
is_another_viewer_running = true;
} else {
let server_options = re_sdk_comms::ServerOptions {
max_latency_sec: parse_max_latency(args.drop_at_latency.as_ref()),
quiet: false,
};
let tcp_listener: Receiver<LogMsg> =
re_sdk_comms::serve(&args.bind, args.port, server_options)?;
rxs.push(tcp_listener);
}
}

rxs
Expand Down Expand Up @@ -1030,6 +1044,22 @@ fn run_impl(

return Ok(());
}
} else if is_another_viewer_running {
re_log::info!("Another viewer is already running, streaming data to it.");
// get current active recording
use re_sdk::RecordingStream;
let static_ = true;

let Some(active_recording) = RecordingStream::get(re_sdk::StoreKind::Recording, None)
else {
anyhow::bail!("No active recording found!");
};
// map is lazy, so we need take the result by let _ = ...
let _ = args
.url_or_paths
.iter()
.map(|path| active_recording.log_file_from_path(path, None, static_));
Ok(())
} else {
#[cfg(feature = "native_viewer")]
return re_viewer::run_native_app(
Expand Down

0 comments on commit 41bd4b6

Please sign in to comment.