bastos (owner)

Revisions

gist: 35917 Download_button fork
public
Public Clone URL: git://gist.github.com/35917.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require "rubygems"
require "dnssd"
require "set"
require "socket"
require "webrick"
 
module AliveJour
  include Socket::Constants
 
  Paste = Struct.new(:name, :host, :port)
 
  SERVICE = "_mongrelintercon._tcp"
 
  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|
      STDERR.puts "Found mongrels: '#{string}'"
    }
  end
  
 
  def self.find(name, first=true)
    hosts = Set.new
 
    waiting = Thread.current
 
    service = DNSSD.browse(SERVICE) do |reply|
      if name === reply.name
        DNSSD.resolve(reply.name, reply.type, reply.domain) do |rr|
          hosts << Paste.new(reply.name, rr.target, rr.port)
          waiting.run if first
        end
      end
    end
 
    sleep 5
    service.stop
 
    hosts
  end
 
  def self.get(name)
    hosts = find(name)
 
    if hosts.empty?
      STDERR.puts "ERROR: Unable to find #{name}"
    elsif hosts.size > 1
      STDERR.puts "ERROR: Multiple possibles found:"
      hosts.each do |host|
        STDERR.puts " #{host.name} (#{host.host}:#{host.port})"
      end
    else
      # Set is weird. There is no #[] or #at
      hosts.each do |host|
        STDERR.puts "(#{host.name} from #{host.host}:#{host.port})"
        sock = TCPSocket.open host.host, host.port
        return sock.read
      end
    end
  end
 
  def self.serve(name, multiple, contents, port)
    tr = DNSSD::TextRecord.new
    tr["description"] = "Im #{$$}."
    
    DNSSD.register(name, SERVICE, "local", port, tr.encode) do |reply|
      STDERR.puts "MONGREL ALIVE #{name} "
    end
 
    log = WEBrick::Log.new(true) # true fools it
    def log.log(*anything); end # send it to the abyss
 
    server = WEBrick::GenericServer.new(:Port => port, :Logger => log)
 
    %w(INT TERM).each do |signal|
      trap signal do
        server.shutdown
        exit!
      end
    end
 
    server.start do |socket|
      socket.print(contents)
      server.shutdown unless multiple
    end
  end
end
 
 
=begin
if ARGV.first == "list"
AliveJour.list()
else
Thread.abort_on_exception = true
alivejour = Thread.new do
JOUR_PORT = "#{OPTIONS[:port]}0".to_i
AliveJour.serve("#{$$}:#{Time.now.to_f}:#{JOUR_PORT}", true, "OK", JOUR_PORT)
end
end
=end