openthread-br  0.3.0-72c0388
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
otbr::Mdns::Publisher Class Referenceabstract

#include <src/mdns/mdns.hpp>

Inheritance diagram for otbr::Mdns::Publisher:
NonCopyable otbr::Mdns::PublisherAvahi otbr::Mdns::PublisherMDnsSd

Classes

struct  DiscoveredHostInfo
 
struct  DiscoveredInstanceInfo
 
class  HostRegistration
 
class  Registration
 
class  ServiceRegistration
 
struct  TxtEntry
 

Public Types

enum  State { State::kIdle, State::kReady }
 
typedef std::vector< TxtEntryTxtList
 
typedef std::vector< std::string > SubTypeList
 
typedef std::vector< Ip6AddressAddressList
 
using DiscoveredServiceInstanceCallback = std::function< void(const std::string &aType, const DiscoveredInstanceInfo &aInstanceInfo)>
 
using DiscoveredHostCallback = std::function< void(const std::string &aHostName, const DiscoveredHostInfo &aHostInfo)>
 
using StateCallback = std::function< void(State aNewState)>
 
using ResultCallback = OnceCallback< void(otbrError aError)>
 

Public Member Functions

virtual otbrError Start (void)=0
 
virtual void Stop (void)=0
 
virtual bool IsStarted (void) const =0
 
void PublishService (const std::string &aHostName, const std::string &aName, const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, const TxtList &aTxtList, ResultCallback &&aCallback)
 
virtual void UnpublishService (const std::string &aName, const std::string &aType, ResultCallback &&aCallback)=0
 
void PublishHost (const std::string &aName, const std::vector< Ip6Address > &aAddresses, ResultCallback &&aCallback)
 
virtual void UnpublishHost (const std::string &aName, ResultCallback &&aCallback)=0
 
virtual void SubscribeService (const std::string &aType, const std::string &aInstanceName)=0
 
virtual void UnsubscribeService (const std::string &aType, const std::string &aInstanceName)=0
 
virtual void SubscribeHost (const std::string &aHostName)=0
 
virtual void UnsubscribeHost (const std::string &aHostName)=0
 
uint64_t AddSubscriptionCallbacks (DiscoveredServiceInstanceCallback aInstanceCallback, DiscoveredHostCallback aHostCallback)
 
void RemoveSubscriptionCallbacks (uint64_t aSubscriberId)
 
const MdnsTelemetryInfoGetMdnsTelemetryInfo () const
 

Static Public Member Functions

static PublisherCreate (StateCallback aCallback)
 
static void Destroy (Publisher *aPublisher)
 
static otbrError EncodeTxtData (const TxtList &aTxtList, std::vector< uint8_t > &aTxtData)
 
static otbrError DecodeTxtData (TxtList &aTxtList, const uint8_t *aTxtData, uint16_t aTxtLength)
 

Protected Types

using ServiceRegistrationPtr = std::unique_ptr< ServiceRegistration >
 
using ServiceRegistrationMap = std::map< std::string, ServiceRegistrationPtr >
 
using HostRegistrationPtr = std::unique_ptr< HostRegistration >
 
using HostRegistrationMap = std::map< std::string, HostRegistrationPtr >
 

Protected Member Functions

virtual otbrError PublishServiceImpl (const std::string &aHostName, const std::string &aName, const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, const TxtList &aTxtList, ResultCallback &&aCallback)=0
 
virtual otbrError PublishHostImpl (const std::string &aName, const std::vector< Ip6Address > &aAddresses, ResultCallback &&aCallback)=0
 
virtual void OnServiceResolveFailedImpl (const std::string &aType, const std::string &aInstanceName, int32_t aErrorCode)=0
 
virtual void OnHostResolveFailedImpl (const std::string &aHostName, int32_t aErrorCode)=0
 
virtual otbrError DnsErrorToOtbrError (int32_t aError)=0
 
void AddServiceRegistration (ServiceRegistrationPtr &&aServiceReg)
 
void RemoveServiceRegistration (const std::string &aName, const std::string &aType, otbrError aError)
 
ServiceRegistrationFindServiceRegistration (const std::string &aName, const std::string &aType)
 
void OnServiceResolved (const std::string &aType, const DiscoveredInstanceInfo &aInstanceInfo)
 
void OnServiceResolveFailed (const std::string &aType, const std::string &aInstanceName, int32_t aErrorCode)
 
