1 | /* |
---|
2 | * This file Copyright (C) Mnemosyne LLC |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: completion.h 11892 2011-02-15 16:04:56Z jordan $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef __TRANSMISSION__ |
---|
14 | #error only libtransmission should #include this header. |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifndef TR_COMPLETION_H |
---|
18 | #define TR_COMPLETION_H |
---|
19 | |
---|
20 | #include <assert.h> |
---|
21 | |
---|
22 | #include "transmission.h" |
---|
23 | #include "bitfield.h" |
---|
24 | |
---|
25 | typedef struct tr_completion |
---|
26 | { |
---|
27 | tr_bool sizeWhenDoneIsDirty; |
---|
28 | tr_bool blocksWantedIsDirty; |
---|
29 | tr_bool haveValidIsDirty; |
---|
30 | |
---|
31 | tr_torrent * tor; |
---|
32 | |
---|
33 | /* do we have this block? */ |
---|
34 | tr_bitfield blockBitfield; |
---|
35 | |
---|
36 | /* do we have this piece? */ |
---|
37 | tr_bitfield pieceBitfield; |
---|
38 | |
---|
39 | /* a block is complete if and only if we have it */ |
---|
40 | uint16_t * completeBlocks; |
---|
41 | |
---|
42 | /* total number of blocks that we want downloaded |
---|
43 | DON'T access this directly; it's a lazy field. |
---|
44 | Used by tr_cpBlocksMissing(). */ |
---|
45 | tr_block_index_t blocksWantedLazy; |
---|
46 | |
---|
47 | /* total number of completed blocks that we want downloaded |
---|
48 | DON'T access this directly; it's a lazy field. |
---|
49 | Used by tr_cpBlocksMissing(). */ |
---|
50 | tr_block_index_t blocksWantedCompleteLazy; |
---|
51 | |
---|
52 | /* number of bytes we'll have when done downloading. [0..info.totalSize] |
---|
53 | DON'T access this directly; it's a lazy field. |
---|
54 | use tr_cpSizeWhenDone() instead! */ |
---|
55 | uint64_t sizeWhenDoneLazy; |
---|
56 | |
---|
57 | /* number of bytes we'll have when done downloading. [0..info.totalSize] |
---|
58 | DON'T access this directly; it's a lazy field. |
---|
59 | use tr_cpHaveValid() instead! */ |
---|
60 | uint64_t haveValidLazy; |
---|
61 | |
---|
62 | /* number of bytes we want or have now. [0..sizeWhenDone] */ |
---|
63 | uint64_t sizeNow; |
---|
64 | } |
---|
65 | tr_completion; |
---|
66 | |
---|
67 | /** |
---|
68 | *** Life Cycle |
---|
69 | **/ |
---|
70 | |
---|
71 | tr_completion * tr_cpConstruct( tr_completion *, tr_torrent * ); |
---|
72 | |
---|
73 | tr_completion * tr_cpDestruct( tr_completion * ); |
---|
74 | |
---|
75 | /** |
---|
76 | *** General |
---|
77 | **/ |
---|
78 | |
---|
79 | tr_completeness tr_cpGetStatus( const tr_completion * ); |
---|
80 | |
---|
81 | uint64_t tr_cpHaveValid( const tr_completion * ); |
---|
82 | |
---|
83 | tr_block_index_t tr_cpBlocksMissing( const tr_completion * ); |
---|
84 | |
---|
85 | uint64_t tr_cpSizeWhenDone( const tr_completion * ); |
---|
86 | |
---|
87 | void tr_cpInvalidateDND( tr_completion * ); |
---|
88 | |
---|
89 | void tr_cpGetAmountDone( const tr_completion * completion, |
---|
90 | float * tab, |
---|
91 | int tabCount ); |
---|
92 | |
---|
93 | static inline uint64_t tr_cpHaveTotal( const tr_completion * cp ) |
---|
94 | { |
---|
95 | return cp->sizeNow; |
---|
96 | } |
---|
97 | |
---|
98 | static inline uint64_t tr_cpLeftUntilComplete( const tr_completion * cp ) |
---|
99 | { |
---|
100 | return tr_torrentInfo(cp->tor)->totalSize - cp->sizeNow; |
---|
101 | } |
---|
102 | |
---|
103 | static inline uint64_t tr_cpLeftUntilDone( const tr_completion * cp ) |
---|
104 | { |
---|
105 | return tr_cpSizeWhenDone( cp ) - cp->sizeNow; |
---|
106 | } |
---|
107 | |
---|
108 | static inline double tr_cpPercentComplete( const tr_completion * cp ) |
---|
109 | { |
---|
110 | const double ratio = tr_getRatio( cp->sizeNow, tr_torrentInfo(cp->tor)->totalSize ); |
---|
111 | if( (int)ratio == TR_RATIO_NA ) |
---|
112 | return 0.0; |
---|
113 | else if( (int)ratio == TR_RATIO_INF ) |
---|
114 | return 1.0; |
---|
115 | else |
---|
116 | return ratio; |
---|
117 | } |
---|
118 | |
---|
119 | static inline double tr_cpPercentDone( const tr_completion * cp ) |
---|
120 | { |
---|
121 | const double ratio = tr_getRatio( cp->sizeNow, tr_cpSizeWhenDone( cp ) ); |
---|
122 | const int iratio = (int)ratio; |
---|
123 | return ((iratio == TR_RATIO_NA) || (iratio == TR_RATIO_INF)) ? 0.0 : ratio; |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | *** Pieces |
---|
128 | **/ |
---|
129 | |
---|
130 | int tr_cpMissingBlocksInPiece( const tr_completion * cp, |
---|
131 | tr_piece_index_t piece ); |
---|
132 | |
---|
133 | tr_bool tr_cpPieceIsComplete( const tr_completion * cp, |
---|
134 | tr_piece_index_t piece ); |
---|
135 | |
---|
136 | void tr_cpPieceAdd( tr_completion * completion, |
---|
137 | tr_piece_index_t piece ); |
---|
138 | |
---|
139 | void tr_cpPieceRem( tr_completion * completion, |
---|
140 | tr_piece_index_t piece ); |
---|
141 | |
---|
142 | tr_bool tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t ); |
---|
143 | |
---|
144 | /** |
---|
145 | *** Blocks |
---|
146 | **/ |
---|
147 | |
---|
148 | static inline tr_bool tr_cpBlockIsCompleteFast( const tr_completion * cp, tr_block_index_t block ) |
---|
149 | { |
---|
150 | return tr_bitfieldHasFast( &cp->blockBitfield, block ); |
---|
151 | } |
---|
152 | |
---|
153 | static inline tr_bool tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t block ) |
---|
154 | { |
---|
155 | return tr_bitfieldHas( &cp->blockBitfield, block ); |
---|
156 | } |
---|
157 | |
---|
158 | void tr_cpBlockAdd( tr_completion * completion, |
---|
159 | tr_block_index_t block ); |
---|
160 | |
---|
161 | tr_bool tr_cpBlockBitfieldSet( tr_completion * completion, |
---|
162 | struct tr_bitfield * blocks ); |
---|
163 | |
---|
164 | void tr_cpSetHaveAll( tr_completion * completion ); |
---|
165 | |
---|
166 | /*** |
---|
167 | **** |
---|
168 | ***/ |
---|
169 | |
---|
170 | static inline const struct tr_bitfield * tr_cpPieceBitfield( const tr_completion * cp ) { |
---|
171 | return &cp->pieceBitfield; |
---|
172 | } |
---|
173 | |
---|
174 | static inline const struct tr_bitfield * tr_cpBlockBitfield( const tr_completion * cp ) { |
---|
175 | assert( cp ); |
---|
176 | assert( cp->blockBitfield.bits ); |
---|
177 | assert( cp->blockBitfield.bitCount ); |
---|
178 | return &cp->blockBitfield; |
---|
179 | } |
---|
180 | |
---|
181 | #endif |
---|