/
journeta_top.rb
executable file
·73 lines (59 loc) · 1.53 KB
/
journeta_top.rb
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
#!/usr/bin/env ruby
banner =<<EOL
A "top"-like tool for monitoring network status.
Will not run on JRuby due to native dependencies.
Copyright 2011, Preston Lee. http://prestonlee.com
Usage: #{$0}
EOL
puts banner
current_dir = File.dirname(File.expand_path(__FILE__))
lib_path = File.join(current_dir, '..', 'lib')
$LOAD_PATH.unshift lib_path
require 'journeta'
include Journeta
include Journeta::Common
include Journeta::Common::Shutdown
require 'rubygems'
begin
require 'curses'
rescue
puts 'Please install the curses gem to use this example.'
exit 1
end
peer_port = (2048 + rand( 2 ** 8))
@journeta = Journeta::Engine.new(:peer_port => peer_port)
@journeta.start
stop_on_shutdown(@journeta)
Curses.init_screen
Curses.setpos(0,0)
Curses::addstr("Press ^C to quit. ENTER to refresh.\n")
@run = true
@run_lock = Mutex.new
refresh = Thread.new do
keep_going = true
begin
@run_lock.synchronize do
keep_going = @run
end
all = @journeta.known_peers
Curses.clear
Curses.setpos(0,0)
Curses.addstr "UUID\t\tVersion\tIP Address\tDiscovered\t\t\tUpdated\t\t\t\tGroups\n"
all.keys.sort.each do |uuid|
Curses.addstr "#{all[uuid].uuid}\t#{all[uuid].version}\t#{all[uuid].ip_address}\t#{all[uuid].created_at}\t#{all[uuid].updated_at}\t#{all[uuid].groups}\n"
end
sleep(0.1)
end while keep_going
end
begin
c = nil
begin
c = Curses.getch
end until c == ?C or c == ?\e
Curses.setpos(0,0)
Curses.clear
Curses.setpos(0,0)
end until c == ?\e
@run_lock.synchronize do
@run = false
end