void OnServiceRemoved (uint32_t aNetifIndex, const std::string &aType, const std::string &aInstanceName)
 
void OnHostResolved (const std::string &aHostName, const DiscoveredHostInfo &aHostInfo)
 
void OnHostResolveFailed (const std::string &aHostName, int32_t aErrorCode)
 
ResultCallback HandleDuplicateServiceRegistration (const std::string &aHostName, const std::string &aName, const std::string &aType, const SubTypeList &aSubTypeList, uint16_t aPort, const TxtList &aTxtList, ResultCallback &&aCallback)
 
ResultCallback HandleDuplicateHostRegistration (const std::string &aName, const std::vector< Ip6Address > &aAddresses, ResultCallback &&aCallback)
 
void AddHostRegistration (HostRegistrationPtr &&aHostReg)
 
void RemoveHostRegistration (const std::string &aName, otbrError aError)
 
HostRegistrationFindHostRegistration (const std::string &aName)
 
void UpdateServiceRegistrationEmaLatency (const std::string &aInstanceName, const std::string &aType, otbrError aError)
 
void UpdateHostRegistrationEmaLatency (const std::string &aHostName, otbrError aError)
 
void UpdateServiceInstanceResolutionEmaLatency (const std::string &aInstanceName, const std::string &aType, otbrError aError)
 
void UpdateHostResolutionEmaLatency (const std::string &aHostName, otbrError aError)
 

Static Protected Member Functions

static SubTypeList SortSubTypeList (SubTypeList aSubTypeList)
 
static TxtList SortTxtList (TxtList aTxtList)
 
static AddressList SortAddressList (AddressList aAddressList)
 
static std::string MakeFullServiceName (const std::string &aName, const std::string &aType)
 
static std::string MakeFullHostName (const std::string &aName)
 
static void UpdateMdnsResponseCounters (otbr::MdnsResponseCounters &aCounters, otbrError aError)
 
static void UpdateEmaLatency (uint32_t &aEmaLatency, uint32_t aLatency, otbrError aError)
 

Protected Attributes

ServiceRegistrationMap mServiceRegistrations
 
HostRegistrationMap mHostRegistrations
 
uint64_t mNextSubscriberId = 1
 
std::map< uint64_t, std::pair< DiscoveredServiceInstanceCallback, DiscoveredHostCallback > > mDiscoveredCallbacks
 
std::map< std::pair< std::string, std::string >, Timepoint > mServiceRegistrationBeginTime
 
std::map< std::string, Timepoint > mHostRegistrationBeginTime
 
std::map< std::pair< std::string, std::string >, Timepoint > mServiceInstanceResolutionBeginTime
 
std::map< std::string, Timepoint > mHostResolutionBeginTime
 
otbr::MdnsTelemetryInfo mTelemetryInfo {}
 

Static Protected Attributes

static constexpr uint8_t kMaxTextEntrySize = 255
 

Detailed Description

This interface defines the functionality of mDNS publisher.

Member Typedef Documentation

◆ DiscoveredHostCallback

using otbr::Mdns::Publisher::DiscoveredHostCallback = std::function<void(const std::string &aHostName, const DiscoveredHostInfo &aHostInfo)>

This function is called to notify a discovered host.

◆ DiscoveredServiceInstanceCallback

using otbr::Mdns::Publisher::DiscoveredServiceInstanceCallback = std::function<void(const std::string &aType, const DiscoveredInstanceInfo &aInstanceInfo)>

This function is called to notify a discovered service instance.

◆ ResultCallback

The callback for receiving the result of a operation.

◆ StateCallback

using otbr::Mdns::Publisher::StateCallback = std::function<void(State aNewState)>

The callback for receiving mDNS publisher state changes.

Member Enumeration Documentation

◆ State

mDNS state values.

Enumerator
kIdle 

Unable to publish service.

kReady 

Ready to publish service.

Member Function Documentation

◆ AddSubscriptionCallbacks()

uint64_t otbr::Mdns::Publisher::AddSubscriptionCallbacks ( Publisher::DiscoveredServiceInstanceCallback  aInstanceCallback,
Publisher::DiscoveredHostCallback  aHostCallback 
)

This method sets the callbacks for subscriptions.

Parameters
[in]aInstanceCallbackThe callback function to receive discovered service instances.
[in]aHostCallbackThe callback function to receive discovered hosts.
Returns
The Subscriber ID for the callbacks.

