Class: Maze::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/network.rb

Overview

Provides network utility functionality

Class Method Summary collapse

Class Method Details

.port_open?(host, port, seconds = 0.1) ⇒ Boolean

Attempts to connect to a port, timing out after a time.

Parameters:

  • host (String)

    The name of the host to connect to

  • port (String)

    The port to attempt to connect to

  • seconds (Float) (defaults to: 0.1)

    Optional. The length of time to wait before timing out.

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/maze/network.rb', line 35

def port_open?(host, port, seconds=0.1)
  Timeout::timeout(seconds) do
    begin
      TCPSocket.new(host, port).close
      true
    rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError
      false
    end
  end
rescue Timeout::Error
  false
end

.wait_for_port(host, port) ⇒ Object

Repeatedly pings a port to see if the host is ready for a connection. The maximum amount of attempts is determined by the MAX_MAZE_CONNECT_ATTEMPTS variable.

Parameters:

  • host (String)

    The name of the host to connect to

  • port (String)

    The port to attempt to connect to

Raises:

  • (StandardError)

    When the port is not available for a connection



19
20
21
22
23
24
25
26
27
28
# File 'lib/maze/network.rb', line 19

def wait_for_port(host, port)
  attempts = 0
  up = false
  until (attempts >= MAX_MAZE_CONNECT_ATTEMPTS) || up
    attempts += 1
    up = port_open?(host, port)
    sleep 0.1 unless up
  end
  raise "Port not ready in time!" unless up
end