Class: Maze::Network
- Inherits:
-
Object
- Object
- Maze::Network
- Defined in:
- lib/maze/network.rb
Overview
Provides network utility functionality
Class Method Summary collapse
-
.port_open?(host, port, seconds = 0.1) ⇒ Boolean
Attempts to connect to a port, timing out after a time.
-
.wait_for_port(host, port) ⇒ Object
Repeatedly pings a port to see if the host is ready for a connection.
Class Method Details
.port_open?(host, port, seconds = 0.1) ⇒ Boolean
Attempts to connect to a port, timing out after a time.
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.
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 |