net-ping 1.3.2 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,14 @@
1
+ == 1.3.3 - 21-Jun-2010
2
+ * Bug fixes for JRuby on MS Windows. The code now explicitly checks for JRuby
3
+ in a few places to ensure it doesn't try to load unsupported libraries
4
+ on MS Windows. Thanks go to Rob Schultz for the spot and some patches.
5
+ * The Net::Ping::HTTP class will no longer fail because a root URI
6
+ is missing a trailing slash. If the URI#path is empty, it now defaults
7
+ to the root path.
8
+ * The Rakefile tasks and naming were refactored.
9
+ * Some tests were refactored to take advantage of test-unit 2.x features,
10
+ as well as make them better and more descriptive in general.
11
+
1
12
  == 1.3.2 - 21-Sep-2009
2
13
  * Doing a 'require "net/ping"' was not automatically loading the
3
14
  Net::Ping::WMI class on MS Windows. This has been fixed. Thanks go to
data/README CHANGED
@@ -4,17 +4,13 @@
4
4
  == Prerequisites
5
5
  * Ruby 1.8.0 or later
6
6
  * The win32-open3 (1.8.x) and windows-pr libraries are required on
7
- MS Windows when using the Net::Ping::External class.
7
+ MS Windows when using the Net::Ping::External class unless you're
8
+ using JRuby.
8
9
  * Windows 2000 or later is required to use the Ping::WMI class.
9
10
 
10
11
  == Installation
11
- === Remote Installation
12
12
  gem install net-ping
13
13
 
14
- === Local Installation
15
- rake test (optional)
16
- rake install (standard) OR rake gem_install (gems)
17
-
18
14
  == Notes
19
15
  Please read the documentation under the 'doc' directory. Especially pay
20
16
  attention to the documentation pertaining to ECONNREFUSED and TCP pings.
@@ -40,6 +36,8 @@
40
36
 
41
37
  == Known Issues
42
38
  Older versions of Ruby 1.9.x may not work with UDP pings.
39
+ As of JRuby 1.5.1, UDP pings will return false positives because of an
40
+ incorrect error class being raised. See JRuby-4896.
43
41
 
44
42
  == License
45
43
  Artistic 2.0
data/Rakefile CHANGED
@@ -2,31 +2,18 @@ require 'rake'
2
2
  require 'rake/testtask'
3
3
  include Config
4
4
 
5
- desc "Install the net-ping package (non-gem)"
6
- task :install do
7
- dest1 = File.join(CONFIG['sitelibdir'], 'net')
8
- dest2 = File.join(dest1, 'ping')
9
-
10
- Dir.mkdir(dest1) unless File.exists?(dest1)
11
- Dir.mkdir(dest2) unless File.exists?(dest2)
12
-
13
- FileUtils.cp('lib/net/ping.rb', dest1, :verbose => true)
14
-
15
- Dir['lib/net/ping/*.rb'].each{ |file|
16
- FileUtils.cp(file, dest2, :verbose => true)
17
- }
18
- end
19
-
20
- desc 'Create and install the net-ping gem'
21
- task :gem_install => [:gem] do
22
- gem_file = Dir["*.gem"].first
23
- sh "gem install #{gem_file}"
24
- end
5
+ namespace 'gem' do
6
+ desc 'Create the net-ping gem'
7
+ task :create do
8
+ spec = eval(IO.read('net-ping.gemspec'))
9
+ Gem::Builder.new(spec).build
10
+ end
25
11
 
26
- desc 'Create the net-ping gem'
27
- task :gem do
28
- spec = eval(IO.read('net-ping.gemspec'))
29
- Gem::Builder.new(spec).build
12
+ desc 'Install the net-ping gem'
13
+ task :install => [:create] do
14
+ gem_file = Dir["*.gem"].first
15
+ sh "gem install #{gem_file}"
16
+ end
30
17
  end
31
18
 
32
19
  namespace 'example' do
data/doc/ping.txt CHANGED
@@ -238,7 +238,7 @@ Ping#warning
238
238
  Artistic 2.0
239
239
 
240
240
  = Copyright
241
- (C) 2003-2009 Daniel J. Berger, All Rights Reserved
241
+ (C) 2003-2010 Daniel J. Berger, All Rights Reserved
242
242
 
243
243
  = Warranty
244
244
  This package is provided "as is" and without any express or
@@ -247,5 +247,3 @@ Ping#warning
247
247
 
248
248
  = Author
249
249
  Daniel J. Berger
250
- djberg96 at gmail dot com
251
- imperator on IRC (irc.freenode.net)
data/lib/net/ping.rb CHANGED
@@ -10,6 +10,8 @@ require File.join(File.dirname(__FILE__), 'ping/icmp')
10
10
  require File.join(File.dirname(__FILE__), 'ping/external')
