Add discover example to upnp

This commit is contained in:
Igor Katson 2024-08-29 13:51:45 +01:00
parent af00713e4d
commit babe470f9a
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 110 additions and 49 deletions

View file

@ -0,0 +1,35 @@
use std::time::Duration;
use librqbit_upnp::{discover_once, discover_services, SSDP_SEARCH_ROOT_ST};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt().init();
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let (stx, mut srx) = tokio::sync::mpsc::unbounded_channel::<()>();
let f1 = async move { discover_once(&tx, SSDP_SEARCH_ROOT_ST, Duration::from_secs(10)).await };
let f2 = async move {
while let Some(r) = rx.recv().await {
let stx = stx.clone();
tokio::spawn(async move {
match discover_services(r.location.clone()).await {
Ok(s) => {
println!("{}: {s:#?}", r.location);
}
Err(e) => {
tracing::error!(location=%r.location, "error discovering")
}
}
drop(stx);
});
}
};
let f3 = async move { while (srx.recv().await).is_some() {} };
tokio::join!(f1, f2, f3);
Ok(())
}