-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtransfer.cpp
415 lines (334 loc) · 10.3 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* transfer.cpp
* some transfer-scope code
*
* Created by Victor Grishchenko on 10/6/09.
* Copyright 2009-2012 TECHNISCHE UNIVERSITEIT DELFT. All rights reserved.
*
*/
#include <errno.h>
#include <string>
#include <sstream>
#include <cfloat>
#include "swift.h"
#include "ext/seq_picker.cpp" // FIXME FIXME FIXME FIXME
#include "ext/vod_picker.cpp"
using namespace swift;
std::vector<FileTransfer*> FileTransfer::files(20);
#define BINHASHSIZE (sizeof(bin64_t)+sizeof(Sha1Hash))
#define TRACKER_RETRY_INTERVAL_START (5*TINT_SEC)
#define TRACKER_RETRY_INTERVAL_EXP 1.1 // exponent used to increase INTERVAL_START
#define TRACKER_RETRY_INTERVAL_MAX (1800*TINT_SEC) // 30 minutes
// FIXME: separate Bootstrap() and Download(), then Size(), Progress(), SeqProgress()
FileTransfer::FileTransfer(std::string filename, const Sha1Hash& root_hash, bool force_check_diskvshash, bool check_netwvshash, uint32_t chunk_size, bool zerostate) :
Operational(), fd_(files.size()+1), cb_installed(0), mychannels_(),
speedzerocount_(0), tracker_(), tracker_retry_interval_(TRACKER_RETRY_INTERVAL_START),
tracker_retry_time_(NOW), zerostate_(zerostate)
{
if (files.size()<fd()+1)
files.resize(fd()+1);
files[fd()] = this;
std::string destdir;
int ret = file_exists_utf8(filename);
if (ret == 2 && root_hash != Sha1Hash::ZERO) {
// Filename is a directory, download root_hash there
destdir = filename;
filename = destdir+FILE_SEP+root_hash.hex();
} else {
destdir = dirname_utf8(filename);
if (destdir == "")
destdir = ".";
}
// MULTIFILE
storage_ = new Storage(filename,destdir,fd());
std::string hash_filename;
hash_filename.assign(filename);
hash_filename.append(".mhash");
std::string binmap_filename;
binmap_filename.assign(filename);
binmap_filename.append(".mbinmap");
if (!zerostate_)
{
hashtree_ = (HashTree *)new MmapHashTree(storage_,root_hash,chunk_size,hash_filename,force_check_diskvshash,check_netwvshash,binmap_filename);
if (ENABLE_VOD_PIECEPICKER) {
// Ric: init availability
availability_ = new Availability();
// Ric: TODO assign picker based on input params...
picker_ = new VodPiecePicker(this);
}
else
picker_ = new SeqPiecePicker(this);
picker_->Randomize(rand()&63);
}
else
{
// ZEROHASH
hashtree_ = (HashTree *)new ZeroHashTree(storage_,root_hash,chunk_size,hash_filename,binmap_filename);
}
init_time_ = Channel::Time();
cur_speed_[DDIR_UPLOAD] = MovingAverageSpeed();
cur_speed_[DDIR_DOWNLOAD] = MovingAverageSpeed();
max_speed_[DDIR_UPLOAD] = DBL_MAX;
max_speed_[DDIR_DOWNLOAD] = DBL_MAX;
// SAFECLOSE
evtimer_assign(&evclean_,Channel::evbase,&FileTransfer::LibeventCleanCallback,this);
evtimer_add(&evclean_,tint2tv(5*TINT_SEC));
UpdateOperational();
}
// SAFECLOSE
void FileTransfer::LibeventCleanCallback(int fd, short event, void *arg)
{
// Arno, 2012-02-24: Why-oh-why, update NOW
Channel::Time();
FileTransfer *ft = (FileTransfer *)arg;
if (ft == NULL)
return;
// STL and MS and conditional delete from set not a happy place :-(
channels_t delset;
channels_t::iterator iter;
bool hasestablishedpeers=false;
for (iter=ft->mychannels_.begin(); iter!=ft->mychannels_.end(); iter++)
{
Channel *c = *iter;
if (c != NULL) {
if (c->IsScheduled4Close())
delset.push_back(c);
if (c->is_established ()) {
hasestablishedpeers = true;
//fprintf(stderr,"%s peer %s\n", ft->hashtree()->root_hash().hex().c_str(), c->peer().str() );
}
}
}
for (iter=delset.begin(); iter!=delset.end(); iter++)
{
Channel *c = *iter;
dprintf("%s #%u clean cb close\n",tintstr(),c->id());
c->Close();
delete c; // Does erase from transfer() list of channels
}
// Arno, 2012-02-24: Check for liveliness.
ft->ReConnectToTrackerIfAllowed(hasestablishedpeers);
// Reschedule cleanup
evtimer_add(&(ft->evclean_),tint2tv(5*TINT_SEC));
}
void FileTransfer::ReConnectToTrackerIfAllowed(bool hasestablishedpeers)
{
// If I'm not connected to any
// peers, try to contact the tracker again.
if (!hasestablishedpeers)
{
if (NOW > tracker_retry_time_)
{
ConnectToTracker();
tracker_retry_interval_ *= TRACKER_RETRY_INTERVAL_EXP;
if (tracker_retry_interval_ > TRACKER_RETRY_INTERVAL_MAX)
tracker_retry_interval_ = TRACKER_RETRY_INTERVAL_MAX;
tracker_retry_time_ = NOW + tracker_retry_interval_;
}
}
else
{
tracker_retry_interval_ = TRACKER_RETRY_INTERVAL_START;
tracker_retry_time_ = NOW + tracker_retry_interval_;
}
}
void FileTransfer::ConnectToTracker()
{
if (!IsOperational())
return;
Channel *c = NULL;
if (tracker_ != Address())
c = new Channel(this,INVALID_SOCKET,tracker_);
else if (Channel::tracker!=Address())
c = new Channel(this);
}
Channel * FileTransfer::FindChannel(const Address &addr, Channel *notc)
{
channels_t::iterator iter;
for (iter=mychannels_.begin(); iter!=mychannels_.end(); iter++)
{
Channel *c = *iter;
if (c != NULL) {
if (c != notc && (c->peer() == addr || c->recv_peer() == addr)) {
return c;
}
}
}
return NULL;
}
void FileTransfer::UpdateOperational()
{
if ((hashtree_ != NULL && !hashtree_->IsOperational()) || !storage_->IsOperational())
SetBroken();
}
void Channel::CloseTransfer (FileTransfer* trans) {
for(int i=0; i<Channel::channels.size(); i++)
if (Channel::channels[i] && Channel::channels[i]->transfer_==trans)
{
//fprintf(stderr,"Channel::CloseTransfer: delete #%i\n", Channel::channels[i]->id());
Channel::channels[i]->Close(); // ARNO
delete Channel::channels[i];
}
}
void swift::AddProgressCallback (int transfer,ProgressCallback cb,uint8_t agg) {
//fprintf(stderr,"swift::AddProgressCallback: transfer %i\n", transfer );
FileTransfer* trans = FileTransfer::file(transfer);
if (!trans)
return;
//fprintf(stderr,"swift::AddProgressCallback: ft obj %p %p\n", trans, cb );
trans->cb_agg[trans->cb_installed] = agg;
trans->callbacks[trans->cb_installed] = cb;
trans->cb_installed++;
}
void swift::ExternallyRetrieved (int transfer,bin_t piece) {
FileTransfer* trans = FileTransfer::file(transfer);
if (!trans)
return;
trans->ack_out()->set(piece); // that easy
}
void swift::RemoveProgressCallback (int transfer, ProgressCallback cb) {
//fprintf(stderr,"swift::RemoveProgressCallback: transfer %i\n", transfer );
FileTransfer* trans = FileTransfer::file(transfer);
if (!trans)
return;
//fprintf(stderr,"swift::RemoveProgressCallback: transfer %i ft obj %p %p\n", transfer, trans, cb );
for(int i=0; i<trans->cb_installed; i++)
if (trans->callbacks[i]==cb)
trans->callbacks[i]=trans->callbacks[--trans->cb_installed];
for(int i=0; i<trans->cb_installed; i++)
{
fprintf(stderr,"swift::RemoveProgressCallback: transfer %i remain %p\n", transfer, trans->callbacks[i] );
}
}
FileTransfer::~FileTransfer ()
{
Channel::CloseTransfer(this);
delete hashtree_;
delete storage_;
files[fd()] = NULL;
if (!IsZeroState())
{
delete picker_;
delete availability_;
}
// Arno, 2012-02-06: Cancel cleanup timer, otherwise chaos!
evtimer_del(&evclean_);
}
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;
}
bool FileTransfer::OnPexAddIn (const Address& addr) {
//fprintf(stderr,"FileTransfer::OnPexAddIn: %s\n", addr.str() );
// Arno: this brings safety, but prevents private swift installations.
// TODO: detect public internet.
//if (addr.is_private())
// return false;
// Gertjan fix: PEX redo
if (hs_in_.size()<SWIFT_MAX_CONNECTIONS)
{
// Arno, 2012-02-27: Check if already connected to this peer.
Channel *c = FindChannel(addr,NULL);
if (c == NULL)
new Channel(this,Channel::default_socket(),addr);
else
return false;
}
return true;
}
//Gertjan
int FileTransfer::RandomChannel (int own_id) {
binqueue choose_from;
int i;
for (i = 0; i < (int) hs_in_.size(); i++) {
if (hs_in_[i].toUInt() == own_id)
continue;
Channel *c = Channel::channel(hs_in_[i].toUInt());
if (c == NULL || c->transfer().fd() != this->fd()) {
/* Channel was closed or is not associated with this FileTransfer (anymore). */
hs_in_[i] = hs_in_[0];
hs_in_.pop_front();
i--;
continue;
}
if (!c->is_established())
continue;
choose_from.push_back(hs_in_[i]);
}
if (choose_from.size() == 0)
return -1;
return choose_from[rand() % choose_from.size()].toUInt();
}
void FileTransfer::OnRecvData(int n)
{
// Got n ~ 32K
cur_speed_[DDIR_DOWNLOAD].AddPoint((uint64_t)n);
}
void FileTransfer::OnSendData(int n)
{
// Sent n ~ 1K
cur_speed_[DDIR_UPLOAD].AddPoint((uint64_t)n);
}
void FileTransfer::OnSendNoData()
{
// AddPoint(0) everytime we don't AddData gives bad speed measurement
// batch 32 such events into 1.
speedzerocount_++;
if (speedzerocount_ >= 32)
{
cur_speed_[DDIR_UPLOAD].AddPoint((uint64_t)0);
speedzerocount_ = 0;
}
}
double FileTransfer::GetCurrentSpeed(data_direction_t ddir)
{
return cur_speed_[ddir].GetSpeedNeutral();
}
void FileTransfer::SetMaxSpeed(data_direction_t ddir, double m)
{
max_speed_[ddir] = m;
// Arno, 2012-01-04: Be optimistic, forget history.
cur_speed_[ddir].Reset();
}
double FileTransfer::GetMaxSpeed(data_direction_t ddir)
{
return max_speed_[ddir];
}
uint32_t FileTransfer::GetNumLeechers()
{
uint32_t count = 0;
channels_t::iterator iter;
for (iter=mychannels_.begin(); iter!=mychannels_.end(); iter++)
{
Channel *c = *iter;
if (c != NULL)
if (!c->IsComplete()) // incomplete?
count++;
}
return count;
}
uint32_t FileTransfer::GetNumSeeders()
{
uint32_t count = 0;
channels_t::iterator iter;
for (iter=mychannels_.begin(); iter!=mychannels_.end(); iter++)
{
Channel *c = *iter;
if (c != NULL)
if (c->IsComplete()) // complete?
count++;
}
return count;
}
void FileTransfer::AddPeer(Address &peer)
{
Channel *c = new Channel(this,INVALID_SOCKET,peer);
}