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.
Files changed (3) hide show
  1. data/lib/injour.rb +42 -39
  2. data/lib/injour/version.rb +1 -1
  3. metadata +1 -1
@@ -13,11 +13,13 @@ require "injour/version"
13
13
  Thread.abort_on_exception = true
14
14
 
15
15
  module Injour
16
- Server = Struct.new(:name, :host, :port)
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
- hosts = find(name)
52
+ host = find(name)
51
53
 
52
- if hosts.empty?
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
- # Set is weird. There is no #[] or #at
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
- return get(name) if name
69
- hosts = []
70
-
71
- service = DNSSD.browse(SERVICE) do |reply|
72
- DNSSD.resolve(reply.name, reply.type, reply.domain) do |rr|
73
- host = Server.new(reply.name, rr.target, rr.port)
74
- unless hosts.include? host
75
- puts "#{host.name} (#{host.host}:#{host.port}) -> #{retrieve_status_using_http(host.host, host.port, 1)}"
76
- hosts << host
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
- sleep 5
82
- service.stop
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, first=true)
95
- hosts = Set.new
96
-
97
- waiting = Thread.current
103
+ def self.find(name)
104
+ found = nil
98
105
 
99
- service = DNSSD.browse(SERVICE) do |reply|
100
- if name === reply.name
101
- DNSSD.resolve(reply.name, reply.type, reply.domain) do |rr|
102
- hosts << Server.new(reply.name, rr.target, rr.port)
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
- sleep 5
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 In/Out Records..."
135
+ puts "\nStarting #{name}'s Injour..."
133
136
  end
134
137
 
135
138
  # Don't log anything, everything goes in an abyss
136
- log = WEBrick::Log.new('/dev/null', WEBrick::Log::DEBUG)
137
- server = WEBrick::HTTPServer.new(:Port => port.to_i, :Logger => log, :AccessLog => log)
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|
@@ -1,3 +1,3 @@
1
1
  module Injour
2
- VERSION = "0.2.2".freeze
2
+ VERSION = "0.2.3".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arunthampi-injour
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arun Thampi