Class: DNSSD::Reply::Resolve

Inherits:
DNSSD::Reply show all
Defined in:
lib/dnssd/reply/resolve.rb

Overview

Created by DNSSD::Service#resolve

Instance Attribute Summary collapse

Attributes inherited from DNSSD::Reply

#flags, #interface, #service

Instance Method Summary collapse

Methods inherited from DNSSD::Reply

#fullname, #interface_name, #protocol, #service_name, #set_fullname, #set_names

Constructor Details

#initialize(service, flags, interface, fullname, target, port, text_record) ⇒ Resolve

Creates a new Resolve, called internally by DNSSD::Service#resolve



39
40
41
42
43
44
45
46
47
48
# File 'lib/dnssd/reply/resolve.rb', line 39

def initialize(service, flags, interface, fullname, target, port,
               text_record)
  super service, flags, interface

  set_fullname fullname

  @target = target
  @port = port
  @text_record = DNSSD::TextRecord.new text_record
end

Instance Attribute Details

#domainObject (readonly)

A domain for registration or browsing



9
10
11
# File 'lib/dnssd/reply/resolve.rb', line 9

def domain
  @domain
end

#nameObject (readonly)

The service name



14
15
16
# File 'lib/dnssd/reply/resolve.rb', line 14

def name
  @name
end

#portObject (readonly)

The port for this service



19
20
21
# File 'lib/dnssd/reply/resolve.rb', line 19

def port
  @port
end

#targetObject (readonly)

The hostname of the host provide the service



24
25
26
# File 'lib/dnssd/reply/resolve.rb', line 24

def target
  @target
end

#text_recordObject (readonly)

The service’s primary text record



29
30
31
# File 'lib/dnssd/reply/resolve.rb', line 29

def text_record
  @text_record
end

#typeObject (readonly)

The service type



34
35
36
# File 'lib/dnssd/reply/resolve.rb', line 34

def type
  @type
end

Instance Method Details

#connect(family = Socket::AF_UNSPEC, addrinfo_flags = 0) ⇒ Object

Connects to this Reply. If #target and #port are missing, DNSSD.resolve is automatically called.

family can be used to select a particular address family (IPv6 vs IPv4).

addrinfo_flags are passed to DNSSD::Service#getaddrinfo as flags.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dnssd/reply/resolve.rb', line 58

def connect(family = Socket::AF_UNSPEC, addrinfo_flags = 0)
  addrinfo_protocol = case family
                      when Socket::AF_INET   then DNSSD::Service::IPv4
                      when Socket::AF_INET6  then DNSSD::Service::IPv6
                      when Socket::AF_UNSPEC then 0
                      else raise ArgumentError, "invalid family #{family}"
                      end

  service = DNSSD::Service.getaddrinfo target, addrinfo_protocol,
    addrinfo_flags, @interface

  service.each do |addrinfo|
    address = addrinfo.address

    begin
      socket = nil

      case protocol
      when 'tcp' then
        socket = TCPSocket.new address, port
      when 'udp' then
        socket = UDPSocket.new
        socket.connect address, port
      end

      service.stop
      return socket
    rescue
      next if addrinfo.flags.more_coming?
      raise
    end
  end
end

#inspectObject

:nodoc:



92
93
94
95
96
97
# File 'lib/dnssd/reply/resolve.rb', line 92

def inspect # :nodoc:
  "#<%s:0x%x %s at %s:%d text_record: %p interface: %s flags: %p>" % [
    self.class, object_id,
    fullname, @target, @port, @text_record, interface_name, @flags
  ]
end