Class: Fargo::Connection::Download
Instance Attribute Summary collapse
Attributes inherited from Base
#socket
Attributes included from Publisher
#subscribers
Instance Method Summary
collapse
Methods included from Parser
#parse_command_message, #parse_message
Methods included from Utils
#encode_char, #generate_key, #generate_lock
Methods inherited from Base
#connect, #connected?, #initialize, #listen, #open_socket, #write
Methods included from Publisher
#publish, #subscribe, #subscribed_to?, #unsubscribe
Instance Attribute Details
#download ⇒ Object
Returns the value of attribute download.
15
16
17
|
# File 'lib/fargo/connection/download.rb', line 15
def download
@download
end
|
Instance Method Details
#begin_download! ⇒ Object
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/fargo/connection/download.rb', line 159
def begin_download!
@file = File.new download_path, File::CREAT | File::WRONLY
@file.seek @download.offset
@file.sync = true
@socket.sync = true
@handshake_step = 5
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' end
end
if @client_extensions.include? 'ADCGet'
download_query = @download.file
if @download.tth && @client_extensions.include?('TTHF')
download_query = @download.tth.gsub ':', '/'
end
zlig = ''
if @client_extensions.include? 'ZLIG'
zlig = 'ZL1'
Fargo.logger.debug "Enabling zlib compression on: #{@download.file}"
end
write "$ADCGET file #{download_query} #{@download.offset} #{@download.size} #{zlig}"
else
write "$Get #{@download.file}$#{@download.offset + 1}"
end
@exit_time = 20
@exit_thread = Thread.start {
while @exit_time > 0
sleep 1
@exit_time -= 1
Fargo.logger.debug "#{self} time out in #{@exit_time} seconds"
end
download_failed! 'Download timeout!'
}
Fargo.logger.debug "#{self}: Beginning download of #{@download}"
end
|
#disconnect ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/fargo/connection/download.rb', line 240
def disconnect
Fargo.logger.debug "#{self} Disconnecting from: #{@other_nick}"
super
if @download
download_failed! @last_error, :recvd => @recvd, :length => @length
end
reset_download
end
|
#download_failed!(msg, opts = {}) ⇒ Object
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/fargo/connection/download.rb', line 210
def download_failed! msg, opts = {}
Fargo.logger.debug "#{self}: #{msg} #{@download}"
path, download = download_path, @download
reset_download
publish :download_failed, opts.merge(:nick => @other_nick,
:download => download,
:file => path,
:last_error => msg)
@exit_thread = nil
end
|
#download_finished! ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/fargo/connection/download.rb', line 227
def download_finished!
Fargo.logger.debug "#{self}: Finished download of #{@download}"
path, download = download_path, @download
reset_download
publish :download_finished, :file => path, :download => download,
:nick => @other_nick
end
|
#pre_listen ⇒ Object
17
18
19
20
21
22
23
24
25
|
# File 'lib/fargo/connection/download.rb', line 17
def pre_listen
Fargo.logger.debug "Initiating connection on: #{config.address}:#{config.port}"
config.quit_on_disconnect = false
@lock, @pk = generate_lock
@handshake_step = 0
@buffer_size = (2 << 12).freeze
end
|
#read_data ⇒ Object
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
59
60
61
62
|
# File 'lib/fargo/connection/download.rb', line 31
def read_data
return super if @handshake_step != 6
@exit_time = 20
data = @socket.readpartial @buffer_size
if @zlib
@zs = Zlib::Inflate.new if @zs.nil?
data = @zs.inflate data
end
@file << data
@recvd += data.length
if @recvd == @length
download_finished!
elsif @recvd > @length
error "#{self} #{@recvd} > #{@length}!!!"
download_finished!
else
publish :download_progress, :percent => @recvd.to_f / @length,
:file => download_path,
:nick => @other_nick,
:download => @download,
:size => @length,
:compressed => @zlib
end
rescue IOError => e
error "#{self}: IOError, disconnecting #{e}"
end
|
#receive(data) ⇒ Object
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
153
154
155
156
157
|
# File 'lib/fargo/connection/download.rb', line 64
def receive data
message = parse_message data
case message[:type]
when :mynick
if @handshake_step == 0
@handshake_step = 1
@other_nick = message[:nick]
@client.connected_with! @other_nick
@client.lock_connection_with! @other_nick, 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 config.first
out = ''
out << '$Supports TTHF ADCGet ZLIG|'
out << "$Direction Download #{@my_num = rand(10000)}|"
out << "$Key #{generate_key @remote_lock}|"
write out
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]
write "$Send" unless @client_extensions.include? 'ADCGet'
publish :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 #{self[:download]}"
download_failed! 'No Slots'
end
when :error
error "#{self}: Error! #{message[:message]}"
else
@client.publish message[:type], message
end
end
|
#send_lock ⇒ Object
27
28
29
|
# File 'lib/fargo/connection/download.rb', line 27
def send_lock
write "$MyNick #{@client.config.nick}|$Lock #{@lock} Pk=#{@pk}"
end
|