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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/frank-cucumber/bonjour.rb', line 39

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



11
12
13
# File 'lib/frank-cucumber/bonjour.rb', line 11

def debug string
  puts string if $DEBUG
end

#found_a_frank(reply) ⇒ Object



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

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|
      address = addrinfo.address
      break
    end

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

#lookup_frank_base_uriObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/frank-cucumber/bonjour.rb', line 54

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