Class Hookout::ReverseHttpConnector
In: lib/hookout/reversehttp_connector.rb
Parent: Object

Methods

new   next_request   start   stop  

Constants

DEFAULT_SERVER = 'http://localhost:8000/reversehttp'
DEFAULT_LABEL = 'ruby'

Attributes

failure_delay  [RW] 
lease_seconds  [RW] 
location_change_callback  [RW] 
report_poll_exceptions  [RW] 

Public Class methods

[Source]

# File lib/hookout/reversehttp_connector.rb, line 11
    def initialize(label, server_address, handler)
      @label = label
      @server_address = server_address
      @handler = handler
    
      @next_req = nil
      @location = nil
      @failure_delay = 2
      @token = '-'
      @lease_seconds = 30
      @report_poll_exceptions = false
      @location_change_callback = nil
      @closed = false
      @requestor = Hookout::Utils.new
    end

Public Instance methods

[Source]

# File lib/hookout/reversehttp_connector.rb, line 47
    def next_request
      until @closed
        declare_mode = (@next_req == nil)
        begin
          response = nil
          if declare_mode
            response = @requestor.post_form @server_address, {:name => @label, :token => @token}
          else
            response = @requestor.get @next_req
          end
          
          return nil if response.nil?
      
          @failure_delay = 2
          if declare_mode
            link_headers = parse_link_headers(response)
            
            @next_req = link_headers['first']
            location_text = link_headers['related']
            if location_text
              @location = location_text
              on_location_changed()
            end
          elsif response['Requesting-Client']
            client_addr = response['Requesting-Client'].split(":")
            this_req = @next_req
            @next_req = parse_link_headers(response)['next']
            return [ReverseHttpRequest.new(this_req, @server_address, response.body), client_addr]
          end
        rescue
          if @report_poll_exceptions
            report_poll_exception
          end
          sleep(@failure_delay)  unless @closed
          
          if @failure_delay < 30
            @failure_delay = @failure_delay * 2
          end
        end
      end
    end

[Source]

# File lib/hookout/reversehttp_connector.rb, line 27
    def start
      until @closed
        begin
          (request, client) = next_request()
          unless request.nil?
            @handler.handle_request(request)
            request.close
          end
        rescue
          puts $!
          puts $@
        end
      end
    end

[Source]

# File lib/hookout/reversehttp_connector.rb, line 42
    def stop
      @closed = true
      @requestor.abort
    end

[Validate]