Class: DNSSD::Service
- Inherits:
-
Object
- Object
- DNSSD::Service
- Includes:
- ResourceCache
- Defined in:
- lib/dns-sd/service.rb
Overview
The generic service.
Allows you to get the list of instances of a given service.
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
The name of the service, without the leading underscore.
-
#protocol ⇒ Symbol
readonly
The protocol of the service.
Instance Method Summary collapse
-
#cached_until ⇒ Time?
Let us know how long until the cache expires.
-
#initialize(fqdn) ⇒ Service
constructor
Create the service.
-
#instance(name) ⇒ DNSSD::ServiceInstance
Create an object for a specific instance of this service.
-
#instances ⇒ Hash<String, DNSSD::ServiceInstance>
Enumerate all existing instances of this service.
Constructor Details
#initialize(fqdn) ⇒ Service
Create the service.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dns-sd/service.rb', line 30 def initialize(fqdn) unless fqdn.is_a?(Resolv::DNS::Name) raise ArgumentError, "FQDN must be a Resolv::DNS::Name (got an instance of #{fqdn.class})" end @fqdn = fqdn if fqdn[0].to_s =~ /\A_([A-Za-z0-9][A-Za-z0-9-]+)\z/ @name = $1 else raise ArgumentError, "Invalid service name #{fqdn[0].inspect}; see RFC6763 s. 7" end @protocol = case fqdn[1].to_s.downcase when "_tcp" :TCP when "_udp" :UDP else raise ArgumentError, "Invalid service protocol #{@protocol}, must be '_tcp' or '_udp'" end end |
Instance Attribute Details
#name ⇒ String (readonly)
The name of the service, without the leading underscore.
18 19 20 |
# File 'lib/dns-sd/service.rb', line 18 def name @name end |
#protocol ⇒ Symbol (readonly)
The protocol of the service.
24 25 26 |
# File 'lib/dns-sd/service.rb', line 24 def protocol @protocol end |
Instance Method Details
#cached_until ⇒ Time?
Let us know how long until the cache expires.
This can be handy if you've got something that wants to poll the record repeatedly; this way we can be a bit more intelligent about when to retry, since if we re-poll too often, we'll just get the cached data back again anyway.
92 93 94 |
# File 'lib/dns-sd/service.rb', line 92 def cached_until entry_expiry_time(@fqdn, Resolv::DNS::Resource::IN::PTR) end |
#instance(name) ⇒ DNSSD::ServiceInstance
Create an object for a specific instance of this service.
62 63 64 |
# File 'lib/dns-sd/service.rb', line 62 def instance(name) DNSSD::ServiceInstance.new(Resolv::DNS::Name.new([name] + @fqdn.to_a)) end |
#instances ⇒ Hash<String, DNSSD::ServiceInstance>
Enumerate all existing instances of this service.
71 72 73 74 75 76 77 78 |
# File 'lib/dns-sd/service.rb', line 71 def instances {}.tap do |instances| cached_resources(@fqdn, Resolv::DNS::Resource::IN::PTR).each do |rr| i = DNSSD::ServiceInstance.new(rr.name) instances[i.name] = i end end end |