summaryrefslogtreecommitdiff
path: root/trackerlogic.c
diff options
context:
space:
mode:
authorerdgeist <>2007-01-17 11:51:55 +0000
committererdgeist <>2007-01-17 11:51:55 +0000
commit005ff4e2313da86eb01c939c1859b92608e181c7 (patch)
treee1f4c341fdc5d255b42a50d66d3096ba08aad71b /trackerlogic.c
parent4eefe49307440649133b93ba9a1afeb2b7e2c862 (diff)
Simple top5 by peers/seeders table
Diffstat (limited to 'trackerlogic.c')
-rw-r--r--trackerlogic.c44
1 files changed, 39 insertions, 5 deletions
diff --git a/trackerlogic.c b/trackerlogic.c
index 2a79df2..b044245 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -311,26 +311,60 @@ size_t return_scrape_for_torrent( ot_hash *hash, char *reply ) {
311 return r - reply; 311 return r - reply;
312} 312}
313 313
314typedef struct { int val; ot_torrent * torrent; } ot_record;
315
314/* Fetches stats from tracker */ 316/* Fetches stats from tracker */
315size_t return_stats_for_tracker( char *reply ) { 317size_t return_stats_for_tracker( char *reply, int mode ) {
316 time_t time_now = NOW; 318 time_t time_now = NOW;
317 int torrent_count = 0, peer_count = 0, seed_count = 0; 319 int torrent_count = 0, peer_count = 0, seed_count = 0;
320 ot_record top5s[5], top5c[5];
318 char *r = reply; 321 char *r = reply;
319 int i,j,k; 322 int i,j,k;
320 323
324 byte_zero( top5s, sizeof( top5s ) );
325 byte_zero( top5c, sizeof( top5c ) );
326
321 for( i=0; i<256; ++i ) { 327 for( i=0; i<256; ++i ) {
322 ot_vector *torrents_list = &all_torrents[i]; 328 ot_vector *torrents_list = &all_torrents[i];
323 torrent_count += torrents_list->size; 329 torrent_count += torrents_list->size;
324 for( j=0; j<torrents_list->size; ++j ) { 330 for( j=0; j<torrents_list->size; ++j ) {
325 ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; 331 ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list;
332 int local_peers = 0, local_seeds = 0;
326 clean_peerlist( time_now, peer_list ); 333 clean_peerlist( time_now, peer_list );
327 for( k=0; k<OT_POOLS_COUNT; ++k ) { 334 for( k=0; k<OT_POOLS_COUNT; ++k ) {
328 peer_count += peer_list->peers[k].size; 335 local_peers += peer_list->peers[k].size;
329 seed_count += peer_list->seed_count[k]; 336 local_seeds += peer_list->seed_count[k];
337 }
338 if( mode == STATS_TOP5 ) {
339 int idx = 4; while( (idx >= 0) && ( local_peers > top5c[idx].val ) ) --idx;
340 if ( idx++ != 4 ) {
341 memmove( top5c + idx + 1, top5c + idx, ( 4 - idx ) * sizeof( ot_record ) );
342 top5c[idx].val = local_peers;
343 top5c[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
344 }
345 idx = 4; while( (idx >= 0) && ( local_seeds > top5s[idx].val ) ) --idx;
346 if ( idx++ != 4 ) {
347 memmove( top5s + idx + 1, top5s + idx, ( 4 - idx ) * sizeof( ot_record ) );
348 top5s[idx].val = local_seeds;
349 top5s[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
350 }
330 } 351 }
352 peer_count += local_peers; seed_count += local_seeds;
331 } 353 }
332 } 354 }
333 r += sprintf( r, "%i\n%i\nopentracker serving %i torrents\nSomething else.", peer_count, seed_count, torrent_count ); 355 if( mode == STATS_TOP5 ) {
356 int idx;
357 r += sprintf( r, "Top5 torrents by peers:\n" );
358 for( idx=0; idx<5; ++idx )
359 if( top5c[idx].torrent )
360 r += sprintf( r, "\t%i\t%s\n", top5c[idx].val, to_hex(top5c[idx].torrent->hash) );
361 r += sprintf( r, "Top5 torrents by seeds:\n" );
362 for( idx=0; idx<5; ++idx )
363 if( top5s[idx].torrent )
364 r += sprintf( r, "\t%i\t%s\n", top5s[idx].val, to_hex(top5s[idx].torrent->hash) );
365 } else {
366 r += sprintf( r, "%i\n%i\nopentracker serving %i torrents\nSomething else.", peer_count, seed_count, torrent_count );
367 }
334 368
335 return r - reply; 369 return r - reply;
336} 370}