11
11
  require File.join(File.dirname(__FILE__), 'ping/http')
12
12
 
13
- if Config::CONFIG['host_os'] =~ /dos|mswin|cygwin|mingw|win32/i
14
- require File.join(File.dirname(__FILE__), 'ping/wmi')
13
+ if Config::CONFIG['host_os'] =~ /msdos|mswin|cygwin|mingw|win32/i &&
14
+ JAVA_PLATFORM != 'java'
15
+ then
16
+ require File.join(File.dirname(__FILE__), 'ping/wmi')
15
17
  end
@@ -1,119 +1,126 @@
1
1
  require 'rbconfig'
2
2
  require File.join(File.dirname(__FILE__), 'ping')
3
3
 
4
- if Config::CONFIG['host_os'] =~ /mswin|win32|dos|cygwin|mingw/i
5
- if RUBY_VERSION.to_f < 1.9
6
- require 'win32/open3'
7
- end
8
- require 'windows/console'
4
+ if Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i &&
5
+ RUBY_PLATFORM != 'java'
6
+ then
7
+ if RUBY_VERSION.to_f < 1.9
8
+ require 'win32/open3'
9
+ end
10
+ require 'windows/console'
9
11
  else
10
- require 'open3'
12
+ require 'open3'
11
13
  end
12
14
 
13
15
  # The Net module serves as a namespace only.
14
16
  module Net
15
17
 
16
- # The Ping::External class encapsulates methods for external (system) pings.
17
- class Ping::External < Ping
18
+ # The Ping::External class encapsulates methods for external (system) pings.
19
+ class Ping::External < Ping
20
+
21
+ CWINDOWS = Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i &&
22
+ RUBY_PLATFORM != 'java'
18
23
 
19
- if Config::CONFIG['host_os'] =~ /mswin|win32|dos|cygwin|mingw/i
20
- include Windows::Console
21
- end
24
+ if CWINDOWS
25
+ include Windows::Console
26
+ end
22
27
 
23
- # Pings the host using your system's ping utility and checks for any
24
- # errors or warnings. Returns true if successful, or false if not.
25
- #
26
- # If the ping failed then the Ping::External#exception method should
27
- # contain a string indicating what went wrong. If the ping succeeded then
28
- # the Ping::External#warning method may or may not contain a value.
29
- #
30
- def ping(host = @host)
31
- super(host)
28
+ # Pings the host using your system's ping utility and checks for any
29
+ # errors or warnings. Returns true if successful, or false if not.
30
+ #
31
+ # If the ping failed then the Ping::External#exception method should
32
+ # contain a string indicating what went wrong. If the ping succeeded then
33
+ # the Ping::External#warning method may or may not contain a value.
34
+ #
35
+ def ping(host = @host)
36
+ super(host)
32
37
 
33
- input, output, error = ""
34
- pstring = "ping "
35
- bool = false
36
- orig_cp = nil
37
-
38
- case Config::CONFIG['host_os']
39
- when /linux|bsd|osx|mach|darwin/i
40
- pstring += "-c 1 #{host}"
41
- when /solaris|sunos/i
42
- pstring += "#{host} 1"
43
- when /hpux/i
44
- pstring += "#{host} -n 1"
45
- when /win32|windows|mswin/i
46
- orig_cp = GetConsoleCP()
47
- SetConsoleCP(437) if orig_cp != 437 # United States
48
- pstring += "-n 1 #{host}"
49
- else
50
- pstring += "#{host}"
51
- end
52
-
53
- start_time = Time.now
54
-
55
- begin
56
- e = nil
38
+ input, output, error = ""
39
+ pstring = "ping "
40
+ bool = false
41
+ orig_cp = nil
42
+
43
+ case Config::CONFIG['host_os']
44
+ when /linux|bsd|osx|mach|darwin/i
45
+ pstring += "-c 1 #{host}"
46
+ when /solaris|sunos/i
47
+ pstring += "#{host} 1"
48
+ when /hpux/i
49
+ pstring += "#{host} -n 1"
50
+ when /win32|windows|msdos|mswin|cygwin|mingw/i
51
+ if RUBY_PLATFORM != 'java'
52
+ orig_cp = GetConsoleCP()
53
+ SetConsoleCP(437) if orig_cp != 437 # United States
54
+ end
55
+ pstring += "-n 1 #{host}"
56
+ else
57
+ pstring += "#{host}"
58
+ end
59
+
60
+ start_time = Time.now
61
+
62
+ begin
63
+ err = nil
57
64
 
