class Airplay::Browser

Public: Browser class to find Airplay-enabled devices in the network

Constants

NoDevicesFound

Public Class Methods

new() click to toggle source
# File lib/airplay/browser.rb, line 15
def initialize
  @logger = Airplay::Logger.new("airplay::browser")
end

Public Instance Methods

browse() click to toggle source

Public: Browses in the search of devices and adds them to the nodes

Returns nothing or raises NoDevicesFound if there are no devices

# File lib/airplay/browser.rb, line 23
def browse
  timeout(5) do
    nodes = []
    DNSSD.browse!(SEARCH) do |node|
      nodes << node
      next if node.flags.more_coming?

      nodes.each do |node|
        resolve(node)
      end

      break
    end
  end
rescue Timeout::Error => e
  raise NoDevicesFound
end
devices() click to toggle source

Public: Access to the node list

Returns the Devices list object

# File lib/airplay/browser.rb, line 45
def devices
  @_devices ||= Devices.new
end

Private Instance Methods

create_device(name, address, type) click to toggle source

Private: Creates a device

name - The device name address - The device address

Returns nothing

# File lib/airplay/browser.rb, line 116
def create_device(name, address, type)
  Device.new(
    name:     name.gsub(/\u00a0/, ' '),
    address:  address,
    type:     type
  )
end
get_device_address(resolved) click to toggle source

Private: Resolves the node complete address

resolved - The DNS Resolved object

Returns a string with the address (host:ip)

# File lib/airplay/browser.rb, line 91
def get_device_address(resolved)
  host = get_device_host(resolved.target)
  "#{host}:#{resolved.port}"
end
get_device_host(target) click to toggle source

Private: Resolves the node ip or hostname

resolved - The DNS Resolved object

Returns a string with the ip or the hostname

# File lib/airplay/browser.rb, line 102
def get_device_host(target)
  info = Socket.getaddrinfo(target, nil, Socket::AF_INET)
  info[0][2]
rescue SocketError
  target
end
get_type(records) click to toggle source

Private: Gets the device type

records - The text records hash to be investigated

Returns a symbol with the type

# File lib/airplay/browser.rb, line 76
def get_type(records)
  # rhd means Remote HD the first product of the Airserver people
  if records.has_key?("rhd")
    :airserver
  else
    :apple_tv
  end
end
node_resolver(node, resolved) click to toggle source

Private: Resolves a node given a node and a resolver

node - The given node resolver - The DNSSD::Server that is resolving nodes

Returns if there are more nodes coming

# File lib/airplay/browser.rb, line 58
def node_resolver(node, resolved)
  address = get_device_address(resolved)
  type = get_type(resolved.text_record)

  device = create_device(node.name, address, type)
  device.text_records = resolved.text_record

  devices << device

  resolved.flags.more_coming?
end
resolve(node) click to toggle source

Private: Resolves the node information given a node

node - The node from the DNSSD browsing

Returns nothing

# File lib/airplay/browser.rb, line 130
def resolve(node)
  DNSSD.resolve(node) do |resolved|
    break unless node_resolver(node, resolved)
  end
end