Class: Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/pipeline_toolkit/util/socket_util.rb

Overview

Extend socket class

Class Method Summary collapse

Class Method Details

.select_random_port(low, high, host = "localhost") ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/pipeline_toolkit/util/socket_util.rb', line 5

def self.select_random_port(low, high, host = "localhost")
  raise "'low' must be lower than 'high'" unless low < high
  port = nil
  begin
    port = low + rand(high - low)
  end while Socket.socket_in_use?(host, port)
  port
end

.socket_in_use?(host, port) ⇒ Boolean

OPTIMIZE. Is there a better way to do this? Feels ugly to me.

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/pipeline_toolkit/util/socket_util.rb', line 15

def self.socket_in_use?(host, port)
  begin
    TCPSocket.new(host, port)
  rescue Errno::ECONNREFUSED => e
    return false
  end
  return true
end