save ok
authorvictor <victor@e16421f0-f15b-0410-abcd-98678b794739>
Tue, 13 Oct 2009 10:16:19 +0000 (10:16 +0000)
committervictor <victor@e16421f0-f15b-0410-abcd-98678b794739>
Tue, 13 Oct 2009 10:16:19 +0000 (10:16 +0000)
git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@403 e16421f0-f15b-0410-abcd-98678b794739

p2tp.h
tests/binstest2.cpp
tests/transfertest.cpp
transfer.cpp

diff --git a/p2tp.h b/p2tp.h
index 2e3016f..122a469 100644 (file)
--- a/p2tp.h
+++ b/p2tp.h
@@ -108,8 +108,9 @@ namespace p2tp {
     public:
 
                static std::vector<FileTransfer*> files;
-        static const char* HASH_FILE_TEPMLATE;
-        static const char* PEAK_FILE_TEPMLATE;
+        static const char* HASH_FILE_TEMPLATE;
+        static const char* PEAK_FILE_TEMPLATE;
+        static int instance;
                        
                /**     file descriptor. */
                int                             fd;
index 5882eff..9d76467 100755 (executable)
@@ -188,6 +188,28 @@ TEST(BinsTest,Remove) {
     EXPECT_EQ(bins::FILLED,b.get(bin64_t(2,3)));
     EXPECT_EQ(bins::FILLED,b.get(bin64_t(4,1)));
     
+    bins b16, b1024, b8192;
+    b16.set(bin64_t(3,1));
+    b1024.set(bin64_t(3,1));
+    b1024.set(bin64_t(4,2));
+    b1024.set(bin64_t(8,3));
+    b8192.set(bin64_t(8,3));
+    b8192.set(bin64_t(10,7));
+    
+    b1024.remove(b16);
+    b1024.remove(b8192);
+    
+    EXPECT_EQ(bins::EMPTY,b1024.get(bin64_t(3,1)));
+    EXPECT_EQ(bins::EMPTY,b1024.get(bin64_t(5,0)));
+    EXPECT_EQ(bins::EMPTY,b1024.get(bin64_t(9,1)));
+    EXPECT_EQ(bins::EMPTY,b1024.get(bin64_t(12,1)));
+    EXPECT_EQ(bins::FILLED,b1024.get(bin64_t(4,2)));
+    
+    b8192.set(bin64_t(2,3));
+    b16.remove(b8192);
+    EXPECT_EQ(bins::EMPTY,b16.get(bin64_t(2,3)));
+    EXPECT_EQ(bins::FILLED,b16.get(bin64_t(2,2)));
+    
 }
 
 /*TEST(BinsTest,AddSub) {
index 5e60ec6..24683e6 100644 (file)
@@ -46,6 +46,7 @@ TEST(TransferTest,TransferFile) {
     
     // retrieve it
     unlink("copy");
+    FileTransfer::instance = 1;
     FileTransfer* leech = new FileTransfer(seed->root_hash,"copy");
     // transfer peak hashes
     for(int i=0; i<seed->peak_count; i++)
@@ -60,11 +61,14 @@ TEST(TransferTest,TransferFile) {
     leech->OfferHash(bin64_t(1,0), seed->hashes[bin64_t(1,0)]);
     leech->OfferHash(bin64_t(1,1), seed->hashes[bin64_t(1,1)]);
     for (int i=0; i<5; i++) {
-        /*if (leech->seq_complete==3) {
+        if (i==2) {
             delete leech;
-            leech = new FileTransfer(seed,"copy");
-            EXPECT_EQ(3,leech->complete);
-        }*/
+            FileTransfer::instance = 1;
+            leech = new FileTransfer(seed->root_hash,"copy");
+            EXPECT_EQ(2,leech->completek);
+            //leech->OfferHash(bin64_t(1,0), seed->hashes[bin64_t(1,0)]);
+            //leech->OfferHash(bin64_t(1,1), seed->hashes[bin64_t(1,1)]);
+        }
         bin64_t next = leech->picker->Pick(seed->ack_out,0);
         ASSERT_NE(bin64_t::NONE,next);
         uint8_t buf[1024];         //size_t len = seed->storer->ReadData(next,&buf);
