3 * some transfer-scope code
5 * Created by Victor Grishchenko on 10/6/09.
6 * Copyright 2009 Delft University of Technology. All rights reserved.
18 #include "compat/util.h"
20 #include "ext/seq_picker.cpp" // FIXME FIXME FIXME FIXME
22 using namespace swift;
24 std::vector<FileTransfer*> FileTransfer::files(20);
26 #define BINHASHSIZE (sizeof(bin64_t)+sizeof(Sha1Hash))
28 // FIXME: separate Bootstrap() and Download(), then Size(), Progress(), SeqProgress()
30 FileTransfer::FileTransfer (const char* filename, const Sha1Hash& _root_hash) :
31 file_(filename,_root_hash), hs_in_offset_(0)
33 if (files.size()<fd()+1)
36 picker_ = new SeqPiecePicker(this);
37 picker_->Randomize(rand()&63);
38 init_time_ = Datagram::Time();
42 void Channel::CloseTransfer (FileTransfer* trans) {
43 for(int i=0; i<Channel::channels.size(); i++)
44 if (Channel::channels[i] && Channel::channels[i]->transfer_==trans)
45 delete Channel::channels[i];
49 FileTransfer::~FileTransfer ()
51 Channel::CloseTransfer(this);
57 FileTransfer* FileTransfer::Find (const Sha1Hash& root_hash) {
58 for(int i=0; i<files.size(); i++)
59 if (files[i] && files[i]->root_hash()==root_hash)
65 void FileTransfer::OnPexIn (const Address& addr) {
66 for(int i=0; i<hs_in_.size(); i++) {
67 Channel* c = Channel::channel(hs_in_[i]);
68 if (c && c->transfer().fd()==this->fd() && c->peer()==addr)
69 return; // already connected
71 if (hs_in_.size()<20) {
72 new Channel(this,Channel::default_socket(),addr);
74 pex_in_.push_back(addr);
75 if (pex_in_.size()>1000)
81 int FileTransfer::RevealChannel (int& pex_out_) { // FIXME brainfuck
82 pex_out_ -= hs_in_offset_;
85 while (pex_out_<hs_in_.size()) {
86 Channel* c = Channel::channel(hs_in_[pex_out_]);
87 if (c && c->transfer().fd()==this->fd()) {
88 if (c->is_established()) {
89 pex_out_ += hs_in_offset_ + 1;
94 hs_in_[pex_out_] = hs_in_[0];
99 pex_out_ += hs_in_offset_;