Sha256: 4782cc75ca96d4c00fdd684d65b905979a17bb9f5a9c067c2a5b75bbb5d580b7

Contents?: true

Size: 766 Bytes

Versions: 54

Compression:

Stored size: 766 Bytes

Contents

require 'socket'

module Locomotive
  module Wagon

    class TcpPort

      MAX_ATTEMPTS = 1000

      def initialize(host, from)
        @host = host
        @from = from
      end

      def first
        current = @from.to_i
        max     = current + MAX_ATTEMPTS
        while open_port(@host, current)
          current += 1
          raise "No available ports from #{@from}" if current >= max
        end
        current.to_s
      end

      private

      def open_port(host, port)
        sock = Socket.new(:INET, :STREAM)
        raw = Socket.sockaddr_in(port, host)
        sock.connect(raw)
        sock.close if sock
        true
      rescue Errno::ECONNREFUSED
        false
      rescue Errno::ETIMEDOUT
        false
      end

    end

  end
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
locomotivecms_wagon-3.2.0.alpha2 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-3.2.0.alpha1 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-3.1.1 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-3.1.0 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-3.1.0.beta1 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-3.0.5 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-3.0.4 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-3.0.3 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-3.0.2 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-3.0.0 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-3.0.0.rc0 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-3.0.0.beta2 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-2.4.1 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-2.4.0 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-2.4.0.rc2 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-2.4.0.rc1 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-2.3.0 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-2.3.0.rc1 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-2.2.0 lib/locomotive/wagon/tools/tcp_port.rb
locomotivecms_wagon-2.2.0.rc3 lib/locomotive/wagon/tools/tcp_port.rb