From 23016b20e168ede2498fde15faa3750440232351 Mon Sep 17 00:00:00 2001 From: victor Date: Tue, 13 Oct 2009 10:16:19 +0000 Subject: [PATCH] save ok git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@403 e16421f0-f15b-0410-abcd-98678b794739 --- p2tp.h | 5 +++-- tests/binstest2.cpp | 22 ++++++++++++++++++++++ tests/transfertest.cpp | 27 +++++++++++++++++---------- transfer.cpp | 18 ++++++++++-------- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/p2tp.h b/p2tp.h index 2e3016f..122a469 100644 --- a/p2tp.h +++ b/p2tp.h @@ -108,8 +108,9 @@ namespace p2tp { public: static std::vector 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; diff --git a/tests/binstest2.cpp b/tests/binstest2.cpp index 5882eff..9d76467 100755 --- a/tests/binstest2.cpp +++ b/tests/binstest2.cpp @@ -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) { diff --git a/tests/transfertest.cpp b/tests/transfertest.cpp index 5e60ec6..24683e6 100644 --- a/tests/transfertest.cpp +++ b/tests/transfertest.cpp @@ -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; ipeak_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; } diff --git a/transfer.cpp b/transfer.cpp index 15b8404..a260842 100644 --- a/transfer.cpp +++ b/transfer.cpp @@ -13,8 +13,9 @@ using namespace p2tp; std::vector 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