1
2
3
4
5
6
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
43
44
45
46
47
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
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
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
190
191
192
193
194
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/***************************************************************************
* Copyright (C) 2005 by Joris Guisson *
* joris.guisson@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <math.h>
#include <util/functions.h>
#include <util/log.h>
#include "globals.h"
#include "peerdownloader.h"
#include "peer.h"
#include "piece.h"
#include "packetwriter.h"
namespace bt
{
TimeStampedRequest::TimeStampedRequest()
{
time_stamp = bt::GetCurrentTime();
}
TimeStampedRequest::TimeStampedRequest(const Request & r) : req(r)
{
time_stamp = bt::GetCurrentTime();
}
TimeStampedRequest::TimeStampedRequest(const TimeStampedRequest & t)
: req(t.req),time_stamp(t.time_stamp)
{
}
bool TimeStampedRequest::operator == (const Request & r)
{
return r == req;
}
bool TimeStampedRequest::operator == (const TimeStampedRequest & r)
{
return r.req == req;
}
TimeStampedRequest & TimeStampedRequest::operator = (const Request & r)
{
time_stamp = bt::GetCurrentTime();
req = r;
return *this;
}
TimeStampedRequest & TimeStampedRequest::operator = (const TimeStampedRequest & r)
{
time_stamp = r.time_stamp;
req = r.req;
return *this;
}
PeerDownloader::PeerDownloader(Peer* peer,Uint32 chunk_size) : peer(peer),grabbed(0),chunk_size(chunk_size / MAX_PIECE_LEN)
{
connect(peer,SIGNAL(piece(const Piece& )),this,SLOT(piece(const Piece& )));
connect(peer,SIGNAL(destroyed()),this,SLOT(peerDestroyed()));
nearly_done = false;
max_wait_queue_size = 25;
}
PeerDownloader::~PeerDownloader()
{
}
#if 0
void PeerDownloader::retransmitRequests()
{
for (QValueList<Request>::iterator i = reqs.begin();i != reqs.end();i++)
peer->getPacketWriter().sendRequest(*i);
}
#endif
bool PeerDownloader::canAddRequest() const
{
return wait_queue.count() < max_wait_queue_size;
}
Uint32 PeerDownloader::getNumRequests() const
{
return reqs.count() /*+ wait_queue.count() */;
}
int PeerDownloader::grab()
{
grabbed++;
return grabbed;
}
void PeerDownloader::release()
{
grabbed--;
if (grabbed < 0)
grabbed = 0;
}
void PeerDownloader::download(const Request & req)
{
if (!peer)
return;
wait_queue.append(req);
update();
}
void PeerDownloader::cancel(const Request & req)
{
if (!peer)
return;
if (wait_queue.contains(req))
{
wait_queue.remove(req);
}
else if (reqs.contains(req))
{
reqs.remove(req);
peer->getPacketWriter().sendCancel(req);
}
}
void PeerDownloader::onRejected(const Request & req)
{
if (!peer)
return;
// Out(SYS_CON|LOG_DEBUG) << "Rejected : " << req.getIndex() << " "
// << req.getOffset() << " " << req.getLength() << endl;
if (reqs.contains(req))
{
reqs.remove(req);
rejected(req);
}
}
void PeerDownloader::cancelAll()
{
if (peer)
{
QValueList<TimeStampedRequest>::iterator i = reqs.begin();
while (i != reqs.end())
{
TimeStampedRequest & tr = *i;
peer->getPacketWriter().sendCancel(tr.req);
i++;
}
}
wait_queue.clear();
reqs.clear();
}
void PeerDownloader::piece(const Piece & p)
{
Request r(p);
if (wait_queue.contains(r))
wait_queue.remove(r);
else if (reqs.contains(r))
reqs.remove(r);
downloaded(p);
update();
}
void PeerDownloader::peerDestroyed()
{
peer = 0;
}
bool PeerDownloader::isChoked() const
{
if (peer)
return peer->isChoked();
else
return true;
}
bool PeerDownloader::hasChunk(Uint32 idx) const
{
if (peer)
return peer->getBitSet().get(idx);
else
return false;
}
Uint32 PeerDownloader::getDownloadRate() const
{
if (peer)
return peer->getDownloadRate();
else
return 0;
}
void PeerDownloader::checkTimeouts()
{
TimeStamp now = bt::GetCurrentTime();
// we use a 60 second interval
const Uint32 MAX_INTERVAL = 60 * 1000;
QValueList<TimeStampedRequest>::iterator i = reqs.begin();
while (i != reqs.end())
{
TimeStampedRequest & tr = *i;
if (now - tr.time_stamp > MAX_INTERVAL)
{
// cancel it
TimeStampedRequest r = tr;
peer->getPacketWriter().sendCancel(r.req);
// retransmit it
peer->getPacketWriter().sendRequest(r.req);
r.time_stamp = now;
// reappend it at the end of the list
i = reqs.erase(i);
reqs.append(r);
Out(SYS_CON|LOG_DEBUG) << "Retransmitting " << r.req.getIndex() << ":" << r.req.getOffset() << endl;
}
else
{
// new requests get appended so once we have found one
// which hasn't timed out all the following will also not have timed out
break;
}
}
}
Uint32 PeerDownloader::getMaxChunkDownloads() const
{
// get the download rate in KB/sec
Uint32 rate_kbs = peer->getDownloadRate();
rate_kbs = rate_kbs / 1024;
Uint32 num_extra = rate_kbs / 50;
if (chunk_size >= 16)
{
return 1 + 16 * num_extra / chunk_size;
}
else
{
return 1 + (16 / chunk_size) * num_extra;
}
}
void PeerDownloader::choked()
{
// choke doesn't mean reject when fast extensions are enabled
if (peer->getStats().fast_extensions)
return;
QValueList<TimeStampedRequest>::iterator i = reqs.begin();
while (i != reqs.end())
{
TimeStampedRequest & tr = *i;
rejected(tr.req);
i++;
}
reqs.clear();
QValueList<Request>::iterator j = wait_queue.begin();
while (j != wait_queue.end())
{
Request & req = *j;
rejected(req);
j++;
}
wait_queue.clear();
}
void PeerDownloader::update()
{
// modify the interval if necessary
double pieces_per_sec = (double)peer->getDownloadRate() / MAX_PIECE_LEN;
Uint32 max_reqs = 1 + (Uint32)ceil(10*pieces_per_sec);
while (wait_queue.count() > 0 && reqs.count() < max_reqs)
{
// get a request from the wait queue and send that
Request req = wait_queue.front();
wait_queue.pop_front();
TimeStampedRequest r = TimeStampedRequest(req);
reqs.append(r);
peer->getPacketWriter().sendRequest(req);
}
max_wait_queue_size = 2*max_reqs;
if (max_wait_queue_size < 10)
max_wait_queue_size = 10;
}
}
#include "peerdownloader.moc"
|