Module: Injour
- Defined in:
- lib/injour.rb,
lib/injour/version.rb
Defined Under Namespace
Classes: Server
Constant Summary collapse
- PORT =
43215
- SERVICE =
"_injour._tcp"
- INJOUR_STATUS =
File.join(ENV['HOME'], '.injour')
- VERSION =
"0.2.2".freeze
Class Method Summary collapse
- .find(name, first = true) ⇒ Object
- .get(name, limit = 10) ⇒ Object
- .get_limit(query_string) ⇒ Object
- .get_status(limit = 5) ⇒ Object
- .list(name = nil) ⇒ Object
- .retrieve_status_using_http(host, port, limit) ⇒ Object
- .serve(name = "", port = PORT) ⇒ Object
- .set_status(message) ⇒ Object
- .usage ⇒ Object
Class Method Details
.find(name, first = true) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/injour.rb', line 94 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 << Server.new(reply.name, rr.target, rr.port) waiting.run if first end end end sleep 5 service.stop hosts end |
.get(name, limit = 10) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/injour.rb', line 49 def self.get(name, limit = 10) 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| puts retrieve_status_using_http(host.host, host.port, limit) end end end |
.get_limit(query_string) ⇒ Object
119 120 121 122 123 |
# File 'lib/injour.rb', line 119 def self.get_limit(query_string) (query_string.match(/number=(\d+)/)[1]).to_i || 5 rescue 5 end |
.get_status(limit = 5) ⇒ Object
113 114 115 116 117 |
# File 'lib/injour.rb', line 113 def self.get_status(limit = 5) File.read(INJOUR_STATUS).split("\n").reverse.slice(0, limit).join("\n") rescue # If file is not present, show an empty string "" end |
.list(name = nil) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/injour.rb', line 67 def self.list(name = nil) return get(name) if name hosts = [] service = DNSSD.browse(SERVICE) do |reply| DNSSD.resolve(reply.name, reply.type, reply.domain) do |rr| host = Server.new(reply.name, rr.target, rr.port) unless hosts.include? host puts "#{host.name} (#{host.host}:#{host.port}) -> #{retrieve_status_using_http(host.host, host.port, 1)}" hosts << host end end end sleep 5 service.stop end |
.retrieve_status_using_http(host, port, limit) ⇒ Object
45 46 47 |
# File 'lib/injour.rb', line 45 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
125 126 127 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 |
# File 'lib/injour.rb', line 125 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 "#{name}'s In/Out Records..." end # Don't log anything, everything goes in an abyss log = WEBrick::Log.new('/dev/null', WEBrick::Log::DEBUG) server = WEBrick::HTTPServer.new(:Port => port.to_i, :Logger => log, :AccessLog => log) # Open up a servlet, so that status can be viewed in a browser server.mount_proc("/") do |req, res| limit = get_limit(req.query_string) res.body = get_status(limit) res['Content-Type'] = "text/plain" end # Ctrl+C must quit it %w(INT TERM).each do |signal| trap signal do server.shutdown exit! end end # Start the server server.start end |
.set_status(message) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/injour.rb', line 85 def self.set_status() File.open(INJOUR_STATUS, 'a') { |file| file.puts("[#{Time.now.strftime("%d-%b-%Y %I:%M %p")}] #{}") } if !~ /\S/ puts 'Your status has been cleared.' else puts "Your status has been set to '#{}'." end end |
.usage ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/injour.rb', line 21 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 |