class Net::Ping::UDP
Constants
- MAX_DATA
The maximum data size that can be sent in a
UDPping.
Attributes
The data to send to the remote host. By default this is 'ping'. This should be MAX_DATA size characters or less.
Public Class Methods
Creates and returns a new Ping::UDP object. This is effectively identical to its superclass constructor.
Net::Ping::new
# File lib/net/ping/udp.rb, line 42 def initialize(host=nil, port=nil, timeout=5) @data = 'ping' super(host, port, timeout) @bind_host = nil @bind_port = nil end
Returns whether or not the connect behavior should enforce remote service availability as well as reachability. The default is true.
# File lib/net/ping/udp.rb, line 13 def self.service_check @@service_check end
Set whether or not the connect behavior should enforce remote service availability as well as reachability. If set to false then Errno::ECONNREFUSED or Errno::ECONNRESET will be considered a successful ping, meaning no actual data handshaking is required. By default, if either of those errors occurs it is considered a failed ping.
# File lib/net/ping/udp.rb, line 24 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
Public Instance Methods
Associates the local end of the UDP connection with the given host and port. This is essentially a wrapper for UDPSocket#bind.
# File lib/net/ping/udp.rb, line 66 def bind(host, port) @bind_host = host @bind_port = port end
Sets the data string sent to the remote host. This value cannot have a size greater than MAX_DATA.
# File lib/net/ping/udp.rb, line 54 def data=(string) if string.size > MAX_DATA err = "cannot set data string larger than #{MAX_DATA} characters" raise ArgumentError, err end @data = string end
Sends a simple text string to the host and checks the return string. If the string sent and the string returned are a match then the ping was successful and true is returned. Otherwise, false is returned.
Net::Ping#ping
# File lib/net/ping/udp.rb, line 75 def ping(host = @host) super(host) bool = false udp = UDPSocket.open array = [] if @bind_host udp.bind(@bind_host, @bind_port) end start_time = Time.now # Process.clock_gettime(Process::CLOCK_MONOTONIC), rather than # Time.now, so a wall-clock adjustment mid-ping can't shorten or # extend the deadline. Matches Timeout.timeout's own treatment of # nil/0 as "no timeout". if @timeout && !@timeout.zero? deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout end begin udp.connect(host, @port) udp.send(@data, 0) # Timeout.timeout relies on interrupting the blocking recvfrom # call, which JRuby does not honor. Wait for readability with # select instead, which enforces the timeout at the OS level. # The remaining time (rather than the full @timeout) is used so # that time already spent on connect/send still counts against # the deadline. if deadline remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC) raise Timeout::Error if remaining <= 0 raise Timeout::Error unless select([udp], nil, nil, remaining) else select([udp]) end array = udp.recvfrom(MAX_DATA) rescue Errno::ECONNREFUSED, Errno::ECONNRESET => err if @@service_check @exception = err else bool = true end rescue Exception => err @exception = err else if array[0] == @data bool = true end ensure udp.close if udp end # There is no duration if the ping failed @duration = Time.now - start_time if bool bool end