<commit_before>require_relative 'configuration'
require_relative 'subscriber'

module RhoDevelopment
  class DeviceFinder
    def run

      adresses = Configuration::own_ip_addresses
      if adresses.length != 0
        puts
        puts 'There are several network interfaces with following IP addresses: '.primary
        adresses.each { |each| puts "#{adresses.index(each) + 1}. #{each}" }
        puts
        puts 'Please choose one of them: '
        input = STDIN.gets.strip.to_i
        choosenAddress = adresses[input - 1]
      else
        choosenAddress = adresses.last
      end
      mask = choosenAddress.split('.')[0, 3].join('.')

      print "Discovering #{mask}.(1..254) ... ".primary
      subscribers = self.parallelDiscovery(mask)
      if subscribers.empty?
        puts 'no devices found'.warning
      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

    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
            response = http.get(url.path)
            puts "#{url} answered: #{response.body}".info
            data = JSON.parse(response.body)
            subscriber = {}
            subscriber['uri'] = "#{data['ip']}:#{data['port']}"
            subscriber['name'] = data['deviceFriendlyName']
            subscriber['platform'] = data['platform']
            subscriber['application'] = data['applicationName']
            subscribers << subscriber
          rescue Errno::ECONNREFUSED, Errno::EHOSTDOWN, Errno::EHOSTUNREACH, Net.const_defined?(:OpenTimeout) ? Net::OpenTimeout : Timeout::Error => e
           # rescue  => e
            #TODO may be it is necessary remove subscriber from list?
            puts "#{url} is not accessible. error: #{e.class}".info
          end
        }
      }
      threads.each { |thread| thread.join }
      return subscribers
    end

  end

end<commit_msg>[server side] Device discovering: Replacing last octet in network IP addresses with asteriks in list of founded IP. Skip user ip selection if only one network interface founded.<commit_after>require_relative 'configuration'
require_relative 'subscriber'

module RhoDevelopment
  class DeviceFinder
    def run

      adresses = Configuration::own_ip_addresses.collect { |each| each.split('.')[0, 3].join('.') }
      if adresses.length != 1
        puts
        puts 'There are several network interfaces with following masks: '.primary
        adresses.each { |each| puts "#{adresses.index(each) + 1}. #{each}.*" }
        puts
        puts 'Please choose one of them: '
        input = STDIN.gets.strip.to_i
        choosenAddress = adresses[input - 1]
      else
        choosenAddress = adresses.last
      end
      mask = choosenAddress.split('.')[0, 3].join('.')

      puts "Network mask #{mask}.* will be used".primary
      print "Discovering..."
      subscribers = self.parallelDiscovery(mask)
      if subscribers.empty?
        puts 'no devices found'.warning
      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

    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
            response = http.get(url.path)
            puts "#{url} answered: #{response.body}".info
            data = JSON.parse(response.body)
            subscriber = {}
            subscriber['uri'] = "#{data['ip']}:#{data['port']}"
            subscriber['name'] = data['deviceFriendlyName']
            subscriber['platform'] = data['platform']
            subscriber['application'] = data['applicationName']
            subscribers << subscriber
          rescue Errno::ECONNREFUSED, Errno::EHOSTDOWN, Errno::EHOSTUNREACH, Net.const_defined?(:OpenTimeout) ? Net::OpenTimeout : Timeout::Error => e
           # rescue  => e
            #TODO may be it is necessary remove subscriber from list?
            #puts "#{url} is not accessible. error: #{e.class}".info
          end
        }
      }
      threads.each { |thread| thread.join }
      return subscribers
    end

  end

end
{'fraction_non_alphanumeric': 0.12129380053908356, 'fraction_numerical': 0.0074642338793282186, 'mean_word_length': 2.2550607287449393, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1870447', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1367, 'n_words': 333}