Class: Net::PingTCP

Inherits:
Ping
  • Object
show all
Defined in:
lib/net/ping.rb

Overview

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.

Constant Summary collapse

@@econnrefused =
false

Constants inherited from Ping

Net::Ping::VERSION

Instance Attribute Summary

Attributes inherited from Ping

#exception, #host, #port, #timeout, #warning

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Ping

#initialize

Constructor Details

This class inherits a constructor from Net::Ping

Class Method Details

.econnrefusedObject

Returns true if the ECONNREFUSED error is to be considered a successful ping. The default is false.



35
36
37
# File 'lib/net/ping.rb', line 35

def self.econnrefused
   @@econnrefused
end

.econnrefused=(bool) ⇒ Object

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



46
47
48
49
50
51
# File 'lib/net/ping.rb', line 46

def self.econnrefused=(bool)
   unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
      raise ArgumentError, "argument must be true or false"
   end
   @@econnrefused = bool
end

.ecrObject

An alias for PingTCP.econnrefused



40
41
42
# File 'lib/net/ping.rb', line 40

def self.ecr
   self.econnrefused
end

.ecr=(bool) ⇒ Object

An alias for PingTCP.econnrefused=



54
55
56
# File 'lib/net/ping.rb', line 54

def self.ecr=(bool)
   self.econnrefused = bool
end

Instance Method Details

#pingObject Also known as: ping?

This method attempts to ping a host and port using a TCPSocket with the host and port values passed to the constructor.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/net/ping.rb', line 60

def ping
   super
   success = false
   t = nil
   begin
      Timeout.timeout(@timeout){
         begin
            t = TCPSocket.new(@host,@port)
         rescue Errno::ECONNREFUSED => e
            if @@econnrefused == true
               success = true
            else
               @exception = e
            end
         rescue Exception => e
            @exception = e
         else
            success = true
         end
      }
   rescue TimeoutError => t
      @exception = t
   ensure
      t.close if t
   end
   success
end