Sha256: c1928e52d62efa9f14d2e9f6cac2042e91090d191db51b80947435728a83b38f

Contents?: true

Size: 1.09 KB

Versions: 19

Compression:

Stored size: 1.09 KB

Contents

module LitmusPaper
  module Dependency
    class TCP
      def initialize(ip, port, options = {})
        @ip, @port = ip, port
        @timeout = options.fetch(:timeout_seconds, 5)
        @input_data = options[:input_data]
        @expected_output = options[:expected_output]
      end

      def available?
        response = true

        Timeout.timeout(@timeout) do
          socket = TCPSocket.new(@ip, @port)
          if @expected_output
            socket.puts(@input_data) if @input_data
            data = socket.gets
            response = data.chomp == @expected_output
            LitmusPaper.logger.info("Response (#{response}) does not match expected output (#{@expected_output})")
          end
          socket.close
        end

        response
      rescue Timeout::Error
        LitmusPaper.logger.info("Timeout connecting #{@ip}:#{@port}")
        false
      rescue => e
        LitmusPaper.logger.info("TCP available check to #{@ip}:#{@port} failed with #{e.message}")
        false
      end

      def to_s
        "Dependency::TCP(tcp://#{@ip}:#{@port})"
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
litmus_paper-1.6.2 lib/litmus_paper/dependency/tcp.rb
litmus_paper-1.6.1 lib/litmus_paper/dependency/tcp.rb
litmus_paper-1.6.0 lib/litmus_paper/dependency/tcp.rb
litmus_paper-1.5.0 lib/litmus_paper/dependency/tcp.rb
litmus_paper-1.4.2 lib/litmus_paper/dependency/tcp.rb
litmus_paper-1.4.1 lib/litmus_paper/dependency/tcp.rb
litmus_paper-1.3.0 lib/litmus_paper/dependency/tcp.rb
litmus_paper-1.2.0 lib/litmus_paper/dependency/tcp.rb
litmus_paper-1.1.1 lib/litmus_paper/dependency/tcp.rb
litmus_paper-1.1.0 lib/litmus_paper/dependency/tcp.rb
litmus_paper-1.0.0 lib/litmus_paper/dependency/tcp.rb
litmus_paper-0.9.9 lib/litmus_paper/dependency/tcp.rb
litmus_paper-0.9.7 lib/litmus_paper/dependency/tcp.rb
litmus_paper-0.9.6 lib/litmus_paper/dependency/tcp.rb
litmus_paper-0.9.5 lib/litmus_paper/dependency/tcp.rb
litmus_paper-0.9.4 lib/litmus_paper/dependency/tcp.rb
litmus_paper-0.9.3 lib/litmus_paper/dependency/tcp.rb
litmus_paper-0.9.2 lib/litmus_paper/dependency/tcp.rb
litmus_paper-0.9.1 lib/litmus_paper/dependency/tcp.rb