Sha256: 11ba9651d5eb83c69024ab5bcc22f7566763c408d0d988765524f2178e4d521d

Contents?: true

Size: 1.29 KB

Versions: 10

Compression:

Stored size: 1.29 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 (not defined?(@signature)) || @signature.nil?
      else
        main_loop
      end
    end

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

    def host
      Billy.config.proxy_host
    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(host, Billy.config.proxy_port, ProxyConnection) do |p|
          p.handler = request_handler
          p.cache = @cache if defined?(@cache)
          p.errback do |msg|
            Billy.log :error, msg
          end
        end

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
puffing-billy-0.12.0 lib/billy/proxy.rb
puffing-billy-0.11.1 lib/billy/proxy.rb
puffing-billy-0.11.0 lib/billy/proxy.rb
puffing-billy-0.10.1 lib/billy/proxy.rb
puffing-billy-0.10.0 lib/billy/proxy.rb
puffing-billy-0.9.2 lib/billy/proxy.rb
puffing-billy-0.9.1 lib/billy/proxy.rb
puffing-billy-0.9.0 lib/billy/proxy.rb
puffing-billy-0.8.0 lib/billy/proxy.rb
puffing-billy-0.7.0 lib/billy/proxy.rb