Class: PDTP::Client::HttpHandler

Inherits:
Mongrel::HttpHandler
  • Object
show all
Defined in:
lib/pdtp/client/http_handler.rb

Overview

Mongrel::HttpHandler to handle incoming HTTP chunk transfers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ HttpHandler

Returns a new instance of HttpHandler.



35
36
37
# File 'lib/pdtp/client/http_handler.rb', line 35

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



33
34
35
# File 'lib/pdtp/client/http_handler.rb', line 33

def client
  @client
end

Instance Method Details

#connection_created(connection) ⇒ Object

This method is called after a connection to the server has been successfully established.



41
42
43
# File 'lib/pdtp/client/http_handler.rb', line 41

def connection_created(connection)
  #@@log.debug("[mongrel] Opened connection...");
end

#connection_destroyed(connection) ⇒ Object

This method is called when the server connection is destroyed



46
47
48
# File 'lib/pdtp/client/http_handler.rb', line 46

def connection_destroyed(connection)
  #@@log.debug("[mongrel] Closed connection...")
end

#get_transfer(connection) ⇒ Object

Returns a transfer object if the given connection is a peer associated with that transfer. Otherwise returns nil.



52
53
54
55
# File 'lib/pdtp/client/http_handler.rb', line 52

def get_transfer(connection)
  @client.transfers.each { |t| return t if t.peer == connection }
  nil
end

#process(request, response) ⇒ Object

This method is called when an HTTP request is received.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pdtp/client/http_handler.rb', line 58

def process(request,response)        
  begin
    #@@log.debug "Creating Transfer::Listener"
    transfer = Transfer::Listener.new(
      @client.connection,
      request, 
      response,
      client.file_service
    )

    @client.transfers << transfer
    return unless transfer.handle_header
    transfer.send_completed_message transfer.hash
  rescue Exception => e
    raise e if transfer.nil?
    transfer.write_http_exception(e)
  end
end

#transfer_matches?(transfer, message) ⇒ Boolean

Returns true if the given message refers to the given transfer

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'lib/pdtp/client/http_handler.rb', line 78

def transfer_matches?(transfer, message)
  transfer.peer       == message["peer"] and 
  transfer.url        == message["url"] and
  transfer.byte_range == message["range"] and
  transfer.peer_id    == message["peer_id"]
end