forked from libswift/libswift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.cpp
769 lines (620 loc) · 17.9 KB
/
api.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
* api.cpp
* Swift top-level API implementation
*
* Created by Victor Grishchenko on 3/6/09.
* Copyright 2009-2016 TECHNISCHE UNIVERSITEIT DELFT. All rights reserved.
*
*/
#include "swift.h"
#include "swarmmanager.h"
using namespace std;
using namespace swift;
#define api_debug false
/*
* Local functions
*/
void StartLibraryCleanup()
{
if (ContentTransfer::cleancounter == 0)
{
// Arno, 2012-10-01: Per-library timer for cleanup on transfers
evtimer_assign(&ContentTransfer::evclean,Channel::evbase,&ContentTransfer::LibeventGlobalCleanCallback,NULL);
evtimer_add(&ContentTransfer::evclean,tint2tv(TINT_SEC));
ContentTransfer::cleancounter = 481;
}
}
/*
* Global Operations
*/
int swift::Listen( Address addr)
{
if (api_debug)
fprintf(stderr,"swift::Listen addr %s\n", addr.str().c_str() );
StartLibraryCleanup();
sckrwecb_t cb;
cb.may_read = &Channel::LibeventReceiveCallback;
cb.sock = Channel::Bind(addr,cb);
// swift UDP receive
event_assign(&Channel::evrecv, Channel::evbase, cb.sock, EV_READ,
cb.may_read, NULL);
event_add(&Channel::evrecv, NULL);
return cb.sock;
}
void swift::Shutdown()
{
if (api_debug)
fprintf(stderr,"swift::Shutdown");
Channel::Shutdown();
}
/*
* Per-Swarm Operations
*/
int swift::Open( std::string filename, const Sha1Hash& hash, Address tracker, bool force_check_diskvshash, bool check_netwvshash, bool zerostate, bool activate, uint32_t chunk_size)
{
if (api_debug)
fprintf(stderr,"swift::Open %s hash %s track %s cdisk %d cnet %d zs %d act %d cs %u\n", filename.c_str(), hash.hex().c_str(), tracker.str().c_str(), force_check_diskvshash, check_netwvshash, zerostate, activate, chunk_size );
SwarmData* swarm = SwarmManager::GetManager().AddSwarm( filename, hash, tracker, force_check_diskvshash, check_netwvshash, zerostate, activate, chunk_size );
if (swarm == NULL)
return -1;
else
return swarm->Id();
}
void swift::Close( int td, bool removestate, bool removecontent ) {
if (api_debug)
fprintf(stderr,"swift::Close td %d rems %d remc%d\n", td, (int)removestate, (int)removecontent );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td );
if (swarm)
SwarmManager::GetManager().RemoveSwarm( swarm->RootHash(), removestate, removecontent );
//LIVE
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt != NULL)
delete lt;
}
int swift::Find(const Sha1Hash& swarmid, bool activate)
{
if (api_debug)
fprintf(stderr,"swift::Find %s act %d\n", swarmid.hex().c_str(), (int)activate );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(swarmid);
if (swarm==NULL)
{
//LIVE
LiveTransfer *lt = LiveTransfer::FindBySwarmID(swarmid);
if (lt == NULL)
return -1;
else
return lt->td();
}
else
{
if (activate)
SwarmManager::GetManager().ActivateSwarm(swarm->RootHash());
return swarm->Id();
}
}
ContentTransfer *swift::GetActivatedTransfer(int td)
{
if (api_debug)
fprintf(stderr,"swift::GetActivatedTransfer td %d\n", td );
ContentTransfer *ct = NULL;
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(td);
if (swarm == NULL)
ct = (ContentTransfer *)LiveTransfer::FindByTD(td);
else
ct = swarm->GetTransfer(false); // Arno: do not activate if not already
return ct;
}
// Local method
static ContentTransfer *FindActivateTransferByTD(int td)
{
ContentTransfer *ct = NULL;
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(td);
if (swarm == NULL)
//LIVE
ct = (ContentTransfer *)LiveTransfer::FindByTD(td);
else
{
if (!swarm->Touch()) {
swarm = SwarmManager::GetManager().ActivateSwarm( swarm->RootHash() );
if (swarm == NULL)
return NULL;
if (!swarm->Touch())
return NULL;
}
ct = swarm->GetTransfer();
}
return ct;
}
ssize_t swift::Read( int td, void *buf, size_t nbyte, int64_t offset )
{
if (api_debug)
fprintf(stderr,"swift::Read td %d buf %p n " PRISIZET " o %lld\n", td, buf, nbyte, offset );
ContentTransfer *ct = FindActivateTransferByTD(td);
if (ct == NULL)
return -1;
else
return ct->GetStorage()->Read(buf, nbyte, offset);
}
ssize_t swift::Write( int td, const void *buf, size_t nbyte, int64_t offset )
{
if (api_debug)
fprintf(stderr,"swift::Write td %d buf %p n " PRISIZET " o %lld\n", td, buf, nbyte, offset );
ContentTransfer *ct = FindActivateTransferByTD(td);
if (ct == NULL)
return -1;
else
return ct->GetStorage()->Write(buf, nbyte, offset);
}
/*
* Swarm Info
*/
uint64_t swift::Size(int td)
{
if (api_debug)
fprintf(stderr,"swift::Size td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(td);
if (swarm == NULL)
return 0; //also for LIVE
return swarm->Size();
}
bool swift::IsComplete(int td)
{
if (api_debug)
fprintf(stderr,"swift::IsComplete td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(td);
if (swarm == NULL)
return false; //also for LIVE
return swarm->IsComplete();
}
uint64_t swift::Complete(int td)
{
if (api_debug)
fprintf(stderr,"swift::Complete td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(td);
if (swarm == NULL)
return 0; //also for LIVE
return swarm->Complete();
}
uint64_t swift::SeqComplete( int td, int64_t offset )
{
if (api_debug)
fprintf(stderr,"swift::SeqComplete td %d o %ld\n", td, offset );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(td);
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return 0;
else
return lt->SeqComplete(); // No range support for live
}
else
{
return swarm->SeqComplete(offset);
}
}
const Sha1Hash& swift::SwarmID(int td)
{
if (api_debug)
fprintf(stderr,"swift::SwarmID td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td );
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return Sha1Hash::ZERO;
else
return lt->swarm_id();
}
else
return swarm->RootHash();
}
/** Returns the number of bytes in a chunk for this transmission */
uint32_t swift::ChunkSize( int td)
{
if (api_debug)
fprintf(stderr,"swift::ChunkSize td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td);
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return 0;
else
return lt->chunk_size();
}
else
return swarm->ChunkSize();
}
tdlist_t swift::GetTransferDescriptors()
{
if (api_debug)
fprintf(stderr,"swift::GetTransferDescriptors\n" );
tdlist_t filetdl = SwarmManager::GetManager().GetTransferDescriptors();
tdlist_t livetdl = LiveTransfer::GetTransferDescriptors();
filetdl.insert(filetdl.end(),livetdl.begin(),livetdl.end()); // append
return filetdl;
}
void swift::SetMaxSpeed(int td, data_direction_t ddir, double speed)
{
if (api_debug)
fprintf(stderr,"swift::SetMaxSpeed td %d dir %d speed %lf\n", td, (int)ddir, speed );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td );
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return;
else
{
// Arno, 2012-05-25: SetMaxSpeed resets the current speed history, so
// be careful here.
if( lt->GetMaxSpeed( ddir ) != speed )
lt->SetMaxSpeed( ddir, speed );
}
}
else
swarm->SetMaxSpeed(ddir,speed); // checks current set speed beforehand
}
double swift::GetCurrentSpeed(int td, data_direction_t ddir)
{
if (api_debug)
fprintf(stderr,"swift::GetCurrentSpeed td %d ddir %d\n", td, (int)ddir );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td );
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return -1.0;
else
return lt->GetCurrentSpeed(ddir);
}
else
{
FileTransfer *ft = swarm->GetTransfer(false); // Arno: do not activate for this
if (!ft)
return -1.0;
else
return ft->GetCurrentSpeed(ddir);
}
}
uint32_t swift::GetNumSeeders(int td)
{
if (api_debug)
fprintf(stderr,"swift::GetNumSeeders td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td );
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return 0;
else
return lt->GetNumSeeders();
}
else
{
FileTransfer *ft = swarm->GetTransfer(false); // Arno: do not activate for this
if (!ft)
return 0;
else
return ft->GetNumSeeders();
}
}
uint32_t swift::GetNumLeechers(int td)
{
if (api_debug)
fprintf(stderr,"swift::GetNumLeechers td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td );
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return 0;
else
return lt->GetNumLeechers();
}
else
{
FileTransfer *ft = swarm->GetTransfer(false); // Arno: do not activate for this
if (!ft)
return 0;
else
return ft->GetNumLeechers();
}
}
transfer_t swift::ttype(int td)
{
//if (api_debug)
// fprintf(stderr,"swift::ttype td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td );
if (swarm == NULL)
return LIVE_TRANSFER; // approx of truth
else
return FILE_TRANSFER;
}
Storage *swift::GetStorage(int td)
{
if (api_debug)
fprintf(stderr,"swift::GetStorage td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td );
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return NULL;
else
return lt->GetStorage();
}
else
{
FileTransfer *ft = swarm->GetTransfer(); // Must activate for this
if (!ft)
return NULL;
else
return ft->GetStorage();
}
}
std::string swift::GetOSPathName(int td)
{
if (api_debug)
fprintf(stderr,"swift::GetOSPathName td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td );
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL || lt->GetStorage() == NULL)
return "";
else
return lt->GetStorage()->GetOSPathName();
}
else
return swarm->OSPathName();
}
bool swift::IsOperational(int td)
{
if (api_debug)
fprintf(stderr,"swift::IsOperational td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td );
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return false;
else
return lt->IsOperational();
}
else
{
FileTransfer *ft = swarm->GetTransfer(false); // Arno: do not activate for this
if (!ft)
return false;
else
return ft->IsOperational();
}
}
bool swift::IsZeroState(int td)
{
if (api_debug)
fprintf(stderr,"swift::IsZeroState td %d\n", td );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm( td );
if (swarm == NULL)
return false;
else
return swarm->IsZeroState();
}
//CHECKPOINT
int swift::Checkpoint(int td)
{
if (api_debug)
fprintf(stderr,"swift::Checkpoint td %d\n", td );
// If file, save transfer's binmap for zero-hashcheck restart
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(td);
if (swarm == NULL)
return -1; // also for LIVE
FileTransfer *ft = swarm->GetTransfer(false);
if (ft == NULL)
return -1; // not activated
if (ft->IsZeroState())
return -1;
MmapHashTree *ht = (MmapHashTree *)ft->hashtree();
if (ht == NULL)
{
fprintf(stderr,"swift: checkpointing: ht is NULL\n");
return -1;
}
std::string binmap_filename = ft->GetStorage()->GetOSPathName();
binmap_filename.append(".mbinmap");
//fprintf(stderr,"swift: HACK checkpointing %s at %lli\n", binmap_filename.c_str(), Complete(td));
FILE *fp = fopen_utf8(binmap_filename.c_str(),"wb");
if (!fp) {
print_error("cannot open mbinmap for writing");
return -1;
}
int ret = ht->serialize(fp);
if (ret < 0)
print_error("writing to mbinmap");
fclose(fp);
return ret;
}
// SEEK
int swift::Seek(int td, int64_t offset, int whence)
{
if (api_debug)
fprintf(stderr,"swift::Seek td %d o %ld w %d\n", td, offset, whence );
dprintf("%s F%d Seek: to %ld\n",tintstr(), td, offset );
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(td);
if (swarm == NULL)
return -1; // also for LIVE
// Quick fail in order not to activate a swarm only to fail after activation
if( whence != SEEK_SET ) // TODO other
return -1;
if( offset >= swift::Size(td) )
return -1;
if( !swarm->Touch() ) {
swarm = SwarmManager::GetManager().ActivateSwarm( swarm->RootHash() );
if (swarm == NULL)
return -1;
if (!swarm->Touch())
return -1;
}
FileTransfer *ft = swarm->GetTransfer();
// whence == SEEK_SET && offset < swift::Size(td) - validated by quick fail above
// Which bin to seek to?
int64_t coff = offset - (offset % ft->hashtree()->chunk_size()); // ceil to chunk
bin_t offbin = bin_t(0,coff/ft->hashtree()->chunk_size());
dprintf("%s F%i Seek: to bin %s\n",tintstr(), td, offbin.str().c_str() );
return ft->picker()->Seek(offbin,whence);
}
void swift::AddPeer(Address& addr, const Sha1Hash& swarmid)
{
if (api_debug)
fprintf(stderr,"swift::AddPeer addr %s hash %s\n", addr.str().c_str(), swarmid.hex().c_str() );
ContentTransfer *ct = NULL;
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(swarmid);
if (swarm == NULL)
ct = (ContentTransfer *)LiveTransfer::FindBySwarmID(swarmid);
else
{
if (!swarm->Touch()) {
swarm = SwarmManager::GetManager().ActivateSwarm(swarmid);
if (swarm == NULL)
return;
if (!swarm->Touch())
return;
}
ct = (ContentTransfer *)swarm->GetTransfer();
}
if (ct == NULL)
return;
else
ct->AddPeer(addr);
// FIXME: When cached addresses are supported in swapped-out swarms, add the peer to that cache instead
}
/*
* Progress Monitoring
*/
void swift::AddProgressCallback(int td,ProgressCallback cb,uint8_t agg)
{
if (api_debug)
fprintf(stderr,"swift::AddProgressCallback: td %d\n", td);
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(td);
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return;
else
lt->AddProgressCallback(cb,agg);
return;
}
else
swarm->AddProgressCallback( cb, agg );
//fprintf(stderr,"swift::AddProgressCallback: swarm obj %p %p\n", swarm, cb );
}
void swift::RemoveProgressCallback(int td, ProgressCallback cb)
{
if (api_debug)
fprintf(stderr,"swift::RemoveProgressCallback: td %d\n", td);
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(td);
if (swarm == NULL)
{
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return;
else
lt->RemoveProgressCallback(cb);
return;
}
else
swarm->RemoveProgressCallback(cb);
}
/*
* Offline hash checking. Writes .mhash and .mbinmap file for the specified
* content filename.
*
* MUST NOT use any swift global variables!
*/
int swift::HashCheckOffline( std::string filename, Sha1Hash *calchashptr, uint32_t chunk_size)
{
if (api_debug)
fprintf(stderr,"swift::HashCheckOffline %s hashptr %p cs %u\n", filename.c_str(), calchashptr, chunk_size );
// From transfer.cpp::FileTransfer constructor
std::string destdir = dirname_utf8(filename);
if (destdir == "")
destdir = ".";
// MULTIFILE
Storage *storage_ = new Storage(filename,destdir,-1);
std::string hash_filename;
hash_filename.assign(filename);
hash_filename.append(".mhash");
std::string binmap_filename;
binmap_filename.assign(filename);
binmap_filename.append(".mbinmap");
MmapHashTree *hashtree_ = new MmapHashTree(storage_,Sha1Hash::ZERO,chunk_size,hash_filename,true,true,binmap_filename);
FILE *fp = fopen_utf8(binmap_filename.c_str(),"wb");
if (!fp) {
print_error("cannot open mbinmap for writing");
return -1;
}
int ret = hashtree_->serialize(fp);
if (ret < 0)
print_error("writing to mbinmap");
fclose(fp);
*calchashptr = hashtree_->root_hash();
return ret;
}
/*
* LIVE
*/
LiveTransfer *swift::LiveCreate(std::string filename, const Sha1Hash& swarmid, uint32_t chunk_size)
{
if (api_debug)
fprintf(stderr,"swift::LiveCreate %s hash %s cs %u\n", filename.c_str(), swarmid.hex().c_str(), chunk_size );
// Arno: LIVE streams are not managed by SwarmManager
fprintf(stderr,"swift::LiveCreate: swarmid: %s\n",swarmid.hex().c_str() );
LiveTransfer *lt = new LiveTransfer(filename,swarmid,true,chunk_size);
if (lt->IsOperational())
return lt;
else
{
fprintf(stderr,"swift::LiveCreate: %s swarm created, but not operational\n",swarmid.hex().c_str() );
delete lt;
return NULL;
}
}
int swift::LiveWrite(LiveTransfer *lt, const void *buf, size_t nbyte)
{
//if (api_debug)
// fprintf(stderr,"swift::LiveWrite lt %p buf %p n " PRISIZET "\n", lt, buf. nbyte );
return lt->AddData(buf,nbyte);
}
int swift::LiveOpen(std::string filename, const Sha1Hash& swarmid, Address tracker, bool check_netwvshash, uint32_t chunk_size)
{
if (api_debug)
fprintf(stderr,"swift::LiveOpen %s hash %s addr %s cnet %d cs %u\n", filename.c_str(), swarmid.hex().c_str(), tracker.str().c_str(), check_netwvshash, chunk_size );
LiveTransfer *lt = new LiveTransfer(filename,swarmid,false,chunk_size);
// initiate tracker connections
// SWIFTPROC
lt->SetTracker(tracker);
lt->ConnectToTracker();
return lt->td();
}
uint64_t swift::GetHookinOffset(int td)
{
if (api_debug)
fprintf(stderr,"swift::GetHookinOffset td %d\n", td );
LiveTransfer *lt = LiveTransfer::FindByTD(td);
if (lt == NULL)
return 0; // also for FileTransfer
else
return lt->GetHookinOffset();
}
// Called from sendrecv.cpp
void swift::Touch(int td)
{
if (api_debug)
fprintf(stderr,"swift::Touch: td %d\n", td);
SwarmData* swarm = SwarmManager::GetManager().FindSwarm(td);
if (swarm != NULL)
swarm->Touch();
}