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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* 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
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "DefaultBtMessageDispatcher.h"
#include <algorithm>
#include "prefs.h"
#include "BtAbortOutstandingRequestEvent.h"
#include "BtCancelSendingPieceEvent.h"
#include "BtChokingEvent.h"
#include "BtMessageFactory.h"
#include "message.h"
#include "DownloadContext.h"
#include "PeerStorage.h"
#include "PieceStorage.h"
#include "BtMessage.h"
#include "Peer.h"
#include "Piece.h"
#include "LogFactory.h"
#include "Logger.h"
#include "a2functional.h"
#include "a2algo.h"
#include "RequestGroupMan.h"
#include "RequestGroup.h"
#include "util.h"
namespace aria2 {
DefaultBtMessageDispatcher::DefaultBtMessageDispatcher():
cuid(0),
requestTimeout_(0),
logger_(LogFactory::getInstance()) {}
DefaultBtMessageDispatcher::~DefaultBtMessageDispatcher()
{
if(logger_->debug()) {
logger_->debug("DefaultBtMessageDispatcher::deleted");
}
}
void DefaultBtMessageDispatcher::addMessageToQueue
(const BtMessageHandle& btMessage)
{
btMessage->onQueued();
messageQueue_.push_back(btMessage);
}
void DefaultBtMessageDispatcher::addMessageToQueue
(const std::vector<SharedHandle<BtMessage> >& btMessages)
{
for(std::vector<SharedHandle<BtMessage> >::const_iterator itr =
btMessages.begin(), eoi = btMessages.end(); itr != eoi; ++itr) {
addMessageToQueue(*itr);
}
}
void DefaultBtMessageDispatcher::sendMessages() {
std::vector<SharedHandle<BtMessage> > tempQueue;
while(!messageQueue_.empty()) {
BtMessageHandle msg = messageQueue_.front();
messageQueue_.pop_front();
if(msg->isUploading() && !msg->isSendingInProgress()) {
if(requestGroupMan_->doesOverallUploadSpeedExceed() ||
downloadContext_->getOwnerRequestGroup()->doesUploadSpeedExceed()) {
tempQueue.push_back(msg);
continue;
}
}
msg->send();
if(msg->isUploading()) {
peerStorage_->updateTransferStatFor(peer_);
}
if(msg->isSendingInProgress()) {
messageQueue_.push_front(msg);
break;
}
}
if(!tempQueue.empty()) {
// Insert pending message to the front, so that message is likely sent in
// the same order as it is queued.
if(!messageQueue_.empty() && messageQueue_.front()->isSendingInProgress()) {
messageQueue_.insert(messageQueue_.begin()+1,
tempQueue.begin(), tempQueue.end());
} else {
messageQueue_.insert(messageQueue_.begin(),
tempQueue.begin(), tempQueue.end());
}
}
}
// Cancel sending piece message to peer.
void DefaultBtMessageDispatcher::doCancelSendingPieceAction
(size_t index, uint32_t begin, size_t length)
{
BtCancelSendingPieceEvent event(index, begin, length);
std::vector<SharedHandle<BtMessage> > tempQueue
(messageQueue_.begin(), messageQueue_.end());
forEachMemFunSH(tempQueue.begin(), tempQueue.end(),
&BtMessage::onCancelSendingPieceEvent, event);
}
// Cancel sending piece message to peer.
// TODO Is this method really necessary?
void DefaultBtMessageDispatcher::doCancelSendingPieceAction
(const SharedHandle<Piece>& piece)
{
}
class AbortOutstandingRequest {
private:
SharedHandle<Piece> piece_;
cuid_t cuid_;
Logger* logger_;
public:
AbortOutstandingRequest(const SharedHandle<Piece>& piece, cuid_t cuid):
piece_(piece),
cuid_(cuid),
logger_(LogFactory::getInstance()) {}
void operator()(const RequestSlot& slot) const
{
if(logger_->debug()) {
logger_->debug(MSG_DELETING_REQUEST_SLOT,
util::itos(cuid_).c_str(),
slot.getIndex(),
slot.getBlockIndex());
logger_->debug("index=%d, begin=%d", slot.getIndex(), slot.getBegin());
}
piece_->cancelBlock(slot.getBlockIndex());
}
};
// localhost cancels outstanding download requests to the peer.
void DefaultBtMessageDispatcher::doAbortOutstandingRequestAction
(const SharedHandle<Piece>& piece) {
RequestSlot rs(piece->getIndex(), 0, 0, 0);
std::deque<RequestSlot>::iterator first =
std::lower_bound(requestSlots_.begin(), requestSlots_.end(), rs);
rs.setIndex(piece->getIndex()+1);
std::deque<RequestSlot>::iterator last =
std::lower_bound(requestSlots_.begin(), requestSlots_.end(), rs);
std::for_each(first, last, AbortOutstandingRequest(piece, cuid));
requestSlots_.erase(first, last);
BtAbortOutstandingRequestEvent event(piece);
std::vector<SharedHandle<BtMessage> > tempQueue
(messageQueue_.begin(), messageQueue_.end());
forEachMemFunSH(tempQueue.begin(), tempQueue.end(),
&BtMessage::onAbortOutstandingRequestEvent, event);
}
class ProcessChokedRequestSlot {
private:
cuid_t cuid_;
SharedHandle<Peer> peer_;
SharedHandle<PieceStorage> pieceStorage_;
Logger* logger_;
public:
ProcessChokedRequestSlot(cuid_t cuid,
const SharedHandle<Peer>& peer,
const SharedHandle<PieceStorage>& pieceStorage):
cuid_(cuid),
peer_(peer),
pieceStorage_(pieceStorage),
logger_(LogFactory::getInstance()) {}
void operator()(const RequestSlot& slot) const
{
if(!peer_->isInPeerAllowedIndexSet(slot.getIndex())) {
if(logger_->debug()) {
logger_->debug(MSG_DELETING_REQUEST_SLOT_CHOKED,
util::itos(cuid_).c_str(),
slot.getIndex(),
slot.getBlockIndex());
logger_->debug("index=%d, begin=%d", slot.getIndex(), slot.getBegin());
}
SharedHandle<Piece> piece = pieceStorage_->getPiece(slot.getIndex());
piece->cancelBlock(slot.getBlockIndex());
}
}
};
class FindChokedRequestSlot {
private:
SharedHandle<Peer> peer_;
public:
FindChokedRequestSlot(const SharedHandle<Peer>& peer):
peer_(peer) {}
bool operator()(const RequestSlot& slot) const
{
return !peer_->isInPeerAllowedIndexSet(slot.getIndex());
}
};
// localhost received choke message from the peer.
void DefaultBtMessageDispatcher::doChokedAction()
{
std::for_each(requestSlots_.begin(), requestSlots_.end(),
ProcessChokedRequestSlot(cuid, peer_, pieceStorage_));
requestSlots_.erase(std::remove_if(requestSlots_.begin(), requestSlots_.end(),
FindChokedRequestSlot(peer_)),
requestSlots_.end());
}
// localhost dispatched choke message to the peer.
void DefaultBtMessageDispatcher::doChokingAction()
{
BtChokingEvent event;
std::vector<SharedHandle<BtMessage> > tempQueue
(messageQueue_.begin(), messageQueue_.end());
forEachMemFunSH(tempQueue.begin(), tempQueue.end(),
&BtMessage::onChokingEvent, event);
}
class ProcessStaleRequestSlot {
private:
cuid_t cuid_;
SharedHandle<Peer> peer_;
SharedHandle<PieceStorage> pieceStorage_;
BtMessageDispatcher* messageDispatcher_;
WeakHandle<BtMessageFactory> messageFactory_;
time_t requestTimeout_;
Logger* logger_;
public:
ProcessStaleRequestSlot(cuid_t cuid, const SharedHandle<Peer>& peer,
const SharedHandle<PieceStorage>& pieceStorage,
BtMessageDispatcher* dispatcher,
const WeakHandle<BtMessageFactory>& factory,
time_t requestTimeout):
cuid_(cuid),
peer_(peer),
pieceStorage_(pieceStorage),
messageDispatcher_(dispatcher),
messageFactory_(factory),
requestTimeout_(requestTimeout),
logger_(LogFactory::getInstance()) {}
void operator()(const RequestSlot& slot)
{
if(slot.isTimeout(requestTimeout_)) {
if(logger_->debug()) {
logger_->debug(MSG_DELETING_REQUEST_SLOT_TIMEOUT,
util::itos(cuid_).c_str(),
slot.getBlockIndex());
logger_->debug("index=%d, begin=%d", slot.getIndex(), slot.getBegin());
}
slot.getPiece()->cancelBlock(slot.getBlockIndex());
peer_->snubbing(true);
} else if(slot.getPiece()->hasBlock(slot.getBlockIndex())) {
if(logger_->debug()) {
logger_->debug(MSG_DELETING_REQUEST_SLOT_ACQUIRED,
util::itos(cuid_).c_str(),
slot.getBlockIndex());
logger_->debug("index=%d, begin=%d", slot.getIndex(), slot.getBegin());
}
messageDispatcher_->addMessageToQueue
(messageFactory_->createCancelMessage(slot.getIndex(),
slot.getBegin(),
slot.getLength()));
}
}
};
class FindStaleRequestSlot {
private:
SharedHandle<PieceStorage> pieceStorage_;
time_t requestTimeout_;
public:
FindStaleRequestSlot(const SharedHandle<PieceStorage>& pieceStorage,
time_t requestTimeout):
pieceStorage_(pieceStorage),
requestTimeout_(requestTimeout) {}
bool operator()(const RequestSlot& slot)
{
if(slot.isTimeout(requestTimeout_)) {
return true;
} else {
if(slot.getPiece()->hasBlock(slot.getBlockIndex())) {
return true;
} else {
return false;
}
}
}
};
void DefaultBtMessageDispatcher::checkRequestSlotAndDoNecessaryThing()
{
std::for_each(requestSlots_.begin(), requestSlots_.end(),
ProcessStaleRequestSlot(cuid,
peer_,
pieceStorage_,
this,
messageFactory_,
requestTimeout_));
requestSlots_.erase(std::remove_if(requestSlots_.begin(), requestSlots_.end(),
FindStaleRequestSlot(pieceStorage_,
requestTimeout_)),
requestSlots_.end());
}
bool DefaultBtMessageDispatcher::isSendingInProgress()
{
if(messageQueue_.empty()) {
return false;
} else {
return messageQueue_.front()->isSendingInProgress();
}
}
class BlockIndexLess {
public:
bool operator()(const RequestSlot& lhs, const RequestSlot& rhs) const
{
if(lhs.getIndex() == rhs.getIndex()) {
return lhs.getBlockIndex() < rhs.getBlockIndex();
} else {
return lhs.getIndex() < rhs.getIndex();
}
}
};
bool DefaultBtMessageDispatcher::isOutstandingRequest
(size_t index, size_t blockIndex) {
RequestSlot rs(index, 0, 0, blockIndex);
std::deque<RequestSlot>::iterator i =
std::lower_bound(requestSlots_.begin(), requestSlots_.end(),
rs, BlockIndexLess());
return i != requestSlots_.end() &&
(*i).getIndex() == index && (*i).getBlockIndex() == blockIndex;
}
RequestSlot
DefaultBtMessageDispatcher::getOutstandingRequest
(size_t index, uint32_t begin, size_t length)
{
RequestSlot ret;
RequestSlot rs(index, begin, length, 0);
std::deque<RequestSlot>::iterator i =
std::lower_bound(requestSlots_.begin(), requestSlots_.end(), rs);
if(i != requestSlots_.end() && (*i) == rs) {
ret = *i;
} else {
ret = RequestSlot::nullSlot;
}
return ret;
}
void DefaultBtMessageDispatcher::removeOutstandingRequest
(const RequestSlot& slot)
{
std::deque<RequestSlot>::iterator i =
std::lower_bound(requestSlots_.begin(), requestSlots_.end(), slot);
if(i != requestSlots_.end() && (*i) == slot) {
AbortOutstandingRequest(slot.getPiece(), cuid)(*i);
requestSlots_.erase(i);
}
}
void DefaultBtMessageDispatcher::addOutstandingRequest
(const RequestSlot& slot)
{
std::deque<RequestSlot>::iterator i =
std::lower_bound(requestSlots_.begin(), requestSlots_.end(), slot);
if(i == requestSlots_.end() || (*i) != slot) {
requestSlots_.insert(i, slot);
}
}
size_t DefaultBtMessageDispatcher::countOutstandingUpload()
{
return std::count_if(messageQueue_.begin(), messageQueue_.end(),
mem_fun_sh(&BtMessage::isUploading));
}
void DefaultBtMessageDispatcher::setPeer(const SharedHandle<Peer>& peer)
{
peer_ = peer;
}
void DefaultBtMessageDispatcher::setDownloadContext
(const SharedHandle<DownloadContext>& downloadContext)
{
downloadContext_ = downloadContext;
}
void DefaultBtMessageDispatcher::setPieceStorage
(const SharedHandle<PieceStorage>& pieceStorage)
{
pieceStorage_ = pieceStorage;
}
void DefaultBtMessageDispatcher::setPeerStorage
(const SharedHandle<PeerStorage>& peerStorage)
{
peerStorage_ = peerStorage;
}
void DefaultBtMessageDispatcher::setBtMessageFactory
(const WeakHandle<BtMessageFactory>& factory)
{
messageFactory_ = factory;
}
void DefaultBtMessageDispatcher::setRequestGroupMan
(const WeakHandle<RequestGroupMan>& rgman)
{
requestGroupMan_ = rgman;
}
} // namespace aria2
|