Class: PDTP::Client::Transfer::Connector

Inherits:
Base
  • Object
show all
Defined in:
lib/pdtp/client/transfer.rb

Overview

Implements http transfer between two peers from the connector’s (client) perspective

Instance Attribute Summary

Attributes inherited from Base

#byte_range, #connection, #file_service, #hash, #method, #peer, #peer_id, #url

Instance Method Summary collapse

Methods inherited from Base

#matches_message?, #parse_http_range, #send_ask_verify_message, #send_completed_message

Constructor Details

#initialize(connection, message, file_service) ⇒ Connector

Returns a new instance of Connector.



183
184
185
186
187
188
189
190
191
192
# File 'lib/pdtp/client/transfer.rb', line 183

def initialize(connection, message, file_service)
  @connection   = connection
  @file_service = file_service
  @peer         = message["host"]
  @port         = message["port"]
  @method       = message["method"]
  @url          = message["url"]
  @byte_range   = message["range"]
  @peer_id      = message["peer_id"]
end

Instance Method Details

#runObject

Perform the transfer



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/pdtp/client/transfer.rb', line 195

def run
  info = @file_service.get_info(@url)

  #compute the vhost and path
  #FIXME work with ports
  uri = URI.parse(@url)
  
  options = {
    :method => @method,
    :host => @peer,
    :host_header => uri.host,
    :port => @port,
    :request => uri.path,
    :range => @byte_range,
    :agent => "DistribuStream #{PDTP::VERSION}",
    :headers => "X-PDTP-Peer-Id: #{@connection.client.client_id}"
  }
  
  case @method
  when 'get'
  when 'put' then options[:content] = info.read(@byte_range)
  else raise HTTPException.new(405, "Invalid method: #{@method}")
  end
  
  http = HttpClient.request options          
  http.callback do |response|
    hash = nil
    
    if @method == 'get' and response[:status] == 206
      #@@log.debug("Body Downloaded: url=#{@url} range=#{@byte_range} peer=#{@peer}:#{@port}")
      info.write(@byte_range.first,response[:content])
      hash = Digest::SHA256.hexdigest(response[:content]) rescue nil
    elsif @method == 'put' and response[:status] == 200
      #@@log.debug("Body Uploaded: url=#{@url} range=#{@byte_range} peer=#{@peer}:#{@port}")
    else
      raise RuntimeError, "Invalid method/status combination: #{@method} #{response[:status]}"
    end
    
    send_completed_message hash
  end
end