2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "completion.h"
|
|
|
|
|
|
|
|
struct tr_completion_s
|
|
|
|
{
|
2007-06-30 21:11:17 +00:00
|
|
|
tr_torrent_t * tor;
|
|
|
|
|
|
|
|
/* number of peers from whom we've requested this block */
|
|
|
|
uint8_t * blockDownloaders;
|
|
|
|
|
|
|
|
/* do we have this block? */
|
|
|
|
tr_bitfield_t * blockBitfield;
|
|
|
|
|
|
|
|
/* do we have this piece? */
|
|
|
|
tr_bitfield_t * pieceBitfield;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
/* a block is complete if and only if we have it */
|
|
|
|
int * completeBlocks;
|
|
|
|
};
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_completion_t * tr_cpInit( tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
tr_completion_t * cp;
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
cp = tr_new( tr_completion_t, 1 );
|
2006-07-16 19:39:23 +00:00
|
|
|
cp->tor = tor;
|
2007-03-05 23:03:38 +00:00
|
|
|
cp->blockBitfield = tr_bitfieldNew( tor->blockCount );
|
2007-06-27 14:54:31 +00:00
|
|
|
cp->blockDownloaders = tr_new( uint8_t, tor->blockCount );
|
2007-03-05 23:03:38 +00:00
|
|
|
cp->pieceBitfield = tr_bitfieldNew( tor->info.pieceCount );
|
2007-06-27 14:54:31 +00:00
|
|
|
cp->completeBlocks = tr_new( int, tor->info.pieceCount );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_cpReset( cp );
|
|
|
|
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_cpClose( tr_completion_t * cp )
|
|
|
|
{
|
2007-06-27 14:54:31 +00:00
|
|
|
tr_free( cp->completeBlocks );
|
2007-03-05 23:03:38 +00:00
|
|
|
tr_bitfieldFree( cp->pieceBitfield );
|
2007-06-27 14:54:31 +00:00
|
|
|
tr_free( cp->blockDownloaders );
|
|
|
|
tr_bitfieldFree( cp->blockBitfield );
|
|
|
|
tr_free( cp );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_cpReset( tr_completion_t * cp )
|
|
|
|
{
|
|
|
|
tr_torrent_t * tor = cp->tor;
|
|
|
|
int i;
|
|
|
|
|
2007-03-05 23:03:38 +00:00
|
|
|
tr_bitfieldClear( cp->blockBitfield );
|
2006-07-16 19:39:23 +00:00
|
|
|
memset( cp->blockDownloaders, 0, tor->blockCount );
|
2007-03-05 23:03:38 +00:00
|
|
|
tr_bitfieldClear( cp->pieceBitfield );
|
2007-06-30 12:54:26 +00:00
|
|
|
for( i = 0; i < tor->info.pieceCount; ++i )
|
2007-06-18 03:40:41 +00:00
|
|
|
cp->completeBlocks[i] = 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
int tr_cpPieceHasAllBlocks( const tr_completion_t * cp, int piece )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
return tr_cpPieceIsComplete( cp, piece );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
int tr_cpPieceIsComplete( const tr_completion_t * cp, int piece )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-25 01:59:46 +00:00
|
|
|
return cp->completeBlocks[piece] >= TR_BLOCKS_IN_PIECE(cp->tor,piece);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
const tr_bitfield_t * tr_cpPieceBitfield( const tr_completion_t * cp )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return cp->pieceBitfield;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_cpPieceAdd( tr_completion_t * cp, int piece )
|
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
const tr_torrent_t * tor = cp->tor;
|
2007-07-25 01:59:46 +00:00
|
|
|
const int n_blocks = TR_BLOCKS_IN_PIECE(tor,piece);
|
|
|
|
const int startBlock = TOR_PIECE_FIRST_BLOCK(tor,piece);
|
2007-06-18 03:40:41 +00:00
|
|
|
const int endBlock = startBlock + n_blocks;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
cp->completeBlocks[piece] = n_blocks;
|
2007-07-15 03:52:51 +00:00
|
|
|
tr_bitfieldAddRange( cp->blockBitfield, startBlock, endBlock );
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_bitfieldAdd( cp->pieceBitfield, piece );
|
|
|
|
}
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
void tr_cpPieceRem( tr_completion_t * cp, int piece )
|
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
const tr_torrent_t * tor = cp->tor;
|
2007-07-25 01:59:46 +00:00
|
|
|
const int n_blocks = TR_BLOCKS_IN_PIECE(tor,piece);
|
|
|
|
const int startBlock = TOR_PIECE_FIRST_BLOCK(tor,piece);
|
2007-06-18 03:40:41 +00:00
|
|
|
const int endBlock = startBlock + n_blocks;
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-07-24 20:49:47 +00:00
|
|
|
assert( cp != NULL );
|
|
|
|
assert( 0 <= piece );
|
|
|
|
assert( piece < tor->info.pieceCount );
|
|
|
|
assert( 0 <= startBlock );
|
|
|
|
assert( startBlock < tor->blockCount );
|
|
|
|
assert( startBlock <= endBlock );
|
2007-07-25 01:59:46 +00:00
|
|
|
assert( endBlock <= tor->blockCount );
|
2007-07-24 20:49:47 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
cp->completeBlocks[piece] = 0;
|
2007-07-15 03:52:51 +00:00
|
|
|
tr_bitfieldRemRange ( cp->blockBitfield, startBlock, endBlock );
|
2007-01-14 12:00:21 +00:00
|
|
|
tr_bitfieldRem( cp->pieceBitfield, piece );
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
/* Blocks */
|
|
|
|
void tr_cpDownloaderAdd( tr_completion_t * cp, int block )
|
|
|
|
{
|
2007-06-30 12:54:26 +00:00
|
|
|
++cp->blockDownloaders[block];
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_cpDownloaderRem( tr_completion_t * cp, int block )
|
|
|
|
{
|
2007-06-30 12:54:26 +00:00
|
|
|
--cp->blockDownloaders[block];
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-09 15:36:46 +00:00
|
|
|
int tr_cpBlockIsComplete( const tr_completion_t * cp, int block )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return tr_bitfieldHas( cp->blockBitfield, block );
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_cpBlockAdd( tr_completion_t * cp, int block )
|
|
|
|
{
|
2007-07-25 01:59:46 +00:00
|
|
|
const tr_torrent_t * tor = cp->tor;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( !tr_cpBlockIsComplete( cp, block ) )
|
|
|
|
{
|
2007-07-25 01:59:46 +00:00
|
|
|
const int piece = TOR_BLOCK_PIECE(tor, block);
|
2007-07-24 11:50:29 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
++cp->completeBlocks[piece];
|
|
|
|
|
2007-07-25 01:59:46 +00:00
|
|
|
if( cp->completeBlocks[piece] == TR_BLOCKS_IN_PIECE(tor,piece) )
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_bitfieldAdd( cp->pieceBitfield, piece );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_bitfieldAdd( cp->blockBitfield, block );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
const tr_bitfield_t * tr_cpBlockBitfield( const tr_completion_t * cp )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
assert( cp != NULL );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
return cp->blockBitfield;
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
void
|
|
|
|
tr_cpBlockBitfieldSet( tr_completion_t * cp, tr_bitfield_t * bitfield )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
int i;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
assert( cp != NULL );
|
|
|
|
assert( bitfield != NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_cpReset( cp );
|
|
|
|
|
|
|
|
for( i=0; i < cp->tor->blockCount; ++i )
|
|
|
|
if( tr_bitfieldHas( bitfield, i ) )
|
|
|
|
tr_cpBlockAdd( cp, i );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
float tr_cpPercentBlocksInPiece( const tr_completion_t * cp, int piece )
|
2006-10-12 23:34:20 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
assert( cp != NULL );
|
2006-10-13 00:23:20 +00:00
|
|
|
|
2007-07-25 01:59:46 +00:00
|
|
|
return cp->completeBlocks[piece] / (double)TR_BLOCKS_IN_PIECE(cp->tor,piece);
|
2006-10-12 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2007-06-30 12:54:26 +00:00
|
|
|
int
|
|
|
|
tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
const tr_torrent_t * tor = cp->tor;
|
2007-07-25 01:59:46 +00:00
|
|
|
const int start = TOR_PIECE_FIRST_BLOCK(tor,piece);
|
|
|
|
const int end = start + TR_BLOCKS_IN_PIECE(tor,piece);
|
2007-06-30 12:54:26 +00:00
|
|
|
|
|
|
|
n = 0;
|
|
|
|
for( i = start; i < end; ++i )
|
|
|
|
if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] )
|
|
|
|
++n;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-06-09 15:36:46 +00:00
|
|
|
int tr_cpMissingBlockInPiece( const tr_completion_t * cp, int piece )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
int i;
|
2007-06-09 15:36:46 +00:00
|
|
|
const tr_torrent_t * tor = cp->tor;
|
2007-07-25 01:59:46 +00:00
|
|
|
const int start = TOR_PIECE_FIRST_BLOCK(tor,piece);
|
|
|
|
const int end = start + TR_BLOCKS_IN_PIECE(tor,piece);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
for( i = start; i < end; ++i )
|
|
|
|
if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] )
|
|
|
|
return i;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
cp_status_t
|
|
|
|
tr_cpGetStatus ( const tr_completion_t * cp )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = TR_CP_COMPLETE;
|
|
|
|
const tr_info_t * info;
|
|
|
|
|
|
|
|
assert( cp != NULL );
|
|
|
|
assert( cp->tor != NULL );
|
|
|
|
|
|
|
|
info = &cp->tor->info;
|
|
|
|
for( i=0; i<info->pieceCount; ++i ) {
|
|
|
|
if( tr_cpPieceIsComplete( cp, i ) )
|
|
|
|
continue;
|
2007-07-10 03:12:46 +00:00
|
|
|
if( !info->pieces[i].dnd )
|
2007-06-18 03:40:41 +00:00
|
|
|
return TR_CP_INCOMPLETE;
|
|
|
|
ret = TR_CP_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
tr_cpLeftUntilComplete ( const tr_completion_t * cp )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint64_t b=0;
|
|
|
|
const tr_torrent_t * tor;
|
|
|
|
const tr_info_t * info;
|
|
|
|
|
|
|
|
assert( cp != NULL );
|
|
|
|
assert( cp->tor != NULL );
|
|
|
|
|
|
|
|
tor = cp->tor;
|
|
|
|
info = &tor->info;
|
|
|
|
for( i=0; i<info->pieceCount; ++i )
|
|
|
|
if( !tr_cpPieceIsComplete( cp, i ) )
|
2007-07-25 01:59:46 +00:00
|
|
|
b += ( TR_BLOCKS_IN_PIECE(tor,i) - cp->completeBlocks[ i ] );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-06-30 21:11:17 +00:00
|
|
|
b *= tor->blockSize;
|
|
|
|
|
2007-07-15 03:52:51 +00:00
|
|
|
if( tor->blockCount && !tr_cpBlockIsComplete( cp, tor->blockCount - 1 ) )
|
|
|
|
b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
|
2007-06-30 21:11:17 +00:00
|
|
|
|
|
|
|
return b;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
tr_cpLeftUntilDone ( const tr_completion_t * cp )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint64_t b=0;
|
|
|
|
const tr_torrent_t * tor;
|
|
|
|
const tr_info_t * info;
|
|
|
|
|
|
|
|
assert( cp != NULL );
|
|
|
|
assert( cp->tor != NULL );
|
|
|
|
|
|
|
|
tor = cp->tor;
|
|
|
|
info = &tor->info;
|
|
|
|
|
|
|
|
for( i=0; i<info->pieceCount; ++i )
|
2007-07-10 03:12:46 +00:00
|
|
|
if( !tr_cpPieceIsComplete( cp, i ) && !info->pieces[i].dnd )
|
2007-07-25 01:59:46 +00:00
|
|
|
b += ( TR_BLOCKS_IN_PIECE(tor,i) - cp->completeBlocks[ i ] );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-06-30 21:11:17 +00:00
|
|
|
b *= tor->blockSize;
|
|
|
|
|
2007-07-15 03:52:51 +00:00
|
|
|
if( tor->blockCount && !tr_cpBlockIsComplete( cp, tor->blockCount-1 )
|
|
|
|
&& !info->pieces[info->pieceCount-1].dnd )
|
|
|
|
b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
|
2007-06-30 21:11:17 +00:00
|
|
|
|
2007-06-30 21:39:03 +00:00
|
|
|
return b;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
tr_cpPercentComplete ( const tr_completion_t * cp )
|
|
|
|
{
|
2007-06-22 00:44:26 +00:00
|
|
|
const uint64_t tilComplete = tr_cpLeftUntilComplete( cp );
|
|
|
|
const uint64_t total = cp->tor->info.totalSize;
|
2007-06-23 02:24:03 +00:00
|
|
|
const float f = 1.0 - (double)tilComplete / total;
|
|
|
|
return MAX(0.0, f);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
tr_cpPercentDone( const tr_completion_t * cp )
|
|
|
|
{
|
2007-06-22 00:44:26 +00:00
|
|
|
const uint64_t tilDone = tr_cpLeftUntilDone( cp );
|
|
|
|
const uint64_t total = cp->tor->info.totalSize;
|
2007-06-23 02:24:03 +00:00
|
|
|
const float f = 1.0 - (double)tilDone / total;
|
|
|
|
return MAX(0.0, f);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
tr_cpDownloadedValid( const tr_completion_t * cp )
|
|
|
|
{
|
2007-06-30 21:11:17 +00:00
|
|
|
const tr_torrent_t * tor = cp->tor;
|
|
|
|
|
|
|
|
uint64_t b = tr_bitfieldCountTrueBits( cp->blockBitfield ) * tor->blockSize;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-07-15 03:52:51 +00:00
|
|
|
if( tor->blockCount && tr_bitfieldHas( cp->blockBitfield, tor->blockCount - 1 ) )
|
2007-07-09 05:20:23 +00:00
|
|
|
b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-06-30 21:11:17 +00:00
|
|
|
return b;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|