Struct Service

Source
pub struct Service { /* private fields */ }
Expand description

Struct representing a ZeroConf service. This should be created with all the information that should be associated with the service and then the publish method can be used to register the service. The ServiceRef returned from publish should be held for as long as the service should continue being advertised, once dropped the service will be deallocated.

§Examples

Normally the default values of domain, host and interface don’t need to be changed.

let service_ref = async_zeroconf::Service::new("Server", "_http._tcp", 80)
                      .publish().await?;
// Service kept alive until service_ref dropped

These fields can be customised if required. More details are available in the DNSServiceRegister documentation.

let service_ref = async_zeroconf::Service::new("Server", "_http._tcp", 80)
                      .set_domain("local".to_string())
                      .set_host("localhost".to_string())
                      .publish().await?;
// Service kept alive until service_ref dropped

Implementations§

Source§

impl Service

Source

pub fn new(name: &str, service_type: &str, port: u16) -> Self

Create a new Service, called name of type service_type that is listening on port port.

This must then be published with Service::publish to advertise the service.

§Examples
// Create a service description
let service = async_zeroconf::Service::new("Web Server", "_http._tcp", 80);
Source

pub fn new_with_txt( name: &str, service_type: &str, port: u16, txt: TxtRecord, ) -> Self

Create a new Service, called name of type service_type that is listening on port port with the TXT records described by txt.

This must then be published with Service::publish to advertise the service.

§Examples
// Create a TXT record collection
let mut txt = async_zeroconf::TxtRecord::new();
txt.add("version".to_string(), "0.1".to_string());
// Create a service description
let service = async_zeroconf::Service::new_with_txt("Web Server", "_http._tcp", 80, txt);
Source

pub fn set_interface(&mut self, interface: Interface) -> &mut Self

Set an interface to advertise the service on rather than all.

By default the service will be advertised on all interfaces.

Source

pub fn interface(&self) -> &Interface

Get this interface associated with this service

Source

pub fn prevent_rename(&mut self) -> &mut Self

Prevent renaming of this service if there is a name collision.

By default the service will be automatically renamed.

Source

pub fn set_domain(&mut self, domain: String) -> &mut Self

Set the (optional) domain for the service.

If not specified, the default domain is used.

Source

pub fn domain(&self) -> &Option<String>

Get the domain of this service

Source

pub fn set_host(&mut self, host: String) -> &mut Self

Set the (optional) hostname for the service.

If not set, the hostname of the host will be used.

Source

pub fn name(&self) -> &str

Get the name of the service

Source

pub fn service_type(&self) -> &str

Get the type of the service

Source

pub fn port(&self) -> u16

Get the port of the service

Source

pub fn host(&self) -> &Option<String>

Get the host of the service

Source

pub fn txt(&self) -> &TxtRecord

Get the TxtRecord for this service

Source

pub fn add_txt(&mut self, k: String, v: String) -> &mut Self

Add a TXT entry to the service

Source

pub async fn publish(&self) -> Result<ServiceRef, ZeroconfError>

Publish the service, returns a ServiceRef which should be held to keep the service alive. Once the ServiceRef is dropped the service will be removed and deallocated.

§Arguments
  • allow_rename - Allow the service to be automatically renamed if a service with the same name already exists
§Examples
// Create a service description
let service = async_zeroconf::Service::new("Server", "_http._tcp", 80);
// Publish the service
let service_ref = service.publish().await?;
// Service kept alive until service_ref dropped
Source

pub fn publish_task( &self, ) -> Result<(ServiceRef, impl ProcessTask, impl Future<Output = Result<(), ZeroconfError>>), ZeroconfError>

Publish the service, returns a ServiceRef which should be held to keep the service alive and a future which should be awaited on to respond to any events associated with keeping the service registered. Once the ServiceRef is dropped the service will be removed and deallocated.

§Note

This method is intended if more control is needed over how the task is spawned. Service::publish will automatically spawn the task. The task should be spawned first to process events, and then the returned future waited on to collect any errors that occurred.

§Examples
// Create a service description
let service = async_zeroconf::Service::new("Server", "_http._tcp", 80);
// Publish the service
let (service_ref, task, service_ok) = service.publish_task()?;
// Spawn the task to respond to events
tokio::spawn(task);
// Wait to confirm service started ok
service_ok.await?;
// Service kept alive until service_ref dropped

Trait Implementations§

Source§

impl Clone for Service

Source§

fn clone(&self) -> Service

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Service

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Service

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Service

Source§

fn eq(&self, other: &Service) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Service

Source§

impl StructuralPartialEq for Service

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.