diff --git a/libtransmission/peer.c b/libtransmission/peer.c index de10c74d7..d4fcdc676 100644 --- a/libtransmission/peer.c +++ b/libtransmission/peer.c @@ -63,6 +63,8 @@ struct tr_peer_s /* The pieces that the peer has */ uint8_t * bitfield; + int pieceCount; + float progress; int goodPcs; int badPcs; @@ -534,6 +536,16 @@ int tr_peerIsDownloading( tr_peer_t * peer ) return peer->outBlockSending; } +/*********************************************************************** + * tr_peerIsDownloading + *********************************************************************** + * + **********************************************************************/ +float tr_peerProgress( tr_peer_t * peer ) +{ + return peer->progress; +} + /*********************************************************************** * tr_peerBitfield *********************************************************************** diff --git a/libtransmission/peer.h b/libtransmission/peer.h index 41b17cfb8..382b19259 100644 --- a/libtransmission/peer.h +++ b/libtransmission/peer.h @@ -43,6 +43,7 @@ int tr_peerIsConnected ( tr_peer_t * ); int tr_peerIsIncoming ( tr_peer_t * ); int tr_peerIsUploading ( tr_peer_t * ); int tr_peerIsDownloading ( tr_peer_t * ); +float tr_peerProgress ( tr_peer_t * ); uint8_t * tr_peerBitfield ( tr_peer_t * ); float tr_peerDownloadRate ( tr_peer_t * ); int tr_peerIsUnchoked ( tr_peer_t * ); diff --git a/libtransmission/peerparse.h b/libtransmission/peerparse.h index cf94ad856..6077e50a6 100644 --- a/libtransmission/peerparse.h +++ b/libtransmission/peerparse.h @@ -106,6 +106,11 @@ static inline int parseHave( tr_torrent_t * tor, tr_peer_t * peer, { peer->bitfield = calloc( ( tor->info.pieceCount + 7 ) / 8, 1 ); } + if( !tr_bitfieldHas( peer->bitfield, piece ) ) + { + peer->pieceCount++; + peer->progress = (float) peer->pieceCount / tor->info.pieceCount; + } tr_bitfieldAdd( peer->bitfield, piece ); updateInterest( tor, peer ); @@ -119,6 +124,7 @@ static inline int parseBitfield( tr_torrent_t * tor, tr_peer_t * peer, { tr_info_t * inf = &tor->info; int bitfieldSize; + int i; bitfieldSize = ( inf->pieceCount + 7 ) / 8; @@ -151,6 +157,17 @@ static inline int parseBitfield( tr_torrent_t * tor, tr_peer_t * peer, peer->bitfield = malloc( bitfieldSize ); } memcpy( peer->bitfield, p, bitfieldSize ); + + peer->pieceCount = 0; + for( i = 0; i < inf->pieceCount; i++ ) + { + if( tr_bitfieldHas( peer->bitfield, i ) ) + { + peer->pieceCount++; + } + } + peer->progress = (float) peer->pieceCount / inf->pieceCount; + updateInterest( tor, peer ); return 0; diff --git a/libtransmission/transmission.c b/libtransmission/transmission.c index db4737e3d..abc05b48e 100644 --- a/libtransmission/transmission.c +++ b/libtransmission/transmission.c @@ -591,6 +591,7 @@ tr_peer_stat_t * tr_torrentPeers( tr_torrent_t * tor, int * peerCount ) peers[i].isIncoming = tr_peerIsIncoming(peer); peers[i].isDownloading = tr_peerIsDownloading(peer); peers[i].isUploading = tr_peerIsUploading(peer); + peers[i].progress = tr_peerProgress(peer); } } diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 88d11449e..16fa29cb3 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -378,6 +378,7 @@ struct tr_peer_stat_s int isIncoming; int isDownloading; int isUploading; + float progress; }; struct tr_msg_list_s