Skip to content

Commit

Permalink
Document code
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinPostma committed Sep 4, 2020
1 parent 0966dd8 commit ba7ee01
Showing 1 changed file with 30 additions and 0 deletions.
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

0 comments on commit ba7ee01

Please sign in to comment.