bittorrent::computeFastSet: Return std::vector

pull/235/merge
Tatsuhiro Tsujikawa 2014-06-04 22:50:39 +09:00
parent d1d5ea5b21
commit 007b890fe4
4 changed files with 20 additions and 18 deletions

View File

@ -229,11 +229,11 @@ void DefaultBtInteractive::addBitfieldMessageToQueue() {
void DefaultBtInteractive::addAllowedFastMessageToQueue() {
if(peer_->isFastExtensionEnabled()) {
std::vector<size_t> fastSet;
bittorrent::computeFastSet(fastSet, peer_->getIPAddress(),
downloadContext_->getNumPieces(),
bittorrent::getInfoHash(downloadContext_),
allowedFastSetSize_);
auto fastSet =
bittorrent::computeFastSet(peer_->getIPAddress(),
downloadContext_->getNumPieces(),
bittorrent::getInfoHash(downloadContext_),
allowedFastSetSize_);
for(std::vector<size_t>::const_iterator itr = fastSet.begin(),
eoi = fastSet.end(); itr != eoi; ++itr) {
dispatcher_->addMessageToQueue

View File

@ -658,14 +658,15 @@ getInfoHashString(DownloadContext* dctx)
return util::toHex(getTorrentAttrs(dctx)->infoHash);
}
void computeFastSet
(std::vector<size_t>& fastSet, const std::string& ipaddr,
std::vector<size_t> computeFastSet
(const std::string& ipaddr,
size_t numPieces, const unsigned char* infoHash, size_t fastSetSize)
{
std::vector<size_t> fastSet;
unsigned char compact[COMPACT_LEN_IPV6];
int compactlen = packcompact(compact, ipaddr, 0);
if(compactlen != COMPACT_LEN_IPV4) {
return;
return fastSet;
}
if(numPieces < fastSetSize) {
fastSetSize = numPieces;
@ -689,7 +690,9 @@ void computeFastSet
memcpy(&ny, x+j, 4);
uint32_t y = ntohl(ny);
size_t index = y%numPieces;
if(std::find(fastSet.begin(), fastSet.end(), index) == fastSet.end()) {
if(std::find(std::begin(fastSet), std::end(fastSet), index) ==
std::end(fastSet)) {
fastSet.push_back(index);
}
}
@ -698,6 +701,8 @@ void computeFastSet
message_digest::digest(temp, sizeof(temp), sha1.get(), x, sizeof(x));
memcpy(x, temp, sizeof(x));
}
return fastSet;
}
std::string generatePeerId(const std::string& peerIdPrefix)

View File

@ -144,9 +144,9 @@ const unsigned char* getStaticPeerId();
// length.
void setStaticPeerId(const std::string& newPeerId);
// Computes fast set index and stores them in fastset.
void computeFastSet
(std::vector<size_t>& fastSet, const std::string& ipaddr,
// Computes fast set index and returns them.
std::vector<size_t> computeFastSet
(const std::string& ipaddr,
size_t numPieces, const unsigned char* infoHash, size_t fastSetSize);
// Make sure that don't recieve return value into std::shared_ptr.

View File

@ -321,23 +321,20 @@ void BittorrentHelperTest::testComputeFastSet()
int fastSetSize = 10;
size_t numPieces = 1000;
{
std::vector<size_t> fastSet;
computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
auto fastSet = computeFastSet(ipaddr, numPieces, infoHash, fastSetSize);
size_t ans[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), std::begin(ans)));
}
ipaddr = "10.0.0.1";
{
std::vector<size_t> fastSet;
computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
auto fastSet = computeFastSet(ipaddr, numPieces, infoHash, fastSetSize);
size_t ans[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), std::begin(ans)));
}
// See when pieces < fastSetSize
numPieces = 9;
{
std::vector<size_t> fastSet;
computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize);
auto fastSet = computeFastSet(ipaddr, numPieces, infoHash, fastSetSize);
size_t ans[] = { 8, 6, 7, 5, 1, 4, 0, 2, 3 };
CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), std::begin(ans)));
}