Add "Status" column to tr_peer_stat.

This commit is contained in:
Charles Kerr 2007-11-17 23:43:33 +00:00
parent c4ac8eeb71
commit 37969406b2
6 changed files with 107 additions and 29 deletions

View File

@ -239,6 +239,7 @@ enum
PEER_COL_DOWNLOAD_RATE,
PEER_COL_IS_UPLOADING,
PEER_COL_UPLOAD_RATE,
PEER_COL_STATUS,
N_PEER_COLS
};
@ -252,7 +253,8 @@ static const char* peer_column_names[N_PEER_COLS] =
N_("Downloading"),
N_("DL Rate"),
N_("Uploading"),
N_("UL Rate")
N_("UL Rate"),
N_("Status")
};
static int compare_peers (const void * a, const void * b)
@ -288,6 +290,7 @@ peer_row_set (GtkTreeStore * store,
PEER_COL_DOWNLOAD_RATE, peer->downloadFromRate,
PEER_COL_IS_UPLOADING, peer->isUploading,
PEER_COL_UPLOAD_RATE, peer->uploadToRate,
PEER_COL_STATUS, peer->status,
-1);
}
@ -316,7 +319,8 @@ peer_model_new (tr_torrent * tor)
G_TYPE_BOOLEAN, /* isDownloading */
G_TYPE_FLOAT, /* downloadFromRate */
G_TYPE_BOOLEAN, /* isUploading */
G_TYPE_FLOAT); /* uploadToRate */
G_TYPE_FLOAT, /* uploadToRate */
G_TYPE_INT ); /* tr_peer_status */
int n_peers = 0;
tr_peer_stat * peers = tr_torrentPeers (tor, &n_peers);
@ -342,6 +346,30 @@ render_encrypted (GtkTreeViewColumn * column UNUSED,
NULL);
}
static void
render_status( GtkTreeViewColumn * column UNUSED,
GtkCellRenderer * renderer,
GtkTreeModel * tree_model,
GtkTreeIter * iter,
gpointer data UNUSED )
{
int status;
const char * text;
gtk_tree_model_get( tree_model, iter, PEER_COL_STATUS, &status, -1 );
switch( status )
{
case TR_PEER_STATUS_HANDSHAKE: text = _( "Handshaking" ); break;
case TR_PEER_STATUS_PEER_IS_CHOKED: text = _( "Peer is Choked" ); break;
case TR_PEER_STATUS_CLIENT_IS_CHOKED: text = _( "Choked" ); break;
case TR_PEER_STATUS_CLIENT_IS_INTERESTED: text = _( "Choked and Interested" ); break;
case TR_PEER_STATUS_READY: text = _( "Ready" ); break;
case TR_PEER_STATUS_REQUEST_SENT: text = _( "Request Sent" ); break;
case TR_PEER_STATUS_ACTIVE : text = _( "Active" ); break;
default: text = "BUG"; break;
}
g_object_set (renderer, "text", text, NULL);
}
static void
render_ul_rate (GtkTreeViewColumn * column UNUSED,
GtkCellRenderer * renderer,
@ -495,7 +523,8 @@ static GtkWidget* peer_page_new ( TrTorrent * gtor )
PEER_COL_PROGRESS,
PEER_COL_IS_ENCRYPTED,
PEER_COL_UPLOAD_RATE,
PEER_COL_DOWNLOAD_RATE };
PEER_COL_DOWNLOAD_RATE,
PEER_COL_STATUS };
m = peer_model_new (tor);
v = gtk_tree_view_new_with_model (m);
@ -569,6 +598,13 @@ static GtkWidget* peer_page_new ( TrTorrent * gtor )
NULL, NULL);
break;
case PEER_COL_STATUS:
r = gtk_cell_renderer_text_new( );
c = gtk_tree_view_column_new_with_attributes (t, r, "text", col, NULL);
gtk_tree_view_column_set_cell_data_func (c, r, render_status, NULL, NULL);
break;
default:
abort ();
}

View File

@ -36,6 +36,7 @@ typedef struct tr_peer
unsigned int clientIsInterested : 1;
unsigned int doPurge : 1;
tr_peer_status status;
/* number of bad pieces they've contributed to */
uint8_t strikes;

