class Net::Ping::TCP

With a TCP ping simply try to open a connection. If we are successful, assume success. In either case close the connection to be polite.

Public Class Methods

econnrefused()
Alias for: service_check
econnrefused=(bool)
Alias for: service_check=
ecr()
Alias for: service_check
ecr=(bool)
Alias for: service_check=
service_check() click to toggle source

Returns whether or not Errno::ECONNREFUSED is considered a successful ping. The default is false.

# File lib/net/ping/tcp.rb, line 15
def self.service_check
  @@service_check
end
Also aliased as: econnrefused, ecr
service_check=(bool) click to toggle source

Sets whether or not an Errno::ECONNREFUSED should be considered a successful ping.

# File lib/net/ping/tcp.rb, line 22
def self.service_check=(bool)
  unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
    raise ArgumentError, 'argument must be true or false'
  end
  @@service_check = bool
end
Also aliased as: econnrefused=, ecr=

Public Instance Methods

ping(host=@host) click to toggle source

This method attempts to ping a host and port using a TCPSocket with the host, port and timeout values passed in the constructor. Returns true if successful, or false otherwise.

Note that, by default, an Errno::ECONNREFUSED return result will be considered a failed ping. See the documentation for the ::service_check= method if you wish to change this behavior.

Calls superclass method Net::Ping#ping
# File lib/net/ping/tcp.rb, line 37
def ping(host=@host)
  super(host)

  bool = false
  tcp = nil
  start_time = Time.now

  begin
    Timeout.timeout(@timeout){
      begin
        tcp = TCPSocket.new(host, @port)
      rescue Errno::ECONNREFUSED => err
        if @@service_check
          bool = true
        else
          @exception = err
        end
      rescue Exception => err
        @exception = err
      else
        bool = true
      end
    }
  rescue Timeout::Error => err
    @exception = err
  ensure
    tcp.close if tcp
  end

  # There is no duration if the ping failed
  @duration = Time.now - start_time if bool

  bool
end
Also aliased as: ping?, pingecho
ping?(host=@host)
Alias for: ping
pingecho(host=@host)
Alias for: ping