Class: Fargo::Protocol::Download

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
DC, Utils
Defined in:
lib/fargo/protocol/download.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DC

#send_message

Methods included from Fargo::Parser

#parse_command_message, #parse_message

Methods included from Utils

#encode_char, #generate_key, #generate_lock

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/fargo/protocol/download.rb', line 10

def client
  @client
end

#downloadObject

Returns the value of attribute download.



10
11
12
# File 'lib/fargo/protocol/download.rb', line 10

def download
  @download
end

Instance Method Details

#begin_download!Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/fargo/protocol/download.rb', line 154

def begin_download!
  @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 = ''
    if @client_extensions.include? 'ZLIG'
      zlig = 'ZL1'
      Fargo.logger.debug "Enabling zlib compression on: #{@download.file}"
    end

    send_message 'ADCGET', "file #{download_query} #{@download.offset} #{@download.size} #{zlig}"
  else
    send_message 'Get', "#{@download.file}$#{@download.offset + 1}"
  end

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

#download_failed!(msg, opts = {}) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/fargo/protocol/download.rb', line 191

def download_failed! msg, opts = {}
  Fargo.logger.debug "#{self}: #{msg} #{@download}"

  # cache because publishing must be at end of method and we're about to
  # clear these
  path, download = download_path, @download

  reset_download

  @client.channel << [:download_failed, opts.merge(:nick => @other_nick,
                                       :download   => download,
                                       :file       => path,
                                       :last_error => msg)]
end

#download_finished!Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/fargo/protocol/download.rb', line 206

def download_finished!
  Fargo.logger.debug "#{self}: Finished download of #{@download}"

  # cache because publishing must be at end of method and we're about to
  # clear these
  path, download = download_path, @download

  reset_download

  @client.channel << [:download_finished,
      {:file => path, :download => download, :nick => @other_nick}]

  close_connection_after_writing if download.file_list?
end

#post_initObject



12
13
14
15
16
17
18
19
# File 'lib/fargo/protocol/download.rb', line 12

def post_init
  super

  set_comm_inactivity_timeout 20

  @lock, @pk      = generate_lock
  @handshake_step = 0
end

#publish_argsObject



221
222
223
# File 'lib/fargo/protocol/download.rb', line 221

def publish_args
  {:nick => @other_nick}
end

#receive_data(data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fargo/protocol/download.rb', line 27

def receive_data data
  # only download if we're at the correct handshake step
  return super if @handshake_step != 6

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

      @last_published = percent
    end

    download_finished! if @recvd == @length
  end
end

#receive_message(type, message) ⇒ Object



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
89
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/fargo/protocol/download.rb', line 60

def receive_message type, message
  case type
    when :mynick
      if @handshake_step == 0
        @handshake_step  = 1
        @other_nick      = message[:nick]

        client.channel << [:download_opened,
            publish_args.merge(:connection => self)]
        @download = @client.lock_next_download! @other_nick, self

        if @download.try(:file).nil?
          error "Nothing to download from:#{@other_nick}!"
        end
      else
        error 'Premature disconnect when mynick received'
      end

    when :lock
      if @handshake_step == 1
        @remote_lock = message[:lock]
        @handshake_step = 2

        send_lock unless @lock_sent

        send_message 'Supports', 'TTHF ADCGet ZLIG'
        send_message 'Direction', "Download #{@my_num = rand(10000)}"
        send_message 'Key', generate_key(@remote_lock)
      else
        error 'Premature disconnect when lock received'
      end

    when :supports
      if @handshake_step == 2
        @client_extensions = message[:extensions]
        @handshake_step    = 3
      else
        error 'Premature disconnect when supports received'
      end

    when :direction
      if @handshake_step == 3 && message[:direction] == 'upload'
        @client_num     = message[:number]
        @handshake_step = 4
      else
        error 'Premature disconnect when direction received'
      end

    when :key
      if @handshake_step == 4 && generate_key(@lock) == message[:key]

        FileUtils.mkdir_p File.dirname(download_path), :mode => 0755

        begin_download!

      else
        error 'Premature disconnect when key received'
      end

    when :file_length, :adcsnd
      if @handshake_step == 5
        @recvd          = 0
        @handshake_step = 6

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

        send_message 'Send' unless @client_extensions.include? 'ADCGet'

        @client.channel << [:download_started, {:file => download_path,
                                   :download  => @download,
                                   :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

#send_lockObject



21
22
23
24
25
# File 'lib/fargo/protocol/download.rb', line 21

def send_lock
  @lock_sent = true
  send_message 'MyNick', @client.config.nick
  send_message 'Lock', "#{@lock} Pk=#{@pk}"
end

#unbindObject



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/fargo/protocol/download.rb', line 225

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