Sha256: 3a815822e8a39fd711edb4a7def6d353ddb379d52cc164967f5d9370127a2846

Contents?: true

Size: 1.11 KB

Versions: 19

Compression:

Stored size: 1.11 KB

Contents

module Invoker
  module Power
    class PortFinder
      STARTING_PORT = 23400
      attr_accessor :dns_port, :http_port, :starting_port, :https_port
      def initialize
        @starting_port = STARTING_PORT
        @ports = []
        @dns_port = nil
        @http_port = nil
      end

      def find_ports
        STARTING_PORT.upto(STARTING_PORT + 100) do |port|
          break if @ports.size > 3
          if check_if_port_is_open(port)
            @ports << port
          else
            next
          end
        end
        @dns_port = @ports[0]
        @http_port = @ports[1]
        @https_port = @ports[2]
      end

      private

      def check_if_port_is_open(port)
        socket_flag = true
        sockets = nil
        begin
          sockets = Socket.tcp_server_sockets(port)
          socket_flag = false if sockets.size <= 1
        rescue Errno::EADDRINUSE
          socket_flag = false
        end
        sockets && close_socket_pairs(sockets)
        socket_flag
      end

      def close_socket_pairs(sockets)
        sockets.each { |s| s.close }
      rescue
        nil
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 3 rubygems

Version Path
itrg-invoker-1.6.1 lib/invoker/power/port_finder.rb
itrg-invoker-1.6.0 lib/invoker/power/port_finder.rb
invoker-1.5.8 lib/invoker/power/port_finder.rb
invoker-1.5.7 lib/invoker/power/port_finder.rb
invoker-1.5.6 lib/invoker/power/port_finder.rb
invoker-1.5.5 lib/invoker/power/port_finder.rb
invoker-1.5.4 lib/invoker/power/port_finder.rb
invoker-1.5.3 lib/invoker/power/port_finder.rb
meepo-1.5.2 lib/invoker/power/port_finder.rb
invoker-1.5.1 lib/invoker/power/port_finder.rb
invoker-1.4.1 lib/invoker/power/port_finder.rb
invoker-1.4.0 lib/invoker/power/port_finder.rb
invoker-1.3.2 lib/invoker/power/port_finder.rb
invoker-1.3.1 lib/invoker/power/port_finder.rb
invoker-1.3.0 lib/invoker/power/port_finder.rb
invoker-1.2.0 lib/invoker/power/port_finder.rb
invoker-1.2.0.pre2 lib/invoker/power/port_finder.rb
invoker-1.2.0.pre1 lib/invoker/power/port_finder.rb
invoker-1.2.0.pre lib/invoker/power/port_finder.rb