Changeset 137
- Timestamp:
- Mar 8, 2006, 9:28:09 PM (16 years ago)
- Location:
- branches/new_api
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/new_api/libtransmission/choking.c
r109 r137 139 139 void tr_chokingPulse( tr_choking_t * c ) 140 140 { 141 int i,peersTotalCount, unchoked, mustOptimistic = 1;141 int peersTotalCount, unchoked, mustOptimistic = 1; 142 142 tr_peer_t ** canChoke, ** canUnchoke; 143 143 tr_peer_t ** canChokeZero, ** canUnchokeZero; … … 153 153 /* Lock all torrents and get the total number of peers */ 154 154 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 { 158 157 tr_lockLock( &tor->lock ); 159 158 peersTotalCount += tor->peerCount; … … 166 165 unchoked = 0; 167 166 168 for( i = 0; i < c->h->torrentCount; i++)167 for( tor = c->h->torrentList; tor; tor = tor->next ) 169 168 { 170 169 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++ ) 175 173 { 176 peer = tor->peers[ j];174 peer = tor->peers[i]; 177 175 178 176 if( !tr_peerIsConnected( peer ) ) … … 321 319 322 320 /* 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 ); 326 324 } 327 325 -
branches/new_api/libtransmission/internal.h
r65 r137 1 1 /****************************************************************************** 2 * Copyright (c) 2005 Eric Petit2 * Copyright (c) 2005-2006 Transmission authors and contributors 3 3 * 4 4 * Permission is hereby granted, free of charge, to any person obtaining a … … 83 83 #define TR_MAX_PEER_COUNT 60 84 84 85 typedef struct tr_torrent_s tr_torrent_t;86 85 typedef struct tr_completion_s tr_completion_t; 87 86 … … 127 126 int blockCount; 128 127 129 #if 0130 /* Status for each block131 -1 = we have it132 n = we are downloading it from n peers */133 char * blockHave;134 int blockHaveCount;135 uint8_t * bitfield;136 #endif137 128 tr_completion_t * completion; 138 129 … … 151 142 uint64_t downloaded; 152 143 uint64_t uploaded; 144 145 tr_stat_t stats[2]; 146 int statCur; 147 148 tr_torrent_t * prev; 149 tr_torrent_t * next; 153 150 }; 154 151 … … 159 156 { 160 157 int torrentCount; 161 tr_torrent_t * torrent s[TR_MAX_TORRENT_COUNT];158 tr_torrent_t * torrentList; 162 159 163 160 tr_ratecontrol_t * upload; -
branches/new_api/libtransmission/tracker.c
r61 r137 58 58 static void recvAnswer ( tr_tracker_t * tc ); 59 59 60 tr_tracker_t * tr_trackerInit( tr_ handle_t * h, tr_torrent_t * tor )60 tr_tracker_t * tr_trackerInit( tr_torrent_t * tor ) 61 61 { 62 62 tr_tracker_t * tc; … … 64 64 tc = calloc( 1, sizeof( tr_tracker_t ) ); 65 65 tc->tor = tor; 66 tc->id = h->id;66 tc->id = tor->id; 67 67 68 68 tc->started = 1; … … 75 75 tc->buf = malloc( tc->size ); 76 76 77 tc->bindPort = h->bindPort; 77 //tc->bindPort = h->bindPort; 78 tc->bindPort = 9090; /*FIXME*/ 78 79 tc->newPort = -1; 79 80 -
branches/new_api/libtransmission/tracker.h
r26 r137 26 26 typedef struct tr_tracker_s tr_tracker_t; 27 27 28 tr_tracker_t * tr_trackerInit ( tr_ handle_t *, tr_torrent_t * );28 tr_tracker_t * tr_trackerInit ( tr_torrent_t * ); 29 29 void tr_trackerChangePort( tr_tracker_t *, int ); 30 30 int tr_trackerPulse ( tr_tracker_t * ); -
branches/new_api/libtransmission/transmission.c
r133 r137 26 26 * Local prototypes 27 27 **********************************************************************/ 28 static void torrentReallyStop( tr_ handle_t * h, int t);28 static void torrentReallyStop( tr_torrent_t * ); 29 29 static void downloadLoop( void * ); 30 30 static void acceptLoop( void * ); … … 86 86 void tr_setBindPort( tr_handle_t * h, int port ) 87 87 { 88 int ii, sock; 88 int sock; 89 tr_torrent_t * tor; 89 90 90 91 if( h->bindPort == port ) … … 107 108 h->bindPort = port; 108 109 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 ); 117 118 } 118 119 … … 147 148 { 148 149 tr_torrent_t * tor; 149 int i;150 150 151 151 *dl = 0.0; 152 152 *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 { 156 155 tr_lockLock( &tor->lock ); 157 156 if( tor->status & TR_STATUS_DOWNLOAD ) … … 168 167 * to fill it. 169 168 **********************************************************************/ 170 inttr_torrentInit( tr_handle_t * h, const char * path )171 { 172 tr_torrent_t * tor ;169 tr_torrent_t * tr_torrentInit( tr_handle_t * h, const char * path ) 170 { 171 tr_torrent_t * tor, * tor_tmp; 173 172 tr_info_t * inf; 174 173 int i; 175 174 char * s1, * s2; 176 175 177 if( h->torrentCount >= TR_MAX_TORRENT_COUNT )178 {179 tr_err( "Maximum number of torrents reached" );180 return 1;181 }182 183 176 tor = calloc( sizeof( tr_torrent_t ), 1 ); 184 177 inf = &tor->info; … … 188 181 { 189 182 free( tor ); 190 return 1;183 return NULL; 191 184 } 192 185 193 186 /* 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, 197 190 SHA_DIGEST_LENGTH ) ) 198 191 { 199 192 tr_err( "Torrent already open" ); 200 193 free( tor ); 201 return 1;194 return NULL; 202 195 } 203 196 } … … 247 240 /* We have a new torrent */ 248 241 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; 250 249 (h->torrentCount)++; 251 250 tr_lockUnlock( &h->acceptLock ); 252 251 253 return 0;252 return tor; 254 253 } 255 254 256 255 /*********************************************************************** 257 256 * 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 **********************************************************************/ 258 int tr_torrentScrape( tr_torrent_t * tor, int * s, int * l ) 259 { 260 return tr_trackerScrape( tor, s, l ); 261 } 262 263 void tr_torrentSetFolder( tr_torrent_t * tor, const char * path ) 264 { 271 265 tor->destination = strdup( path ); 272 266 } 273 267 274 char * tr_torrentGetFolder( tr_handle_t * h, int t ) 275 { 276 tr_torrent_t * tor = h->torrents[t]; 277 268 char * tr_torrentGetFolder( tr_torrent_t * tor ) 269 { 278 270 return tor->destination; 279 271 } 280 272 281 void tr_torrentStart( tr_handle_t * h, int t ) 282 { 283 tr_torrent_t * tor = h->torrents[t]; 284 273 void tr_torrentStart( tr_torrent_t * tor ) 274 { 285 275 if( tor->status & ( TR_STATUS_STOPPING | TR_STATUS_STOPPED ) ) 286 276 { 287 277 /* 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 ); 298 283 299 284 tor->date = tr_date(); … … 302 287 } 303 288 304 void tr_torrentStop( tr_handle_t * h, int t ) 305 { 306 tr_torrent_t * tor = h->torrents[t]; 307 289 void tr_torrentStop( tr_torrent_t * tor ) 290 { 308 291 tr_lockLock( &tor->lock ); 309 292 tr_trackerStopped( tor->tracker ); … … 320 303 * Joins the download thread and frees/closes everything related to it. 321 304 **********************************************************************/ 322 static void torrentReallyStop( tr_handle_t * h, int t ) 323 { 324 tr_torrent_t * tor = h->torrents[t]; 325 305 static void torrentReallyStop( tr_torrent_t * tor ) 306 { 326 307 tor->die = 1; 327 308 tr_threadJoin( &tor->thread ); … … 346 327 } 347 328 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)329 int tr_getFinished( tr_torrent_t * tor ) 330 { 331 return tor->finished; 332 } 333 void tr_setFinished( tr_torrent_t * tor, int val) 334 { 335 tor->finished = val; 336 } 337 338 tr_stat_t * tr_torrentStat( tr_torrent_t * tor ) 358 339 { 359 340 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 395 421 for( j = 0; j < tor->peerCount; j++ ) 396 422 { 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; 471 439 } 472 440 … … 476 444 * Frees memory allocated by tr_torrentInit. 477 445 **********************************************************************/ 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; 446 void tr_torrentClose( tr_handle_t * h, tr_torrent_t * tor ) 447 { 448 tr_info_t * inf = &tor->info; 482 449 483 450 if( tor->status & ( TR_STATUS_STOPPING | TR_STATUS_STOPPED ) ) 484 451 { 485 452 /* Join the thread first */ 486 torrentReallyStop( h, t);453 torrentReallyStop( tor ); 487 454 } 488 455 … … 503 470 free( inf->pieces ); 504 471 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 } 505 481 free( tor ); 506 507 memmove( &h->torrents[t], &h->torrents[t+1],508 ( h->torrentCount - t ) * sizeof( void * ) );509 482 510 483 tr_lockUnlock( &h->acceptLock ); … … 596 569 tr_handle_t * h = _h; 597 570 uint64_t date1, date2, lastchoke = 0; 598 int ii , jj;571 int ii; 599 572 uint8_t * hash; 573 tr_torrent_t * tor; 600 574 601 575 tr_dbg( "Accept thread started" ); … … 641 615 if( NULL != ( hash = tr_peerHash( h->acceptPeers[ii] ) ) ) 642 616 { 643 for( jj = 0; jj < h->torrentCount; jj++)617 for( tor = h->torrentList; tor; tor = tor->next ) 644 618 { 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, 647 621 SHA_DIGEST_LENGTH ) ) 648 622 { 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 ); 651 625 goto removePeer; 652 626 } 653 tr_lockUnlock( & h->torrents[jj]->lock );627 tr_lockUnlock( &tor->lock ); 654 628 } 655 629 tr_peerDestroy( h->fdlimit, h->acceptPeers[ii] ); -
branches/new_api/libtransmission/transmission.h
r20 r137 1 1 /****************************************************************************** 2 * Copyright (c) 2005 Eric Petit2 * Copyright (c) 2005-2006 Transmission authors and contributors 3 3 * 4 4 * Permission is hereby granted, free of charge, to any person obtaining a … … 37 37 # define MAX_PATH_LENGTH 1024 38 38 #endif 39 #define TR_MAX_TORRENT_COUNT 5040 39 41 40 #define TR_DEFAULT_PORT 9090 … … 83 82 84 83 /*********************************************************************** 85 * tr_getFinished86 ***********************************************************************87 * Tests to see if torrent is finished88 **********************************************************************/89 int tr_getFinished( tr_handle_t *, int );90 91 /***********************************************************************92 * tr_setFinished93 ***********************************************************************94 * Sets the boolean value finished in the torrent back to false95 **********************************************************************/96 void tr_setFinished( tr_handle_t *, int, int );97 98 /***********************************************************************99 84 * tr_torrentInit 100 85 *********************************************************************** 101 86 * 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 **********************************************************************/ 90 typedef struct tr_torrent_s tr_torrent_t; 91 92 tr_torrent_t * tr_torrentInit( tr_handle_t *, const char * path ); 106 93 107 94 /*********************************************************************** … … 114 101 * before returning. 115 102 **********************************************************************/ 116 int tr_torrentScrape( tr_ handle_t *, int, int * s, int * l );103 int tr_torrentScrape( tr_torrent_t *, int * s, int * l ); 117 104 118 105 /*********************************************************************** … … 122 109 * therefore tr_torrentStart returns immediately. 123 110 **********************************************************************/ 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);111 void tr_torrentSetFolder( tr_torrent_t *, const char * ); 112 char * tr_torrentGetFolder( tr_torrent_t * ); 113 void tr_torrentStart( tr_torrent_t * ); 127 114 128 115 /*********************************************************************** … … 137 124 * waiting any further. 138 125 **********************************************************************/ 139 void tr_torrentStop( tr_handle_t *, int ); 126 void tr_torrentStop( tr_torrent_t * ); 127 128 /*********************************************************************** 129 * tr_getFinished 130 *********************************************************************** 131 * Tests to see if torrent is finished 132 **********************************************************************/ 133 int tr_getFinished( tr_torrent_t * ); 134 135 /*********************************************************************** 136 * tr_setFinished 137 *********************************************************************** 138 * Sets the boolean value finished in the torrent back to false 139 **********************************************************************/ 140 void tr_setFinished( tr_torrent_t *, int ); 141 142 140 143 141 144 /*********************************************************************** … … 154 157 155 158 int tr_torrentCount( tr_handle_t * h ); 156 int tr_torrentStat( tr_handle_t *, tr_stat_t ** s);159 tr_stat_t * tr_torrentStat( tr_torrent_t * ); 157 160 158 161 /*********************************************************************** … … 162 165 * you must call tr_torrentStop() before closing it. 163 166 **********************************************************************/ 164 void tr_torrentClose( tr_handle_t *, int);167 void tr_torrentClose( tr_handle_t *, tr_torrent_t * ); 165 168 166 169 /*********************************************************************** -
branches/new_api/transmissioncli.c
r13 r137 60 60 int main( int argc, char ** argv ) 61 61 { 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; 65 66 66 67 printf( "Transmission %s - http://transmission.m0k.org/\n\n", … … 105 106 106 107 /* Open and parse torrent file */ 107 if( tr_torrentInit( h, torrentPath) )108 if( !( tor = tr_torrentInit( h, torrentPath ) ) ) 108 109 { 109 110 printf( "Failed opening torrent file `%s'\n", torrentPath ); … … 115 116 tr_info_t * info; 116 117 117 count = tr_torrentStat( h, &s);118 info = &s[0].info;118 s = tr_torrentStat( tor ); 119 info = &s->info; 119 120 120 121 /* Print torrent info (quite à la btshowmetainfo) */ … … 138 139 } 139 140 140 free( s );141 141 goto cleanup; 142 142 } … … 146 146 int seeders, leechers; 147 147 148 if( tr_torrentScrape( h, 0, &seeders, &leechers ) )148 if( tr_torrentScrape( tor, &seeders, &leechers ) ) 149 149 { 150 150 printf( "Scrape failed.\n" ); … … 163 163 tr_setUploadLimit( h, uploadLimit ); 164 164 165 tr_torrentSetFolder( h, 0, "." );166 tr_torrentStart( h, 0);165 tr_torrentSetFolder( tor, "." ); 166 tr_torrentStart( tor ); 167 167 168 168 while( !mustDie ) … … 174 174 sleep( 1 ); 175 175 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 ) 179 179 { 180 180 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 ) 184 184 { 185 185 chars = snprintf( string, 80, 186 186 "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 ) 193 193 { 194 194 chars = snprintf( string, 80, 195 195 "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 ); 198 198 } 199 199 memset( &string[chars], ' ', 79 - chars ); … … 201 201 fprintf( stderr, "\r%s", string ); 202 202 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 ); 206 206 } 207 207 else if( verboseLevel > 0 ) … … 210 210 } 211 211 212 if( tr_getFinished( h, 0) )213 { 214 tr_setFinished( h, 0, 0 );212 if( tr_getFinished( tor ) ) 213 { 214 tr_setFinished( tor, 0 ); 215 215 result = system(finishCall); 216 216 } 217 218 free( s );219 217 } 220 218 fprintf( stderr, "\n" ); 221 219 222 220 /* Try for 5 seconds to notice the tracker that we are leaving */ 223 tr_torrentStop( h, 0);221 tr_torrentStop( tor ); 224 222 for( i = 0; i < 10; i++ ) 225 223 { 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 ) 228 226 { 229 227 /* The 'stopped' message was sent */ 230 free( s );231 228 break; 232 229 } 233 free( s );234 230 usleep( 500000 ); 235 231 } 236 232 237 233 cleanup: 238 tr_torrentClose( h, 0);234 tr_torrentClose( h, tor ); 239 235 240 236 failed:
Note: See TracChangeset
for help on using the changeset viewer.