arunthampi-injour 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/injour.rb +42 -39
- data/lib/injour/version.rb +1 -1
- metadata +1 -1
data/lib/injour.rb
CHANGED
@@ -13,11 +13,13 @@ require "injour/version"
|
|
13
13
|
Thread.abort_on_exception = true
|
14
14
|
|
15
15
|
module Injour
|
16
|
-
|
16
|
+
InjourServer = Struct.new(:name, :host, :port)
|
17
17
|
PORT = 43215
|
18
18
|
SERVICE = "_injour._tcp"
|
19
19
|
INJOUR_STATUS = File.join(ENV['HOME'], '.injour')
|
20
20
|
|
21
|
+
class Done < RuntimeError; end
|
22
|
+
|
21
23
|
def self.usage
|
22
24
|
puts <<-HELP
|
23
25
|
Usage:
|
@@ -47,39 +49,46 @@ help
|
|
47
49
|
end
|
48
50
|
|
49
51
|
def self.get(name, limit = 10)
|
50
|
-
|
52
|
+
host = find(name)
|
51
53
|
|
52
|
-
if
|
54
|
+
if host.nil?
|
53
55
|
STDERR.puts "ERROR: Unable to find #{name}"
|
54
|
-
elsif hosts.size > 1
|
55
|
-
STDERR.puts "ERROR: Multiple possibles found:"
|
56
|
-
hosts.each do |host|
|
57
|
-
STDERR.puts " #{host.name} (#{host.host}:#{host.port})"
|
58
|
-
end
|
59
56
|
else
|
60
|
-
|
61
|
-
hosts.each do |host|
|
62
|
-
puts retrieve_status_using_http(host.host, host.port, limit)
|
63
|
-
end
|
57
|
+
puts retrieve_status_using_http(host.host, host.port, limit)
|
64
58
|
end
|
65
59
|
end
|
66
60
|
|
67
61
|
def self.list(name = nil)
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
62
|
+
service_list.each do |service|
|
63
|
+
puts "=== #{service.name} on #{service.host}:#{service.port} ==="
|
64
|
+
puts "* #{retrieve_status_using_http(service.host, service.port, 1)}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.discover(timeout=5)
|
69
|
+
waiting_thread = Thread.current
|
70
|
+
|
71
|
+
dns = DNSSD.browse SERVICE do |reply|
|
72
|
+
DNSSD.resolve reply.name, reply.type, reply.domain do |resolve_reply|
|
73
|
+
service = InjourServer.new(reply.name, resolve_reply.target, resolve_reply.port)
|
74
|
+
begin
|
75
|
+
yield service
|
76
|
+
rescue Done
|
77
|
+
waiting_thread.run
|
77
78
|
end
|
78
79
|
end
|
79
80
|
end
|
80
81
|
|
81
|
-
|
82
|
-
|
82
|
+
puts "Gathering for up to #{timeout} seconds..."
|
83
|
+
sleep timeout
|
84
|
+
dns.stop
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.service_list
|
88
|
+
list = Set.new
|
89
|
+
discover { |obj| list << obj }
|
90
|
+
|
91
|
+
return list
|
83
92
|
end
|
84
93
|
|
85
94
|
def self.set_status(message)
|
@@ -91,23 +100,17 @@ help
|
|
91
100
|
end
|
92
101
|
end
|
93
102
|
|
94
|
-
def self.find(name
|
95
|
-
|
96
|
-
|
97
|
-
waiting = Thread.current
|
103
|
+
def self.find(name)
|
104
|
+
found = nil
|
98
105
|
|
99
|
-
|
100
|
-
if name
|
101
|
-
|
102
|
-
|
103
|
-
waiting.run if first
|
104
|
-
end
|
106
|
+
discover do |obj|
|
107
|
+
if obj.name == name
|
108
|
+
found = obj
|
109
|
+
raise Done
|
105
110
|
end
|
106
111
|
end
|
107
112
|
|
108
|
-
|
109
|
-
service.stop
|
110
|
-
hosts
|
113
|
+
return found
|
111
114
|
end
|
112
115
|
|
113
116
|
def self.get_status(limit = 5)
|
@@ -129,12 +132,12 @@ help
|
|
129
132
|
tr['description'] = "#{name}'s In/Out"
|
130
133
|
|
131
134
|
DNSSD.register(name, SERVICE, "local", port.to_i, tr.encode) do |reply|
|
132
|
-
puts "#{name}'s
|
135
|
+
puts "\nStarting #{name}'s Injour..."
|
133
136
|
end
|
134
137
|
|
135
138
|
# Don't log anything, everything goes in an abyss
|
136
|
-
|
137
|
-
server = WEBrick::HTTPServer.new(:Port => port.to_i, :Logger =>
|
139
|
+
no_log = WEBrick::Log.new('/dev/null', WEBrick::Log::DEBUG)
|
140
|
+
server = WEBrick::HTTPServer.new(:Port => port.to_i, :Logger => no_log, :AccessLog => no_log)
|
138
141
|
|
139
142
|
# Open up a servlet, so that status can be viewed in a browser
|
140
143
|
server.mount_proc("/") do |req, res|
|
data/lib/injour/version.rb
CHANGED