net-ping 1.4.1-x86-mingw32 → 1.5.3-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/lib/net/ping/ping.rb CHANGED
@@ -1,88 +1,89 @@
1
- require 'socket'
2
- require 'timeout'
3
-
4
- # The Net module serves as a namespace only.
5
- #
6
- module Net
7
-
8
- # The Ping class serves as an abstract base class for all other Ping class
9
- # types. You should not instantiate this class directly.
10
- #
11
- class Ping
12
- # The version of the net-ping library.
13
- VERSION = '1.4.1'
14
-
15
- # The host to ping. In the case of Ping::HTTP, this is the URI.
16
- attr_accessor :host
17
-
18
- # The port to ping. This is set to the echo port (7) by default. The
19
- # Ping::HTTP class defaults to port 80.
20
- #
21
- attr_accessor :port
22
-
23
- # The maximum time a ping attempt is made.
24
- attr_accessor :timeout
25
-
26
- # If a ping fails, this value is set to the error that occurred which
27
- # caused it to fail.
28
- #
29
- attr_reader :exception
30
-
31
- # This value is set if a ping succeeds, but some other condition arose
32
- # during the ping attempt which merits warning, e.g a redirect in the
33
- # case of Ping::HTTP#ping.
34
- #
35
- attr_reader :warning
36
-
37
- # The number of seconds (returned as a Float) that it took to ping
38
- # the host. This is not a precise value, but rather a good estimate
39
- # since there is a small amount of internal calculation that is added
40
- # to the overall time.
41
- #
42
- attr_reader :duration
43
-
44
- # The default constructor for the Net::Ping class. Accepts an optional
45
- # +host+, +port+ and +timeout+. The port defaults to your echo port, or
46
- # 7 if that happens to be undefined. The default timeout is 5 seconds.
47
- #
48
- # The host, although optional in the constructor, must be specified at
49
- # some point before the Net::Ping#ping method is called, or else an
50
- # ArgumentError will be raised.
51
- #
52
- # Yields +self+ in block context.
53
- #
54
- # This class is not meant to be instantiated directly. It is strictly
55
- # meant as an interface for subclasses.
56
- #
57
- def initialize(host=nil, port=nil, timeout=5)
58
- @host = host
59
- @port = port || Socket.getservbyname('echo') || 7
60
- @timeout = timeout
61
- @exception = nil
62
- @warning = nil
63
- @duration = nil
64
-
65
- yield self if block_given?
66
- end
67
-
68
- # The default interface for the Net::Ping#ping method. Each subclass
69
- # should call super() before continuing with their own implementation in
70
- # order to ensure that the @exception and @warning instance variables
71
- # are reset.
72
- #
73
- # If +host+ is nil here, then it will use the host specified in the
74
- # constructor. If the +host+ is nil and there was no host specified
75
- # in the constructor then an ArgumentError is raised.
76
- #--
77
- # The @duration should be set in the subclass' ping method.
78
- #
79
- def ping(host = @host)
80
- raise ArgumentError, 'no host specified' unless host
81
- @exception = nil
82
- @warning = nil
83
- end
84
-
85
- alias ping? ping
86
- alias pingecho ping
87
- end
88
- end
1
+ require 'socket'
2
+ require 'timeout'
3
+
4
+ # The Net module serves as a namespace only.
5
+ #
6
+ module Net
7
+
8
+ # The Ping class serves as an abstract base class for all other Ping class
9
+ # types. You should not instantiate this class directly.
10
+ #
11
+ class Ping
12
+ # The version of the net-ping library.
13
+ VERSION = '1.5.2'
14
+
15
+ # The host to ping. In the case of Ping::HTTP, this is the URI.
16
+ attr_accessor :host
17
+
18
+ # The port to ping. This is set to the echo port (7) by default. The
19
+ # Ping::HTTP class defaults to port 80.
20
+ #
21
+ attr_accessor :port
22
+
23
+ # The maximum time a ping attempt is made.
24
+ attr_accessor :timeout
25
+
26
+ # If a ping fails, this value is set to the error that occurred which
27
+ # caused it to fail.
28
+ #
29
+ attr_reader :exception
30
+
31
+ # This value is set if a ping succeeds, but some other condition arose
32
+ # during the ping attempt which merits warning, e.g a redirect in the
33
+ # case of Ping::HTTP#ping.
34
+ #
35
+ attr_reader :warning
36
+
37
+ # The number of seconds (returned as a Float) that it took to ping
38
+ # the host. This is not a precise value, but rather a good estimate
39
+ # since there is a small amount of internal calculation that is added
40
+ # to the overall time.
41
+ #
42
+ attr_reader :duration
43
+
44
+ # The default constructor for the Net::Ping class. Accepts an optional
45
+ # +host+, +port+ and +timeout+. The port defaults to your echo port, or
46
+ # 7 if that happens to be undefined. The default timeout is 5 seconds.
47
+ #
48
+ # The host, although optional in the constructor, must be specified at
49
+ # some point before the Net::Ping#ping method is called, or else an
50
+ # ArgumentError will be raised.
51
+ #
52
+ # Yields +self+ in block context.
53
+ #
54
+ # This class is not meant to be instantiated directly. It is strictly
55
+ # meant as an interface for subclasses.
56
+ #
57
+ def initialize(host=nil, port=nil, timeout=5)
58
+ @host = host
59
+ @port = port || Socket.getservbyname('echo') || 7
60
+ @timeout = timeout
61
+ @exception = nil
62
+ @warning = nil
63
+ @duration = nil
64
+
65
+ yield self if block_given?
66
+ end
67
+
68
+ # The default interface for the Net::Ping#ping method. Each subclass
69
+ # should call super() before continuing with their own implementation in
70
+ # order to ensure that the @exception and @warning instance variables
71
+ # are reset.
72
+ #
73
+ # If +host+ is nil here, then it will use the host specified in the
74
+ # constructor. If the +host+ is nil and there was no host specified
75
+ # in the constructor then an ArgumentError is raised.
76
+ #--
77
+ # The @duration should be set in the subclass' ping method.
78
+ #
79
+ def ping(host = @host)
80
+ raise ArgumentError, 'no host specified' unless host
81
+ @exception = nil
82
+ @warning = nil
83
+ @duration = nil
84
+ end
85
+
86
+ alias ping? ping
87
+ alias pingecho ping
88
+ end
89
+ end
data/lib/net/ping/tcp.rb CHANGED
@@ -1,83 +1,83 @@
1
- require File.join(File.dirname(__FILE__), 'ping')
2
-
3
- # The Net module serves as a namespace only.
4
- module Net
5
-
6
- # With a TCP ping simply try to open a connection. If we are successful,
7
- # assume success. In either case close the connection to be polite.
8
- #
9
- class Ping::TCP < Ping
10
- @@service_check = false
11
-
12
- # Returns whether or not Errno::ECONNREFUSED is considered a successful
13
- # ping. The default is false.
14
- #
15
- def self.service_check
16
- @@service_check
17
- end
18
-
19
- # Sets whether or not an Errno::ECONNREFUSED should be considered a
20
- # successful ping.
21
- #
22
- def self.service_check=(bool)
23
- unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
24
- raise ArgumentError, 'argument must be true or false'
25
- end
26
- @@service_check = bool
27
- end
28
-
29
- # This method attempts to ping a host and port using a TCPSocket with
30
- # the host, port and timeout values passed in the constructor. Returns
31
- # true if successful, or false otherwise.
32
- #
33
- # Note that, by default, an Errno::ECONNREFUSED return result will be
34
- # considered a failed ping. See the documentation for the
35
- # Ping::TCP.service_check= method if you wish to change this behavior.
36
- #
37
- def ping(host=@host)
38
- super(host)
39
-
40
- bool = false
41
- tcp = nil
42
- start_time = Time.now
43
-
44
- begin
45
- Timeout.timeout(@timeout){
46
- begin
47
- tcp = TCPSocket.new(host, @port)
48
- rescue Errno::ECONNREFUSED => err
49
- if @@service_check
50
- bool = true
51
- else
52
- @exception = err
53
- end
54
- rescue Exception => err
55
- @exception = err
56
- else
57
- bool = true
58
- end
59
- }
60
- rescue Timeout::Error => err
61
- @exception = err
62
- ensure
63
- tcp.close if tcp
64
- end
65
-
66
- # There is no duration if the ping failed
67
- @duration = Time.now - start_time if bool
68
-
69
- bool
70
- end
71
-
72
- alias ping? ping
73
- alias pingecho ping
74
-
75
- # Class method aliases. DEPRECATED.
76
- class << self
77
- alias econnrefused service_check
78
- alias econnrefused= service_check=
79
- alias ecr service_check
80
- alias ecr= service_check=
81
- end
82
- end
83
- end
1
+ require File.join(File.dirname(__FILE__), 'ping')
2
+
3
+ # The Net module serves as a namespace only.
4
+ module Net
5
+
6
+ # With a TCP ping simply try to open a connection. If we are successful,
7
+ # assume success. In either case close the connection to be polite.
8
+ #
9
+ class Ping::TCP < Ping
10
+ @@service_check = false
11
+
12
+ # Returns whether or not Errno::ECONNREFUSED is considered a successful
13
+ # ping. The default is false.
14
+ #
15
+ def self.service_check
16
+ @@service_check
17
+ end
18
+
19
+ # Sets whether or not an Errno::ECONNREFUSED should be considered a
20
+ # successful ping.
21
+ #
22
+ def self.service_check=(bool)
23
+ unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
24
+ raise ArgumentError, 'argument must be true or false'
25
+ end
26
+ @@service_check = bool
27
+ end
28
+
29
+ # This method attempts to ping a host and port using a TCPSocket with
30
+ # the host, port and timeout values passed in the constructor. Returns
31
+ # true if successful, or false otherwise.
32
+ #
33
+ # Note that, by default, an Errno::ECONNREFUSED return result will be
34
+ # considered a failed ping. See the documentation for the
35
+ # Ping::TCP.service_check= method if you wish to change this behavior.
36
+ #
37
+ def ping(host=@host)
38
+ super(host)
39
+
40
+ bool = false
41
+ tcp = nil
42
+ start_time = Time.now
43
+
44
+ begin
45
+ Timeout.timeout(@timeout){
46
+ begin
47
+ tcp = TCPSocket.new(host, @port)
48
+ rescue Errno::ECONNREFUSED => err
49
+ if @@service_check
50
+ bool = true
51
+ else
52
+ @exception = err
53
+ end
54
+ rescue Exception => err
55
+ @exception = err
56
+ else
57
+ bool = true
58
+ end
59
+ }
60
+ rescue Timeout::Error => err
61
+ @exception = err
62
+ ensure
63
+ tcp.close if tcp
64
+ end
65
+
66
+ # There is no duration if the ping failed
67
+ @duration = Time.now - start_time if bool
68
+
69
+ bool
70
+ end
71
+
72
+ alias ping? ping
73
+ alias pingecho ping
74
+
75
+ # Class method aliases. DEPRECATED.
76
+ class << self
77
+ alias econnrefused service_check
78
+ alias econnrefused= service_check=
79
+ alias ecr service_check
80
+ alias ecr= service_check=
81
+ end
82
+ end
83
+ end
data/lib/net/ping/udp.rb CHANGED
@@ -1,119 +1,119 @@
1
- require File.join(File.dirname(__FILE__), 'ping')
2
-
3
- # The Net module serves as a namespace only.
4
- module Net
5
-
6
- # The Ping::UDP class encapsulates methods for UDP pings.
7
- class Ping::UDP < Ping
8
- @@service_check = true
9
-
10
- # Returns whether or not the connect behavior should enforce remote
11
- # service availability as well as reachability. The default is true.
12
- #
13
- def self.service_check
14
- @@service_check
15
- end
16
-
17
- # Set whether or not the connect behavior should enforce remote
18
- # service availability as well as reachability. If set to false
19
- # then Errno::ECONNREFUSED or Errno::ECONNRESET will be considered
20
- # a successful ping, meaning no actual data handshaking is required.
21
- # By default, if either of those errors occurs it is considered a failed
22
- # ping.
23
- #
24
- def self.service_check=(bool)
25
- unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
26
- raise ArgumentError, 'argument must be true or false'
27
- end
28
- @@service_check = bool
29
- end
30
-
31
- # The maximum data size that can be sent in a UDP ping.
32
- MAX_DATA = 64
33
-
34
- # The data to send to the remote host. By default this is 'ping'.
35
- # This should be MAX_DATA size characters or less.
36
- #
37
- attr_reader :data
38
-
39
- # Creates and returns a new Ping::UDP object. This is effectively
40
- # identical to its superclass constructor.
41
- #
42
- def initialize(host=nil, port=nil, timeout=5)
43
- @data = 'ping'
44
-
45
- super(host, port, timeout)
46
-
47
- @bind_host = nil
48
- @bind_port = nil
49
- end
50
-
51
- # Sets the data string sent to the remote host. This value cannot have
52
- # a size greater than MAX_DATA.
53
- #
54
- def data=(string)
55
- if string.size > MAX_DATA
56
- err = "cannot set data string larger than #{MAX_DATA} characters"
57
- raise ArgumentError, err
58
- end
59
-
60
- @data = string
61
- end
62
-
63
- # Associates the local end of the UDP connection with the given +host+
64
- # and +port+. This is essentially a wrapper for UDPSocket#bind.
65
- #
66
- def bind(host, port)
67
- @bind_host = host
68
- @bind_port = port
69
- end
70
-
71
- # Sends a simple text string to the host and checks the return string. If
72
- # the string sent and the string returned are a match then the ping was
73
- # successful and true is returned. Otherwise, false is returned.
74
- #
75
- def ping(host = @host)
76
- super(host)
77
-
78
- bool = false
79
- udp = UDPSocket.open
80
- array = []
81
-
82
- if @bind_host
83
- udp.bind(@bind_host, @bind_port)
84
- end
85
-
86
- start_time = Time.now
87
-
88
- begin
89
- Timeout.timeout(@timeout){
90
- udp.connect(host, @port)
91
- udp.send(@data, 0)
92
- array = udp.recvfrom(MAX_DATA)
93
- }
94
- rescue Errno::ECONNREFUSED, Errno::ECONNRESET => err
95
- if @@service_check
96
- @exception = err
97
- else
98
- bool = true
99
- end
100
- rescue Exception => err
101
- @exception = err
102
- else
103
- if array[0] == @data
104
- bool = true
105
- end
106
- ensure
107
- udp.close if udp
108
- end
109
-
110
- # There is no duration if the ping failed
111
- @duration = Time.now - start_time if bool
112
-
113
- bool
114
- end
115
-
116
- alias ping? ping
117
- alias pingecho ping
118
- end
119
- end
1
+ require File.join(File.dirname(__FILE__), 'ping')
2
+
3
+ # The Net module serves as a namespace only.
4
+ module Net
5
+
6
+ # The Ping::UDP class encapsulates methods for UDP pings.
7
+ class Ping::UDP < Ping
8
+ @@service_check = true
9
+
10
+ # Returns whether or not the connect behavior should enforce remote
11
+ # service availability as well as reachability. The default is true.
12
+ #
13
+ def self.service_check
14
+ @@service_check
15
+ end
16
+
17
+ # Set whether or not the connect behavior should enforce remote
18
+ # service availability as well as reachability. If set to false
19
+ # then Errno::ECONNREFUSED or Errno::ECONNRESET will be considered
20
+ # a successful ping, meaning no actual data handshaking is required.
21
+ # By default, if either of those errors occurs it is considered a failed
22
+ # ping.
23
+ #
24
+ def self.service_check=(bool)
25
+ unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
26
+ raise ArgumentError, 'argument must be true or false'
27
+ end
28
+ @@service_check = bool
29
+ end
30
+
31
+ # The maximum data size that can be sent in a UDP ping.
32
+ MAX_DATA = 64
33
+
34
+ # The data to send to the remote host. By default this is 'ping'.
35
+ # This should be MAX_DATA size characters or less.
36
+ #
37
+ attr_reader :data
38
+
39
+ # Creates and returns a new Ping::UDP object. This is effectively
40
+ # identical to its superclass constructor.
41
+ #
42
+ def initialize(host=nil, port=nil, timeout=5)
43
+ @data = 'ping'
44
+
45
+ super(host, port, timeout)
46
+
47
+ @bind_host = nil
48
+ @bind_port = nil
49
+ end
50
+
51
+ # Sets the data string sent to the remote host. This value cannot have
52
+ # a size greater than MAX_DATA.
53
+ #
54
+ def data=(string)
55
+ if string.size > MAX_DATA
56
+ err = "cannot set data string larger than #{MAX_DATA} characters"
57
+ raise ArgumentError, err
58
+ end
59
+
60
+ @data = string
61
+ end
62
+
63
+ # Associates the local end of the UDP connection with the given +host+
64
+ # and +port+. This is essentially a wrapper for UDPSocket#bind.
65
+ #
66
+ def bind(host, port)
67
+ @bind_host = host
68
+ @bind_port = port
69
+ end
70
+
71
+ # Sends a simple text string to the host and checks the return string. If
72
+ # the string sent and the string returned are a match then the ping was
73
+ # successful and true is returned. Otherwise, false is returned.
74
+ #
75
+ def ping(host = @host)
76
+ super(host)
77
+
78
+ bool = false
79
+ udp = UDPSocket.open
80
+ array = []
81
+
82
+ if @bind_host
83
+ udp.bind(@bind_host, @bind_port)
84
+ end
85
+
86
+ start_time = Time.now
87
+
88
+ begin
89
+ Timeout.timeout(@timeout){
90
+ udp.connect(host, @port)
91
+ udp.send(@data, 0)
92
+ array = udp.recvfrom(MAX_DATA)
93
+ }
94
+ rescue Errno::ECONNREFUSED, Errno::ECONNRESET => err
95
+ if @@service_check
96
+ @exception = err
97
+ else
98
+ bool = true
99
+ end
100
+ rescue Exception => err
101
+ @exception = err
102
+ else
103
+ if array[0] == @data
104
+ bool = true
105
+ end
106
+ ensure
107
+ udp.close if udp
108
+ end
109
+
110
+ # There is no duration if the ping failed
111
+ @duration = Time.now - start_time if bool
112
+
113
+ bool
114
+ end
115
+
116
+ alias ping? ping
117
+ alias pingecho ping
118
+ end
119
+ end