Struct cotton_ssdp::AsyncService
source · pub struct AsyncService { /* private fields */ }
Expand description
High-level asynchronous SSDP service using tokio.
Handles incoming and outgoing searches using async
, await
, and the
Tokio crate.
Implementations§
source§impl AsyncService
impl AsyncService
sourcepub fn new() -> Result<Self, Error>
pub fn new() -> Result<Self, Error>
Create a new AsyncService
, including its two UDP sockets
Errors
Can return a std::io::Error
if any of the underlying socket
calls fail.
Panics
Will panic if the internal mutex cannot be locked; that would indicate a bug in cotton-ssdp.
Examples found in repository?
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
async fn main() -> Result<(), Box<dyn Error>> {
println!(
"ssdp-search from {} {}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
);
let mut s = AsyncService::new()?;
let mut map = HashMap::new();
let uuid = uuid::Uuid::new_v4();
s.advertise(
uuid.to_string(),
Advertisement {
notification_type: "test".to_string(),
location: url::Url::parse("http://127.0.0.1/test").unwrap(),
},
);
let mut stream = s.subscribe("ssdp:all");
while let Some(r) = stream.next().await {
println!("GOT {:?}", r);
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!(" {} at {}", unique_service_name, location);
map.insert(unique_service_name.clone(), r);
}
}
}
Ok(())
}
sourcepub fn subscribe<A>(
&mut self,
notification_type: A
) -> impl Stream<Item = Notification>where
A: Into<String>,
pub fn subscribe<A>( &mut self, notification_type: A ) -> impl Stream<Item = Notification>where A: Into<String>,
Subscribe to SSDP notifications for a resource type.
Panics
Will panic if the internal mutex cannot be locked; that would indicate a bug in cotton-ssdp.
Examples found in repository?
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
async fn main() -> Result<(), Box<dyn Error>> {
println!(
"ssdp-search from {} {}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
);
let mut s = AsyncService::new()?;
let mut map = HashMap::new();
let uuid = uuid::Uuid::new_v4();
s.advertise(
uuid.to_string(),
Advertisement {
notification_type: "test".to_string(),
location: url::Url::parse("http://127.0.0.1/test").unwrap(),
},
);
let mut stream = s.subscribe("ssdp:all");
while let Some(r) = stream.next().await {
println!("GOT {:?}", r);
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!(" {} at {}", unique_service_name, location);
map.insert(unique_service_name.clone(), r);
}
}
}
Ok(())
}
sourcepub fn advertise<USN>(
&mut self,
unique_service_name: USN,
advertisement: Advertisement
)where
USN: Into<String>,
pub fn advertise<USN>( &mut self, unique_service_name: USN, advertisement: Advertisement )where USN: Into<String>,
Announce a new resource
And start responding to any searches matching it.
Panics
Will panic if the internal mutex cannot be locked; that would indicate a bug in cotton-ssdp.
Examples found in repository?
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
async fn main() -> Result<(), Box<dyn Error>> {
println!(
"ssdp-search from {} {}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
);
let mut s = AsyncService::new()?;
let mut map = HashMap::new();
let uuid = uuid::Uuid::new_v4();
s.advertise(
uuid.to_string(),
Advertisement {
notification_type: "test".to_string(),
location: url::Url::parse("http://127.0.0.1/test").unwrap(),
},
);
let mut stream = s.subscribe("ssdp:all");
while let Some(r) = stream.next().await {
println!("GOT {:?}", r);
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!(" {} at {}", unique_service_name, location);
map.insert(unique_service_name.clone(), r);
}
}
}
Ok(())
}
sourcepub fn deadvertise(&mut self, unique_service_name: &str)
pub fn deadvertise(&mut self, unique_service_name: &str)
Announce the disappearance of a resource
And stop responding to searches.
Panics
Will panic if the internal mutex cannot be locked; that would indicate a bug in cotton-ssdp.