◆ Create()

Publisher * otbr::Mdns::Publisher::Create ( StateCallback  aCallback)
static

This function creates a mDNS publisher.

Parameters
[in]aCallbackThe callback for receiving mDNS publisher state changes.
Returns
A pointer to the newly created mDNS publisher.

◆ DecodeTxtData()

otbrError otbr::Mdns::Publisher::DecodeTxtData ( Publisher::TxtList &  aTxtList,
const uint8_t *  aTxtData,
uint16_t  aTxtLength 
)
static

This function decodes a TXT entry list from a TXT data buffer.

The input data should be in standard DNS-SD TXT data format. See RFC 6763 for details: https://tools.ietf.org/html/rfc6763#section-6.

Parameters
[out]aTxtListA TXT entry list.
[in]aTxtDataA pointer to TXT data.
[in]aTxtLengthThe TXT data length.
Return values
OTBR_ERROR_NONESuccessfully decoded the TXT data.
OTBR_ERROR_INVALID_ARGSThe aTxtdata has invalid TXT format.
See also
EncodeTxtData

◆ Destroy()

void otbr::Mdns::Publisher::Destroy ( Publisher aPublisher)
static

This function destroys the mDNS publisher.

Parameters
[in]aPublisherA pointer to the publisher.

◆ EncodeTxtData()

otbrError otbr::Mdns::Publisher::EncodeTxtData ( const TxtList &  aTxtList,
std::vector< uint8_t > &  aTxtData 
)
static

This function writes the TXT entry list to a TXT data buffer. The TXT entries will be sorted by their keys.

The output data is in standard DNS-SD TXT data format. See RFC 6763 for details: https://tools.ietf.org/html/rfc6763#section-6.

Parameters
[in]aTxtListA TXT entry list.
[out]aTxtDataA TXT data buffer.
Return values
OTBR_ERROR_NONESuccessfully write the TXT entry list.
OTBR_ERROR_INVALID_ARGSThe aTxtList includes invalid TXT entry.
See also
DecodeTxtData

◆ GetMdnsTelemetryInfo()

const MdnsTelemetryInfo& otbr::Mdns::Publisher::GetMdnsTelemetryInfo ( ) const
inline

This method returns the mDNS statistics information of the publisher.

Returns
The MdnsTelemetryInfo of the publisher.

◆ IsStarted()

virtual bool otbr::Mdns::Publisher::IsStarted ( void  ) const
pure virtual

This method checks if publisher has been started.

Return values
trueAlready started.
falseNot started.

Implemented in otbr::Mdns::PublisherAvahi, and otbr::Mdns::PublisherMDnsSd.

◆ PublishHost()

void otbr::Mdns::Publisher::PublishHost ( const std::string &  aName,
const std::vector< Ip6Address > &  aAddresses,
ResultCallback &&  aCallback 
)

This method publishes or updates a host.

Publishing a host is advertising an AAAA RR for the host name. This method should be called before a service with non-empty host name is published.

Parameters
[in]aNameThe name of the host.
[in]aAddressesThe addresses of the host.
[in]aCallbackThe callback for receiving the publishing result.OTBR_ERROR_NONE will be returned if the operation is successful and all other values indicate a failure. Specifically, OTBR_ERROR_DUPLICATED indicates that the name has already been published and the caller can re-publish with a new name if an alternative name is available/acceptable.

◆ PublishService()

void otbr::Mdns::Publisher::PublishService ( const std::string &  aHostName,
const std::string &  aName,
const std::string &  aType,
const SubTypeList &  aSubTypeList,
uint16_t  aPort,
const TxtList &  aTxtList,
ResultCallback &&  aCallback 
)

This method publishes or updates a service.

Parameters
[in]aHostNameThe name of the host which this service resides on. If an empty string is provided, this service resides on local host and it is the implementation to provide specific host name. Otherwise, the caller MUST publish the host with method PublishHost.
[in]aNameThe name of this service.
[in]aTypeThe type of this service.
[in]aSubTypeListA list of service subtypes.
[in]aPortThe port number of this service.
[in]aTxtListA list of TXT name/value pairs.
[in]aCallbackThe callback for receiving the publishing result. OTBR_ERROR_NONE will be returned if the operation is successful and all other values indicate a failure. Specifically, OTBR_ERROR_DUPLICATED indicates that the name has already been published and the caller can re-publish with a new name if an alternative name is available/acceptable.

