Sha256: d0b2e9c449cd381c57dbfe0b7e1617099a7b53a338698f0503311be5795e7cf6

Contents?: true

Size: 1.91 KB

Versions: 6

Compression:

Stored size: 1.91 KB

Contents

#!/usr/bin/env ruby

require 'dnssd'
require 'socket'

module LogSimulator
  class Target
    attr_accessor :name,:target_name,:port

    def initialize(name,target_name,port)
      @name = name
      @target_name = target_name
      @port = port
    end

    def eql?(o)
      self == o
    end

    def ==(o)
      o.class == self.class && o.target_name == @target_name && o.port == @port
    end

    def hash
      @target_name.hash
    end
  end

  class Resolver
    def self.select_target(scan_time)
      self.resolve_service(scan_time) do |found_targets|
        if found_targets.count <= 0
          puts 'no possible target found!'
        elsif found_targets.count == 1
          target = found_targets[0]
          yield target
        else
          puts 'Which one to connect?'
          found_targets.each_with_index do |target,index|
            puts "#{index+1}. #{target.name} - #{target.target_name}: #{target.port}"
          end

          puts 'Select: '
          selection = gets.chomp.to_i - 1
          while(selection < 0 || selection >= found_targets.count)
            puts "Select between #{0}-#{found_targets.count}"
            selection = gets.chomp.to_i - 1
          end
          yield found_targets[selection]
        end
      end
    end

    def self.resolve_service(scan_time)
      browser = DNSSD::Service.new
      service_name = '_debugConnection._tcp'
      puts "Browsing for #{service_name}"

      found_targets = []
      Thread.new do
        browser.browse service_name do |reply|
          Thread.new do
            Thread.exclusive do
              resolver = DNSSD::Service.new
              resolver.resolve reply do |r|
                found_targets<<Target.new(r.name,r.target,r.port)
                break unless r.flags.more_coming?
              end
            end
          end
        end
      end
      sleep scan_time
      found_targets.uniq!
      yield found_targets
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
log_simulator-0.2.1 lib/resolve.rb
log_simulator-0.2.0 lib/resolve.rb
log_simulator-0.1.3 lib/resolve.rb
log_simulator-0.1.2 lib/resolve.rb
log_simulator-0.1.1 lib/resolve.rb
log_simulator-0.1.0 lib/resolve.rb