From 73984924605d1ac05fb890238177e2c2368b08d7 Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 9 Nov 2009 13:54:31 +0000 Subject: [PATCH] twisting is back git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@513 e16421f0-f15b-0410-abcd-98678b794739 --- SConstruct | 2 +- ext/seq_picker.cpp | 15 +++++++++++---- tests/transfertest.cpp | 3 +++ transfer.cpp | 3 +-- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/SConstruct b/SConstruct index c7d0246..9791add 100644 --- a/SConstruct +++ b/SConstruct @@ -22,7 +22,7 @@ TestDir='tests' target = 'p2tp' source = [ 'bin64.cpp','hashtree.cpp','datagram.cpp','bins.cpp', 'transfer.cpp', 'p2tp.cpp', 'sendrecv.cpp', 'ext/send_control.cpp', - 'compat/hirestimeofday.cpp', 'compat/util.cpp'] + 'compat/hirestimeofday.cpp', 'compat.cpp', 'compat/util.cpp'] env = Environment() if sys.platform == "win32": diff --git a/ext/seq_picker.cpp b/ext/seq_picker.cpp index e4a624b..5b0bc0a 100644 --- a/ext/seq_picker.cpp +++ b/ext/seq_picker.cpp @@ -14,14 +14,18 @@ using namespace p2tp; class SeqPiecePicker : public PiecePicker { bins ack_hint_out_; - FileTransfer* file_; + FileTransfer* transfer_; uint64_t twist_; public: SeqPiecePicker (FileTransfer* file_to_pick_from) : - file_(file_to_pick_from), ack_hint_out_(), twist_(0) { - ack_hint_out_.copy_range(file_->ack_out(),bin64_t::ALL); + transfer_(file_to_pick_from), ack_hint_out_(), twist_() { + ack_hint_out_.copy_range(file().ack_out(),bin64_t::ALL); + } + + HashTree& file() { + return transfer_->file(); } virtual void Randomize (uint64_t twist) { @@ -30,6 +34,9 @@ public: virtual bin64_t Pick (bins& offer, uint8_t layer) { //dprintf("twist is %lli\n",twist_); + if (!file().size()) + return bin64_t(0,0); // a hack to get peak hashes; FIXME + twist_ &= (file().peak(0)) & ((1<<16)-1); if (twist_) { offer.twist(twist_); ack_hint_out_.twist(twist_); @@ -62,7 +69,7 @@ public: } virtual void Expired (bin64_t b) { - ack_hint_out_.copy_range(file_->ack_out(),b); + ack_hint_out_.copy_range(file().ack_out(),b); } }; diff --git a/tests/transfertest.cpp b/tests/transfertest.cpp index 1c74cc4..89ce3b4 100644 --- a/tests/transfertest.cpp +++ b/tests/transfertest.cpp @@ -47,6 +47,7 @@ TEST(TransferTest,TransferFile) { EXPECT_EQ(5,seed->size_kilo()); EXPECT_EQ(4100,seed->complete()); EXPECT_EQ(4100,seed->seq_complete()); + EXPECT_EQ(bin64_t(2,0),seed->peak(0)); // retrieve it unlink("copy"); @@ -60,6 +61,7 @@ TEST(TransferTest,TransferFile) { ASSERT_EQ(5<<10,leech->size()); ASSERT_EQ(5,leech->size_kilo()); ASSERT_EQ(0,leech->complete()); + EXPECT_EQ(bin64_t(2,0),leech->peak(0)); // transfer data and hashes // ABCD E000 // AB CD E0 0 @@ -74,6 +76,7 @@ TEST(TransferTest,TransferFile) { leech = & leech_transfer->file(); leech_transfer->picker().Randomize(0); EXPECT_EQ(2,leech->complete_kilo()); + EXPECT_EQ(bin64_t(2,0),leech->peak(0)); } bin64_t next = leech_transfer->picker().Pick(seed->ack_out(),0); ASSERT_NE(bin64_t::NONE,next); diff --git a/transfer.cpp b/transfer.cpp index c26cef6..909b4e6 100644 --- a/transfer.cpp +++ b/transfer.cpp @@ -35,8 +35,7 @@ FileTransfer::FileTransfer (const char* filename, const Sha1Hash& _root_hash) : files.resize(fd()+1); files[fd()] = this; picker_ = new SeqPiecePicker(this); - if (file_.size()) - picker_->Randomize(rand()&31&(file_.peak(0).width()-1)); + picker_->Randomize(rand()); } -- 2.20.1