Module: Fargo::Protocol::PeerDownload

Included in:
Peer
Defined in:
lib/fargo/protocol/peer_download.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#downloadObject

Returns the value of attribute download.



5
6
7
# File 'lib/fargo/protocol/peer_download.rb', line 5

def download
  @download
end

Instance Method Details

#begin_download!Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/fargo/protocol/peer_download.rb', line 90

def begin_download!
  FileUtils.mkdir_p File.dirname(download_path), :mode => 0755
  @file = File.open download_path, 'wb'

  @file.seek @download.offset
  @handshake_step = 5
  @last_published = 0

  if @download.file_list?
    if @client_extensions.include? 'XmlBZList'
      @download.file = 'files.xml.bz2'
    elsif @client_extensions.include? 'BZList'
      @download.file = 'MyList.bz2'
    else
      @download.file = 'MyList.DcLst' # TODO: support this?
    end
  end

  if @client_extensions.include? 'ADCGet'
    download_query = @download.file
    if @download.tth && @client_extensions.include?('TTHF')
      download_query = 'TTH/' + @download.tth
    end

    zlig = @client_extensions.include?('ZLIG') ? ' ZL1' : ''

    send_message 'ADCGET', "file #{download_query} #{@download.offset} #{@download.size}#{zlig}"

  # See http://www.teamfair.info/wiki/index.php?title=XmlBZList for
  # what the $Supports extensions mean for the U?GetZ?Block commands
  elsif @client_extensions.include? 'GetZBlock'
    @getblock_sent = true
    @zlib          = true
    send_message 'UGetZBlock',
      "#{@download.offset} #{@download.size} #{@download.file}"
  elsif @client_extensions.include? 'XmlBZList'
    @getblock_sent = true
    @zlib          = false
    send_message 'UGetBlock',
      "#{@download.offset} #{@download.size} #{@download.file}"

  else
    @get_sent = true
    send_message 'Get', "#{@download.file}$#{@download.offset + 1}"
  end

  Fargo.logger.debug "#{self}: Beginning download of #{@download}"
end

#parse_data?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/fargo/protocol/peer_download.rb', line 44

def parse_data?
  @handshake_step != 6
end

#receive_data_chunk(data) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fargo/protocol/peer_download.rb', line 7

def receive_data_chunk data
  # only download if we're at the correct handshake step
  return super if @handshake_step != 6 || @download.nil?

  if @zlib
    @inflator = Zlib::Inflate.new if @inflator.nil?
    data      = @inflator.inflate data
  end

  @file << data
  @recvd += data.length

  if @recvd > @length
    error "#{self} #{@recvd} > #{@length}!!!"
    download_finished!
  else
    percent = @recvd.to_f / @length
    @download.percent = percent

    if percent - @last_published > 0.05
      @file.flush
      @client.channel << [:download_progress, {:percent => percent,
                                  :file           => download_path,
                                  :nick           => @other_nick,
                                  :download       => @download.to_h,
                                  :size           => @recvd,
                                  :compressed     => @zlib}]

      @last_published = percent
    end

    download_finished! if @recvd == @length
  end

  true
end

#receive_message(type, message) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fargo/protocol/peer_download.rb', line 48

def receive_message type, message
  case type
    when :file_length, :adcsnd, :sending
      if @handshake_step == 5
        @recvd          = 0
        @handshake_step = 6

        @zlib   = message[:zlib] unless @getblock_sent
        @length = message[:size]

        send_message 'Send' if @get_sent

        if @zlib
          Fargo.logger.debug(
            "Enabling zlib compression on: #{@download.file}")
        end

        @client.channel << [:download_started, {:file => download_path,
                                   :download  => @download.to_h,
                                   :length    => @length,
                                   :nick      => @other_nick}]
      else
        error "Premature disconnect when #{message[:type]} received"
      end

    when :noslots
      if @download
        Fargo.logger.debug "#{self}: No Slots for #{@download}"

        download_failed! 'No Slots'
      end

    when :error
      Fargo.logger.warn @last_error = "#{self}: Error! #{message[:message]}"
      download_failed! message[:message]

    # This wasn't handled by us, proxy it on up to the client
    else
      super
  end
end

#unbindObject



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fargo/protocol/peer_download.rb', line 139

def unbind
  super

  Fargo.logger.debug "#{self} Disconnected from: #{@other_nick}"

  if @download
    download_failed! @last_error, :recvd => @recvd, :length => @length
  end

  reset_download
end