From d5987f5b901f4aeb734a27e5bc267067897d5f53 Mon Sep 17 00:00:00 2001 From: victor Date: Fri, 16 Oct 2009 11:17:03 +0000 Subject: [PATCH] Google coding conventions git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@430 e16421f0-f15b-0410-abcd-98678b794739 --- ext/seq_picker.cpp | 18 ++-- p2tp.h | 81 +++++++++------ tests/transfertest.cpp | 55 +++++----- transfer.cpp | 226 ++++++++++++++++++++++------------------- 4 files changed, 205 insertions(+), 175 deletions(-) diff --git a/ext/seq_picker.cpp b/ext/seq_picker.cpp index 95dc351..734931f 100644 --- a/ext/seq_picker.cpp +++ b/ext/seq_picker.cpp @@ -13,21 +13,21 @@ using namespace p2tp; class SeqPiecePicker : public PiecePicker { - bins hint_out; - FileTransfer* file; + bins hint_out_; + FileTransfer* file_; public: - SeqPiecePicker (FileTransfer* file_) : file(file_), hint_out() { + SeqPiecePicker (FileTransfer* file_to_pick_from) : file_(file_to_pick_from), hint_out_() { } virtual bin64_t Pick (bins& from, uint8_t layer) { bins may_pick = from; - may_pick.remove (file->ack_out); - may_pick.remove (hint_out); + may_pick.remove (file_->ack_out()); + may_pick.remove (hint_out_); for (int l=layer; l>=0; l--) { - for(int i=0; ipeak_count; i++) { - bin64_t pick = may_pick.find(file->peaks[i],l,bins::FILLED); + for(int i=0; ipeak_count(); i++) { + bin64_t pick = may_pick.find(file_->peak(i),l,bins::FILLED); if (pick!=bin64_t::NONE) return pick; } @@ -36,11 +36,11 @@ public: } virtual void Received (bin64_t b) { - hint_out.set(b,bins::EMPTY); + hint_out_.set(b,bins::EMPTY); } virtual void Snubbed (bin64_t b) { - hint_out.set(b,bins::EMPTY); + hint_out_.set(b,bins::EMPTY); } }; \ No newline at end of file diff --git a/p2tp.h b/p2tp.h index 94a356c..bdb39de 100644 --- a/p2tp.h +++ b/p2tp.h @@ -104,62 +104,70 @@ namespace p2tp { accept or remember or drop. Returns true => ACK is sent. */ bool OfferData (bin64_t bin, uint8_t* data, size_t length); - static FileTransfer* Find (const Sha1Hash& hash); + bin64_t PickBinForRequest (bins& from, uint8_t layer) ; static FileTransfer* Find (const Sha1Hash& hash); static FileTransfer* file (int fd) { return fd files; static const char* HASH_FILE_TEMPLATE; static const char* PEAK_FILE_TEMPLATE; - static int instance; /** file descriptor. */ - int fd; + int fd_; /** File size, as derived from the hashes. */ - size_t size; - size_t sizek; + size_t size_; + size_t sizek_; /** Part the file currently downloaded. */ - size_t complete; - size_t completek; - size_t seq_complete; + size_t complete_; + size_t completek_; + size_t seq_complete_; /** A map for all packets obtained and succesfully checked. */ - bins ack_out; + bins ack_out_; /** History of bin retrieval. */ - binqueue data_in; + binqueue data_in_; /** Piece picker strategy. */ - PiecePicker* picker; + PiecePicker* picker_; /** File for keeping the Merkle hash tree. */ - int hashfd; + int hashfd_; /** Merkle hash tree: root */ - Sha1Hash root_hash; + Sha1Hash root_hash_; /** Merkle hash tree: peak hashes */ - Sha1Hash peak_hashes[64]; - bin64_t peaks[64]; - int peak_count; + Sha1Hash peak_hashes_[64]; + bin64_t peaks_[64]; + int peak_count_; /** Merkle hash tree: the tree, as a bin64_t-indexed array */ - Sha1Hash* hashes; + Sha1Hash* hashes_; /** for recovering saved state */ - bool dry_run; + bool dry_run_; /** Error encountered */ - char* error; + char* error_; protected: void SetSize(size_t bytes); @@ -185,10 +193,12 @@ namespace p2tp { class CongestionController { public: - tint rtt_avg; - tint dev_avg; - int cwnd; - int peer_cwnd; + virtual tint rtt_avg() = 0; + virtual tint dev_avg() = 0; + virtual tint next_send_time() = 0; + virtual int cwnd() = 0; + virtual int peer_cwnd() = 0; + virtual int free_cwnd() = 0; virtual void OnDataSent(bin64_t b) = 0; virtual void OnDataRecvd(bin64_t b) = 0; virtual void OnAckRcvd(const tintbin& tsack) = 0; @@ -231,6 +241,8 @@ namespace p2tp { ~Channel(); static void Recv (int socket); + static void Loop (tint till); + void Recv (Datagram& dgram); tint Send (); @@ -240,6 +252,7 @@ namespace p2tp { void OnHint (Datagram& dgram); void OnHash (Datagram& dgram); void OnPex (Datagram& dgram); + void OnHandshake (Datagram& dgram); void AddHandshake (Datagram& dgram); bin64_t AddData (Datagram& dgram); @@ -302,6 +315,8 @@ namespace p2tp { static int MAX_REORDERING; static tint TIMEOUT; static std::vector channels; + int sockets[4]; + int sock_count; static tint last_tick; }; diff --git a/tests/transfertest.cpp b/tests/transfertest.cpp index 67c6163..b9bffba 100644 --- a/tests/transfertest.cpp +++ b/tests/transfertest.cpp @@ -32,57 +32,58 @@ TEST(TransferTest,TransferFile) { // submit a new file FileTransfer* seed = new FileTransfer(Sha1Hash::ZERO,BTF); - EXPECT_TRUE(A==seed->hashes[0]); - EXPECT_TRUE(E==seed->hashes[bin64_t(0,4)]); - EXPECT_TRUE(ABCD==seed->hashes[bin64_t(2,0)]); - EXPECT_TRUE(ROOT==seed->root_hash); - EXPECT_TRUE(ABCD==seed->peak_hashes[0]); - EXPECT_TRUE(E==seed->peak_hashes[1]); - EXPECT_TRUE(ROOT==seed->root_hash); - EXPECT_EQ(4100,seed->size); - EXPECT_EQ(5,seed->sizek); - EXPECT_EQ(4100,seed->complete); - EXPECT_EQ(4100,seed->seq_complete); + EXPECT_TRUE(A==seed->hash(0)); + EXPECT_TRUE(E==seed->hash(bin64_t(0,4))); + EXPECT_TRUE(ABCD==seed->hash(bin64_t(2,0))); + EXPECT_TRUE(ROOT==seed->root_hash()); + EXPECT_TRUE(ABCD==seed->peak_hash(0)); + EXPECT_TRUE(E==seed->peak_hash(1)); + EXPECT_TRUE(ROOT==seed->root_hash()); + EXPECT_EQ(4100,seed->size()); + EXPECT_EQ(5,seed->size_kilo()); + EXPECT_EQ(4100,seed->complete()); + EXPECT_EQ(4100,seed->seq_complete()); // retrieve it unlink("copy"); FileTransfer::instance = 1; - FileTransfer* leech = new FileTransfer(seed->root_hash,"copy"); + FileTransfer* leech = new FileTransfer(seed->root_hash(),"copy"); // transfer peak hashes - for(int i=0; ipeak_count; i++) - leech->OfferHash(seed->peaks[i],seed->peak_hashes[i]); - ASSERT_EQ(5<<10,leech->size); - ASSERT_EQ(5,leech->sizek); - ASSERT_EQ(0,leech->complete); + for(int i=0; ipeak_count(); i++) + leech->OfferHash(seed->peak(i),seed->peak_hash(i)); + ASSERT_EQ(5<<10,leech->size()); + ASSERT_EQ(5,leech->size_kilo()); + ASSERT_EQ(0,leech->complete()); // transfer data and hashes // ABCD E000 // AB CD E0 0 // AAAA BBBB CCCC DDDD E 0 0 0 // calculated leech->OfferHash(bin64_t(1,0), seed->hashes[bin64_t(1,0)]); - leech->OfferHash(bin64_t(1,1), seed->hashes[bin64_t(1,1)]); + leech->OfferHash(bin64_t(1,1), seed->hash(bin64_t(1,1))); for (int i=0; i<5; i++) { if (i==2) { // now: stop, save, start delete leech; FileTransfer::instance = 1; - leech = new FileTransfer(seed->root_hash,"copy"); - EXPECT_EQ(2,leech->completek); + leech = new FileTransfer(seed->root_hash(),"copy"); + EXPECT_EQ(2,leech->complete_kilo()); } - bin64_t next = leech->picker->Pick(seed->ack_out,0); + bin64_t next = leech->PickBinForRequest(seed->ack_out(),0); ASSERT_NE(bin64_t::NONE,next); uint8_t buf[1024]; //size_t len = seed->storer->ReadData(next,&buf); - size_t len = pread(seed->fd,buf,1024,next.base_offset()<<10); // FIXME TEST FOR ERROR + size_t len = pread(seed->file_descriptor(),buf,1024,next.base_offset()<<10); // FIXME TEST FOR ERROR bin64_t sibling = next.sibling(); - leech->OfferHash(sibling, seed->hashes[sibling]); // i=4 => out of bounds + if (sibling.base_offset()size_kilo()) + leech->OfferHash(sibling, seed->hash(sibling)); uint8_t memo = *buf; *buf = 'z'; EXPECT_FALSE(leech->OfferData(next, buf, len)); *buf = memo; EXPECT_TRUE(leech->OfferData(next, buf, len)); } - EXPECT_EQ(4100,leech->size); - EXPECT_EQ(5,leech->sizek); - EXPECT_EQ(4100,leech->complete); - EXPECT_EQ(4100,leech->seq_complete); + EXPECT_EQ(4100,leech->size()); + EXPECT_EQ(5,leech->size_kilo()); + EXPECT_EQ(4100,leech->complete()); + EXPECT_EQ(4100,leech->seq_complete()); } /* diff --git a/transfer.cpp b/transfer.cpp index 8f2e9b8..9a0db9d 100644 --- a/transfer.cpp +++ b/transfer.cpp @@ -23,23 +23,29 @@ int FileTransfer::instance = 0; // FIXME: separate Bootstrap() and Download(), then Size(), Progress(), SeqProgress() 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), size(0), sizek(0), - complete(0), completek(0), seq_complete(0) + root_hash_(_root_hash), fd_(0), hashfd_(0), dry_run_(false), + 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) + fd_ = open(filename,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); + if (fd_<0) return; - if (root_hash==Sha1Hash::ZERO) // fresh submit, hash it + if (root_hash_==Sha1Hash::ZERO) // fresh submit, hash it Submit(); else RecoverProgress(); - picker = new SeqPiecePicker(this); + picker_ = new SeqPiecePicker(this); } + +bin64_t FileTransfer::PickBinForRequest (bins& from, uint8_t layer) { + return picker_->Pick(from,layer); +} + + void FileTransfer::LoadPeaks () { char file_name[1024]; - sprintf(file_name,PEAK_FILE_TEMPLATE,root_hash.hex().c_str(),instance); + sprintf(file_name,PEAK_FILE_TEMPLATE,root_hash().hex().c_str(),instance); int peakfd = open(file_name,O_RDONLY); if (peakfd<0) return; @@ -56,169 +62,177 @@ void FileTransfer::LoadPeaks () { /** Basically, simulated receiving every single packet, except for some optimizations. */ void FileTransfer::RecoverProgress () { - dry_run = true; + dry_run_ = true; LoadPeaks(); - if (!size) + if (!size()) return; // at this point, we may use mmapd hashes already // so, lets verify hashes and the data we've got - lseek(fd,0,SEEK_SET); - for(int p=0; p>10) + ((size&1023) ? 1 : 0); + size_ = bytes; + completek_ = complete_ = seq_complete_ = 0; + sizek_ = (size_>>10) + ((size_&1023) ? 1 : 0); char file_name[1024]; struct stat st; - fstat(fd, &st); + fstat(fd_, &st); if (st.st_size!=bytes) - if (ftruncate(fd, bytes)) + if (ftruncate(fd_, bytes)) return; // remain in the 0-state // mmap the hash file into memory - 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; + 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; - fstat(hashfd, &hash_file_st); + fstat(hashfd_, &hash_file_st); if ( hash_file_st.st_size != expected_size ) - ftruncate(hashfd, expected_size); - hashes = (Sha1Hash*) mmap (NULL, expected_size, PROT_READ|PROT_WRITE, - MAP_SHARED, hashfd, 0); - if (hashes==MAP_FAILED) { - hashes = NULL; - size = sizek = complete = completek = seq_complete = 0; - error = strerror(errno); + ftruncate(hashfd_, expected_size); + hashes_ = (Sha1Hash*) mmap (NULL, expected_size, PROT_READ|PROT_WRITE, + MAP_SHARED, hashfd_, 0); + if (hashes_==MAP_FAILED) { + hashes_ = NULL; + size_ = sizek_ = complete_ = completek_ = seq_complete_ = 0; + error_ = strerror(errno); // FIXME dprintf() perror("hash tree mmap failed"); return; } - for(int i=0; i>10) + ((size&1023) ? 1 : 0); - hashes = (Sha1Hash*) malloc(Sha1Hash::SIZE*sizek*2); - peak_count = bin64_t::peaks(sizek,peaks); - for (int p=0; p>10) + ((size_&1023) ? 1 : 0); + hashes_ = (Sha1Hash*) malloc(Sha1Hash::SIZE*sizek_*2); + peak_count_ = bin64_t::peaks(sizek_,peaks_); + for (int p=0; pdata_in_.size()) + return bin64_t::NONE; + return data_in_[offset]; } bool FileTransfer::OfferData (bin64_t pos, uint8_t* data, size_t length) { if (!pos.is_base()) return false; - if (length<1024 && pos!=bin64_t(0,sizek-1)) + if (length<1024 && pos!=bin64_t(0,sizek_-1)) return false; - if (ack_out.get(pos)==bins::FILLED) + if (ack_out_.get(pos)==bins::FILLED) return true; // ??? int pi=0; - while (pi>10))==bins::FILLED) - seq_complete+=1024; - if (seq_complete>size) - seq_complete = size; + while (ack_out_.get(bin64_t(0,seq_complete_>>10))==bins::FILLED) + seq_complete_+=1024; + if (seq_complete_>size_) + seq_complete_ = size_; + data_in_.push_back(pos); return true; } Sha1Hash FileTransfer::DeriveRoot () { - int c = peak_count-1; - bin64_t p = peaks[c]; - Sha1Hash hash = peak_hashes[c]; + int c = peak_count_-1; + bin64_t p = peaks_[c]; + Sha1Hash hash = peak_hashes_[c]; c--; while (p!=bin64_t::ALL) { if (p.is_left()) { p = p.parent(); hash = Sha1Hash(hash,Sha1Hash::ZERO); } else { - if (c<0 || peaks[c]!=p.sibling()) + if (c<0 || peaks_[c]!=p.sibling()) return Sha1Hash::ZERO; - hash = Sha1Hash(peak_hashes[c],hash); + hash = Sha1Hash(peak_hashes_[c],hash); p = p.parent(); c--; } @@ -229,18 +243,18 @@ Sha1Hash FileTransfer::DeriveRoot () { void FileTransfer::OfferPeak (bin64_t pos, const Sha1Hash& hash) { - assert(!size); - if (peak_count) { - bin64_t last_peak = peaks[peak_count-1]; + assert(!size_); + if (peak_count_) { + bin64_t last_peak = peaks_[peak_count_-1]; if ( pos.layer()>=last_peak.layer() || pos.base_offset()!=last_peak.base_offset()+last_peak.width() ) - peak_count = 0; + peak_count_ = 0; } - peaks[peak_count] = pos; - peak_hashes[peak_count++] = hash; + peaks_[peak_count_] = pos; + peak_hashes_[peak_count_++] = hash; // check whether peak hash candidates add up to the root hash Sha1Hash mustbe_root = DeriveRoot(); - if (mustbe_root!=root_hash) + if (mustbe_root!=root_hash_) return; // bingo, we now know the file size (rounded up to a KByte) SetSize( (pos.base_offset()+pos.width()) << 10 ); @@ -249,15 +263,15 @@ void FileTransfer::OfferPeak (bin64_t pos, const Sha1Hash& hash) { FileTransfer::~FileTransfer () { - munmap(hashes,sizek*2*Sha1Hash::SIZE); - close(hashfd); - close(fd); + munmap(hashes_,sizek_*2*Sha1Hash::SIZE); + close(hashfd_); + close(fd_); } FileTransfer* FileTransfer::Find (const Sha1Hash& root_hash) { for(int i=0; iroot_hash==root_hash) + if (files[i] && files[i]->root_hash_==root_hash) return files[i]; return NULL; } @@ -270,11 +284,11 @@ int p2tp::Open (const char* filename) { int p2tp::Open (const Sha1Hash& hash, const char* filename) { FileTransfer* ft = new FileTransfer(hash, filename); - if (ft->fd>0) { - if (FileTransfer::files.size()fd) - FileTransfer::files.resize(ft->fd); - FileTransfer::files[ft->fd] = ft; - return ft->fd; + if (ft->fd_>0) { + if (FileTransfer::files.size()fd_) + FileTransfer::files.resize(ft->fd_); + FileTransfer::files[ft->fd_] = ft; + return ft->fd_; } else { delete ft; return -1; @@ -282,7 +296,7 @@ int p2tp::Open (const Sha1Hash& hash, const char* filename) { } -void Close (int fdes) { +void p2tp::Close (int fdes) { // FIXME delete all channels delete FileTransfer::files[fdes]; FileTransfer::files[fdes] = NULL; @@ -329,18 +343,18 @@ void Close (int fdes) { // read root hash char hashbuf[128]; uint64_t binbuf; - lseek(hashfd,0,SEEK_SET); - read(hashfd,&binbuf,sizeof(bin64_t)); - read(hashfd,hashbuf,Sha1Hash::SIZE); + lseek(hashfd_,0,SEEK_SET); + read(hashfd_,&binbuf,sizeof(bin64_t)); + read(hashfd_,hashbuf,Sha1Hash::SIZE); Sha1Hash mustberoot(false,(const char*)hashbuf); if ( binbuf!=bin64_t::ALL || mustberoot != this->root_hash ) { - ftruncate(hashfd,Sha1Hash::SIZE*64); + ftruncate(hashfd_,Sha1Hash::SIZE*64); return; } // read peak hashes for(int i=1; i<64 && !this->size; i++){ - read(hashfd,&binbuf,sizeof(bin64_t)); - read(hashfd,hashbuf,Sha1Hash::SIZE); + read(hashfd_,&binbuf,sizeof(bin64_t)); + read(hashfd_,hashbuf,Sha1Hash::SIZE); Sha1Hash mustbepeak(false,(const char*)hashbuf); if (mustbepeak==Sha1Hash::ZERO) break; -- 2.20.1