class DACPClient::PairingServer

The pairingserver handles pairing with iTunes

Constants

MDNS_TYPE

Attributes

device_type[RW]
peer[R]
pin[RW]

Public Class Methods

generate_pin_challenge(pair, pin) click to toggle source
# File lib/dacpclient/pairingserver.rb, line 41
def self.generate_pin_challenge(pair, pin)
  pin_string = pin.map { |i| "#{i}\x00" }.join
  Digest::MD5.hexdigest(pair.upcase + pin_string).upcase
end
new(name, guid, host = '0.0.0.0', port = 1024) click to toggle source
Calls superclass method
# File lib/dacpclient/pairingserver.rb, line 14
def initialize(name, guid, host = '0.0.0.0', port = 1024)
  @name = name
  @port = port
  @host = host
  @pair = guid
  @pin = [0, 0, 0, 0]
  @peer = nil
  @device_type = 'iPod'
  super port, host
end

Public Instance Methods

serve(client) click to toggle source
# File lib/dacpclient/pairingserver.rb, line 46
def serve(client)
  data = client.gets

  @peer = @browser.services.find do |service|
    data =~ /servicename=#{service.name}/i
  end

  if data =~ /pairingcode=#{@expected}/i && @peer
    client.print "HTTP/1.1 200 OK\n" \
                 "Content-Length: #{@pairing_string.length}\n\n"
    client.print @pairing_string
    client.close
    stop
  else
    client.print "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"
    client.close
  end
end
start() click to toggle source
Calls superclass method
# File lib/dacpclient/pairingserver.rb, line 25
def start
  @pairing_string = generate_pairing_string(@pair, @name, @device_type)
  @expected = PairingServer.generate_pin_challenge(@pair, @pin)
  @service = DNSSD.register!(@name, MDNS_TYPE, 'local', @port, text_record)
  @browser = DACPClient::Browser.new
  @browser.browse

  super
  join

  @service.stop

  sleep 0.5 # sleep so iTunes accepts our login
  peer
end

Private Instance Methods

generate_pairing_string(pair, name, device_type) click to toggle source
# File lib/dacpclient/pairingserver.rb, line 78
def generate_pairing_string(pair, name, device_type)
  PairInfo.build_dmap(pairing_code: pair, name: name, type: device_type)
end
text_record() click to toggle source
# File lib/dacpclient/pairingserver.rb, line 67
def text_record
  DNSSD::TextRecord.new(
    'DvNm' => @name,
    'Revm' => '10000',
    'DvTy' => @device_type,
    'RemN' => 'Remote',
    'txtvers' => '1',
    'Pair' => @pair.upcase
  )
end