mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/ping.rb, lib/readbytes.rb: removed
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14283 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
51b0310930
commit
97591e82d9
4 changed files with 2 additions and 108 deletions
|
@ -2,6 +2,8 @@ Tue Dec 18 05:54:26 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
|||
|
||||
* lib/Env.rb, lib/base64.rb, lib/importenv.rb, lib/eregex.rb: removed.
|
||||
|
||||
* lib/ping.rb, lib/readbytes.rb: removed
|
||||
|
||||
Tue Dec 18 02:30:56 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* configure.in (BUILTIN_ENCS): removed.
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
English.rb lets Perl'ish global variables have English names
|
||||
README this file
|
||||
benchmark.rb a benchmark utility
|
||||
cgi-lib.rb simple CGI support library (old style)
|
||||
cgi.rb CGI support library
|
||||
cgi/session.rb CGI session class
|
||||
complex.rb complex number support
|
||||
|
@ -45,7 +44,6 @@ ostruct.rb python style object
|
|||
parsearg.rb argument parser using getopts
|
||||
parsedate.rb parses date string (obsolete)
|
||||
pathname.rb Object-Oriented Pathname Class
|
||||
ping.rb checks whether host is up, using TCP echo.
|
||||
pp.rb pretty print objects
|
||||
prettyprint.rb pretty printing algorithm
|
||||
profile.rb runs ruby profiler
|
||||
|
@ -54,7 +52,6 @@ pstore.rb persistent object strage using marshal
|
|||
racc/parser.rb racc (Ruby yACC) runtime
|
||||
rational.rb rational number support
|
||||
rdoc source-code documentation tool
|
||||
readbytes.rb define IO#readbytes
|
||||
resolv-replace.rb replace Socket DNS by resolve.rb
|
||||
resolv.rb DNS resolver in Ruby
|
||||
rexml an XML parser for Ruby, in Ruby
|
||||
|
|
64
lib/ping.rb
64
lib/ping.rb
|
@ -1,64 +0,0 @@
|
|||
#
|
||||
# = ping.rb: Check a host for upness
|
||||
#
|
||||
# Author:: Yukihiro Matsumoto
|
||||
# Documentation:: Konrad Meyer
|
||||
#
|
||||
# Performs the function of the basic network testing tool, ping.
|
||||
# See: Ping.
|
||||
#
|
||||
|
||||
require 'timeout'
|
||||
require "socket"
|
||||
|
||||
#
|
||||
# Ping contains routines to test for the reachability of remote hosts.
|
||||
# Currently the only routine implemented is pingecho().
|
||||
#
|
||||
# Ping.pingecho uses a TCP echo (not an ICMP echo) to determine if the
|
||||
# remote host is reachable. This is usually adequate to tell that a remote
|
||||
# host is available to telnet, ftp, or ssh to.
|
||||
#
|
||||
# Warning: Ping.pingecho may block for a long time if DNS resolution is
|
||||
# slow. Requiring 'resolv-replace' allows non-blocking name resolution.
|
||||
#
|
||||
# Usage:
|
||||
#
|
||||
# require 'ping'
|
||||
#
|
||||
# puts "'jimmy' is alive and kicking" if Ping.pingecho('jimmy', 10)
|
||||
#
|
||||
module Ping
|
||||
|
||||
#
|
||||
# Return true if we can open a connection to the hostname or IP address
|
||||
# +host+ on port +service+ (which defaults to the "echo" port) waiting up
|
||||
# to +timeout+ seconds.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# require 'ping'
|
||||
#
|
||||
# Ping.pingecho "google.com", 10, 80
|
||||
#
|
||||
def pingecho(host, timeout=5, service="echo")
|
||||
begin
|
||||
timeout(timeout) do
|
||||
s = TCPSocket.new(host, service)
|
||||
s.close
|
||||
end
|
||||
rescue Errno::ECONNREFUSED
|
||||
return true
|
||||
rescue Timeout::Error, StandardError
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
module_function :pingecho
|
||||
end
|
||||
|
||||
if $0 == __FILE__
|
||||
host = ARGV[0]
|
||||
host ||= "localhost"
|
||||
printf("%s alive? - %s\n", host, Ping::pingecho(host, 5))
|
||||
end
|
|
@ -1,41 +0,0 @@
|
|||
# TruncatedDataError is raised when IO#readbytes fails to read enough data.
|
||||
|
||||
class TruncatedDataError<IOError
|
||||
def initialize(mesg, data) # :nodoc:
|
||||
@data = data
|
||||
super(mesg)
|
||||
end
|
||||
|
||||
# The read portion of an IO#readbytes attempt.
|
||||
attr_reader :data
|
||||
end
|
||||
|
||||
class IO
|
||||
# Reads exactly +n+ bytes.
|
||||
#
|
||||
# If the data read is nil an EOFError is raised.
|
||||
#
|
||||
# If the data read is too short a TruncatedDataError is raised and the read
|
||||
# data is obtainable via its #data method.
|
||||
def readbytes(n)
|
||||
str = read(n)
|
||||
if str == nil
|
||||
raise EOFError, "End of file reached"
|
||||
end
|
||||
if str.size < n
|
||||
raise TruncatedDataError.new("data truncated", str)
|
||||
end
|
||||
str
|
||||
end
|
||||
end
|
||||
|
||||
if __FILE__ == $0
|
||||
begin
|
||||
loop do
|
||||
print STDIN.readbytes(6)
|
||||
end
|
||||
rescue TruncatedDataError
|
||||
p $!.data
|
||||
raise
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue