Class: Frank::Cucumber::Bonjour

Inherits:
Object
  • Object
show all
Defined in:
lib/frank-cucumber/bonjour.rb

Constant Summary collapse

FRANK_SERVICE_NAME =
'Frank UISpec server'
FRANK_PORT =
37265
LOOKUP_TIMEOUT =
10

Instance Method Summary collapse

Instance Method Details

#browse_for_franks_addressObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/frank-cucumber/bonjour.rb', line 44

def browse_for_franks_address
  require 'dnssd'

  DNSSD.browse! '_http._tcp.' do |reply|
    debug 'got a reply'
    if reply.name == FRANK_SERVICE_NAME
      address = found_a_frank(reply)
      if address
        debug "OK WE HAVE AN ADDRESS: #{address}"
        return address
      end
    end
  end
end

#debug(string) ⇒ Object



13
14
15
# File 'lib/frank-cucumber/bonjour.rb', line 13

def debug string
  puts string if $DEBUG
end

#found_a_frank(reply) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/frank-cucumber/bonjour.rb', line 17

def found_a_frank( reply )
  debug reply.inspect
  unless reply.flags.add?
    debug 'got a non-add reply'
    debug "flags: #{reply.flags.to_a.inspect}"
    return nil
  end

  resolve_service = DNSSD::Service.new
  addr_service = DNSSD::Service.new
  resolve_service.resolve reply do |r|
    debug "#{r.name} on #{r.target}:#{r.port}"

    address = nil
    addr_service.getaddrinfo r.target do |addrinfo|
      ipaddr = (IPAddr.new(addrinfo.address) rescue nil)
      if ipaddr != nil and ipaddr.family == Socket::AF_INET
        address = addrinfo.address
        break
      end
    end

    debug "first address for #{r.target} is #{address}"
    return address
  end
end

#lookup_frank_base_uriObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/frank-cucumber/bonjour.rb', line 59

def lookup_frank_base_uri
  puts "finding Frank server via Bonjour..."
  address = begin
    Timeout::timeout(LOOKUP_TIMEOUT){ address = browse_for_franks_address }
  rescue Timeout::Error
    puts "could not find Frank within #{LOOKUP_TIMEOUT} seconds"
  end

  if address
    puts "...found Frank via Bonjour: #{address}"
    return URI::HTTP.new( 'http', nil, address, FRANK_PORT, nil, nil, nil, nil, nil )
  else
    puts '...failed to find Frank server via Bonjour'
    return nil
  end
end