Module: Injour
- Defined in:
- lib/injour.rb,
lib/injour/version.rb
Defined Under Namespace
Classes: Done, InjourServer
Constant Summary
collapse
- PORT =
43215
- SERVICE =
"_injour._tcp"
- INJOUR_STATUS =
File.join(ENV['HOME'], '.injour')
- VERSION =
"0.2.3".freeze
Class Method Summary
collapse
Class Method Details
.discover(timeout = 5) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/injour.rb', line 68
def self.discover(timeout=5)
waiting_thread = Thread.current
dns = DNSSD.browse SERVICE do |reply|
DNSSD.resolve reply.name, reply.type, reply.domain do |resolve_reply|
service = InjourServer.new(reply.name, resolve_reply.target, resolve_reply.port)
begin
yield service
rescue Done
waiting_thread.run
end
end
end
puts "Gathering for up to #{timeout} seconds..."
sleep timeout
dns.stop
end
|
.find(name) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/injour.rb', line 103
def self.find(name)
found = nil
discover do |obj|
if obj.name == name
found = obj
raise Done
end
end
return found
end
|
.get(name, limit = 10) ⇒ Object
51
52
53
54
55
56
57
58
59
|
# File 'lib/injour.rb', line 51
def self.get(name, limit = 10)
host = find(name)
if host.nil?
STDERR.puts "ERROR: Unable to find #{name}"
else
puts retrieve_status_using_http(host.host, host.port, limit)
end
end
|
.get_limit(query_string) ⇒ Object
122
123
124
125
126
|
# File 'lib/injour.rb', line 122
def self.get_limit(query_string)
(query_string.match(/number=(\d+)/)[1]).to_i || 5
rescue
5
end
|
.get_status(limit = 5) ⇒ Object
116
117
118
119
120
|
# File 'lib/injour.rb', line 116
def self.get_status(limit = 5)
File.read(INJOUR_STATUS).split("\n").reverse.slice(0, limit).join("\n")
rescue ""
end
|
.list(name = nil) ⇒ Object
61
62
63
64
65
66
|
# File 'lib/injour.rb', line 61
def self.list(name = nil)
service_list.each do |service|
puts "=== #{service.name} on #{service.host}:#{service.port} ==="
puts "* #{retrieve_status_using_http(service.host, service.port, 1)}"
end
end
|
.retrieve_status_using_http(host, port, limit) ⇒ Object
47
48
49
|
# File 'lib/injour.rb', line 47
def self.retrieve_status_using_http(host, port, limit)
Net::HTTP.get_response(URI.parse("http://#{host}:#{port}/?number=#{limit}")).body
end
|
.serve(name = "", port = PORT) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/injour.rb', line 128
def self.serve(name="", port=PORT)
name = ENV['USER'] if name.empty?
tr = DNSSD::TextRecord.new
tr['description'] = "#{name}'s In/Out"
DNSSD.register(name, SERVICE, "local", port.to_i, tr.encode) do |reply|
puts "\nStarting #{name}'s Injour..."
end
no_log = WEBrick::Log.new('/dev/null', WEBrick::Log::DEBUG)
server = WEBrick::HTTPServer.new(:Port => port.to_i, :Logger => no_log, :AccessLog => no_log)
server.mount_proc("/") do |req, res|
limit = get_limit(req.query_string)
res.body = get_status(limit)
res['Content-Type'] = "text/plain"
end
%w(INT TERM).each do |signal|
trap signal do
server.shutdown
exit!
end
end
server.start
end
|
.service_list ⇒ Object
87
88
89
90
91
92
|
# File 'lib/injour.rb', line 87
def self.service_list
list = Set.new
discover { |obj| list << obj }
return list
end
|
.set_status(message) ⇒ Object
94
95
96
97
98
99
100
101
|
# File 'lib/injour.rb', line 94
def self.set_status(message)
File.open(INJOUR_STATUS, 'a') { |file| file.puts("[#{Time.now.strftime("%d-%b-%Y %I:%M %p")}] #{message}") }
if message !~ /\S/
puts 'Your status has been cleared.'
else
puts "Your status has been set to '#{message}'."
end
end
|
.usage ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/injour.rb', line 23
def self.usage
puts <<-HELP
Usage:
serve [<name>] [<port>]
Start up your injour server as <name> on <port>. <name> is your username
by default, and <port> is 43215. If you want to use the default <name>,
pass it as "".
status/st [<message>]
Publishes your [<message>] on Injour.
list/ls
List all people who are publishing statuses on Injour
show user [<number_of_statuses_to_show>]
Lists the last five updates from the 'user' by default. If you specify number_of_statuses_to_show, it will limit to that number.
help
Displays this message.
HELP
end
|