Class: Cucumber::Chef::TCPSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/chef/tcp_socket.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, data = nil) ⇒ TCPSocket

Returns a new instance of TCPSocket.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cucumber/chef/tcp_socket.rb', line 31

def initialize(host, port, data=nil)
  @host, @port, @data = host, port, data

  if !host
    message = "You must supply a host!"
    $logger.fatal { message } if $logger
    raise TCPSocketError, message
  end

  if !port
    message = "You must supply a port!"
    $logger.fatal { message } if $logger
    raise TCPSocketError, message
  end
end

Instance Method Details

#ready?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cucumber/chef/tcp_socket.rb', line 49

def ready?
  socket = ::TCPSocket.new(@host, @port)

  if @data.nil?
    $logger.debug { "read(#{@host}:#{@port})" } if $logger
    ((::IO.select([socket], nil, nil, 5) && socket.gets) ? true : false)
  else
    $logger.debug { "write(#{@host}:#{@port}, '#{@data}')" } if $logger
    ((::IO.select(nil, [socket], nil, 5) && socket.write(@data)) ? true : false)
  end

rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e
  $logger.debug { "#{@host}:#{@port} - #{e.message}" } if $logger
  false
ensure
  (socket && socket.close)
end

#waitObject



69
70
71
72
73
74
# File 'lib/cucumber/chef/tcp_socket.rb', line 69

def wait
  begin
    success = ready?
    sleep(1)
  end until success
end