@@ -78,8 +82,6 @@ TEST(TransferTest,TransferFile) {
     EXPECT_EQ(4100,leech->complete);
     EXPECT_EQ(4100,leech->seq_complete);
     
-    unlink("copy");
-    
 }
 /*
  FIXME
@@ -89,9 +91,16 @@ TEST(TransferTest,TransferFile) {
 
 int main (int argc, char** argv) {
     
-    unlink("/tmp/.70196e6065a42835b1f08227ac3e2fb419cf78c8.hashes");
-    unlink("/tmp/.70196e6065a42835b1f08227ac3e2fb419cf78c8.peaks");
+    unlink("/tmp/.70196e6065a42835b1f08227ac3e2fb419cf78c8.0.hashes");
+    unlink("/tmp/.70196e6065a42835b1f08227ac3e2fb419cf78c8.0.peaks");
+    unlink("/tmp/.70196e6065a42835b1f08227ac3e2fb419cf78c8.1.hashes");
+    unlink("/tmp/.70196e6065a42835b1f08227ac3e2fb419cf78c8.1.peaks");
+    unlink("/tmp/.70196e6065a42835b1f08227ac3e2fb419cf78c8.2.hashes");
+    unlink("/tmp/.70196e6065a42835b1f08227ac3e2fb419cf78c8.2.peaks");
     
+    unlink(BTF);
+    unlink("copy");
+        
        int f = open(BTF,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
     uint8_t buf[1024];
     memset(buf,'A',1024);
@@ -114,7 +123,5 @@ int main (int argc, char** argv) {
        testing::InitGoogleTest(&argc, argv);
        int ret = RUN_ALL_TESTS();
     
-    unlink(BTF);
-    
     return ret;
 }
index 15b8404..a260842 100644 (file)
@@ -13,8 +13,9 @@
 using namespace p2tp;
 
 std::vector<FileTransfer*> FileTransfer::files(20);
-const char* FileTransfer::HASH_FILE_TEPMLATE = "/tmp/.%s.hashes";
-const char* FileTransfer::PEAK_FILE_TEPMLATE = "/tmp/.%s.peaks";
+const char* FileTransfer::HASH_FILE_TEMPLATE = "/tmp/.%s.%i.hashes";
+const char* FileTransfer::PEAK_FILE_TEMPLATE = "/tmp/.%s.%i.peaks";
+int FileTransfer::instance = 0;
 #define BINHASHSIZE (sizeof(bin64_t)+sizeof(Sha1Hash))
 
 #include "ext/seq_picker.cpp"
@@ -22,7 +23,8 @@ const char* FileTransfer::PEAK_FILE_TEPMLATE = "/tmp/.%s.peaks";
 
 FileTransfer::FileTransfer (const Sha1Hash& _root_hash, const char* filename) :
     root_hash(_root_hash), fd(0), hashfd(0), dry_run(false), 
-    peak_count(0), hashes(NULL), error(NULL)
+    peak_count(0), hashes(NULL), error(NULL), size(0), sizek(0),
+    complete(0), completek(0), seq_complete(0)
 {
        fd = open(filename,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
        if (fd<0)
@@ -36,7 +38,7 @@ FileTransfer::FileTransfer (const Sha1Hash& _root_hash, const char* filename) :
 
 void FileTransfer::LoadPeaks () {
     char file_name[1024];
-    sprintf(file_name,PEAK_FILE_TEPMLATE,root_hash.hex().c_str());
+    sprintf(file_name,PEAK_FILE_TEMPLATE,root_hash.hex().c_str(),instance);
     int peakfd = open(file_name,O_RDONLY);
     if (peakfd<0)
         return;
@@ -73,7 +75,7 @@ void            FileTransfer::RecoverProgress () {
 
 void    FileTransfer::SavePeaks () {
     char file_name[1024];
-    sprintf(file_name,PEAK_FILE_TEPMLATE,root_hash.hex().c_str());
+    sprintf(file_name,PEAK_FILE_TEMPLATE,root_hash.hex().c_str(),instance);
     int peakfd = open(file_name,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
     for(int i=0; i<peak_count; i++) {
         write(peakfd,&(peaks[i]),sizeof(bin64_t));
@@ -95,7 +97,7 @@ void FileTransfer::SetSize (size_t bytes) { // peaks/root must be already set
         if (ftruncate(fd, bytes))
             return; // remain in the 0-state
     // mmap the hash file into memory
-    sprintf(file_name,HASH_FILE_TEPMLATE,root_hash.hex().c_str());
+    sprintf(file_name,HASH_FILE_TEMPLATE,root_hash.hex().c_str(),instance);
        hashfd = open(file_name,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
     size_t expected_size = Sha1Hash::SIZE * sizek * 2;
        struct stat hash_file_st;
@@ -103,7 +105,7 @@ void FileTransfer::SetSize (size_t bytes) { // peaks/root must be already set
     if ( hash_file_st.st_size != expected_size )
         ftruncate(hashfd, expected_size);
     hashes = (Sha1Hash*) mmap (NULL, expected_size, PROT_READ|PROT_WRITE, 
-                               MAP_PRIVATE, hashfd, 0);
+                               MAP_SHARED, hashfd, 0);
     if (hashes==MAP_FAILED) {
         hashes = NULL;
         size = sizek = complete = completek = seq_complete = 0;
@@ -183,7 +185,7 @@ bool            FileTransfer::OfferData (bin64_t pos, uint8_t* data, size_t leng
     //printf("g %lli %s\n",(uint64_t)pos,hash.hex().c_str());
        // walk to the nearest proven hash   FIXME 0-layer peak
     ack_out.set(pos,bins::FILLED);
-    pwrite(fd,data,length,pos.base_offset());
+    pwrite(fd,data,length,pos.base_offset()<<10);
     complete += length;
     completek++;
     if (length<1024) {