Class: DACPClient::PairingServer

Inherits:
GServer
  • Object
show all
Defined in:
lib/dacpclient/pairingserver.rb

Overview

The pairingserver handles pairing with iTunes

Constant Summary collapse

MDNS_TYPE =
'_touch-remote._tcp'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, host, port = 1024) ⇒ PairingServer

Returns a new instance of PairingServer.



12
13
14
15
16
17
18
19
20
# File 'lib/dacpclient/pairingserver.rb', line 12

def initialize(client, host, port = 1024)
  @name = client.name
  @port = port
  @host = host
  @pair = client.guid
  @pin = [0, 0, 0, 0]
  @device_type = 'iPod'
  super port, host
end

Instance Attribute Details

#device_typeObject

Returns the value of attribute device_type.



8
9
10
# File 'lib/dacpclient/pairingserver.rb', line 8

def device_type
  @device_type
end

#pinObject

Returns the value of attribute pin.



8
9
10
# File 'lib/dacpclient/pairingserver.rb', line 8

def pin
  @pin
end

Class Method Details

.generate_pin_challenge(pair, pin) ⇒ Object



35
36
37
38
# File 'lib/dacpclient/pairingserver.rb', line 35

def self.generate_pin_challenge(pair, pin)
  pin_string = pin.map { |i| "#{i}\x00" }.join
  Digest::MD5.hexdigest(pair.upcase + pin_string)
end

Instance Method Details

#serve(client) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dacpclient/pairingserver.rb', line 40

def serve(client)
  if client.gets =~ /pairingcode=#{@expected}/i
    client.print "HTTP/1.1 200 OK\r\n" +
                 "Content-Length: #{@pairing_string.length}\r\n\r\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

#startObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dacpclient/pairingserver.rb', line 22

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)

  super
  join

  @service.stop

  sleep 0.5 # sleep so iTunes accepts our login
end