58
- Timeout.timeout(@timeout){
59
- input, output, error = Open3.popen3(pstring)
60
- e = error.gets # Can't chomp yet, might be nil
61
- }
65
+ Timeout.timeout(@timeout){
66
+ input, output, error = Open3.popen3(pstring)
67
+ err = error.gets # Can't chomp yet, might be nil
68
+ }
62
69
 
63
- input.close
64
- error.close
70
+ input.close
71
+ error.close
65
72
 
66
- if Config::CONFIG['host_os'] =~ /mswin|win32|dos/i &&
67
- GetConsoleCP() != orig_cp
68
- then
69
- SetConsoleCP(orig_cp)
70
- end
71
-
72
- unless e.nil?
73
- if e =~ /warning/i
74
- @warning = e.chomp
75
- bool = true
76
- else
77
- @exception = e.chomp
78
- end
79
- # The "no answer" response goes to stdout, not stderr, so check it
80
- else
81
- lines = output.readlines
82
- output.close
83
- if lines.nil? || lines.empty?
84
- bool = true
85
- else
86
- regexp = /
87
- no\ answer|
88
- host\ unreachable|
89
- could\ not\ find\ host|
90
- request\ timed\ out|
91
- 100%\ packet\ loss
92
- /ix
93
- lines.each{ |line|
94
- if regexp.match(line)
95
- @exception = line.chomp
96
- break
97
- end
98
- }
99
- bool = true unless @exception
100
- end
101
- end
102
- rescue Exception => err
103
- @exception = err.message
104
- ensure
105
- input.close if input && !input.closed?
106
- error.close if error && !error.closed?
107
- output.close if output && !output.closed?
108
- end
73
+ if CWINDOWS && GetConsoleCP() != orig_cp
74
+ SetConsoleCP(orig_cp)
75
+ end
76
+
77
+ unless err.nil?
78
+ if err =~ /warning/i
79
+ @warning = err.chomp
80
+ bool = true
81
+ else
82
+ @exception = err.chomp
83
+ end
84
+ # The "no answer" response goes to stdout, not stderr, so check it
85
+ else
86
+ lines = output.readlines
87
+ output.close
88
+ if lines.nil? || lines.empty?
89
+ bool = true
90
+ else
91
+ regexp = /
92
+ no\ answer|
93
+ host\ unreachable|
94
+ could\ not\ find\ host|
95
+ request\ timed\ out|
96
+ 100%\ packet\ loss
97
+ /ix
109
98
 
110
- # There is no duration if the ping failed
111
- @duration = Time.now - start_time if bool
99
+ lines.each{ |line|
100
+ if regexp.match(line)
101
+ @exception = line.chomp
102
+ break
103
+ end
104
+ }
112
105
 
113
- bool
106
+ bool = true unless @exception
107
+ end
108
+ end
109
+ rescue Exception => error
110
+ @exception = error.message
111
+ ensure
112
+ input.close if input && !input.closed?
113
+ error.close if error && !error.closed?
114
+ output.close if output && !output.closed?
114
115
  end
115
116
 
116
- alias ping? ping
117
- alias pingecho ping
118
- end
117
+ # There is no duration if the ping failed
118
+ @duration = Time.now - start_time if bool
119
+
120
+ bool
121
+ end
122
+
123
+ alias ping? ping
124
+ alias pingecho ping
125
+ end
119
126
  end
data/lib/net/ping/http.rb CHANGED
@@ -5,91 +5,94 @@ require 'rbconfig'
5
5
 
6
6
  # Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
7
7
  # Windows because it (ironically) causes blocking problems.
8
- unless Config::CONFIG['host_os'] =~ /mswin|win32|dos|cygwin|mingw/i
9
- require 'resolv-replace'
8
+ unless Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i
9
+ require 'resolv-replace'
10
10
  end
11
11
 
12
12
  # The Net module serves as a namespace only.
13
13
  module Net
14
14
 
15
- # The Ping::HTTP class encapsulates methods for HTTP pings.
16
- class Ping::HTTP < Ping
15
+ # The Ping::HTTP class encapsulates methods for HTTP pings.
16
+ class Ping::HTTP < Ping
17
17
 
18
- # By default an http ping will follow a redirect and give you the result
19
- # of the final URI. If this value is set to false, then it will not
20
- # follow a redirect and will return false immediately on a redirect.
21
- #
22
- attr_accessor :follow_redirect
18
+ # By default an http ping will follow a redirect and give you the result
19
+ # of the final URI. If this value is set to false, then it will not
20
+ # follow a redirect and will return false immediately on a redirect.
21
+ #
22
+ attr_accessor :follow_redirect
23
23
 
24
- # Creates and returns a new Ping::HTTP object. Note that the default
25
- # port for Ping::HTTP is 80.
26
- #
27
- def initialize(uri=nil, port=80, timeout=5)
28
- @follow_redirect = true
29
- super(uri, port, timeout)
30
- end
24
+ # Creates and returns a new Ping::HTTP object. Note that the default
25
+ # port for Ping::HTTP is 80.
26
+ #
27
+ def initialize(uri=nil, port=80, timeout=5)
28
+ @follow_redirect = true
29
+ super(uri, port, timeout)
30
+ end
31
31
 
32
- # Looks for an HTTP response from the URI passed to the constructor.
33
- # If the result is a kind of Net::HTTPSuccess then the ping was
34
- # boolful and true is returned. Otherwise, false is returned
35
- # and the Ping::HTTP#exception method should contain a string
36
- # indicating what went wrong.
37
- #
38
- # If the HTTP#follow_redirect accessor is set to true (which it is
39
- # by default) and a redirect occurs during the ping, then the
40
- # HTTP#warning attribute is set to the redirect message, but the
41
- # return result is still true. If it's set to false then a false
42
- # value is returned if a redirect occurs.
43
- #
44
- def ping(host = @host)
45
- super(host)
46
- bool = false
47
- uri = URI.parse(host)
32
+ # Looks for an HTTP response from the URI passed to the constructor.
33
+ # If the result is a kind of Net::HTTPSuccess then the ping was
34
+ # successful and true is returned. Otherwise, false is returned
35
+ # and the Ping::HTTP#exception method should contain a string
36
+ # indicating what went wrong.
37
+ #
38
+ # If the HTTP#follow_redirect accessor is set to true (which it is
39
+ # by default) and a redirect occurs during the ping, then the
40
+ # HTTP#warning attribute is set to the redirect message, but the
41
+ # return result is still true. If it's set to false then a redirect
42
+ # response is considered a failed ping.
43
+ #
44
+ # If no file or path is specified in the URI, then '/' is assumed.
45
+ #
46
+ def ping(host = @host)
47
+ super(host)
48
+ bool = false
49
+ uri = URI.parse(host)
48
50
 
49
- start_time = Time.now
51
+ start_time = Time.now
50
52
 
51
- begin
52
- response = nil
53
- Timeout.timeout(@timeout){
54
- response = Net::HTTP.get_response(uri.host, uri.path, @port)
55
- }
56
- rescue Exception => err
57
- @exception = err.message
58
- else
53
+ begin
54
+ response = nil
55
+ uri_path = uri.path.empty? ? '/' : uri.path
56
+ Timeout.timeout(@timeout){
57
+ response = Net::HTTP.get_response(uri.host, uri_path, @port)
58
+ }
59
+ rescue Exception => err
60
+ @exception = err.message
61
+ else
62
+ if response.is_a?(Net::HTTPSuccess)
63
+ bool = true
64
+ else
65
+ if @follow_redirect
66
+ @warning = response.message
67
+
68
+ while response.is_a?(Net::HTTPRedirection)
69
+ redirect = URI.parse(response['location'])
70
+ redirect = uri + redirect if redirect.relative?
71
+ response = Net::HTTP.get_response(redirect.host, redirect.path, @port)
72
+ end
73
+
59
74
  if response.is_a?(Net::HTTPSuccess)
60
- bool = true
75
+ bool = true
61
76
  else
62
- if @follow_redirect
63
- @warning = response.message
64
-
65
- while response.is_a?(Net::HTTPRedirection)
66
- redirect = URI.parse(response['location'])
67
- redirect = uri + redirect if redirect.relative?
68
- response = Net::HTTP.get_response(redirect.host, redirect.path, @port)
69
- end
70
-
71
- if response.is_a?(Net::HTTPSuccess)
72
- bool = true
73
- else
74
- @warning = nil
75
- @exception = response.message
76
- end
77
- else
78
- @exception = response.message
79
- end
77
+ @warning = nil
78
+ @exception = response.message
80
79
  end
81
- end
80
+ else
81
+ @exception = response.message
82
+ end
83
+ end
84
+ end
82
85
 
83
- # There is no duration if the ping failed
84
- @duration = Time.now - start_time if bool
86
+ # There is no duration if the ping failed
87
+ @duration = Time.now - start_time if bool
85
88
 
86
- bool
87
- end
89
+ bool
90
+ end
88
91
 
89
- alias ping? ping
90
- alias pingecho ping
91
- alias follow_redirect? follow_redirect
92
- alias uri host
93
- alias uri= host=
94
- end
92
+ alias ping? ping
93
+ alias pingecho ping
94
+ alias follow_redirect? follow_redirect
95
+ alias uri host
96
+ alias uri= host=
97
+ end
95
98
  end