Class: KnocKnoc::Watchman
- Inherits:
-
Object
- Object
- KnocKnoc::Watchman
show all
- Defined in:
- lib/knoc-knoc/watchman.rb
Constant Summary
collapse
- SUBNET =
/([0-9]+\.){3}/.match(self.local_ip).to_s
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Watchman.
19
20
21
|
# File 'lib/knoc-knoc/watchman.rb', line 19
def initialize
@whos_there, @whos_new, @whos_left = [], [], []
end
|
Instance Attribute Details
#whos_left ⇒ Object
Returns the value of attribute whos_left.
5
6
7
|
# File 'lib/knoc-knoc/watchman.rb', line 5
def whos_left
@whos_left
end
|
#whos_new ⇒ Object
Returns the value of attribute whos_new.
5
6
7
|
# File 'lib/knoc-knoc/watchman.rb', line 5
def whos_new
@whos_new
end
|
#whos_there ⇒ Object
Returns the value of attribute whos_there.
5
6
7
|
# File 'lib/knoc-knoc/watchman.rb', line 5
def whos_there
@whos_there
end
|
Class Method Details
.local_ip ⇒ Object
7
8
9
10
11
12
13
14
15
|
# File 'lib/knoc-knoc/watchman.rb', line 7
def self.local_ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true UDPSocket.open do |s|
s.connect '172.0.0.1', 1
s.addr.last
end
ensure
Socket.do_not_reverse_lookup = orig
end
|
.ping(ip) ⇒ Object
75
76
77
|
# File 'lib/knoc-knoc/watchman.rb', line 75
def self.ping ip
!!(/1 received/.match(`ping #{ip} -c 1 -w 1`))
end
|
.ping_all ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/knoc-knoc/watchman.rb', line 60
def self.ping_all
threads, list = [], []
(1..254).select do |num|
threads << Thread.new(num) do |n|
ip = SUBNET + n.to_s
if Net::Ping::External.new(ip, 1).ping?
list.push( ip )
end
end
end
threads.each { |th| th.join() }
list
end
|
Instance Method Details
#cleanup ⇒ Object
79
80
|
# File 'lib/knoc-knoc/watchman.rb', line 79
def cleanup
end
|
#refresh_lists ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/knoc-knoc/watchman.rb', line 35
def refresh_lists
current_ips = self.class.ping_all
whos_there_ips = @whos_there.map{|h| h.ip }
new_ips = current_ips - whos_there_ips
left_ips = whos_there_ips - current_ips
self.update_new_ips new_ips
self.update_whos_left left_ips
end
|
#update_new_ips(new_ips) ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/knoc-knoc/watchman.rb', line 44
def update_new_ips new_ips
new_ips.each do |ip|
host = Host.new(ip)
@whos_there.push host
@whos_new.push host
end
end
|
#update_whos_left(left_ips) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/knoc-knoc/watchman.rb', line 52
def update_whos_left left_ips
left_ips.each do |ip|
host = @whos_there.find{ |x| x.ip == ip }
@whos_there = @whos_there - [host]
@whos_left.push host
end
end
|