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 whether or not 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.



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
   tcp = nil
   begin
      Timeout.timeout(@timeout){
         begin
            tcp = TCPSocket.new(@host,@port)
         rescue Errno::ECONNREFUSED => err
            if @@econnrefused == true
               success = true
            else
               @exception = err
            end
         rescue Exception => err
            @exception = err
         else
            success = true
         end
      }
   rescue Timeout::Error => err
      @exception = err
   ensure
      tcp.close if tcp
   end
   success
end