-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssdp-search.rs
53 lines (49 loc) · 1.64 KB
/
ssdp-search.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use cotton_ssdp::{Advertisement, AsyncService, Notification};
use futures_util::StreamExt;
use std::collections::HashMap;
use std::error::Error;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
println!(
"ssdp-search from {} {}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
);
let mut netif = cotton_netif::get_interfaces_async()?;
let mut ssdp = AsyncService::new()?;
let mut map = HashMap::new();
let uuid = uuid::Uuid::new_v4();
ssdp.advertise(
uuid.to_string(),
Advertisement {
notification_type: "test".to_string(),
location: "http://127.0.0.1/test".to_string(),
},
);
let mut stream = ssdp.subscribe("ssdp:all");
loop {
tokio::select! {
notification = stream.next() => {
if let Some(r) = notification {
if let Notification::Alive {
ref notification_type,
ref unique_service_name,
ref location,
} = r
{
if !map.contains_key(unique_service_name) {
println!("+ {notification_type}");
println!(" {unique_service_name} at {location}");
map.insert(unique_service_name.clone(), r);
}
}
}
},
e = netif.next() => {
if let Some(Ok(event)) = e {
ssdp.on_network_event(&event);
}
}
}
}
}