3 * some transfer-scope code
5 * Created by Victor Grishchenko on 10/6/09.
6 * Copyright 2009 Delft University of Technology. All rights reserved.
14 #include "ext/seq_picker.cpp" // FIXME FIXME FIXME FIXME
16 using namespace swift;
18 std::vector<FileTransfer*> FileTransfer::files(20);
20 #define BINHASHSIZE (sizeof(bin64_t)+sizeof(Sha1Hash))
22 // FIXME: separate Bootstrap() and Download(), then Size(), Progress(), SeqProgress()
24 FileTransfer::FileTransfer (const char* filename, const Sha1Hash& _root_hash) :
25 file_(filename,_root_hash), hs_in_offset_(0)
27 if (files.size()<fd()+1)
30 picker_ = new SeqPiecePicker(this);
31 picker_->Randomize(rand()&63);
32 init_time_ = Datagram::Time();
36 void Channel::CloseTransfer (FileTransfer* trans) {
37 for(int i=0; i<Channel::channels.size(); i++)
38 if (Channel::channels[i] && Channel::channels[i]->transfer_==trans)
39 delete Channel::channels[i];
43 FileTransfer::~FileTransfer ()
45 Channel::CloseTransfer(this);
51 FileTransfer* FileTransfer::Find (const Sha1Hash& root_hash) {
52 for(int i=0; i<files.size(); i++)
53 if (files[i] && files[i]->root_hash()==root_hash)
59 void FileTransfer::OnPexIn (const Address& addr) {
60 for(int i=0; i<hs_in_.size(); i++) {
61 Channel* c = Channel::channel(hs_in_[i]);
62 if (c && c->transfer().fd()==this->fd() && c->peer()==addr)
63 return; // already connected
65 if (hs_in_.size()<20) {
66 new Channel(this,Channel::default_socket(),addr);
68 pex_in_.push_back(addr);
69 if (pex_in_.size()>1000)
75 int FileTransfer::RevealChannel (int& pex_out_) { // FIXME brainfuck
76 pex_out_ -= hs_in_offset_;
79 while (pex_out_<hs_in_.size()) {
80 Channel* c = Channel::channel(hs_in_[pex_out_]);
81 if (c && c->transfer().fd()==this->fd()) {
82 if (c->is_established()) {
83 pex_out_ += hs_in_offset_ + 1;
88 hs_in_[pex_out_] = hs_in_[0];
93 pex_out_ += hs_in_offset_;