Changeset 137


Ignore:
Timestamp:
Mar 8, 2006, 9:28:09 PM (16 years ago)
Author:
titer
Message:

Adds a permanent handle for every torrent.
Removed the limit on the number of open torrents.

Location:
branches/new_api
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/new_api/libtransmission/choking.c

    r109 r137  
    139139void tr_chokingPulse( tr_choking_t * c )
    140140{
    141     int i, peersTotalCount, unchoked, mustOptimistic = 1;
     141    int peersTotalCount, unchoked, mustOptimistic = 1;
    142142    tr_peer_t ** canChoke, ** canUnchoke;
    143143    tr_peer_t ** canChokeZero, ** canUnchokeZero;
     
    153153    /* Lock all torrents and get the total number of peers */
    154154    peersTotalCount = 0;
    155     for( i = 0; i < c->h->torrentCount; i++ )
    156     {
    157         tor = c->h->torrents[i];
     155    for( tor = c->h->torrentList; tor; tor = tor->next )
     156    {
    158157        tr_lockLock( &tor->lock );
    159158        peersTotalCount += tor->peerCount;
     
    166165    unchoked        = 0;
    167166
    168     for( i = 0; i < c->h->torrentCount; i++ )
     167    for( tor = c->h->torrentList; tor; tor = tor->next )
    169168    {
    170169        tr_peer_t * peer;
    171         int j;
    172 
    173         tor = c->h->torrents[i];
    174         for( j = 0; j < tor->peerCount; j++ )
     170        int i;
     171
     172        for( i = 0; i < tor->peerCount; i++ )
    175173        {
    176             peer = tor->peers[j];
     174            peer = tor->peers[i];
    177175
    178176            if( !tr_peerIsConnected( peer ) )
     
    321319
    322320    /* Unlock all torrents */
    323     for( i = 0; i < c->h->torrentCount; i++ )
    324     {
    325         tr_lockUnlock( &c->h->torrents[i]->lock );
     321    for( tor = c->h->torrentList; tor; tor = tor->next )
     322    {
     323        tr_lockUnlock( &tor->lock );
    326324    }
    327325
  • branches/new_api/libtransmission/internal.h

    r65 r137  
    11/******************************************************************************
    2  * Copyright (c) 2005 Eric Petit
     2 * Copyright (c) 2005-2006 Transmission authors and contributors
    33 *
    44 * Permission is hereby granted, free of charge, to any person obtaining a
     
    8383#define TR_MAX_PEER_COUNT 60
    8484
    85 typedef struct tr_torrent_s tr_torrent_t;
    8685typedef struct tr_completion_s tr_completion_t;
    8786
     
    127126    int               blockCount;
    128127   
    129 #if 0
    130     /* Status for each block
    131        -1 = we have it
    132         n = we are downloading it from n peers */
    133     char            * blockHave;
    134     int               blockHaveCount;
    135     uint8_t         * bitfield;
    136 #endif
    137128    tr_completion_t * completion;
    138129
     
    151142    uint64_t          downloaded;
    152143    uint64_t          uploaded;
     144
     145    tr_stat_t         stats[2];
     146    int               statCur;
     147
     148    tr_torrent_t    * prev;
     149    tr_torrent_t    * next;
    153150};
    154151
     
    159156{
    160157    int            torrentCount;
    161     tr_torrent_t * torrents[TR_MAX_TORRENT_COUNT];
     158    tr_torrent_t * torrentList;
    162159
    163160    tr_ratecontrol_t * upload;
  • branches/new_api/libtransmission/tracker.c

    r61 r137  
    5858static void recvAnswer ( tr_tracker_t * tc );
    5959
    60 tr_tracker_t * tr_trackerInit( tr_handle_t * h, tr_torrent_t * tor )
     60tr_tracker_t * tr_trackerInit( tr_torrent_t * tor )
    6161{
    6262    tr_tracker_t * tc;
     
    6464    tc           = calloc( 1, sizeof( tr_tracker_t ) );
    6565    tc->tor      = tor;
    66     tc->id       = h->id;
     66    tc->id       = tor->id;
    6767
    6868    tc->started  = 1;
     
    7575    tc->buf      = malloc( tc->size );
    7676
    77     tc->bindPort = h->bindPort;
     77    //tc->bindPort = h->bindPort;
     78    tc->bindPort = 9090; /*FIXME*/
    7879    tc->newPort  = -1;
    7980
  • branches/new_api/libtransmission/tracker.h

    r26 r137  
    2626typedef struct tr_tracker_s tr_tracker_t;
    2727
    28 tr_tracker_t * tr_trackerInit      ( tr_handle_t *, tr_torrent_t * );
     28tr_tracker_t * tr_trackerInit      ( tr_torrent_t * );
    2929void           tr_trackerChangePort( tr_tracker_t *, int );
    3030int            tr_trackerPulse     ( tr_tracker_t * );
  • branches/new_api/libtransmission/transmission.c

    r133 r137  
    2626 * Local prototypes
    2727 **********************************************************************/
    28 static void torrentReallyStop( tr_handle_t * h, int t );
     28static void torrentReallyStop( tr_torrent_t * );
    2929static void  downloadLoop( void * );
    3030static void  acceptLoop( void * );
     
    8686void tr_setBindPort( tr_handle_t * h, int port )
    8787{
    88     int ii, sock;
     88    int sock;
     89    tr_torrent_t * tor;
    8990
    9091    if( h->bindPort == port )
     
    107108    h->bindPort = port;
    108109
    109     for( ii = 0; ii < h->torrentCount; ii++ )
    110     {
    111         tr_lockLock( &h->torrents[ii]->lock );
    112         if( NULL != h->torrents[ii]->tracker )
    113         {
    114             tr_trackerChangePort( h->torrents[ii]->tracker, port );
    115         }
    116         tr_lockUnlock( &h->torrents[ii]->lock );
     110    for( tor = h->torrentList; tor; tor = tor->next )
     111    {
     112        tr_lockLock( &tor->lock );
     113        if( NULL != tor->tracker )
     114        {
     115            tr_trackerChangePort( tor->tracker, port );
     116        }
     117        tr_lockUnlock( &tor->lock );
    117118    }
    118119
     
    147148{
    148149    tr_torrent_t * tor;
    149     int i;
    150150
    151151    *dl = 0.0;
    152152    *ul = 0.0;
    153     for( i = 0; i < h->torrentCount; i++ )
    154     {
    155         tor = h->torrents[i];
     153    for( tor = h->torrentList; tor; tor = tor->next )
     154    {
    156155        tr_lockLock( &tor->lock );
    157156        if( tor->status & TR_STATUS_DOWNLOAD )
     
    168167 * to fill it.
    169168 **********************************************************************/
    170 int tr_torrentInit( tr_handle_t * h, const char * path )
    171 {
    172     tr_torrent_t  * tor;
     169tr_torrent_t * tr_torrentInit( tr_handle_t * h, const char * path )
     170{
     171    tr_torrent_t  * tor, * tor_tmp;
    173172    tr_info_t     * inf;
    174173    int             i;
    175174    char          * s1, * s2;
    176175
    177     if( h->torrentCount >= TR_MAX_TORRENT_COUNT )
    178     {
    179         tr_err( "Maximum number of torrents reached" );
    180         return 1;
    181     }
    182 
    183176    tor = calloc( sizeof( tr_torrent_t ), 1 );
    184177    inf = &tor->info;
     
    188181    {
    189182        free( tor );
    190         return 1;
     183        return NULL;
    191184    }
    192185
    193186    /* Make sure this torrent is not already open */
    194     for( i = 0; i < h->torrentCount; i++ )
    195     {
    196         if( !memcmp( tor->info.hash, h->torrents[i]->info.hash,
     187    for( tor_tmp = h->torrentList; tor_tmp; tor_tmp = tor_tmp->next )
     188    {
     189        if( !memcmp( tor->info.hash, tor_tmp->info.hash,
    197190                     SHA_DIGEST_LENGTH ) )
    198191        {
    199192            tr_err( "Torrent already open" );
    200193            free( tor );
    201             return 1;
     194            return NULL;
    202195        }
    203196    }
     
    247240    /* We have a new torrent */
    248241    tr_lockLock( &h->acceptLock );
    249     h->torrents[h->torrentCount] = tor;
     242    tor->prev = NULL;
     243    tor->next = h->torrentList;
     244    if( tor->next )
     245    {
     246        tor->next->prev = tor;
     247    }
     248    h->torrentList = tor;
    250249    (h->torrentCount)++;
    251250    tr_lockUnlock( &h->acceptLock );
    252251
    253     return 0;
     252    return tor;
    254253}
    255254
    256255/***********************************************************************
    257256 * tr_torrentScrape
    258  ***********************************************************************
    259  * Allocates a tr_torrent_t structure, then relies on tr_metainfoParse
    260  * to fill it.
    261  **********************************************************************/
    262 int tr_torrentScrape( tr_handle_t * h, int t, int * s, int * l )
    263 {
    264     return tr_trackerScrape( h->torrents[t], s, l );
    265 }
    266 
    267 void tr_torrentSetFolder( tr_handle_t * h, int t, const char * path )
    268 {
    269     tr_torrent_t * tor = h->torrents[t];
    270 
     257 **********************************************************************/
     258int tr_torrentScrape( tr_torrent_t * tor, int * s, int * l )
     259{
     260    return tr_trackerScrape( tor, s, l );
     261}
     262
     263void tr_torrentSetFolder( tr_torrent_t * tor, const char * path )
     264{
    271265    tor->destination = strdup( path );
    272266}
    273267
    274 char * tr_torrentGetFolder( tr_handle_t * h, int t )
    275 {
    276     tr_torrent_t * tor = h->torrents[t];
    277 
     268char * tr_torrentGetFolder( tr_torrent_t * tor )
     269{
    278270    return tor->destination;
    279271}
    280272
    281 void tr_torrentStart( tr_handle_t * h, int t )
    282 {
    283     tr_torrent_t * tor = h->torrents[t];
    284 
     273void tr_torrentStart( tr_torrent_t * tor )
     274{
    285275    if( tor->status & ( TR_STATUS_STOPPING | TR_STATUS_STOPPED ) )
    286276    {
    287277        /* Join the thread first */
    288         torrentReallyStop( h, t );
    289     }
    290 
    291     tor->status   = TR_STATUS_CHECK;
    292     tor->tracker  = tr_trackerInit( h, tor );
    293 
    294     if( 0 > h->bindPort )
    295     {
    296         tr_setBindPort( h, TR_DEFAULT_PORT );
    297     }
     278        torrentReallyStop( tor );
     279    }
     280
     281    tor->status  = TR_STATUS_CHECK;
     282    tor->tracker = tr_trackerInit( tor );
    298283
    299284    tor->date = tr_date();
     
    302287}
    303288
    304 void tr_torrentStop( tr_handle_t * h, int t )
    305 {
    306     tr_torrent_t * tor = h->torrents[t];
    307 
     289void tr_torrentStop( tr_torrent_t * tor )
     290{
    308291    tr_lockLock( &tor->lock );
    309292    tr_trackerStopped( tor->tracker );
     
    320303 * Joins the download thread and frees/closes everything related to it.
    321304 **********************************************************************/
    322 static void torrentReallyStop( tr_handle_t * h, int t )
    323 {
    324     tr_torrent_t * tor = h->torrents[t];
    325 
     305static void torrentReallyStop( tr_torrent_t * tor )
     306{
    326307    tor->die = 1;
    327308    tr_threadJoin( &tor->thread );
     
    346327}
    347328
    348 int tr_getFinished( tr_handle_t * h, int i)
    349 {
    350         return h->torrents[i]->finished;
    351 }
    352 void tr_setFinished( tr_handle_t * h, int i, int val)
    353 {
    354         h->torrents[i]->finished = val;
    355 }
    356 
    357 int tr_torrentStat( tr_handle_t * h, tr_stat_t ** stat )
     329int tr_getFinished( tr_torrent_t * tor )
     330{
     331        return tor->finished;
     332}
     333void tr_setFinished( tr_torrent_t * tor, int val)
     334{
     335        tor->finished = val;
     336}
     337
     338tr_stat_t * tr_torrentStat( tr_torrent_t * tor )
    358339{
    359340    tr_stat_t * s;
    360     tr_torrent_t * tor;
    361     tr_info_t * inf;
    362     int i, j, k, piece;
    363 
    364     if( h->torrentCount < 1 )
    365     {
    366         *stat = NULL;
    367         return 0;
    368     }
    369 
    370     s = malloc( h->torrentCount * sizeof( tr_stat_t ) );
    371 
    372     for( i = 0; i < h->torrentCount; i++ )
    373     {
    374         tor = h->torrents[i];
    375         inf = &tor->info;
    376 
    377         if( ( tor->status & TR_STATUS_STOPPED ) ||
    378             ( ( tor->status & TR_STATUS_STOPPING ) &&
    379               tr_date() > tor->stopDate + 60000 ) )
    380         {
    381             torrentReallyStop( h, i );
    382             tor->status = TR_STATUS_PAUSE;
    383         }
    384 
    385         tr_lockLock( &tor->lock );
    386 
    387         memcpy( &s[i].info, &tor->info, sizeof( tr_info_t ) );
    388         s[i].status = tor->status;
    389         memcpy( s[i].error, tor->error, sizeof( s[i].error ) );
    390 
    391         s[i].peersTotal       = 0;
    392         s[i].peersUploading   = 0;
    393         s[i].peersDownloading = 0;
    394 
     341    tr_info_t * inf = &tor->info;
     342    int i, j, piece;
     343
     344    tor->statCur = ( tor->statCur + 1 ) % 2;
     345    s = &tor->stats[tor->statCur];
     346
     347    if( ( tor->status & TR_STATUS_STOPPED ) ||
     348        ( ( tor->status & TR_STATUS_STOPPING ) &&
     349          tr_date() > tor->stopDate + 60000 ) )
     350    {
     351        torrentReallyStop( tor );
     352        tor->status = TR_STATUS_PAUSE;
     353    }
     354
     355    tr_lockLock( &tor->lock );
     356
     357    memcpy( &s->info, &tor->info, sizeof( tr_info_t ) );
     358    s->status = tor->status;
     359    memcpy( s->error, tor->error, sizeof( s->error ) );
     360
     361    s->peersTotal       = 0;
     362    s->peersUploading   = 0;
     363    s->peersDownloading = 0;
     364
     365    for( i = 0; i < tor->peerCount; i++ )
     366    {
     367        if( tr_peerIsConnected( tor->peers[i] ) )
     368        {
     369            (s->peersTotal)++;
     370            if( tr_peerIsUploading( tor->peers[i] ) )
     371            {
     372                (s->peersUploading)++;
     373            }
     374            if( tr_peerIsDownloading( tor->peers[i] ) )
     375            {
     376                (s->peersDownloading)++;
     377            }
     378        }
     379    }
     380
     381    s->progress = tr_cpCompletionAsFloat( tor->completion );
     382    if( tor->status & TR_STATUS_DOWNLOAD )
     383        s->rateDownload = tr_rcRate( tor->download );
     384    else
     385        /* tr_rcRate() doesn't make the difference between 'piece'
     386           messages and other messages, which causes a non-zero
     387           download rate even tough we are not downloading. So we
     388           force it to zero not to confuse the user. */
     389        s->rateDownload = 0.0;
     390    s->rateUpload = tr_rcRate( tor->upload );
     391   
     392    s->seeders    = tr_trackerSeeders(tor);
     393        s->leechers       = tr_trackerLeechers(tor);
     394
     395    if( s->rateDownload < 0.1 )
     396    {
     397        s->eta = -1;
     398    }
     399    else
     400    {
     401        s->eta = (float) ( 1.0 - s->progress ) *
     402            (float) inf->totalSize / s->rateDownload / 1024.0;
     403        if( s->eta > 99 * 3600 + 59 * 60 + 59 )
     404        {
     405            s->eta = -1;
     406        }
     407    }
     408
     409    for( i = 0; i < 120; i++ )
     410    {
     411        piece = i * inf->pieceCount / 120;
     412
     413        if( tr_cpPieceIsComplete( tor->completion, piece ) )
     414        {
     415            s->pieces[i] = -1;
     416            continue;
     417        }
     418
     419        s->pieces[i] = 0;
     420       
    395421        for( j = 0; j < tor->peerCount; j++ )
    396422        {
    397             if( tr_peerIsConnected( tor->peers[j] ) )
    398             {
    399                 (s[i].peersTotal)++;
    400                 if( tr_peerIsUploading( tor->peers[j] ) )
    401                 {
    402                     (s[i].peersUploading)++;
    403                 }
    404                 if( tr_peerIsDownloading( tor->peers[j] ) )
    405                 {
    406                     (s[i].peersDownloading)++;
    407                 }
    408             }
    409         }
    410 
    411         s[i].progress     = tr_cpCompletionAsFloat( tor->completion );
    412         if( tor->status & TR_STATUS_DOWNLOAD )
    413             s[i].rateDownload = tr_rcRate( tor->download );
    414         else
    415             /* tr_rcRate() doesn't make the difference between 'piece'
    416                messages and other messages, which causes a non-zero
    417                download rate even tough we are not downloading. So we
    418                force it to zero not to confuse the user. */
    419             s[i].rateDownload = 0.0;
    420         s[i].rateUpload = tr_rcRate( tor->upload );
    421        
    422         s[i].seeders      = tr_trackerSeeders(tor);
    423                 s[i].leechers     = tr_trackerLeechers(tor);
    424 
    425         if( s[i].rateDownload < 0.1 )
    426         {
    427             s[i].eta = -1;
    428         }
    429         else
    430         {
    431             s[i].eta = (float) ( 1.0 - s[i].progress ) *
    432                 (float) inf->totalSize / s[i].rateDownload / 1024.0;
    433             if( s[i].eta > 99 * 3600 + 59 * 60 + 59 )
    434             {
    435                 s[i].eta = -1;
    436             }
    437         }
    438 
    439         for( j = 0; j < 120; j++ )
    440         {
    441             piece = j * inf->pieceCount / 120;
    442 
    443             if( tr_cpPieceIsComplete( tor->completion, piece ) )
    444             {
    445                 s[i].pieces[j] = -1;
    446                 continue;
    447             }
    448 
    449             s[i].pieces[j] = 0;
    450            
    451             for( k = 0; k < tor->peerCount; k++ )
    452             {
    453                 if( tr_peerBitfield( tor->peers[k] ) &&
    454                     tr_bitfieldHas( tr_peerBitfield( tor->peers[k] ), piece ) )
    455                 {
    456                     (s[i].pieces[j])++;
    457                 }
    458             }
    459         }
    460 
    461         s[i].downloaded = tor->downloaded;
    462         s[i].uploaded   = tor->uploaded;
    463 
    464         s[i].folder = tor->destination;
    465 
    466         tr_lockUnlock( &tor->lock );
    467     }
    468 
    469     *stat = s;
    470     return h->torrentCount;
     423            if( tr_peerBitfield( tor->peers[j] ) &&
     424                tr_bitfieldHas( tr_peerBitfield( tor->peers[j] ), piece ) )
     425            {
     426                (s->pieces[i])++;
     427            }
     428        }
     429    }
     430
     431    s->downloaded = tor->downloaded;
     432    s->uploaded   = tor->uploaded;
     433
     434    s->folder = tor->destination;
     435
     436    tr_lockUnlock( &tor->lock );
     437
     438    return s;
    471439}
    472440
     
    476444 * Frees memory allocated by tr_torrentInit.
    477445 **********************************************************************/
    478 void tr_torrentClose( tr_handle_t * h, int t )
    479 {
    480     tr_torrent_t * tor = h->torrents[t];
    481     tr_info_t    * inf = &tor->info;
     446void tr_torrentClose( tr_handle_t * h, tr_torrent_t * tor )
     447{
     448    tr_info_t * inf = &tor->info;
    482449
    483450    if( tor->status & ( TR_STATUS_STOPPING | TR_STATUS_STOPPED ) )
    484451    {
    485452        /* Join the thread first */
    486         torrentReallyStop( h, t );
     453        torrentReallyStop( tor );
    487454    }
    488455
     
    503470    free( inf->pieces );
    504471    free( inf->files );
     472
     473    if( tor->prev )
     474    {
     475        tor->prev->next = tor->next;
     476    }
     477    if( tor->next )
     478    {
     479        tor->next->prev = tor->prev;
     480    }
    505481    free( tor );
    506 
    507     memmove( &h->torrents[t], &h->torrents[t+1],
    508              ( h->torrentCount - t ) * sizeof( void * ) );
    509482
    510483    tr_lockUnlock( &h->acceptLock );
     
    596569    tr_handle_t * h = _h;
    597570    uint64_t      date1, date2, lastchoke = 0;
    598     int           ii, jj;
     571    int           ii;
    599572    uint8_t     * hash;
     573    tr_torrent_t * tor;
    600574
    601575    tr_dbg( "Accept thread started" );
     
    641615            if( NULL != ( hash = tr_peerHash( h->acceptPeers[ii] ) ) )
    642616            {
    643                 for( jj = 0; jj < h->torrentCount; jj++ )
     617                for( tor = h->torrentList; tor; tor = tor->next )
    644618                {
    645                     tr_lockLock( &h->torrents[jj]->lock );
    646                     if( 0 == memcmp( h->torrents[jj]->info.hash, hash,
     619                    tr_lockLock( &tor->lock );
     620                    if( 0 == memcmp( tor->info.hash, hash,
    647621                                     SHA_DIGEST_LENGTH ) )
    648622                    {
    649                       tr_peerAttach( h->torrents[jj], h->acceptPeers[ii] );
    650                       tr_lockUnlock( &h->torrents[jj]->lock );
     623                      tr_peerAttach( tor, h->acceptPeers[ii] );
     624                      tr_lockUnlock( &tor->lock );
    651625                      goto removePeer;
    652626                    }
    653                     tr_lockUnlock( &h->torrents[jj]->lock );
     627                    tr_lockUnlock( &tor->lock );
    654628                }
    655629                tr_peerDestroy( h->fdlimit, h->acceptPeers[ii] );
  • branches/new_api/libtransmission/transmission.h

    r20 r137  
    11/******************************************************************************
    2  * Copyright (c) 2005 Eric Petit
     2 * Copyright (c) 2005-2006 Transmission authors and contributors
    33 *
    44 * Permission is hereby granted, free of charge, to any person obtaining a
     
    3737# define MAX_PATH_LENGTH      1024
    3838#endif
    39 #define TR_MAX_TORRENT_COUNT 50
    4039
    4140#define TR_DEFAULT_PORT      9090
     
    8382
    8483/***********************************************************************
    85  * tr_getFinished
    86  ***********************************************************************
    87  * Tests to see if torrent is finished
    88  **********************************************************************/
    89 int tr_getFinished( tr_handle_t *, int );
    90 
    91 /***********************************************************************
    92  * tr_setFinished
    93  ***********************************************************************
    94  * Sets the boolean value finished in the torrent back to false
    95  **********************************************************************/
    96 void tr_setFinished( tr_handle_t *, int, int );
    97 
    98 /***********************************************************************
    9984 * tr_torrentInit
    10085 ***********************************************************************
    10186 * Opens and parses torrent file at 'path'. If the file exists and is a
    102  * valid torrent file, returns 0 and adds it to the list of torrents
    103  * (but doesn't start it). Returns a non-zero value otherwise.
    104  **********************************************************************/
    105 int tr_torrentInit( tr_handle_t *, const char * path );
     87 * valid torrent file, returns an handle and adds it to the list of
     88 * torrents (but doesn't start it). Returns NULL otherwise.
     89 **********************************************************************/
     90typedef struct tr_torrent_s tr_torrent_t;
     91
     92tr_torrent_t * tr_torrentInit( tr_handle_t *, const char * path );
    10693
    10794/***********************************************************************
     
    114101 * before returning.
    115102 **********************************************************************/
    116 int tr_torrentScrape( tr_handle_t *, int, int * s, int * l );
     103int tr_torrentScrape( tr_torrent_t *, int * s, int * l );
    117104
    118105/***********************************************************************
     
    122109 * therefore tr_torrentStart returns immediately.
    123110 **********************************************************************/
    124 void   tr_torrentSetFolder( tr_handle_t *, int, const char * );
    125 char * tr_torrentGetFolder( tr_handle_t *, int );
    126 void   tr_torrentStart( tr_handle_t *, int );
     111void   tr_torrentSetFolder( tr_torrent_t *, const char * );
     112char * tr_torrentGetFolder( tr_torrent_t * );
     113void   tr_torrentStart( tr_torrent_t * );
    127114
    128115/***********************************************************************
     
    137124 *   waiting any further.
    138125 **********************************************************************/
    139 void tr_torrentStop( tr_handle_t *, int );
     126void tr_torrentStop( tr_torrent_t * );
     127
     128/***********************************************************************
     129 * tr_getFinished
     130 ***********************************************************************
     131 * Tests to see if torrent is finished
     132 **********************************************************************/
     133int tr_getFinished( tr_torrent_t * );
     134
     135/***********************************************************************
     136 * tr_setFinished
     137 ***********************************************************************
     138 * Sets the boolean value finished in the torrent back to false
     139 **********************************************************************/
     140void tr_setFinished( tr_torrent_t *, int );
     141
     142
    140143
    141144/***********************************************************************
     
    154157
    155158int tr_torrentCount( tr_handle_t * h );
    156 int tr_torrentStat( tr_handle_t *, tr_stat_t ** s );
     159tr_stat_t * tr_torrentStat( tr_torrent_t * );
    157160
    158161/***********************************************************************
     
    162165 * you must call tr_torrentStop() before closing it.
    163166 **********************************************************************/
    164 void tr_torrentClose( tr_handle_t *, int );
     167void tr_torrentClose( tr_handle_t *, tr_torrent_t * );
    165168
    166169/***********************************************************************
  • branches/new_api/transmissioncli.c

    r13 r137  
    6060int main( int argc, char ** argv )
    6161{
    62     int           i, count;
    63     tr_handle_t * h;
    64     tr_stat_t   * s;
     62    int i;
     63    tr_handle_t  * h;
     64    tr_torrent_t * tor;
     65    tr_stat_t    * s;
    6566
    6667    printf( "Transmission %s - http://transmission.m0k.org/\n\n",
     
    105106
    106107    /* Open and parse torrent file */
    107     if( tr_torrentInit( h, torrentPath ) )
     108    if( !( tor = tr_torrentInit( h, torrentPath ) ) )
    108109    {
    109110        printf( "Failed opening torrent file `%s'\n", torrentPath );
     
    115116        tr_info_t * info;
    116117
    117         count = tr_torrentStat( h, &s );
    118         info  = &s[0].info;
     118        s = tr_torrentStat( tor );
     119        info = &s->info;
    119120
    120121        /* Print torrent info (quite à la btshowmetainfo) */
     
    138139        }
    139140
    140         free( s );
    141141        goto cleanup;
    142142    }
     
    146146        int seeders, leechers;
    147147
    148         if( tr_torrentScrape( h, 0, &seeders, &leechers ) )
     148        if( tr_torrentScrape( tor, &seeders, &leechers ) )
    149149        {
    150150            printf( "Scrape failed.\n" );
     
    163163    tr_setUploadLimit( h, uploadLimit );
    164164   
    165     tr_torrentSetFolder( h, 0, "." );
    166     tr_torrentStart( h, 0 );
     165    tr_torrentSetFolder( tor, "." );
     166    tr_torrentStart( tor );
    167167
    168168    while( !mustDie )
     
    174174        sleep( 1 );
    175175
    176         count = tr_torrentStat( h, &s );
    177 
    178         if( s[0].status & TR_STATUS_CHECK )
     176        s = tr_torrentStat( tor );
     177
     178        if( s->status & TR_STATUS_CHECK )
    179179        {
    180180            chars = snprintf( string, 80,
    181                 "Checking files... %.2f %%", 100.0 * s[0].progress );
    182         }
    183         else if( s[0].status & TR_STATUS_DOWNLOAD )
     181                "Checking files... %.2f %%", 100.0 * s->progress );
     182        }
     183        else if( s->status & TR_STATUS_DOWNLOAD )
    184184        {
    185185            chars = snprintf( string, 80,
    186186                "Progress: %.2f %%, %d peer%s, dl from %d (%.2f KB/s), "
    187                 "ul to %d (%.2f KB/s)", 100.0 * s[0].progress,
    188                 s[0].peersTotal, ( s[0].peersTotal == 1 ) ? "" : "s",
    189                 s[0].peersUploading, s[0].rateDownload,
    190                 s[0].peersDownloading, s[0].rateUpload );
    191         }
    192         else if( s[0].status & TR_STATUS_SEED )
     187                "ul to %d (%.2f KB/s)", 100.0 * s->progress,
     188                s->peersTotal, ( s->peersTotal == 1 ) ? "" : "s",
     189                s->peersUploading, s->rateDownload,
     190                s->peersDownloading, s->rateUpload );
     191        }
     192        else if( s->status & TR_STATUS_SEED )
    193193        {
    194194            chars = snprintf( string, 80,
    195195                "Seeding, uploading to %d of %d peer(s), %.2f KB/s",
    196                 s[0].peersDownloading, s[0].peersTotal,
    197                 s[0].rateUpload );
     196                s->peersDownloading, s->peersTotal,
     197                s->rateUpload );
    198198        }
    199199        memset( &string[chars], ' ', 79 - chars );
     
    201201        fprintf( stderr, "\r%s", string );
    202202
    203         if( s[0].status & TR_TRACKER_ERROR )
    204         {
    205             fprintf( stderr, "\n%s\n", s[0].error );
     203        if( s->status & TR_TRACKER_ERROR )
     204        {
     205            fprintf( stderr, "\n%s\n", s->error );
    206206        }
    207207        else if( verboseLevel > 0 )
     
    210210        }
    211211       
    212         if( tr_getFinished( h, 0 ) )
    213         {
    214             tr_setFinished( h, 0, 0 );
     212        if( tr_getFinished( tor ) )
     213        {
     214            tr_setFinished( tor, 0 );
    215215            result = system(finishCall);
    216216        }
    217 
    218         free( s );
    219217    }
    220218    fprintf( stderr, "\n" );
    221219
    222220    /* Try for 5 seconds to notice the tracker that we are leaving */
    223     tr_torrentStop( h, 0 );
     221    tr_torrentStop( tor );
    224222    for( i = 0; i < 10; i++ )
    225223    {
    226         count = tr_torrentStat( h, &s );
    227         if( s[0].status & TR_STATUS_PAUSE )
     224        s = tr_torrentStat( tor );
     225        if( s->status & TR_STATUS_PAUSE )
    228226        {
    229227            /* The 'stopped' message was sent */
    230             free( s );
    231228            break;
    232229        }
    233         free( s );
    234230        usleep( 500000 );
    235231    }
    236232   
    237233cleanup:
    238     tr_torrentClose( h, 0 );
     234    tr_torrentClose( h, tor );
    239235
    240236failed:
Note: See TracChangeset for help on using the changeset viewer.