lib/ztk/tcp_socket_check.rb in ztk-0.2.4 vs lib/ztk/tcp_socket_check.rb in ztk-0.2.5

- old
+ new

@@ -88,48 +88,40 @@ # @option config [Integer, String] :port Port to connect to. # @option config [String] :data Data to send to host to provoke a response. # @option config [Integer] :timeout (5) Set the IO select timeout. # @option config [Integer] :wait (60) Set the amount of time before the wait # method call will timeout. - def initialize(config={}) + def initialize(configuration={}) super({ :timeout => 5, :wait => 60 - }.merge(config)) + }.merge(configuration)) + config.logger.debug { "config(#{config.inspect})" } end # Check to see if socket on the host and port specified is ready. This # method will timeout and return false after the amount of seconds specified # in *config.timeout* has passed if the socket has not become ready. # # @return [Boolean] Returns true or false depending on weither the socket # is ready or not. def ready? - if @config.host.nil? - message = "You must supply a host!" - log(:fatal) { message } - raise TCPSocketCheckError, message - end + config.host.nil? and log_and_raise(TCPSocketCheckError, "You must supply a host!") + config.port.nil? and log_and_raise(TCPSocketCheckError, "You must supply a port!") - if @config.port.nil? - message = "You must supply a port!" - log(:fatal) { message } - raise TCPSocketCheckError, message - end + socket = TCPSocket.new(config.host, config.port) - socket = TCPSocket.new(@config.host, @config.port) - - if @config.data.nil? - log(:debug) { "read(#{@config.host}:#{@config.port})" } - ((IO.select([socket], nil, nil, @config.timeout) && socket.gets) ? true : false) + if config.data.nil? + config.logger.debug { "read(#{config.host}:#{config.port})" } + ((IO.select([socket], nil, nil, config.timeout) && socket.gets) ? true : false) else - log(:debug) { "write(#{@config.host}:#{@config.port}, '#{@config.data}')" } - ((IO.select(nil, [socket], nil, @config.timeout) && socket.write(@config.data)) ? true : false) + config.logger.debug { "write(#{config.host}:#{config.port}, #{config.data.size} bytes)" } + ((IO.select(nil, [socket], nil, config.timeout) && socket.write(config.data)) ? true : false) end rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH => e - log(:debug) { "#{@config.host}:#{@config.port} - #{e.message}" } + config.logger.debug { "#{config.host}:#{config.port} - #{e.message}" } false ensure (socket && socket.close) end @@ -138,19 +130,19 @@ # in *config.wait* has passed if the socket has not become ready. # # @return [Boolean] Returns true or false depending on weither the socket # became ready or not. def wait - log(:debug) { "waiting for socket to become available; timeout after #{@config.wait} seconds" } - Timeout.timeout(@config.wait) do + config.logger.debug { "Waiting for socket to become available; timeout after #{config.wait} seconds." } + Timeout.timeout(config.wait) do until ready? - log(:debug) { "sleeping 1 second" } + config.logger.debug { "Sleeping 1 second." } sleep(1) end end true rescue Timeout::Error => e - log(:warn) { "socket(#{@config.host}:#{@config.port}) timeout!" } + config.logger.warn { "socket(#{config.host}:#{config.port}) timeout!" } false end end