source: trunk/libtransmission/completion.c @ 3

Last change on this file since 3 was 3, checked in by root, 15 years ago

Update 2005-11-17

File size: 6.7 KB
Line 
1/******************************************************************************
2 * Copyright (c) 2005 Eric Petit
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *****************************************************************************/
22
23#include "transmission.h"
24
25tr_completion_t * tr_cpInit( tr_torrent_t * tor )
26{
27    tr_completion_t * cp;
28    int i;
29
30    cp                   = calloc( 1, sizeof( tr_completion_t ) );
31    cp->tor              = tor;
32    cp->blockBitfield    = calloc( 1, ( tor->blockCount + 7 ) / 8 );
33    cp->blockDownloaders = calloc( 1, tor->blockCount );
34    cp->pieceBitfield    = calloc( 1, ( tor->info.pieceCount + 7 ) / 8 );
35    cp->missingBlocks    = calloc( 1, tor->info.pieceCount * sizeof( int ) );
36
37    for( i = 0; i < tor->info.pieceCount; i++ )
38    {
39        cp->missingBlocks[i] = tr_pieceCountBlocks( i );
40    }
41
42    return cp;
43}
44
45void tr_cpClose( tr_completion_t * cp )
46{
47    free( cp->blockBitfield );
48    free( cp->blockDownloaders );
49    free( cp->pieceBitfield );
50    free( cp->missingBlocks );
51    free( cp );
52}
53
54float tr_cpCompletionAsFloat( tr_completion_t * cp )
55{
56    return (float) cp->blockCount / (float) cp->tor->blockCount;
57}
58
59uint64_t tr_cpLeftBytes( tr_completion_t * cp )
60{
61    tr_torrent_t * tor = cp->tor;
62    uint64_t left;
63    left = (uint64_t) ( cp->tor->blockCount - cp->blockCount ) *
64           (uint64_t) tor->blockSize;
65    if( !tr_bitfieldHas( cp->blockBitfield, cp->tor->blockCount - 1 ) &&
66        tor->info.totalSize % tor->blockSize )
67    {
68        left += tor->info.totalSize % tor->blockSize;
69        left -= tor->blockSize;
70    }
71    return left;
72}
73
74/* Pieces */
75int tr_cpPieceIsComplete( tr_completion_t * cp, int piece )
76{
77    return tr_bitfieldHas( cp->pieceBitfield, piece );
78}
79
80uint8_t * tr_cpPieceBitfield( tr_completion_t * cp )
81{
82    return cp->pieceBitfield;
83}
84
85void tr_cpPieceAdd( tr_completion_t * cp, int piece )
86{
87    tr_torrent_t * tor = cp->tor;
88    int startBlock, endBlock, i;
89
90    startBlock = tr_pieceStartBlock( piece );
91    endBlock   = startBlock + tr_pieceCountBlocks( piece );
92    for( i = startBlock; i < endBlock; i++ )
93    {
94        tr_cpBlockAdd( cp, i );
95    }
96
97    tr_bitfieldAdd( cp->pieceBitfield, piece );
98}
99
100/* Blocks */
101void tr_cpDownloaderAdd( tr_completion_t * cp, int block )
102{
103    tr_torrent_t * tor = cp->tor;
104    if( !cp->blockDownloaders[block] && !tr_cpBlockIsComplete( cp, block ) )
105    {
106        cp->missingBlocks[tr_blockPiece(block)]--;
107    }
108    (cp->blockDownloaders[block])++;
109}
110
111void tr_cpDownloaderRem( tr_completion_t * cp, int block )
112{
113    tr_torrent_t * tor = cp->tor;
114    (cp->blockDownloaders[block])--;
115    if( !cp->blockDownloaders[block] && !tr_cpBlockIsComplete( cp, block ) )
116    {
117        cp->missingBlocks[tr_blockPiece(block)]++;
118    }
119}
120
121int tr_cpBlockIsComplete( tr_completion_t * cp, int block )
122{
123    return tr_bitfieldHas( cp->blockBitfield, block );
124}
125
126void tr_cpBlockAdd( tr_completion_t * cp, int block )
127{
128    tr_torrent_t * tor = cp->tor;
129    if( !tr_cpBlockIsComplete( cp, block ) )
130    {
131        (cp->blockCount)++;
132        if( !cp->blockDownloaders[block] )
133        {
134            (cp->missingBlocks[tr_blockPiece(block)])--;
135        }
136    }
137    tr_bitfieldAdd( cp->blockBitfield, block );
138}
139
140void tr_cpBlockRem( tr_completion_t * cp, int block )
141{
142    tr_torrent_t * tor = cp->tor;
143    if( tr_cpBlockIsComplete( cp, block ) )
144    {
145        (cp->blockCount)--;
146        if( !cp->blockDownloaders[block] )
147        {
148            (cp->missingBlocks[tr_blockPiece(block)])++;
149        }
150    }
151    tr_bitfieldRem( cp->blockBitfield, block );
152}
153
154uint8_t * tr_cpBlockBitfield( tr_completion_t * cp )
155{
156    return cp->blockBitfield;
157}
158
159void tr_cpBlockBitfieldSet( tr_completion_t * cp, uint8_t * bitfield )
160{
161    tr_torrent_t * tor = cp->tor;
162    int i, j;
163    int startBlock, endBlock;
164    int pieceComplete;
165
166    for( i = 0; i < cp->tor->info.pieceCount; i++ )
167    {
168        startBlock    = tr_pieceStartBlock( i );
169        endBlock      = startBlock + tr_pieceCountBlocks( i );
170        pieceComplete = 1;
171
172        for( j = startBlock; j < endBlock; j++ )
173        {
174            if( tr_bitfieldHas( bitfield, j ) )
175            {
176                tr_cpBlockAdd( cp, j );
177            }
178            else
179            {
180                pieceComplete = 0;
181            }
182        }
183        if( pieceComplete )
184        {
185            tr_cpPieceAdd( cp, i );
186        }
187    }
188}
189
190int tr_cpMissingBlockInPiece( tr_completion_t * cp, int piece )
191{
192    tr_torrent_t * tor = cp->tor;
193    int start, count, end, i;
194
195    start = tr_pieceStartBlock( piece );
196    count = tr_pieceCountBlocks( piece );
197    end   = start + count;
198
199    for( i = start; i < end; i++ )
200    {
201        if( tr_cpBlockIsComplete( cp, i ) || cp->blockDownloaders[i] )
202        {
203            continue;
204        }
205        return i;
206    }
207
208    return -1;
209}
210
211int tr_cpMostMissingBlockInPiece( tr_completion_t * cp, int piece,
212                                  int * downloaders )
213{
214    tr_torrent_t * tor = cp->tor;
215    int start, count, end, i;
216    int * pool, poolSize, min, ret;
217
218    start = tr_pieceStartBlock( piece );
219    count = tr_pieceCountBlocks( piece );
220    end   = start + count;
221
222    pool     = malloc( count * sizeof( int ) );
223    poolSize = 0;
224    min      = 255;
225
226    for( i = start; i < end; i++ )
227    {
228        if( tr_cpBlockIsComplete( cp, i ) || cp->blockDownloaders[i] > min )
229        {
230            continue;
231        }
232        if( cp->blockDownloaders[i] < min )
233        {
234            min      = cp->blockDownloaders[i];
235            poolSize = 0;
236        }
237        if( cp->blockDownloaders[i] <= min )
238        {
239            pool[poolSize++] = i;
240        }
241    }
242
243    if( poolSize < 1 )
244    {
245        return -1;
246    }
247
248    ret = pool[0];
249    free( pool );
250    *downloaders = min;
251    return ret;
252}
253
Note: See TracBrowser for help on using the repository browser.