Class: Transmission::Torrent

Inherits:
Object
  • Object
show all
Defined in:
ext/r_transmission.c,
lib/transmission.rb,
ext/r_transmission.c

Overview

Summary

The Torrent class represent a torrent instance. You can use this instance start and stop downloading process and keep track of its progress.

Defined Under Namespace

Classes: FileInfo, Stat

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns true if torrent is in active state.

Returns:

  • (Boolean)


667
668
669
670
671
672
673
674
675
676
# File 'ext/r_transmission.c', line 667

static VALUE
r_torrent_active(VALUE self)
{
    r_torrent_t *torrent;
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    CHECK_TORRENT(torrent);
    
    return STARTED(torrent) ? Qtrue : Qfalse;
}

#availability(size) ⇒ Object

Use this to draw an advanced progress bar which is ‘size’ pixels wide. Returns an array of integer where each integer is set to either -1 if we have the piece, otherwise it is set to the number of connected peers who have the piece.



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
# File 'ext/r_transmission.c', line 703

static VALUE
r_torrent_availability(VALUE self, VALUE size)
{
    r_torrent_t *torrent;
    int8_t *data;
    VALUE *result_data;
    VALUE result;
    int length, i;
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    CHECK_TORRENT(torrent);
    
    length = FIX2INT(size);
    data = ALLOC_N(int8_t, length);
    result_data = ALLOC_N(VALUE, length);
    tr_torrentAvailability(torrent->handle, data, length);
    for( i = 0; i < length; i += 1 )
    {
        result_data[i] = INT2FIX(data[i]);
    }
    result = rb_ary_new4(length, result_data);
    xfree(data);
    xfree(result_data);
    return result;
}

#closed?Boolean

Returns true if torrent is closed and can’t be used anymore.

Returns:

  • (Boolean)


684
685
686
687
688
689
690
691
692
# File 'ext/r_transmission.c', line 684

static VALUE
r_torrent_closed(VALUE self)
{
    r_torrent_t *torrent;
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    
    return CLOSED(torrent) ? Qtrue : Qfalse;
}

#folder=(folder) ⇒ Object

Changes the directory wher torrent file will be stored.



604
605
606
607
608
609
610
611
612
613
614
# File 'ext/r_transmission.c', line 604

static VALUE
r_torrent_set_folder(VALUE self, VALUE file) 
{
    r_torrent_t *torrent;
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    CHECK_TORRENT(torrent);
    
    tr_torrentSetFolder(torrent->handle, StringValuePtr(file));
    return file;
}

#file_countInteger

Returns the total number of files for this torrent.

Returns:

  • (Integer)


522
523
524
525
526
# File 'ext/r_transmission.c', line 522

static VALUE
r_torrent_file_count(VALUE self) 
{
    return INT2FIX(r_torrent_info(self)->fileCount);
}

#file_nameString

Returns file name of the torrent file.

Returns:

  • (String)


387
388
389
390
391
# File 'ext/r_transmission.c', line 387

static VALUE
r_torrent_file_name(VALUE self) 
{
    return rb_str_new2(r_torrent_info(self)->torrent);
}

#filesArray

Returns the array of FileInfo.

Returns:

  • (Array)


534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'ext/r_transmission.c', line 534

static VALUE
r_torrent_files(VALUE self) 
{
    VALUE *files;
    VALUE result;
    tr_info_t *info;
    tr_file_t *f;
    int i;
    
    info = r_torrent_info(self);
    
    files = ALLOC_N(VALUE, info->fileCount);
    for( i = 0; i < info->fileCount; i += 1 )
    {
        f = ALLOC(tr_file_t);
        MEMCPY(f, &info->files[i], tr_file_t, 1);
        files[i] = Data_Wrap_Struct(cFileInfo, NULL, xfree, f);
    }
    result = rb_ary_new4(info->fileCount, files);
    xfree(files);
    return result;
}

#folderString

Returns the path where torrent files will be stored.

Returns:

  • (String)


583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'ext/r_transmission.c', line 583

