Class: Ping::UDP

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

Overview

The Ping::UDP class encapsulates methods for UDP pings.

Constant Summary collapse

MAX_DATA =

The maximum data size that can be sent in a UDP ping.

64
@@service_check =
true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = nil, port = nil, timeout = 5) ⇒ UDP

Creates and returns a new Ping::UDP object. This is effectively identical to its superclass constructor.



42
43
44
45
46
47
48
49
# 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

Instance Attribute Details

#dataObject

The data to send to the remote host. By default this is 'ping'. This should be MAX_DATA size characters or less.



37
38
39
# File 'lib/net/ping/udp.rb', line 37

def data
  @data
end

Class Method Details

.service_checkObject

Returns whether or not the connect behavior should enforce remote service availability as well as reachability. The default is true.



13
14
15
# File 'lib/net/ping/udp.rb', line 13

def self.service_check
  @@service_check
end

.service_check=(bool) ⇒ Object

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.



24
25
26
27
28
29
# 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

Instance Method Details

#bind(host, port) ⇒ Object

Associates the local end of the UDP connection with the given host and port. This is essentially a wrapper for UDPSocket#bind.



66
67
68
69
# File 'lib/net/ping/udp.rb', line 66

def bind(host, port)
  @bind_host = host
  @bind_port = port
end

#ping(host = @host) ⇒ Object

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.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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

  begin
    Timeout.timeout(@timeout){
      udp.connect(host, @port)
      udp.send(@data, 0)
      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