module DNSSD

Constants

VERSION

The version of DNSSD you're using.

Public Class Methods

announce(socket, name, service = nil, text_record = nil, flags = 0, interface = DNSSD::InterfaceAny, &block) click to toggle source

Registers socket with DNSSD as name. If service is omitted it is looked up using getservbyport and the ports address. text_record, flags and interface are used as in register.

Returns the Service created by registering the socket. The Service will automatically be shut down when close or close_read is called on the socket.

Only for bound TCP and UDP sockets.

# File lib/dnssd.rb, line 29
def self.announce(socket, name, service = nil, text_record = nil, flags = 0,
                  interface = DNSSD::InterfaceAny, &block)
  _, port, = socket.addr

  raise ArgumentError, 'socket not bound' if port == 0

  service ||= DNSSD.getservbyport port

  proto = case socket
          when TCPSocket then 'tcp'
          when UDPSocket then 'udp'
          else raise ArgumentError, 'tcp or udp sockets only'
          end

  type = "_#{service}._#{proto}"

  registrar = register(name, type, nil, port, text_record, flags, interface,
                       &block)

  socket.instance_variable_set :@registrar, registrar

  def socket.close
    result = super
    @registrar.stop
    return result
  end

  def socket.close_read
    result = super
    @registrar.stop
    return result
  end

  registrar
end
browse(type, domain = nil, flags = 0, interface = DNSSD::InterfaceAny, &block) click to toggle source

Asynchronous version of DNSSD::Service#browse

# File lib/dnssd.rb, line 68
def self.browse(type, domain = nil, flags = 0,
                interface = DNSSD::InterfaceAny, &block)
  service = DNSSD::Service.new

  Thread.start do
    run(service, :browse, type, domain, flags, interface, &block)
  end

  service
end
browse!(type, domain = nil, flags = 0, interface = DNSSD::InterfaceAny, &block) click to toggle source

Synchronous version of DNSSD::Service#browse

# File lib/dnssd.rb, line 82
def self.browse!(type, domain = nil, flags = 0,
                interface = DNSSD::InterfaceAny, &block)
  service = DNSSD::Service.new

  run(service, :browse, type, domain, flags, interface, &block)
end
enumerate_domains(flags = DNSSD::Flags::BrowseDomains, interface = DNSSD::InterfaceAny, &block) click to toggle source

Asynchronous version of DNSSD::Service#enumerate_domains

# File lib/dnssd.rb, line 92
def self.enumerate_domains(flags = DNSSD::Flags::BrowseDomains,
                           interface = DNSSD::InterfaceAny, &block)
  service = DNSSD::Service.new

  Thread.start do
    run(service, :enumerate_domains, flags, interface, &block)
  end

  service
end
enumerate_domains!(flags = DNSSD::Flags::BrowseDomains, interface = DNSSD::InterfaceAny, &block) click to toggle source

Synchronous version of DNSSD::Service#enumerate_domains

# File lib/dnssd.rb, line 106
def self.enumerate_domains!(flags = DNSSD::Flags::BrowseDomains,
                            interface = DNSSD::InterfaceAny, &block)
  service = DNSSD::Service.new

  run(service, :enumerate_domains, flags, interface, &block)
end
register(name, type, domain, port, text_record = nil, flags = 0, interface = DNSSD::InterfaceAny, &block) click to toggle source

Asynchronous version of DNSSD::Service#register

# File lib/dnssd.rb, line 116
def self.register(name, type, domain, port, text_record = nil, flags = 0,
                  interface = DNSSD::InterfaceAny, &block)
  service = DNSSD::Service.new

  if block_given? then
    Thread.start do
      run(service, :register, name, type, domain, port, nil, text_record,
          flags, interface, &block)
    end
  else
    service.register name, type, domain, port, nil, text_record, flags,
                     interface
  end

  service
end
register!(name, type, domain, port, text_record = nil, flags = 0, interface = DNSSD::InterfaceAny, &block) click to toggle source

Synchronous version of DNSSD::Service#register

# File lib/dnssd.rb, line 136
def self.register!(name, type, domain, port, text_record = nil, flags = 0,
                   interface = DNSSD::InterfaceAny, &block)
  service = DNSSD::Service.new

  if block_given? then
    run(service, :register, name, type, domain, port, nil, text_record, flags,
        interface, &block)
  else
    service.register name, type, domain, port, nil, text_record, flags,
                     interface
  end

  service
end
resolve(*args, &block) click to toggle source

Asynchronous version of DNSSD::Service#resolve

# File lib/dnssd.rb, line 154
def self.resolve(*args, &block)
  service = DNSSD::Service.new

  Thread.start do
    run(service, :resolve, *args, &block)
  end

  service
end
resolve!(*args, &block) click to toggle source

Synchronous version of DNSSD::Service#resolve

# File lib/dnssd.rb, line 167
def self.resolve!(*args, &block)
  service = DNSSD::Service.new

  run(service, :resolve, *args, &block)
end
run(service, method, *args, &block) click to toggle source

Dispatches args and block to method on service and ensures service is shut down after use.

# File lib/dnssd.rb, line 177
def self.run(service, method, *args, &block)
  service.send(method, *args, &block)

  service
ensure
  service.stop unless service.stopped?
end