Class: PDTP::Client::Transfer::Base

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

Overview

The base information and methods needed by client transfers

Direct Known Subclasses

Connector, Listener

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#byte_rangeObject (readonly)

Returns the value of attribute byte_range.



44
45
46
# File 'lib/pdtp/client/transfer.rb', line 44

def byte_range
  @byte_range
end

#connectionObject (readonly)

Returns the value of attribute connection.



45
46
47
# File 'lib/pdtp/client/transfer.rb', line 45

def connection
  @connection
end

#file_serviceObject (readonly)

Returns the value of attribute file_service.



45
46
47
# File 'lib/pdtp/client/transfer.rb', line 45

def file_service
  @file_service
end

#hashObject (readonly)

Returns the value of attribute hash.



45
46
47
# File 'lib/pdtp/client/transfer.rb', line 45

def hash
  @hash
end

#methodObject (readonly)

Returns the value of attribute method.



45
46
47
# File 'lib/pdtp/client/transfer.rb', line 45

def method
  @method
end

#peerObject (readonly)

Returns the value of attribute peer.



44
45
46
# File 'lib/pdtp/client/transfer.rb', line 44

def peer
  @peer
end

#peer_idObject (readonly)

Returns the value of attribute peer_id.



44
45
46
# File 'lib/pdtp/client/transfer.rb', line 44

def peer_id
  @peer_id
end

#urlObject (readonly)

Returns the value of attribute url.



44
45
46
# File 'lib/pdtp/client/transfer.rb', line 44

def url
  @url
end

Instance Method Details

#matches_message?(message) ⇒ Boolean

Returns true if a server message matches this transfer

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/pdtp/client/transfer.rb', line 48

def matches_message?(message)
  @peer       == message["peer"] and
  @url        == message["url"] and
  @byte_range == message["range"] and
  @peer_id    == message["peer_id"]
end

#parse_http_range(string) ⇒ Object

Takes an HTTP range and returns a ruby Range object

Raises:

  • (RuntimeError)


56
57
58
59
# File 'lib/pdtp/client/transfer.rb', line 56

def parse_http_range(string)
  raise RuntimeError, "Can't parse range string: #{string}" unless string =~ /bytes=([0-9]+)-([0-9]+)/
  (($1).to_i)..(($2).to_i)
end

#send_ask_verify_messageObject



73
74
75
76
77
78
79
80
# File 'lib/pdtp/client/transfer.rb', line 73

def send_ask_verify_message
  @connection.send_message(:ask_verify,
    :url => @url,
    :peer => @peer,
    :range => @byte_range,
    :peer_id => @peer_id
  )
end

#send_completed_message(hash) ⇒ Object

Notify the server of transfer completion. Hash field is used to denote success or failure



63
64
65
66
67
68
69
70
71
# File 'lib/pdtp/client/transfer.rb', line 63

def send_completed_message(hash)
  @connection.send_message(:completed,
    :url => @url,
    :peer => @peer,
    :range => @byte_range,
    :peer_id => @peer_id,
    :hash => hash
  )
end