forked from gritzko/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransfer.cpp
More file actions
155 lines (126 loc) · 3.98 KB
/
transfer.cpp
File metadata and controls
155 lines (126 loc) · 3.98 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* 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), files_index_(-1)
{
initialize();
}
FileTransfer::FileTransfer (DataStorage* dataStorage, const Sha1Hash& root_hash, HashStorage* hashStorage) :
file_(dataStorage, root_hash, hashStorage), hs_in_offset_(0), cb_installed(0), files_index_(-1)
{
initialize();
}
void FileTransfer::initialize() {
// NOTE: This should probably be guarded against multithreaded access faults (very easy to break this array)
for(int i=0; i<files.size();i++) {
if(!files[i]) {
files_index_=i;
break;
}
}
if(files_index_==-1) {
files_index_=files.size()+1;
files.resize(files_index_);
}
files[files_index_] = 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 FileTransfer::AddProgressCallback (ProgressCallback cb, uint8_t agg) {
cb_agg[cb_installed] = agg;
callbacks[cb_installed] = cb;
cb_installed++;
}
void swift::ExternallyRetrieved (FileTransfer* trans,bin64_t piece) {
if (!trans)
return;
trans->ack_out().set(piece); // that easy
}
void FileTransfer::RemoveProgressCallback (ProgressCallback cb) {
for(int i=0; i<cb_installed; i++) {
if (callbacks[i]==cb) {
callbacks[i]=callbacks[--cb_installed];
cb_agg[i]=cb_agg[cb_installed];
}
}
}
void FileTransfer::callCallbacks(bin64_t& cover) {
for(int i=0; i<cb_installed; i++) {
if(cover.layer()>=cb_agg[i])
callbacks[i](this,cover);
}
}
FileTransfer::~FileTransfer ()
{
Channel::CloseTransfer(this);
files[files_index_] = 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;
}
FileTransfer* swift:: Find (Sha1Hash hash) {
FileTransfer* t = FileTransfer::Find(hash);
if (t)
return t;
return NULL;
}
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().files_index_==files_index_ && 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().files_index_==files_index_) {
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;
}