◆ RemoveSubscriptionCallbacks()

void otbr::Mdns::Publisher::RemoveSubscriptionCallbacks ( uint64_t  aSubscriberId)

This method cancels callbacks for subscriptions.

Parameters
[in]aSubscriberIdThe Subscriber ID previously returned by AddSubscriptionCallbacks.

◆ Start()

virtual otbrError otbr::Mdns::Publisher::Start ( void  )
pure virtual

This method starts the mDNS publisher.

Return values
OTBR_ERROR_NONESuccessfully started mDNS publisher;
OTBR_ERROR_MDNSFailed to start mDNS publisher.

Implemented in otbr::Mdns::PublisherAvahi, and otbr::Mdns::PublisherMDnsSd.

◆ Stop()

virtual void otbr::Mdns::Publisher::Stop ( void  )
pure virtual

This method stops the mDNS publisher.

Implemented in otbr::Mdns::PublisherAvahi, and otbr::Mdns::PublisherMDnsSd.

◆ SubscribeHost()

virtual void otbr::Mdns::Publisher::SubscribeHost ( const std::string &  aHostName)
pure virtual

This method subscribes a given host.

mDNS implementations should use the DiscoveredHostCallback function to notify discovered hosts.

Note
Discovery Proxy implementation guarantees no duplicate subscriptions for the same host.
Parameters
[in]aHostNameThe host name (without domain).

Implemented in otbr::Mdns::PublisherAvahi, and otbr::Mdns::PublisherMDnsSd.

◆ SubscribeService()

virtual void otbr::Mdns::Publisher::SubscribeService ( const std::string &  aType,
const std::string &  aInstanceName 
)
pure virtual

This method subscribes a given service or service instance.

If aInstanceName is not empty, this method subscribes the service instance. Otherwise, this method subscribes the service. mDNS implementations should use the DiscoveredServiceInstanceCallback function to notify discovered service instances.

Note
Discovery Proxy implementation guarantees no duplicate subscriptions for the same service or service instance.
Parameters
[in]aTypeThe service type.
[in]aInstanceNameThe service instance to subscribe, or empty to subscribe the service.

Implemented in otbr::Mdns::PublisherAvahi, and otbr::Mdns::PublisherMDnsSd.

◆ UnpublishHost()

virtual void otbr::Mdns::Publisher::UnpublishHost ( const std::string &  aName,
ResultCallback &&  aCallback 
)
pure virtual

This method un-publishes a host.

Parameters
[in]aNameA host name.
[in]aCallbackThe callback for receiving the publishing result.

Implemented in otbr::Mdns::PublisherAvahi, and otbr::Mdns::PublisherMDnsSd.

◆ UnpublishService()

virtual void otbr::Mdns::Publisher::UnpublishService ( const std::string &  aName,
const std::string &  aType,
ResultCallback &&  aCallback 
)
pure virtual

This method un-publishes a service.

Parameters
[in]aNameThe name of this service.
[in]aTypeThe type of this service.
[in]aCallbackThe callback for receiving the publishing result.

Implemented in otbr::Mdns::PublisherAvahi, and otbr::Mdns::PublisherMDnsSd.

◆ UnsubscribeHost()

virtual void otbr::Mdns::Publisher::UnsubscribeHost ( const std::string &  aHostName)
pure virtual

This method unsubscribes a given host.

Note
Discovery Proxy implementation guarantees no redundant unsubscription for a host.
Parameters
[in]aHostNameThe host name (without domain).

Implemented in otbr::Mdns::PublisherAvahi, and otbr::Mdns::PublisherMDnsSd.

◆ UnsubscribeService()

virtual void otbr::Mdns::Publisher::UnsubscribeService ( const std::string &  aType,
const std::string &  aInstanceName 
)
pure virtual

This method unsubscribes a given service or service instance.

If aInstanceName is not empty, this method unsubscribes the service instance. Otherwise, this method unsubscribes the service.

Note
Discovery Proxy implementation guarantees no redundant unsubscription for a service or service instance.
Parameters
[in]aTypeThe service type.
[in]aInstanceNameThe service instance to unsubscribe, or empty to unsubscribe the service.

Implemented in otbr::Mdns::PublisherAvahi, and otbr::Mdns::PublisherMDnsSd.


The documentation for this class was generated from the following files: