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

source

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?
examples/ssdp-search.rs (line 14)
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(())
}
source

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?
examples/ssdp-search.rs (line 28)
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(())
}
source

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?
examples/ssdp-search.rs (lines 20-26)
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(())
}
source

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.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V