View File

@ -35,6 +35,7 @@
#include "ptrarray.h"
#include "ratecontrol.h"
#include "shared.h"
#include "trcompat.h" /* strlcpy */
#include "trevent.h"
#include "utils.h"
@ -1413,15 +1414,16 @@ tr_peerMgrPeerStats( const tr_peerMgr * manager,
tr_peer_stat * stat = ret + i;
tr_netNtop( &peer->in_addr, stat->addr, sizeof(stat->addr) );
strlcpy( stat->client, (peer->client ? peer->client : ""), sizeof(stat->client) );
stat->port = peer->port;
stat->from = atom->from;
stat->client = tr_strdup( peer->client ? peer->client : "" );
stat->progress = peer->progress;
stat->isEncrypted = tr_peerIoIsEncrypted( peer->io ) ? 1 : 0;
stat->uploadToRate = peer->rateToPeer;
stat->downloadFromRate = peer->rateToClient;
stat->isDownloading = stat->uploadToRate > 0.01;
stat->isUploading = stat->downloadFromRate > 0.01;
stat->status = peer->status;
}
*setmeCount = size;
@ -1482,7 +1484,7 @@ getWeightedThroughput( const tr_peer * peer )
static void
rechoke( Torrent * t )
{
int i, peerCount, size=0, unchoked=0;
int i, peerCount, size=0;
const time_t fibrillationTime = time(NULL) - MIN_CHOKE_PERIOD_SEC;
tr_peer ** peers = getConnectedPeers( t, &peerCount );
struct ChokeData * choke = tr_new0( struct ChokeData, peerCount );
@ -1505,14 +1507,11 @@ rechoke( Torrent * t )
qsort( choke, size, sizeof(struct ChokeData), compareChoke );
for( i=0; i<size && i<NUM_UNCHOKED_PEERS_PER_TORRENT; ++i ) {
for( i=0; i<size && i<NUM_UNCHOKED_PEERS_PER_TORRENT; ++i )
choke[i].doUnchoke = 1;
++unchoked;
}
for( ; i<size; ++i ) {
choke[i].doUnchoke = 1;
++unchoked;
if( choke[i].peer->peerIsInterested )
break;
}

View File

@ -140,6 +140,7 @@ struct tr_peermsgs
time_t clientSentPexAt;
time_t clientSentAnythingAt;
unsigned int peerSentBitfield : 1;
unsigned int peerSupportsPex : 1;
unsigned int clientSentLtepHandshake : 1;
unsigned int peerSentLtepHandshake : 1;
@ -1026,6 +1027,19 @@ messageLengthIsCorrect( const tr_peermsgs * msg, uint8_t id, uint32_t len )
static int
clientGotBlock( tr_peermsgs * msgs, const uint8_t * block, const struct peer_request * req );
static void
clientGotBytes( tr_peermsgs * msgs, uint32_t byteCount )
{
tr_torrent * tor = msgs->torrent;
tor->activityDate = tr_date( );
tor->downloadedCur += byteCount;
msgs->info->pieceDataActivityDate = time( NULL );
tr_rcTransferred( msgs->info->rcToClient, byteCount );
tr_rcTransferred( tor->download, byteCount );
tr_rcTransferred( tor->handle->download, byteCount );
}
static int
readBtPiece( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen )
{
@ -1053,6 +1067,7 @@ readBtPiece( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen )
uint8_t * buf = tr_new( uint8_t, n );
tr_peerIoReadBytes( msgs->io, inbuf, buf, n );
evbuffer_add( msgs->incoming.block, buf, n );
clientGotBytes( msgs, n );
tr_free( buf );
dbgmsg( msgs, "got %d bytes for block %u:%u->%u ... %d remain",
(int)n, req->index, req->offset, req->length,
@ -1138,6 +1153,7 @@ readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen )
case BT_BITFIELD: {
const int clientIsSeed = tr_torrentIsSeed( msgs->torrent );
dbgmsg( msgs, "got a bitfield" );
msgs->peerSentBitfield = 1;
tr_peerIoReadBytes( msgs->io, inbuf, msgs->info->have->bits, msglen );
updatePeerProgress( msgs );
tr_peerMsgsSetChoke( msgs, !clientIsSeed || (msgs->info->progress<1.0) );
@ -1232,18 +1248,6 @@ readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen )
return READ_AGAIN;
}
static void
clientGotBytes( tr_peermsgs * msgs, uint32_t byteCount )
{
tr_torrent * tor = msgs->torrent;
tor->activityDate = tr_date( );
tor->downloadedCur += byteCount;
msgs->info->pieceDataActivityDate = time( NULL );
tr_rcTransferred( msgs->info->rcToClient, byteCount );
tr_rcTransferred( tor->download, byteCount );
tr_rcTransferred( tor->handle->download, byteCount );
}
static void
peerGotBytes( tr_peermsgs * msgs, uint32_t byteCount )
{
@ -1368,7 +1372,6 @@ clientGotBlock( tr_peermsgs * msgs,
**/
msgs->info->peerSentPieceDataAt = time( NULL );
clientGotBytes( msgs, req->length );
i = tr_ioWrite( tor, req->index, req->offset, req->length, data );
if( i )
return 0;
@ -1480,6 +1483,33 @@ popNextRequest( tr_peermsgs * msgs )
return ret;
}
static void
updatePeerStatus( tr_peermsgs * msgs )
{
tr_peer * peer = msgs->info;
if( !msgs->peerSentBitfield )
peer->status = TR_PEER_STATUS_HANDSHAKE;
else if( ( time(NULL) - peer->pieceDataActivityDate ) < 3 )
peer->status = TR_PEER_STATUS_ACTIVE;
else if( peer->clientIsChoked )
peer->status = TR_PEER_STATUS_CLIENT_IS_CHOKED;
else if( peer->peerIsChoked )
peer->status = TR_PEER_STATUS_PEER_IS_CHOKED;
else if( msgs->clientAskedFor != NULL )
peer->status = TR_PEER_STATUS_REQUEST_SENT;
else if( peer->clientIsInterested )
peer->status = TR_PEER_STATUS_CLIENT_IS_INTERESTED;
else
peer->status = TR_PEER_STATUS_READY;
}
static int
pulse( void * vmsgs )
{
@ -1489,8 +1519,8 @@ pulse( void * vmsgs )
size_t len;
tr_peerIoTryRead( msgs->io );
pumpRequestQueue( msgs );
updatePeerStatus( msgs );
if( !canWrite( msgs ) )
{
@ -1740,6 +1770,7 @@ tr_peerMsgsNew( struct tr_torrent * torrent,
m->handle = torrent->handle;
m->torrent = torrent;
m->io = info->io;
m->info->status = TR_PEER_STATUS_HANDSHAKE;
m->info->clientIsChoked = 1;
m->info->peerIsChoked = 1;
m->info->clientIsInterested = 0;

View File

@ -880,11 +880,8 @@ tr_torrentPeers( const tr_torrent * tor, int * peerCount )
}
void
tr_torrentPeersFree( tr_peer_stat * peers, int peerCount )
tr_torrentPeersFree( tr_peer_stat * peers, int peerCount UNUSED )
{
int i;
for( i=0; i<peerCount; ++i )
tr_free( (char*) peers[i].client );
tr_free( peers );
}

View File

@ -683,15 +683,29 @@ struct tr_file_stat
float progress;
};
typedef enum
{
TR_PEER_STATUS_HANDSHAKE,
TR_PEER_STATUS_PEER_IS_CHOKED,
TR_PEER_STATUS_CLIENT_IS_CHOKED,
TR_PEER_STATUS_CLIENT_IS_INTERESTED,
TR_PEER_STATUS_READY,
TR_PEER_STATUS_REQUEST_SENT,
TR_PEER_STATUS_ACTIVE
}
tr_peer_status;
struct tr_peer_stat
{
char addr[INET_ADDRSTRLEN];
const char * client;
char addr[INET_ADDRSTRLEN];
char client[80];
unsigned int isEncrypted : 1;
unsigned int isDownloading : 1;
unsigned int isUploading : 1;
tr_peer_status status;
uint8_t from;
uint16_t port;