Class: Presence::Scanner
- Inherits:
-
Object
- Object
- Presence::Scanner
- Defined in:
- lib/presence/scanner.rb
Overview
Scans a network for connected clients, captures the MAC address, and dispatches related events to any registered listeners.
Constant Summary collapse
Instance Attribute Summary collapse
-
#listeners ⇒ Object
Returns the value of attribute listeners.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #dispatch(event, *args) ⇒ Object
-
#initialize(options = nil) ⇒ Scanner
constructor
A new instance of Scanner.
- #ip_prefix ⇒ Object
- #localhost_ip ⇒ Object
- #octet_range ⇒ Object
- #process_scan_result(ip, cmd, result) ⇒ Object
- #register_listener(l) ⇒ Object
- #scan ⇒ Object
- #scan_ip(ip) ⇒ Object
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#listeners ⇒ Object
Returns the value of attribute listeners.
12 13 14 |
# File 'lib/presence/scanner.rb', line 12 def listeners @listeners end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/presence/scanner.rb', line 12 def @options end |
Class Method Details
.check_env ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/presence/scanner.rb', line 83 def check_env commands = Commands.new if commands.run('which ifconfig').size == 0 raise Presence::InvalidEnvironment.new("Unsupported platform: ifconfig not found.") end if commands.run('which arping').size == 0 raise Presence::InvalidEnvironment.new("Unsupported platform: arping not found.") raise Presence::InvalidEnvironment.new("Install arping first: brew install arping") end end |
.scan_loop {|scanner| ... } ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/presence/scanner.rb', line 94 def scan_loop(&block) scanner = self.new yield(scanner) if block_given? while(true) begin scanner.scan rescue Interrupt break end end end |
Instance Method Details
#dispatch(event, *args) ⇒ Object
72 73 74 75 76 |
# File 'lib/presence/scanner.rb', line 72 def dispatch(event, *args) listeners.each do |l| l.send(event, *args) if l.respond_to?(event) end end |
#ip_prefix ⇒ Object
37 38 39 40 41 42 |
# File 'lib/presence/scanner.rb', line 37 def ip_prefix unless [:ip_prefix] [:ip_prefix] = localhost_ip.split('.')[0,3].join('.') end [:ip_prefix] end |
#localhost_ip ⇒ Object
30 31 32 33 34 35 |
# File 'lib/presence/scanner.rb', line 30 def localhost_ip unless @localhost_ip @localhost_ip = @commands.local_ip end @localhost_ip end |
#octet_range ⇒ Object
44 45 46 |
# File 'lib/presence/scanner.rb', line 44 def octet_range [:octet_range] end |
#process_scan_result(ip, cmd, result) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/presence/scanner.rb', line 62 def process_scan_result(ip, cmd, result) if result =~ MAC_REGEXP mac = $1 dispatch(:localhost_found, ip, mac) if ip == localhost_ip dispatch(:mac_found, ip, mac) else dispatch(:mac_not_found, ip) end end |
#register_listener(l) ⇒ Object
24 25 26 27 28 |
# File 'lib/presence/scanner.rb', line 24 def register_listener(l) raise Presence::InvalidListener.new("Listener cannot be nil") if l.nil? listeners << l dispatch(:listener_registered, l, self) end |
#scan ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/presence/scanner.rb', line 48 def scan dispatch(:scan_started, ip_prefix, octet_range) octet_range.each do |i| scan_ip("#{ip_prefix}.#{i}") end dispatch(:scan_finished, ip_prefix, octet_range) end |
#scan_ip(ip) ⇒ Object
56 57 58 59 60 |
# File 'lib/presence/scanner.rb', line 56 def scan_ip(ip) cmd, result = @commands.arping(ip) dispatch(:ip_scanned, ip, cmd, result) process_scan_result(ip, cmd, result) end |
#to_s ⇒ Object
78 79 80 |
# File 'lib/presence/scanner.rb', line 78 def to_s "<#{self.class} ip_prefix: '#{self.ip_prefix}' octet_range: (#{self.octet_range})>" end |