source: trunk/libtransmission/completion.c @ 6425

Last change on this file since 6425 was 6425, checked in by charles, 13 years ago

minor text cleanup

  • Property svn:keywords set to Date Rev Author Id
File size: 8.9 KB
Line 
1/******************************************************************************
2 * $Id: completion.c 6425 2008-08-01 16:43:22Z charles $
3 *
4 * Copyright (c) 2005-2008 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include <assert.h>
26#include <string.h>
27
28#include "transmission.h"
29#include "completion.h"
30#include "torrent.h"
31#include "utils.h"
32
33struct tr_completion
34{
35    unsigned int sizeWhenDoneIsDirty : 1;
36
37    tr_torrent * tor;
38
39    /* do we have this block? */
40    tr_bitfield * blockBitfield;
41
42    /* do we have this piece? */
43    tr_bitfield * pieceBitfield;
44
45    /* a block is complete if and only if we have it */
46    uint16_t * completeBlocks;
47
48    /* number of bytes we'll have when done downloading. [0..info.totalSize]
49       DON'T access this directly; it's a lazy field.
50       use tr_cpSizeWhenDone() instead! */
51    uint64_t sizeWhenDoneLazy;
52
53    /* number of bytes we want or have now. [0..sizeWhenDone] */
54    uint64_t sizeNow;
55};
56
57static void
58tr_cpReset( tr_completion * cp )
59{
60    tr_bitfieldClear( cp->pieceBitfield );
61    tr_bitfieldClear( cp->blockBitfield );
62    memset( cp->completeBlocks, 0, sizeof(uint16_t) * cp->tor->info.pieceCount );
63    cp->sizeNow = 0;
64    cp->sizeWhenDoneIsDirty = 1;
65}
66
67tr_completion *
68tr_cpInit( tr_torrent * tor )
69{
70    tr_completion * cp  = tr_new( tr_completion, 1 );
71    cp->tor             = tor;
72    cp->blockBitfield   = tr_bitfieldNew( tor->blockCount );
73    cp->pieceBitfield   = tr_bitfieldNew( tor->info.pieceCount );
74    cp->completeBlocks  = tr_new( uint16_t, tor->info.pieceCount );
75    tr_cpReset( cp );
76    return cp;
77}
78
79void
80tr_cpClose( tr_completion * cp )
81{
82    tr_free        ( cp->completeBlocks );
83    tr_bitfieldFree( cp->pieceBitfield );
84    tr_bitfieldFree( cp->blockBitfield );
85    tr_free        ( cp );
86}
87
88void
89tr_cpInvalidateDND ( tr_completion * cp )
90{
91    cp->sizeWhenDoneIsDirty = 1;
92}
93
94uint64_t
95tr_cpSizeWhenDone( const tr_completion * ccp )
96{
97    if( ccp->sizeWhenDoneIsDirty )
98    {
99        tr_completion * cp = (tr_completion *) ccp; /* mutable */
100        const tr_torrent * tor = cp->tor;
101        const tr_info * info = &tor->info;
102        tr_piece_index_t i;
103        uint64_t size = 0;
104
105        for( i=0; i<info->pieceCount; ++i )
106        {
107            if( !info->pieces[i].dnd ) {
108                /* we want the piece... */
109                size += tr_torPieceCountBytes( tor, i );
110            } else if( tr_cpPieceIsComplete( cp, i ) ) {
111                /* we have the piece... */
112                size += tr_torPieceCountBytes( tor, i );
113            } else if( cp->completeBlocks[i] ) {
114                /* we have part of the piece... */
115                const tr_block_index_t b = tr_torPieceFirstBlock( tor, i );
116                const tr_block_index_t e = b + tr_torPieceCountBlocks( tor, i );
117                tr_block_index_t j;
118                for( j=b; j<e; ++j )
119                    if( tr_cpBlockIsComplete( cp, j ) )
120                        size += tr_torBlockCountBytes( tor, j );
121            }
122        }
123
124        cp->sizeWhenDoneLazy = size;
125        cp->sizeWhenDoneIsDirty = 0;
126    }
127
128    assert( ccp->sizeWhenDoneLazy <= ccp->tor->info.totalSize );
129    assert( ccp->sizeWhenDoneLazy >= ccp->sizeNow );
130    return ccp->sizeWhenDoneLazy;
131}
132
133int
134tr_cpPieceIsComplete( const tr_completion  * cp,
135                      tr_piece_index_t       piece )
136{
137    return cp->completeBlocks[piece] == tr_torPieceCountBlocks(cp->tor,piece);
138}
139
140const tr_bitfield *
141tr_cpPieceBitfield( const tr_completion * cp )
142{
143    return cp->pieceBitfield;
144}
145
146void
147tr_cpPieceAdd( tr_completion * cp, tr_piece_index_t piece )
148{
149    const tr_torrent * tor = cp->tor;
150    const tr_block_index_t start = tr_torPieceFirstBlock(tor,piece);
151    const tr_block_index_t end = start + tr_torPieceCountBlocks(tor, piece);
152    tr_block_index_t i;
153
154    for( i=start; i<end; ++i )
155        tr_cpBlockAdd( cp, i );
156}
157
158void
159tr_cpPieceRem( tr_completion * cp, tr_piece_index_t piece )
160{
161    const tr_torrent * tor = cp->tor;
162    const tr_block_index_t start = tr_torPieceFirstBlock( tor, piece );
163    const tr_block_index_t end = start + tr_torPieceCountBlocks( tor, piece );
164    tr_block_index_t block;
165
166    assert( cp );
167    assert( piece < tor->info.pieceCount );
168    assert( start < tor->blockCount );
169    assert( start <= end );
170    assert( end <= tor->blockCount );
171
172    for( block=start; block<end; ++block )
173        if( tr_cpBlockIsComplete( cp, block ) )
174            cp->sizeNow -= tr_torBlockCountBytes( tor, block );
175
176    cp->sizeWhenDoneIsDirty = 1;
177    cp->completeBlocks[piece] = 0;
178    tr_bitfieldRemRange ( cp->blockBitfield, start, end );
179    tr_bitfieldRem( cp->pieceBitfield, piece );
180}
181
182int
183tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t block )
184{
185    return tr_bitfieldHas( cp->blockBitfield, block );
186}
187
188void
189tr_cpBlockAdd( tr_completion * cp, tr_block_index_t block )
190{
191    const tr_torrent * tor = cp->tor;
192
193    if( !tr_cpBlockIsComplete( cp, block ) )
194    {
195        const tr_piece_index_t piece = tr_torBlockPiece( tor, block );
196        const int blockSize = tr_torBlockCountBytes( tor, block );
197
198        ++cp->completeBlocks[piece];
199
200        if( cp->completeBlocks[piece] == tr_torPieceCountBlocks( tor, piece ) )
201            tr_bitfieldAdd( cp->pieceBitfield, piece );
202
203        tr_bitfieldAdd( cp->blockBitfield, block );
204
205        cp->sizeNow += blockSize;
206
207        cp->sizeWhenDoneIsDirty = 1;
208    }
209}
210
211const tr_bitfield *
212tr_cpBlockBitfield( const tr_completion * cp )
213{
214    assert( cp );
215    assert( cp->blockBitfield );
216    assert( cp->blockBitfield->bits );
217    assert( cp->blockBitfield->bitCount );
218
219    return cp->blockBitfield;
220}
221
222tr_errno
223tr_cpBlockBitfieldSet( tr_completion * cp, tr_bitfield * bitfield )
224{
225    tr_block_index_t i;
226
227    assert( cp );
228    assert( bitfield );
229    assert( cp->blockBitfield );
230
231    if( !tr_bitfieldTestFast( bitfield, cp->tor->blockCount-1 ) )
232        return TR_ERROR_ASSERT;
233
234    tr_cpReset( cp );
235    for( i=0; i<cp->tor->blockCount; ++i )
236        if( tr_bitfieldHasFast( bitfield, i ) )
237            tr_cpBlockAdd( cp, i );
238
239    return 0;
240}
241
242int
243tr_cpMissingBlocksInPiece( const tr_completion * cp, tr_piece_index_t piece )
244{
245    return tr_torPieceCountBlocks( cp->tor, piece ) - cp->completeBlocks[piece];
246}
247
248/***
249****
250***/
251
252float
253tr_cpPercentDone( const tr_completion * cp )
254{
255    return tr_getRatio( cp->sizeNow, tr_cpSizeWhenDone(cp) );
256}
257
258float
259tr_cpPercentComplete ( const tr_completion * cp )
260{
261    return tr_getRatio( cp->sizeNow, cp->tor->info.totalSize );
262}
263
264uint64_t
265tr_cpLeftUntilDone ( const tr_completion * cp )
266{
267    return tr_cpSizeWhenDone(cp) - cp->sizeNow;
268}
269
270uint64_t
271tr_cpLeftUntilComplete ( const tr_completion * cp )
272{
273    return cp->tor->info.totalSize - cp->sizeNow;
274}
275
276cp_status_t
277tr_cpGetStatus ( const tr_completion * cp )
278{
279    if( cp->sizeNow == cp->tor->info.totalSize ) return TR_CP_COMPLETE;
280    if( cp->sizeNow == tr_cpSizeWhenDone(cp) ) return TR_CP_DONE;
281    return TR_CP_INCOMPLETE;
282}
283
284uint64_t
285tr_cpHaveValid( const tr_completion * cp )
286{
287    uint64_t b = 0;
288    tr_piece_index_t i;
289    const tr_torrent * tor = cp->tor;
290
291    for( i=0; i<tor->info.pieceCount; ++i )
292        if( tr_cpPieceIsComplete( cp, i ) )
293            b += tr_torPieceCountBytes( tor, i );
294
295    return b;
296}
297
298uint64_t
299tr_cpHaveTotal( const tr_completion * cp )
300{
301    return cp->sizeNow;
302}
303
304void
305tr_cpGetAmountDone( const tr_completion * cp, float * tab, int tabCount )
306{
307    int i;
308    const tr_torrent * tor = cp->tor;
309    const float interval = tor->info.pieceCount / (float)tabCount;
310    const int isComplete = tr_cpGetStatus ( tor->completion ) == TR_CP_COMPLETE;
311
312    for( i=0; i<tabCount; ++i )
313    {
314        const tr_piece_index_t piece = i * interval;
315
316        if( tor == NULL )
317            tab[i] = 0.0f;
318        else if( isComplete || tr_cpPieceIsComplete( cp, piece ) )
319            tab[i] = 1.0f;
320        else 
321            tab[i] = (float)cp->completeBlocks[piece] / tr_torPieceCountBlocks( tor, piece );
322    }
323}
Note: See TracBrowser for help on using the repository browser.