Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document code #15

Merged
merged 1 commit into from
Sep 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct Service {
type ResponderTask = Box<dyn Future<Output = ()> + Send + Unpin>;

impl Responder {
/// Spawn a responder task on an os thread.
pub fn new() -> io::Result<Responder> {
let (tx, rx) = std::sync::mpsc::sync_channel(0);
thread::Builder::new()
Expand All @@ -61,12 +62,23 @@ impl Responder {
rx.recv().expect("rx responder channel closed")
}

/// Spawn a `Responder` with the provided tokio `Handle`.
///
/// # Example
/// ```no_run
/// use libmdns::Responder;
///
/// let rt = tokio::runtime::Runtime::new().unwrap();
/// let handle = rt.handle().clone();
/// let responder = Responder::spawn(&handle)?;
/// ```
pub fn spawn(handle: &Handle) -> io::Result<Responder> {
let (responder, task) = Self::with_default_handle()?;
handle.spawn(task);
Ok(responder)
}

/// Spawn a `Responder` on the default tokio handle.
pub fn with_default_handle() -> io::Result<(Responder, ResponderTask)> {
let mut hostname = match hostname::get() {
Ok(s) => match s.into_string() {
Expand Down Expand Up @@ -115,6 +127,24 @@ impl Responder {
}

impl Responder {
/// Register a service to be advertised by the `Responder`. The service is unregistered on
/// drop.
///
/// # example
///
/// ```no_run
/// use libmdns::Responder;
///
/// let responder = Responder::new()?;
/// // bind service
/// let _http_svc = responder.register(
/// "_http._tcp".into(),
/// "my http server".into(),
/// 80,
/// &["path=/"]
/// );
/// ```
#[must_use]
pub fn register(&self, svc_type: String, svc_name: String, port: u16, txt: &[&str]) -> Service {
let txt = if txt.is_empty() {
vec![0]
Expand Down