Sha256: 6fa35d57462113f3f8b7cbbaa302e0aacfacb88f298627b26809661fa4bd8eb6

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

require 'cgi'
require 'eventmachine'

module Billy
  class Proxy
    extend Forwardable
    attr_reader :request_handler

    def_delegators :request_handler, :stub, :reset, :reset_cache, :restore_cache, :handle_request

    def initialize
      @request_handler = Billy::RequestHandler.new
      reset
    end

    def start(threaded = true)
      if threaded
        Thread.new { main_loop }
        sleep(0.01) while @signature.nil?
      else
        main_loop
      end
    end

    def url
      "http://#{host}:#{port}"
    end

    def host
      'localhost'
    end

    def port
      Socket.unpack_sockaddr_in(EM.get_sockname(@signature)).first
    end

    def cache
      Billy::Cache.instance
    end

    protected

    def main_loop
      EM.run do
        EM.error_handler do |e|
          Billy.log :error, "#{e.class} (#{e.message}):"
          Billy.log :error, e.backtrace.join("\n")
        end

        @signature = EM.start_server('127.0.0.1', Billy.config.proxy_port, ProxyConnection) do |p|
          p.handler = request_handler
          p.cache = @cache
        end

        Billy.log(:info, "puffing-billy: Proxy listening on #{url}")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
puffing-billy-0.5.1 lib/billy/proxy.rb
puffing-billy-0.5.0 lib/billy/proxy.rb
puffing-billy-0.4.1 lib/billy/proxy.rb