static VALUE
r_torrent_get_folder(VALUE self) 
{
    r_torrent_t *torrent;
    char *folder;
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    CHECK_TORRENT(torrent);
    
    folder = tr_torrentGetFolder(torrent->handle);
    if(folder)
        return rb_str_new2(folder);
    return Qnil;
}

#folder=(folder) ⇒ Object

Changes the directory wher torrent file will be stored.



604
605
606
607
608
609
610
611
612
613
614
# File 'ext/r_transmission.c', line 604

static VALUE
r_torrent_set_folder(VALUE self, VALUE file) 
{
    r_torrent_t *torrent;
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    CHECK_TORRENT(torrent);
    
    tr_torrentSetFolder(torrent->handle, StringValuePtr(file));
    return file;
}

#hash_stringString

Returns the hash string of the torrent file.

Returns:

  • (String)


411
412
413
414
415
# File 'ext/r_transmission.c', line 411

static VALUE
r_torrent_hash_string(VALUE self) 
{
    return rb_str_new2(r_torrent_info(self)->hashString);
}

#just_finished?Boolean

The first call after a torrent is completed returns true. Returns false in other cases.

Returns:

  • (Boolean)


736
737
738
739
740
741
742
743
744
# File 'ext/r_transmission.c', line 736

static VALUE
r_torrent_just_finished(VALUE self)
{
    r_torrent_t *torrent;
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    
    return tr_getFinished(torrent->handle) ? Qtrue : Qfalse;
}

#nameString

Returns the description of the torrent file.

Returns:

  • (String)


399
400
401
402
403
# File 'ext/r_transmission.c', line 399

static VALUE
r_torrent_name(VALUE self) 
{
    return rb_str_new2(r_torrent_info(self)->name);
}

#piece_countString

Returns piece count.

Returns:

  • (String)


471
472
473
474
475
# File 'ext/r_transmission.c', line 471

static VALUE
r_torrent_piece_count(VALUE self) 
{
    return INT2FIX(r_torrent_info(self)->pieceCount);
}

#piece_sizeInteger

Returns piece size in bytes.

Returns:

  • (Integer)


459
460
461
462
463
# File 'ext/r_transmission.c', line 459

static VALUE
r_torrent_piece_size(VALUE self) 
{
    return INT2FIX(r_torrent_info(self)->pieceSize);
}

#piecesArray

Returns the array of pieces.

Returns:

  • (Array)


495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'ext/r_transmission.c', line 495

static VALUE
r_torrent_pieces(VALUE self) 
{
    VALUE *pieces;
    tr_info_t *info;
    VALUE result;
    int i;
    
    info = r_torrent_info(self);
    
    pieces = ALLOC_N(VALUE, info->pieceCount);
    for( i = 0; i < info->pieceCount; i += 1 )
    {
        pieces[i] = INT2FIX(info->pieces[i]);
    }
    
    result = rb_ary_new4(info->pieceCount, pieces);
    xfree(pieces);
    return result;
}

#remove(saved = false) ⇒ nil

Removes torrent from the current session. If ‘saved’ parameter is true, it also deletes a private torrent copy if there is one.

Returns:

  • (nil)


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'ext/r_transmission.c', line 313

static VALUE
r_torrent_remove(int argc, VALUE *argv, VALUE self)
{
    r_transmission_t *transmission;
    r_torrent_t *torrent;
    VALUE remove_saved = Qfalse;
    
    rb_scan_args(argc, argv, "01", &remove_saved);
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    CHECK_TORRENT(torrent);
    Data_Get_Struct(torrent->transmission, r_transmission_t, transmission);
    if(RTEST(remove_saved))
        tr_torrentRemoveSaved(torrent->handle);
    rb_ary_delete(transmission->torrents, self);
    r_torrent_close(torrent);
    return Qnil;
}

#scrapeArray

Scrapes the tracker and returns a number of seeders and leechers.

Returns:

  • (Array)


354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'ext/r_transmission.c', line 354

static VALUE
r_torrent_scrape(VALUE self)
{
    r_torrent_t *torrent;
    int seeders, leechers;
    int ret_code;
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    CHECK_TORRENT(torrent);
    
    if((ret_code = tr_torrentScrape(torrent->handle, &seeders, &leechers)) != 0)
    {
        rb_raise(eTorrentError, r_torrent_scrape_error(ret_code));
    }
    return rb_ary_new3(2, INT2FIX(seeders), INT2FIX(leechers));
}

#total_sizeString

Returns the total size of all pieces in bytes.

Returns:

  • (String)


483
484
485
486
487
# File 'ext/r_transmission.c', line 483

static VALUE
r_torrent_total_size(VALUE self) 
{
    return ULONG2NUM(r_torrent_info(self)->totalSize);
}

#startnil

Starts the torrent downloading. The download is launched in a seperate thread, therefore torrent.start returns immediately.

Returns:

  • (nil)


623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'ext/r_transmission.c', line 623

static VALUE
r_torrent_start(VALUE self) 
{
    r_torrent_t *torrent;
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    CHECK_TORRENT(torrent);
    
    if(!STARTED(torrent))
    {
        tr_torrentStart(torrent->handle);
    }
    
    return Qnil;
}

#statStat

Returns the stat of this torrent.

Returns:



563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'ext/r_transmission.c', line 563

static VALUE
r_torrent_stat(VALUE self) 
{
    r_torrent_t *torrent;
    tr_stat_t *stat;
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    CHECK_TORRENT(torrent);
    
    stat = ALLOC(tr_stat_t);
    MEMCPY(stat, tr_torrentStat(torrent->handle), tr_stat_t, 1);
    return Data_Wrap_Struct(cStat, NULL, xfree, stat);
}

#stopnil

Stops the torrent downloading.

Returns:

  • (nil)


645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'ext/r_transmission.c', line 645

static VALUE
r_torrent_stop(VALUE self) 
{
    r_torrent_t *torrent;
    
    Data_Get_Struct(self, r_torrent_t, torrent);
    CHECK_TORRENT(torrent);
    
    if(STARTED(torrent))
    {
        tr_torrentStop(torrent->handle);
    }
    
    return Qnil;
}

#to_sObject



21
22
23
24
# File 'lib/transmission.rb', line 21

def to_s
  format = "Torrent: %s\nHash:    %s\nStatus:  %s"
  format % [name, hash_string, stat.status_name, ]
end

#total_sizeString

Returns the total size of all pieces in bytes.

Returns:

  • (String)


483
484
485
486
487
# File 'ext/r_transmission.c', line 483

static VALUE
r_torrent_total_size(VALUE self) 
{
    return ULONG2NUM(r_torrent_info(self)->totalSize);
}

#tracker_addressString

Returns the tracker address.

Returns:

  • (String)


423
424
425
426
427
# File 'ext/r_transmission.c', line 423

static VALUE
r_torrent_tracker_address(VALUE self) 
{
    return rb_str_new2(r_torrent_info(self)->trackerAddress);
}

#tracker_addressString

Returns the tracker address.

Returns:

  • (String)


423
424
425
426
427
# File 'ext/r_transmission.c', line 423

static VALUE
r_torrent_tracker_address(VALUE self) 
{
    return rb_str_new2(r_torrent_info(self)->trackerAddress);
}

#tracker_announceString

Returns the tracker announce address.

Returns:

  • (String)


447
448
449
450
451
# File 'ext/r_transmission.c', line 447

static VALUE
r_torrent_tracker_announce(VALUE self) 
{
    return rb_str_new2(r_torrent_info(self)->trackerAnnounce);
}

#tracker_announceString

Returns the tracker announce address.

Returns:

  • (String)


447
448
449
450
451
# File 'ext/r_transmission.c', line 447

static VALUE
r_torrent_tracker_announce(VALUE self) 
{
    return rb_str_new2(r_torrent_info(self)->trackerAnnounce);
}

#tracker_portInteger

Returns the tracker port.

Returns:

  • (Integer)


435
436
437
438
439
# File 'ext/r_transmission.c', line 435

static VALUE
r_torrent_tracker_port(VALUE self) 
{
    return INT2FIX(r_torrent_info(self)->trackerPort);
}