Class: RhoDevelopment::DeviceFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/build/development/device_finder.rb

Instance Method Summary collapse

Instance Method Details

#discovery(aString) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/build/development/device_finder.rb', line 31

def discovery(aString)
  puts "Network mask #{aString}.* will be used".primary
  print 'Discovering...'
  subscribers = self.parallelDiscovery(aString)
  if subscribers.empty?
    puts 'no devices found'.warning
    exit 1
  else
    puts 'done'.success
    subscribers.each { |each|
      puts each.to_s.info }
    print 'Storing subscribers...'.primary
    Configuration::store_subscribers(subscribers)
    puts 'done'.success
  end
end

#parallelDiscovery(aString) ⇒ Object



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
74
75
# File 'lib/build/development/device_finder.rb', line 48

def parallelDiscovery(aString)
  subscribers = []
  threads = []
  1.upto(254) { |each|
    threads << Thread.new {
      url = URI("http://#{aString}.#{each}:37579/development/get_info")
      begin
        http = Net::HTTP.new(url.host, url.port)
        http.open_timeout = 5
        http.read_timeout = 5
        response = http.get(url.path)
        data = JSON.parse(response.body)
        subscriber = {}
        subscriber['uri'] = "#{data['ip']}:#{data['port']}"
        subscriber['name'] = data['deviceFriendlyName']
        subscriber['platform'] = data['platform']
        subscriber['application'] = data['applicationName']
        subscriber['enabled'] = true
        subscribers << subscriber
      rescue *Configuration::handledNetworkExceptions => e
        #TODO may be it is necessary to remove subscriber from list?
        #puts "#{url} is not accessible. error: #{e.class}".info
      end
    }
  }
  threads.each { |thread| thread.join }
  return subscribers
end

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/build/development/device_finder.rb', line 8

def run
  addresses = Network::available_addresses
  if (addresses.empty?)
    puts 'Network interfaces were not found.'.warning
    return
  end
  if addresses.length != 1
    puts
    puts 'There are several network interfaces with following masks: '.primary
    addresses.each { |each|
      _mask = each.split('.')[0, 3].join('.')
      puts "#{addresses.index(each) + 1}. #{_mask}.*" }
    puts
    puts 'Please choose one of them: '
    input = STDIN.gets.strip.to_i
    selected_address = addresses[input - 1]
  else
    selected_address = addresses.last
  end
  mask = selected_address.split('.')[0, 3].join('.')
  self.discovery(mask)
end