Browse Source

Simple top5 by peers/seeders table

dynamic-accesslists
erdgeist 18 years ago
parent
commit
005ff4e231
  1. 33
      opentracker.c
  2. 44
      trackerlogic.c

33
opentracker.c

@ -106,7 +106,7 @@ void httpresponse(int64 s,struct http_data* h) @@ -106,7 +106,7 @@ void httpresponse(int64 s,struct http_data* h)
ot_peer peer;
ot_torrent *torrent;
ot_hash *hash = NULL;
int numwant, tmp, scanon;
int numwant, tmp, scanon, mode;
unsigned short port = htons(6881);
size_t reply_size = 0;
@ -130,8 +130,37 @@ e400: @@ -130,8 +130,37 @@ e400:
case 5: /* scrape ? */
if (byte_diff(data,5,"stats"))
goto e404;
scanon = 1;
mode = STATS_MRTG;
while( scanon ) {
switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_PARAM ) ) {
case -2: /* terminator */
scanon = 0;
break;
case -1: /* error */
goto e404;
case 4:
if(byte_diff(data,4,"mode")) {
scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
continue;
}
size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
if( len <= 0 ) goto e400_param;
if( !byte_diff(data,4,"mrtg"))
mode = STATS_MRTG;
else if( !byte_diff(data,4,"top5"))
mode = STATS_TOP5;
else
goto e400_param;
default:
scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
break;
}
}
/* Enough for http header + whole scrape string */
if( ( reply_size = return_stats_for_tracker( SUCCESS_HTTP_HEADER_LENGTH + static_reply ) ) <= 0 )
if( ( reply_size = return_stats_for_tracker( SUCCESS_HTTP_HEADER_LENGTH + static_reply, mode ) ) <= 0 )
goto e500;
break;
case 6: /* scrape ? */

44
trackerlogic.c

@ -311,26 +311,60 @@ size_t return_scrape_for_torrent( ot_hash *hash, char *reply ) { @@ -311,26 +311,60 @@ size_t return_scrape_for_torrent( ot_hash *hash, char *reply ) {
return r - reply;
}
typedef struct { int val; ot_torrent * torrent; } ot_record;
/* Fetches stats from tracker */
size_t return_stats_for_tracker( char *reply ) {
size_t return_stats_for_tracker( char *reply, int mode ) {
time_t time_now = NOW;
int torrent_count = 0, peer_count = 0, seed_count = 0;
ot_record top5s[5], top5c[5];
char *r = reply;
int i,j,k;
byte_zero( top5s, sizeof( top5s ) );
byte_zero( top5c, sizeof( top5c ) );
for( i=0; i<256; ++i ) {
ot_vector *torrents_list = &all_torrents[i];
torrent_count += torrents_list->size;
for( j=0; j<torrents_list->size; ++j ) {
ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list;
ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list;
int local_peers = 0, local_seeds = 0;
clean_peerlist( time_now, peer_list );
for( k=0; k<OT_POOLS_COUNT; ++k ) {
peer_count += peer_list->peers[k].size;
seed_count += peer_list->seed_count[k];
local_peers += peer_list->peers[k].size;
local_seeds += peer_list->seed_count[k];
}
if( mode == STATS_TOP5 ) {
int idx = 4; while( (idx >= 0) && ( local_peers > top5c[idx].val ) ) --idx;
if ( idx++ != 4 ) {
memmove( top5c + idx + 1, top5c + idx, ( 4 - idx ) * sizeof( ot_record ) );
top5c[idx].val = local_peers;
top5c[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
}
idx = 4; while( (idx >= 0) && ( local_seeds > top5s[idx].val ) ) --idx;
if ( idx++ != 4 ) {
memmove( top5s + idx + 1, top5s + idx, ( 4 - idx ) * sizeof( ot_record ) );
top5s[idx].val = local_seeds;
top5s[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
}
}
peer_count += local_peers; seed_count += local_seeds;
}
}
r += sprintf( r, "%i\n%i\nopentracker serving %i torrents\nSomething else.", peer_count, seed_count, torrent_count );
if( mode == STATS_TOP5 ) {
int idx;
r += sprintf( r, "Top5 torrents by peers:\n" );
for( idx=0; idx<5; ++idx )
if( top5c[idx].torrent )
r += sprintf( r, "\t%i\t%s\n", top5c[idx].val, to_hex(top5c[idx].torrent->hash) );
r += sprintf( r, "Top5 torrents by seeds:\n" );
for( idx=0; idx<5; ++idx )
if( top5s[idx].torrent )
r += sprintf( r, "\t%i\t%s\n", top5s[idx].val, to_hex(top5s[idx].torrent->hash) );
} else {
r += sprintf( r, "%i\n%i\nopentracker serving %i torrents\nSomething else.", peer_count, seed_count, torrent_count );
}
return r - reply;
}

Loading…
Cancel
Save