Added README file
[swift-upb.git] / transfer.cpp
1 /*
2  *  transfer.cpp
3  *  some transfer-scope code
4  *
5  *  Created by Victor Grishchenko on 10/6/09.
6  *  Copyright 2009 Delft University of Technology. All rights reserved.
7  *
8  */
9 #ifdef _WIN32
10 #include "compat.h"
11 #else
12 #include <sys/mman.h>
13 #endif
14 #include <errno.h>
15 #include <string>
16 #include <sstream>
17 #include "swift.h"
18 #include "compat/util.h"
19
20 #include "ext/seq_picker.cpp" // FIXME FIXME FIXME FIXME 
21
22 using namespace swift;
23
24 std::vector<FileTransfer*> FileTransfer::files(20);
25
26 #define BINHASHSIZE (sizeof(bin64_t)+sizeof(Sha1Hash))
27
28 // FIXME: separate Bootstrap() and Download(), then Size(), Progress(), SeqProgress()
29
30 FileTransfer::FileTransfer (const char* filename, const Sha1Hash& _root_hash) :
31     file_(filename,_root_hash), hs_in_offset_(0)
32 {
33     if (files.size()<fd()+1)
34         files.resize(fd()+1);
35     files[fd()] = this;
36     picker_ = new SeqPiecePicker(this);
37     picker_->Randomize(rand()&63);
38     init_time_ = Datagram::Time();
39 }
40
41
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];
46 }
47
48
49 FileTransfer::~FileTransfer ()
50 {
51     Channel::CloseTransfer(this);
52     files[fd()] = NULL;
53     delete picker_;
54 }
55
56
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)
60             return files[i];
61     return NULL;
62 }
63
64
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
70     }
71     if (hs_in_.size()<20) {
72         new Channel(this,Channel::default_socket(),addr);
73     } else {
74         pex_in_.push_back(addr);
75         if (pex_in_.size()>1000)
76             pex_in_.pop_front();
77     }
78 }
79
80
81 int        FileTransfer::RevealChannel (int& pex_out_) { // FIXME brainfuck
82     pex_out_ -= hs_in_offset_;
83     if (pex_out_<0)
84         pex_out_ = 0;
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;
90                 return c->id();
91             } else
92                 pex_out_++;
93         } else {
94             hs_in_[pex_out_] = hs_in_[0];
95             hs_in_.pop_front();
96             hs_in_offset_++;
97         }
98     }
99     pex_out_ += hs_in_offset_;
100     return -1;
101 }
102