Sha256: b5ff427e13bd85727c026b5738ee0a6a0131d1f45ec2e97ad918dd99ef26c4f7

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'thread'

module Wifidiag
  # Collect client and AP information from +an adapter+
  class Collector
    def initialize(adapter)
      @lock = Mutex.new

      @adapter = adapter

      @clients = nil
      @clients_by_ip_address = nil
      @clients_by_mac_address = nil
      @last_update = nil
    end

    attr_reader :adapter, :last_update

    def start_periodic_update(interval) # XXX:
      self.collect

      Thread.new do
        loop do
          begin
            self.collect
            sleep interval
          rescue Exception => e
            $stderr.puts "Periodic update error: #{e.inspect}"
            e.backtrace.each do |x|
              $stderr.puts "\t#{x}"
            end
            sleep interval
          end
        end
      end
    end

    def collect
      @lock.synchronize do
        clients = adapter.collect()
        clients_by_ip_address = clients.map { |_| [_.ip_address, _] }.to_h
        clients_by_mac_address = clients.map { |_| [_.mac_address, _] }.to_h
        @clients = clients
        @clients_by_ip_address = clients_by_ip_address
        @clients_by_mac_address = clients_by_mac_address
        @last_update = Time.now
      end
    end

    def client_data_for_ip_address(address)
      @clients_by_ip_address[address]
    end

    def client_data_for_mac_address(address)
      @clients_by_mac_address[address]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wifidiag-0.1.0 lib/wifidiag/collector.rb