1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
require 'timeout'
module Net # :nodoc:
module DNS
class Resolver
class DnsTimeout
attr_reader :seconds
def initialize(seconds)
if seconds.is_a?(Numeric) && seconds >= 0
@seconds = seconds
else
raise ArgumentError, "Invalid value for tcp timeout"
end
end
# Returns a string representation of the timeout corresponding
# to the number of <tt>@seconds</tt>.
def to_s
@seconds == 0 ? @output.to_s : @seconds.to_s
end
def pretty_to_s
transform(@seconds)
end
# Executes the method's block. If the block execution terminates before +sec+
# seconds has passed, it returns true. If not, it terminates the execution
# and raises Timeout::Error.
# If @seconds is 0 or nil, no timeout is set.
def timeout(&block)
raise LocalJumpError, "no block given" unless block_given?
Timeout.timeout(@seconds, &block)
end
private
def transform(secs)
case secs
when 0
to_s
when 1..59
"#{secs} seconds"
when 60..3559
"#{secs / 60} minutes and #{secs % 60} seconds"
else
hours = secs / 3600
secs -= (hours * 3600)
"#{hours} hours, #{secs / 60} minutes and #{secs % 60} seconds"
end
end
end
class TcpTimeout < DnsTimeout
def initialize(seconds)
@output = "infinite"
super
end
end
class UdpTimeout < DnsTimeout
def initialize(seconds)
@output = "not defined"
super
end
end
end
end
end
|