net-ping 1.2.2-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,86 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+ require 'ping'
3
+
4
+ module Net
5
+
6
+ # With a TCP ping, simply try to open a connection. If we are successful,
7
+ # assume bool. 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
+
84
+ # Class alias for backwards compatibility
85
+ PingTCP = Ping::TCP
86
+ end
@@ -0,0 +1,119 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+ require 'ping'
3
+
4
+ module Net
5
+ class Ping::UDP < Ping
6
+ @@service_check = true
7
+
8
+ # Returns whether or not the connect behavior should enforce remote
9
+ # service availability as well as reachability. The default is true.
10
+ #
11
+ def self.service_check
12
+ @@service_check
13
+ end
14
+
15
+ # Set whether or not the connect behavior should enforce remote
16
+ # service availability as well as reachability. If set to false
17
+ # then Errno::ECONNREFUSED or Errno::ECONNRESET will be considered
18
+ # a successful ping, meaning no actual data handshaking is required.
19
+ # By default, if either of those errors occurs it is considered a failed
20
+ # 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
+ MAX_DATA = 64
30
+
31
+ # The data to send to the remote host. By default this is 'ping'.
32
+ # This should be MAX_DATA size characters or less.
33
+ #
34
+ attr_reader :data
35
+
36
+ # Creates and returns a new Ping::UDP object. This is effectively
37
+ # identical to its superclass constructor.
38
+ #
39
+ def initialize(host=nil, port=nil, timeout=5)
40
+ @data = 'ping'
41
+
42
+ super(host, port, timeout)
43
+
44
+ @bind_host = nil
45
+ @bind_port = nil
46
+ end
47
+
48
+ # Sets the data string sent to the remote host. This value cannot have
49
+ # a size greater than MAX_DATA.
50
+ #
51
+ def data=(string)
52
+ if string.size > MAX_DATA
53
+ err = "cannot set data string larger than #{MAX_DATA} characters"
54
+ raise ArgumentError, err
55
+ end
56
+
57
+ @data = string
58
+ end
59
+
60
+ # Associates the local end of the UDP connection with the given +host+
61
+ # and +port+. This is essentially a wrapper for UDPSocket#bind.
62
+ #
63
+ def bind(host, port)
64
+ @bind_host = host
65
+ @bind_port = port
66
+ end
67
+
68
+ # Sends a simple text string to the host and checks the return string. If
69
+ # the string sent and the string returned are a match then the ping was
70
+ # successful and true is returned. Otherwise, false is returned.
71
+ #
72
+ def ping(host = @host)
73
+ super(host)
74
+
75
+ bool = false
76
+ udp = UDPSocket.open
77
+ array = []
78
+
79
+ if @bind_host
80
+ udp.bind(@bind_host, @bind_port)
81
+ end
82
+
83
+ start_time = Time.now
84
+
85
+ begin
86
+ Timeout.timeout(@timeout){
87
+ udp.connect(host, @port)
88
+ udp.send(@data, 0)
89
+ array = udp.recvfrom(MAX_DATA)
90
+ }
91
+ rescue Errno::ECONNREFUSED, Errno::ECONNRESET => err
92
+ if @@service_check
93
+ @exception = err
94
+ else
95
+ bool = true
96
+ end
97
+ rescue Exception => err
98
+ @exception = err
99
+ else
100
+ if array[0] == @data
101
+ bool = true
102
+ end
103
+ ensure
104
+ udp.close if udp
105
+ end
106
+
107
+ # There is no duration if the ping failed
108
+ @duration = Time.now - start_time if bool
109
+
110
+ bool
111
+ end
112
+
113
+ alias ping? ping
114
+ alias pingecho ping
115
+ end
116
+
117
+ # Class alias for backwards compatibility
118
+ PingUDP = Ping::UDP
119
+ end
File without changes
data/net-ping.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ require "rubygems"
2
+
3
+ spec = Gem::Specification.new do |gem|
4
+ gem.name = "net-ping"
5
+ gem.version = "1.2.2"
6
+ gem.author = "Daniel J. Berger"
7
+ gem.email = "djberg96@gmail.com"
8
+ gem.homepage = "http://www.rubyforge.org/projects/shards"
9
+ gem.summary = "A ping interface for Ruby."
10
+ gem.test_file = "test/ts_ping.rb"
11
+ gem.has_rdoc = true
12
+ gem.rubyforge_project = "shards"
13
+ gem.files = Dir["lib/net/*"] + Dir['[A-Z]*'] + Dir['test/*']
14
+ gem.files += Dir["lib/net/ping/*"]
15
+ gem.files.reject! { |fn| fn.include? "CVS" }
16
+ gem.extra_rdoc_files = ["README", "CHANGES", "doc/ping.txt"]
17
+
18
+ case RUBY_PLATFORM
19
+ when /mswin/i
20
+ gem.platform = Gem::Platform::CURRENT
21
+ gem.add_dependency('win32-open3', '>= 0.2.5') # Ping::External
22
+ gem.add_dependency('windows-pr', '>= 0.7.4') # Ping::External
23
+ else
24
+ gem.platform = Gem::Platform::RUBY
25
+ end
26
+
27
+ description = "A ping interface for Ruby. Includes TCP, HTTP, ICMP, UDP, "
28
+ description << "and External ping interfaces."
29
+ gem.description = description
30
+ end
31
+
32
+ if $0 == __FILE__
33
+ Gem.manage_gems
34
+ Gem::Builder.new(spec).build
35
+ end
data/net-ping.gemspec~ ADDED
@@ -0,0 +1,34 @@
1
+ require "rubygems"
2
+
3
+ spec = Gem::Specification.new do |gem|
4
+ gem.name = "net-ping"
5
+ gem.version = "1.2.2"
6
+ gem.author = "Daniel J. Berger"
7
+ gem.email = "djberg96@gmail.com"
8
+ gem.homepage = "http://www.rubyforge.org/projects/shards"
9
+ gem.summary = "A ping interface for Ruby."
10
+ gem.test_file = "test/ts_ping.rb"
11
+ gem.has_rdoc = true
12
+ gem.files = Dir["lib/net/*"] + Dir['[A-Z]*'] + Dir['test/*']
13
+ gem.files += Dir["lib/net/ping/*"]
14
+ gem.files.reject! { |fn| fn.include? "CVS" }
15
+ gem.extra_rdoc_files = ["README", "CHANGES", "doc/ping.txt"]
16
+
17
+ case RUBY_PLATFORM
18
+ when /mswin/i
19
+ gem.platform = Gem::Platform::CURRENT
20
+ gem.add_dependency('win32-open3', '>= 0.2.5') # Ping::External
21
+ gem.add_dependency('windows-pr', '>= 0.7.4') # Ping::External
22
+ else
23
+ gem.platform = Gem::Platform::RUBY
24
+ end
25
+
26
+ description = "A ping interface for Ruby. Includes TCP, HTTP, ICMP, UDP, "
27
+ description << "and External ping interfaces."
28
+ gem.description = description
29
+ end
30
+
31
+ if $0 == __FILE__
32
+ Gem.manage_gems
33
+ Gem::Builder.new(spec).build
34
+ end
@@ -0,0 +1,89 @@
1
+ #########################################################################
2
+ # tc_pingexternal.rb
3
+ #
4
+ # Test case for the Net::PingExternal class. Run this via the 'test' or
5
+ # 'test_external' rake task.
6
+ #########################################################################
7
+ require 'test/unit'
8
+ require 'net/ping/external'
9
+ include Net
10
+
11
+ class TC_PingExternal < Test::Unit::TestCase
12
+ def setup
13
+ @host = 'www.ruby-lang.org'
14
+ @bogus = 'foo.bar.baz'
15
+ @pe = Ping::External.new(@host)
16
+ @bad = Ping::External.new(@bogus)
17
+ end
18
+
19
+ def test_version
20
+ assert_equal('1.2.2', PingExternal::VERSION)
21
+ end
22
+
23
+ def test_ping
24
+ assert_respond_to(@pe, :ping)
25
+ assert_nothing_raised{ @pe.ping }
26
+ assert_nothing_raised{ @pe.ping(@host) }
27
+ end
28
+
29
+ def test_ping_aliases
30
+ assert_respond_to(@pe, :ping?)
31
+ assert_respond_to(@pe, :pingecho)
32
+ assert_nothing_raised{ @pe.ping? }
33
+ assert_nothing_raised{ @pe.ping?(@host) }
34
+ assert_nothing_raised{ @pe.pingecho }
35
+ assert_nothing_raised{ @pe.pingecho(@host) }
36
+ end
37
+
38
+ def test_good_ping
39
+ assert_equal(true, @pe.ping?)
40
+ end
41
+
42
+ def test_bad_ping
43
+ assert_equal(false, @bad.ping?)
44
+ assert_equal(false, @bad.exception.nil?, "Bad exception data")
45
+ end
46
+
47
+ def test_duration
48
+ assert_nothing_raised{ @pe.ping }
49
+ assert_respond_to(@pe, :duration)
50
+ assert_kind_of(Float, @pe.duration)
51
+ end
52
+
53
+ def test_host
54
+ assert_respond_to(@pe, :host)
55
+ assert_respond_to(@pe, :host=)
56
+ assert_equal('www.ruby-lang.org', @pe.host)
57
+ end
58
+
59
+ def test_port
60
+ assert_respond_to(@pe, :port)
61
+ assert_respond_to(@pe, :port=)
62
+ assert_equal(7, @pe.port)
63
+ end
64
+
65
+ def test_timeout
66
+ assert_respond_to(@pe, :timeout)
67
+ assert_respond_to(@pe, :timeout=)
68
+ assert_equal(5, @pe.timeout)
69
+ end
70
+
71
+ def test_exception
72
+ assert_respond_to(@pe, :exception)
73
+ assert_nothing_raised{ @pe.ping }
74
+ assert_nothing_raised{ @bad.ping }
75
+ assert_nil(@pe.exception)
76
+ assert_not_nil(@bad.exception)
77
+ end
78
+
79
+ def test_warning
80
+ assert_respond_to(@pe, :warning)
81
+ end
82
+
83
+ def teardown
84
+ @host = nil
85
+ @bogus = nil
86
+ @pe = nil
87
+ @bad = nil
88
+ end
89
+ end
@@ -0,0 +1,83 @@
1
+ #################################################################################
2
+ # tc_pinghttp.rb
3
+ #
4
+ # Test case for the Net::PingHTTP class. This should be run via the 'test' or
5
+ # 'test_http' test task.
6
+ #################################################################################
7
+ require 'test/unit'
8
+ require 'net/ping/http'
9
+ include Net
10
+
11
+ class TC_PingHTTP < Test::Unit::TestCase
12
+ def setup
13
+ @uri = 'http://www.google.com/index.html'
14
+ @http = Ping::HTTP.new(@uri, 80, 30)
15
+ @bad = Ping::HTTP.new('http://www.blabfoobarurgh.com') # One hopes not
16
+ end
17
+
18
+ def test_version
19
+ assert_equal('1.2.2', Ping::HTTP::VERSION)
20
+ end
21
+
22
+ def test_ping
23
+ assert_respond_to(@http, :ping)
24
+ assert_nothing_raised{ @http.ping }
25
+ end
26
+
27
+ def test_ping_aliases
28
+ assert_respond_to(@http, :ping?)
29
+ assert_respond_to(@http, :pingecho)
30
+ assert_nothing_raised{ @http.ping? }
31
+ assert_nothing_raised{ @http.pingecho }
32
+ end
33
+
34
+ def test_ping_success
35
+ assert_equal(true, @http.ping?)
36
+ assert_equal(false, @bad.ping?)
37
+ assert_not_nil(@bad.exception)
38
+ end
39
+
40
+ def test_duration
41
+ assert_nothing_raised{ @http.ping }
42
+ assert_respond_to(@http, :duration)
43
+ assert_kind_of(Float, @http.duration)
44
+ end
45
+
46
+ def test_host
47
+ assert_respond_to(@http, :host)
48
+ assert_respond_to(@http, :host=)
49
+ assert_respond_to(@http, :uri) # Alias
50
+ assert_respond_to(@http, :uri=) # Alias
51
+ assert_equal('http://www.google.com/index.html', @http.host)
52
+ end
53
+
54
+ def test_port
55
+ assert_respond_to(@http, :port)
56
+ assert_respond_to(@http, :port=)
57
+ assert_equal(80, @http.port)
58
+ end
59
+
60
+ def test_timeout
61
+ assert_respond_to(@http, :timeout)
62
+ assert_respond_to(@http, :timeout=)
63
+ assert_equal(30, @http.timeout)
64
+ assert_equal(5, @bad.timeout)
65
+ end
66
+
67
+ def test_exception
68
+ assert_respond_to(@http, :exception)
69
+ assert_nothing_raised{ @http.ping }
70
+ assert_nothing_raised{ @bad.ping }
71
+ assert_nil(@http.exception)
72
+ assert_not_nil(@bad.exception)
73
+ end
74
+
75
+ def test_warning
76
+ assert_respond_to(@http, :warning)
77
+ end
78
+
79
+ def teardown
80
+ @uri = nil
81
+ @http = nil
82
+ end
83
+ end