Sha256: b8f7862358855721a9cdc7186970a666f3f2c12c4c5a8e375540d9e1a56240c3

Contents?: true

Size: 1.56 KB

Versions: 79

Compression:

Stored size: 1.56 KB

Contents

require 'timeout'
require 'cucumber/wire_support/wire_protocol'

module Cucumber
  module WireSupport
    class Connection
      class ConnectionError < StandardError; end
        
      include WireProtocol
      
      def initialize(config)
        @config = config
      end
      
      def call_remote(request_handler, message, params)
        packet = WirePacket.new(message, params)

        begin
          send_data_to_socket(packet.to_json)
          response = fetch_data_from_socket(@config.timeout(message))
          response.handle_with(request_handler)
        rescue Timeout::Error => e
          backtrace = e.backtrace ; backtrace.shift # because Timeout puts some wierd stuff in there
          raise Timeout::Error, "Timed out calling wire server with message '#{message}'", backtrace
        end
      end
      
      def exception(params)
        WireException.new(params, @config.host, @config.port)
      end

      private
      
      def send_data_to_socket(data)
        Timeout.timeout(@config.timeout) { socket.puts(data) }
      end

      def fetch_data_from_socket(timeout)
        raw_response = 
          if timeout == :never
            socket.gets
          else
            Timeout.timeout(timeout) { socket.gets }
          end
        WirePacket.parse(raw_response)
      end
      
      def socket
        @socket ||= TCPSocket.new(@config.host, @config.port)
      rescue Errno::ECONNREFUSED => exception
        raise(ConnectionError, "Unable to contact the wire server at #{@config.host}:#{@config.port}. Is it up?")
      end
    end
  end
end

Version data entries

79 entries across 77 versions & 12 rubygems

Version Path
candlepin-api-0.4.0 bundle/ruby/1.9.1/gems/cucumber-1.2.1/lib/cucumber/wire_support/connection.rb
candlepin-api-0.4.0 bundle/ruby/1.8/gems/cucumber-1.2.1/lib/cucumber/wire_support/connection.rb
candlepin-api-0.4.0 bundle/ruby/gems/cucumber-1.2.1/lib/cucumber/wire_support/connection.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/cucumber-1.2.1/lib/cucumber/wire_support/connection.rb
librarian-puppet-0.9.8 vendor/gems/ruby/1.9.1/gems/cucumber-1.2.1/lib/cucumber/wire_support/connection.rb
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/cucumber-1.2.1/lib/cucumber/wire_support/connection.rb
casecumber-1.0.2.1 lib/cucumber/wire_support/connection.rb
casecumber-1.2.1.cb2 lib/cucumber/wire_support/connection.rb
librarian-puppet-0.9.3 vendor/gems/ruby/1.8/gems/cucumber-1.2.1/lib/cucumber/wire_support/connection.rb
cucumber-1.2.1 lib/cucumber/wire_support/connection.rb
resque-pool-0.3.0 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/wire_support/connection.rb
ftl-0.2.0 vendor/bundle/gems/cucumber-1.2.0/lib/cucumber/wire_support/connection.rb
cucumber-1.2.0 lib/cucumber/wire_support/connection.rb
frameworks-capybara-0.2.0.rc6 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/wire_support/connection.rb
frameworks-capybara-0.2.0.rc5 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/wire_support/connection.rb
frameworks-capybara-0.2.0.rc4 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/wire_support/connection.rb
frameworks-capybara-0.2.0.rc3 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/wire_support/connection.rb
frameworks-capybara-0.2.0.rc2 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/wire_support/connection.rb
resque-pool-0.3.0.beta.2 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/wire_support/connection.rb
radiant-1.0.0 ruby-debug/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/wire_support/connection.rb