-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtransfer.cpp
133 lines (107 loc) · 3.43 KB
/
transfer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* transfer.cpp
* some transfer-scope code
*
* Created by Victor Grishchenko on 10/6/09.
* Copyright 2009 Delft University of Technology. All rights reserved.
*
*/
#include <errno.h>
#include <string>
#include <sstream>
#include "swift.h"
#include "ext/seq_picker.cpp" // FIXME FIXME FIXME FIXME
using namespace swift;
std::vector<FileTransfer*> FileTransfer::files(20);
#define BINHASHSIZE (sizeof(bin64_t)+sizeof(Sha1Hash))
// FIXME: separate Bootstrap() and Download(), then Size(), Progress(), SeqProgress()
FileTransfer::FileTransfer (const char* filename, const Sha1Hash& _root_hash) :
file_(filename,_root_hash), hs_in_offset_(0), cb_installed(0)
{
if (files.size()<fd()+1)
files.resize(fd()+1);
files[fd()] = this;
picker_ = new SeqPiecePicker(this);
picker_->Randomize(rand()&63);
init_time_ = Datagram::Time();
}
void Channel::CloseTransfer (FileTransfer* trans) {
for(int i=0; i<Channel::channels.size(); i++)
if (Channel::channels[i] && Channel::channels[i]->transfer_==trans)
delete Channel::channels[i];
}
void swift::AddProgressCallback (int transfer,ProgressCallback cb,uint8_t agg) {
FileTransfer* trans = FileTransfer::file(transfer);
if (!trans)
return;
trans->cb_agg[trans->cb_installed] = agg;
trans->callbacks[trans->cb_installed] = cb;
trans->cb_installed++;
}
void swift::ExternallyRetrieved (int transfer,bin64_t piece) {
FileTransfer* trans = FileTransfer::file(transfer);
if (!trans)
return;
trans->ack_out().set(piece); // that easy
}
void swift::RemoveProgressCallback (int transfer, ProgressCallback cb) {
FileTransfer* trans = FileTransfer::file(transfer);
if (!trans)
return;
for(int i=0; i<trans->cb_installed; i++)
if (trans->callbacks[i]==cb)
trans->callbacks[i]=trans->callbacks[--trans->cb_installed];
}
FileTransfer::~FileTransfer ()
{
Channel::CloseTransfer(this);
files[fd()] = NULL;
delete picker_;
}
FileTransfer* FileTransfer::Find (const Sha1Hash& root_hash) {
for(int i=0; i<files.size(); i++)
if (files[i] && files[i]->root_hash()==root_hash)
return files[i];
return NULL;
}
int swift:: Find (Sha1Hash hash) {
FileTransfer* t = FileTransfer::Find(hash);
if (t)
return t->fd();
return -1;
}
void FileTransfer::OnPexIn (const Address& addr) {
for(int i=0; i<hs_in_.size(); i++) {
Channel* c = Channel::channel(hs_in_[i]);
if (c && c->transfer().fd()==this->fd() && c->peer()==addr)
return; // already connected
}
if (hs_in_.size()<20) {
new Channel(this,Datagram::default_socket(),addr);
} else {
pex_in_.push_back(addr);
if (pex_in_.size()>1000)
pex_in_.pop_front();
}
}
int FileTransfer::RevealChannel (int& pex_out_) { // FIXME brainfuck
pex_out_ -= hs_in_offset_;
if (pex_out_<0)
pex_out_ = 0;
while (pex_out_<hs_in_.size()) {
Channel* c = Channel::channel(hs_in_[pex_out_]);
if (c && c->transfer().fd()==this->fd()) {
if (c->is_established()) {
pex_out_ += hs_in_offset_ + 1;
return c->id();
} else
pex_out_++;
} else {
hs_in_[pex_out_] = hs_in_[0];
hs_in_.pop_front();
hs_in_offset_++;
}
}
pex_out_ += hs_in_offset_;
return -1;
}