Module: Appjour

Defined in:
lib/appjour.rb,
lib/appjour/version.rb

Defined Under Namespace

Classes: App

Constant Summary collapse

SERVICE =
"_http._tcp"
VERSION =
"0.1.1".freeze

Class Method Summary collapse

Class Method Details

.find(name, first = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/appjour.rb', line 28

def self.find(name,first=nil)
  hosts = Set.new

  waiting = Thread.current
  
  STDERR.puts "searching for #{name}"

  service = DNSSD.browse(SERVICE) do |reply|
    if name === reply.name
      DNSSD.resolve(reply.name, reply.type, reply.domain) do |rr|
        hosts << App.new(reply.name, rr.target, rr.port)
        waiting.run if first
      end
    end
  end
  
  # wait to resolve multiple apps
  sleep 5
  service.stop

  hosts
end

.guess_port(kind) ⇒ Object



83
84
85
86
87
# File 'lib/appjour.rb', line 83

def self.guess_port(kind)
  case kind
  when :mongrel
  end
end

.listObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/appjour.rb', line 12

def self.list
  servers = {}
  service = DNSSD.browse(SERVICE) do |reply|
    servers[reply.name] ||= reply
  end
  STDERR.puts "Searching for servers (3 seconds)"
  # Wait for something to happen
  sleep 3
  service.stop
  servers.each { |string,obj| 
    name, port = string.split ":" 
    STDERR.puts "Found web app called '#{name}'"
  }
end

.open(name) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/appjour.rb', line 51

def self.open(name)
  host = find(name,true).to_a[0]
  
  unless host
    STDERR.puts "unable to find #{name}"
  else
    system "open 'http://#{host.host}:#{host.port}'"
  end
end

.publish(name, port = :guess) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/appjour.rb', line 64

def self.publish(name,port=:guess)
  STDERR.puts "Publishing #{name} on #{port}"
  
  if port.is_a?(Symbol)
    port = guess_port(port)
  else
    port = port.to_i
  end
  
  tr = DNSSD::TextRecord.new
  tr["description"] = "An app."
  
  DNSSD.register(name, SERVICE, "local", port.to_i, tr.encode) do |reply|
    STDERR.puts "Announcing #{name}..."
  end
  
  sleep
end