twisting is back
authorvictor <victor@e16421f0-f15b-0410-abcd-98678b794739>
Mon, 9 Nov 2009 13:54:31 +0000 (13:54 +0000)
committervictor <victor@e16421f0-f15b-0410-abcd-98678b794739>
Mon, 9 Nov 2009 13:54:31 +0000 (13:54 +0000)
git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@513 e16421f0-f15b-0410-abcd-98678b794739

SConstruct
ext/seq_picker.cpp
tests/transfertest.cpp
transfer.cpp

index c7d0246..9791add 100644 (file)
@@ -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":
index e4a624b..5b0bc0a 100644 (file)
@@ -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);
     }
     
 };
index 1c74cc4..89ce3b4 100644 (file)
@@ -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);
index c26cef6..909b4e6 100644 (file)
@@ -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());
 }