class Locomotive::Wagon::TcpPort

Constants

MAX_ATTEMPTS

Public Class Methods

new(host, from) click to toggle source
# File lib/locomotive/wagon/tools/tcp_port.rb, line 10
def initialize(host, from)
  @host = host
  @from = from
end

Public Instance Methods

first() click to toggle source
# File lib/locomotive/wagon/tools/tcp_port.rb, line 15
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 Instance Methods

open_port(host, port) click to toggle source
# File lib/locomotive/wagon/tools/tcp_port.rb, line 27
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