Class: DNSSD::Service

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(fqdn) ⇒ Service

Create the service.

Parameters:

  • fqdn (Resolv::DNS::Name)


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

#nameString (readonly)

The name of the service, without the leading underscore.

Returns:

  • (String)


18
19
20
# File 'lib/dns-sd/service.rb', line 18

def name
  @name
end

#protocolSymbol (readonly)

The protocol of the service.

Returns:

  • (Symbol)

    :TCP or :UDP.



24
25
26
# File 'lib/dns-sd/service.rb', line 24

def protocol
  @protocol
end

Instance Method Details

#cached_untilTime?

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.

Returns:

  • (Time, nil)

    if the entry is currently cached, a Time instance will be returned indicating when the entry will expire (potentially this could be in the past, if the cache has expired). If we have no knowledge of the entry, nil will be returned.



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.

Parameters:

  • name (String)

    the name of the service instance.

Returns:



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

#instancesHash<String, DNSSD::ServiceInstance>

Enumerate all existing instances of this service.

Returns:



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