Class: PDTP::Client::Transfer::Connector
- 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
-
#initialize(connection, message, file_service) ⇒ Connector
constructor
A new instance of Connector.
-
#run ⇒ Object
Perform the transfer.
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, , file_service) @connection = connection @file_service = file_service @peer = ["host"] @port = ["port"] @method = ["method"] @url = ["url"] @byte_range = ["range"] @peer_id = ["peer_id"] end |
Instance Method Details
#run ⇒ Object
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) = { :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 [:content] = info.read(@byte_range) else raise HTTPException.new(405, "Invalid method: #{@method}") end http = HttpClient.request 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 hash end end |