2008-11-15 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Given custom random number generator to std::random_shuffle.
	* src/AnnounceList.cc
	* src/BtLeecherStateChoke.cc
	* src/BtSeederStateChoke.cc
	* src/DefaultBtRequestFactory.cc
	* src/MetalinkEntry.cc
	* src/RarestPieceSelector.cc
	* src/SimpleRandomizer.cc
	* src/SimpleRandomizer.h
pull/1/head
Tatsuhiro Tsujikawa 2008-11-16 04:25:24 +00:00
parent a3bfe5c504
commit 9b71a5d0b5
9 changed files with 55 additions and 14 deletions

View File

@ -1,3 +1,15 @@
2008-11-15 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Given custom random number generator to std::random_shuffle.
* src/AnnounceList.cc
* src/BtLeecherStateChoke.cc
* src/BtSeederStateChoke.cc
* src/DefaultBtRequestFactory.cc
* src/MetalinkEntry.cc
* src/RarestPieceSelector.cc
* src/SimpleRandomizer.cc
* src/SimpleRandomizer.h
2008-11-15 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Now the default value of --enable-direct-io is true.

View File

@ -33,10 +33,13 @@
*/
/* copyright --> */
#include "AnnounceList.h"
#include <algorithm>
#include "List.h"
#include "Data.h"
#include "A2STR.h"
#include <algorithm>
#include "SimpleRandomizer.h"
namespace aria2 {
@ -238,7 +241,8 @@ void AnnounceList::shuffle() {
for(AnnounceTiers::iterator itr = tiers.begin();
itr != tiers.end(); itr++) {
std::deque<std::string>& urls = (*itr)->urls;
random_shuffle(urls.begin(), urls.end());
std::random_shuffle(urls.begin(), urls.end(),
*(SimpleRandomizer::getInstance().get()));
}
}

View File

@ -33,11 +33,14 @@
*/
/* copyright --> */
#include "BtLeecherStateChoke.h"
#include <algorithm>
#include "Peer.h"
#include "Logger.h"
#include "LogFactory.h"
#include "a2time.h"
#include <algorithm>
#include "SimpleRandomizer.h"
namespace aria2 {
@ -101,7 +104,8 @@ void BtLeecherStateChoke::plannedOptimisticUnchoke(std::deque<Peer*>& peers)
std::deque<Peer*>::iterator i = std::partition(peers.begin(), peers.end(), PeerFilter(true, true));
if(i != peers.begin()) {
std::random_shuffle(peers.begin(), i);
std::random_shuffle(peers.begin(), i,
*(SimpleRandomizer::getInstance().get()));
(*peers.begin())->optUnchoking(true);
_logger->info("POU: %s", (*peers.begin())->ipaddr.c_str());
}
@ -130,7 +134,8 @@ void BtLeecherStateChoke::regularUnchoke(std::deque<Peer*>& peers)
}
}
if(fastOptUnchoker) {
std::random_shuffle(peerIter, peers.end());
std::random_shuffle(peerIter, peers.end(),
*(SimpleRandomizer::getInstance().get()));
for(std::deque<Peer*>::iterator i = peerIter; i != peers.end(); ++i) {
if((*i)->peerInterested()) {
(*i)->optUnchoking(true);

View File

@ -47,6 +47,7 @@
#include "Logger.h"
#include "LogFactory.h"
#include "a2time.h"
#include "SimpleRandomizer.h"
namespace aria2 {
@ -113,7 +114,9 @@ void BtSeederStateChoke::unchoke(std::deque<Peer*>& peers)
(*r)->calculateUploadSpeed(now));
}
if(_round == 2 && r != peers.end()) {
std::random_shuffle(r, peers.end());
std::random_shuffle(r, peers.end(),
*(SimpleRandomizer::getInstance().get()));
// TODO Is r invalidated here?
(*r)->optUnchoking(true);
_logger->info("POU: %s", (*r)->ipaddr.c_str());
}

View File

@ -46,6 +46,7 @@
#include "BtMessageFactory.h"
#include "BtMessage.h"
#include "a2functional.h"
#include "SimpleRandomizer.h"
namespace aria2 {
@ -171,7 +172,8 @@ void DefaultBtRequestFactory::createRequestMessagesOnEndGame
PieceHandle& piece = *itr;
std::deque<size_t> missingBlockIndexes;
piece->getAllMissingBlockIndexes(missingBlockIndexes);
std::random_shuffle(missingBlockIndexes.begin(), missingBlockIndexes.end());
std::random_shuffle(missingBlockIndexes.begin(), missingBlockIndexes.end(),
*(SimpleRandomizer::getInstance().get()));
for(std::deque<size_t>::const_iterator bitr = missingBlockIndexes.begin();
bitr != missingBlockIndexes.end() && requests.size() < max; bitr++) {
size_t blockIndex = *bitr;

View File

@ -33,6 +33,9 @@
*/
/* copyright --> */
#include "MetalinkEntry.h"
#include <algorithm>
#include "MetalinkResource.h"
#include "FileEntry.h"
#include "Util.h"
@ -42,7 +45,7 @@
# include "ChunkChecksum.h"
#endif // ENABLE_MESSAGE_DIGEST
#include "Signature.h"
#include <algorithm>
#include "SimpleRandomizer.h"
namespace aria2 {
@ -138,7 +141,8 @@ public:
};
void MetalinkEntry::reorderResourcesByPreference() {
std::random_shuffle(resources.begin(), resources.end());
std::random_shuffle(resources.begin(), resources.end(),
*(SimpleRandomizer::getInstance().get()));
std::sort(resources.begin(), resources.end(), PrefOrder());
}

View File

@ -33,8 +33,11 @@
*/
/* copyright --> */
#include "RarestPieceSelector.h"
#include <algorithm>
#include "SimpleRandomizer.h"
namespace aria2 {
PieceStat::PieceStat(size_t index):_order(0), _index(index), _count(0) {}
@ -101,7 +104,8 @@ RarestPieceSelector::RarestPieceSelector(size_t pieceNum, bool randomShuffle):
_sortedPieceStats = _pieceStats;
// we need some randomness in ordering.
if(randomShuffle) {
std::random_shuffle(_sortedPieceStats.begin(), _sortedPieceStats.end());
std::random_shuffle(_sortedPieceStats.begin(), _sortedPieceStats.end(),
*(SimpleRandomizer::getInstance().get()));
}
{
size_t order = 0;

View File

@ -40,9 +40,9 @@
namespace aria2 {
SharedHandle<Randomizer> SimpleRandomizer::_randomizer;
SharedHandle<SimpleRandomizer> SimpleRandomizer::_randomizer;
SharedHandle<Randomizer> SimpleRandomizer::getInstance()
SharedHandle<SimpleRandomizer> SimpleRandomizer::getInstance()
{
if(_randomizer.isNull()) {
_randomizer.reset(new SimpleRandomizer());
@ -74,4 +74,9 @@ long int SimpleRandomizer::getRandomNumber(long int to)
return(int32_t)(((double)to)*getRandomNumber()/(getMaxRandomNumber()+1.0));
}
long int SimpleRandomizer::operator()(long int to)
{
return getRandomNumber(to);
}
} // namespace aria2

View File

@ -41,12 +41,12 @@ namespace aria2 {
class SimpleRandomizer : public Randomizer {
private:
static SharedHandle<Randomizer> _randomizer;
static SharedHandle<SimpleRandomizer> _randomizer;
SimpleRandomizer();
public:
static SharedHandle<Randomizer> getInstance();
static SharedHandle<SimpleRandomizer> getInstance();
static void init();
@ -60,6 +60,8 @@ public:
* Returns random number in [0, to).
*/
virtual long int getRandomNumber(long int to);
long int operator()(long int to);
};
} // namespace aria2