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

View File

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

View File

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

View File

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