From 1ec5dd21fa833d1c44805c29e807a0308d689c4a Mon Sep 17 00:00:00 2001 From: johannesd3 Date: Fri, 12 Mar 2021 16:39:58 +0100 Subject: [PATCH] Add discovery example --- Cargo.lock | 27 +++++++++++++++++++++++++++ discovery/Cargo.toml | 6 ++++++ discovery/examples/discovery.rs | 25 +++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 discovery/examples/discovery.rs diff --git a/Cargo.lock b/Cargo.lock index 955044b6..3f62136d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -237,6 +237,17 @@ dependencies = [ "libloading 0.7.0", ] +[[package]] +name = "colored" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + [[package]] name = "combine" version = "4.5.2" @@ -1236,7 +1247,9 @@ dependencies = [ "cfg-if 1.0.0", "dns-sd", "form_urlencoded", + "futures", "futures-core", + "hex", "hmac", "hyper", "libmdns", @@ -1245,6 +1258,7 @@ dependencies = [ "rand", "serde_json", "sha-1", + "simple_logger", "tokio", ] @@ -2094,6 +2108,19 @@ dependencies = [ "libc", ] +[[package]] +name = "simple_logger" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd57f17c093ead1d4a1499dc9acaafdd71240908d64775465543b8d9a9f1d198" +dependencies = [ + "atty", + "chrono", + "colored", + "log", + "winapi", +] + [[package]] name = "slab" version = "0.4.3" diff --git a/discovery/Cargo.toml b/discovery/Cargo.toml index 1946c710..ddb382c1 100644 --- a/discovery/Cargo.toml +++ b/discovery/Cargo.toml @@ -29,5 +29,11 @@ path = "../core" default_features = false version = "0.2.0" +[dev-dependencies] +futures = "0.3" +hex = "0.4" +simple_logger = "1.11" +tokio = { version = "1.0", features = ["macros", "rt"] } + [features] with-dns-sd = ["dns-sd"] diff --git a/discovery/examples/discovery.rs b/discovery/examples/discovery.rs new file mode 100644 index 00000000..267cee4f --- /dev/null +++ b/discovery/examples/discovery.rs @@ -0,0 +1,25 @@ +use futures::StreamExt; +use librespot_discovery::DeviceType; +use sha1::{Digest, Sha1}; +use simple_logger::SimpleLogger; + +#[tokio::main(flavor = "current_thread")] +async fn main() { + SimpleLogger::new() + .with_level(log::LevelFilter::Debug) + .init() + .unwrap(); + + let name = "Librespot".to_string(); + let device_id = hex::encode(Sha1::digest(name.as_bytes())); + + let mut server = librespot_discovery::Discovery::builder(device_id) + .name(name) + .device_type(DeviceType::Computer) + .launch() + .unwrap(); + + while let Some(x) = server.next().await { + println!("Received {:?}", x); + } +}