Sha256: ab52bf826136eba5c7d6ddf416751d909ac5c5c7ebe31aef554a639b9e33d521

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

class TestServer
  attr_accessor :host, :port, :connect_count, :commands

  def initialize(options={})
    default_options = {
      :listen => true,
      :authenticate => true,
      :response => true,
    }
    @options = default_options.merge(options)

    @connect_count = 0
    @connections = []
    @commands = []
    @host = 'localhost'
    listen if @options[:listen]
  end

  def listen
    @port ||= 10001
    @server = TCPServer.new(port)
    Thread.new do
      begin
        # puts "listening"
        loop do
          socket = @server.accept
          Thread.new do
            @connect_count += 1
            @connections << socket
            # puts "connection received"
            loop do
              command = socket.gets.strip
              # puts "got: #{command}"
              commands << command
              if %w[hello authenticate].include?(command.split(' ')[0])
                if @options[:response]
                  if @options[:authenticate]
                    socket.puts "ok"
                  else
                    socket.puts "gtfo"
                  end
                end
              end
            end
          end
        end
      rescue Exception => err
        unless @stopping
          puts "EXCEPTION:", err unless @stopping
          retry
        end
      end
    end
    # puts "server up"
  rescue Exception => err
    # FIXME: doesn't seem to be detecting failures of listen
    puts "failed to get port"
    puts err.message
    @port += 1
    retry
  end

  def host_and_port
    "#{host}:#{port}"
  end

  def stop
    @stopping = true
    disconnect_all
    @server.close if @server
  end

  def disconnect_all
    @connections.each { |c| c.close rescue false }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
instrumental_agent-0.9.1 spec/test_server.rb
instrumental_agent-0.9.0 spec/test_server.rb