2010-03-08 04:29:58 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2010 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2010-10-01 13:33:39 +00:00
|
|
|
* $Id$
|
2010-03-08 04:29:58 +00:00
|
|
|
*/
|
|
|
|
|
2010-11-16 15:17:34 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2010-03-08 04:29:58 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "history.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
struct history_slice
|
|
|
|
{
|
2010-06-30 06:03:55 +00:00
|
|
|
unsigned int n;
|
2010-11-16 15:17:34 +00:00
|
|
|
time_t date;
|
2010-03-08 04:29:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_recentHistory
|
|
|
|
{
|
|
|
|
int newest;
|
|
|
|
int sliceCount;
|
2010-11-16 15:17:34 +00:00
|
|
|
unsigned int precision;
|
2010-03-08 04:29:58 +00:00
|
|
|
struct history_slice * slices;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2010-11-16 15:17:34 +00:00
|
|
|
tr_historyAdd( tr_recentHistory * h, time_t now, unsigned int n )
|
2010-03-08 04:29:58 +00:00
|
|
|
{
|
2010-11-16 15:17:34 +00:00
|
|
|
if( h->slices[h->newest].date + h->precision >= now )
|
2010-03-08 04:29:58 +00:00
|
|
|
h->slices[h->newest].n += n;
|
|
|
|
else {
|
|
|
|
if( ++h->newest == h->sliceCount ) h->newest = 0;
|
|
|
|
h->slices[h->newest].date = now;
|
|
|
|
h->slices[h->newest].n = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-30 06:03:55 +00:00
|
|
|
unsigned int
|
2010-11-16 15:17:34 +00:00
|
|
|
tr_historyGet( const tr_recentHistory * h, time_t now, unsigned int sec )
|
2010-03-08 04:29:58 +00:00
|
|
|
{
|
2010-06-30 06:03:55 +00:00
|
|
|
unsigned int n = 0;
|
2010-11-16 15:17:34 +00:00
|
|
|
const time_t cutoff = (now?now:tr_time()) - sec;
|
2010-03-08 04:29:58 +00:00
|
|
|
int i = h->newest;
|
|
|
|
|
|
|
|
for( ;; )
|
|
|
|
{
|
|
|
|
if( h->slices[i].date <= cutoff )
|
|
|
|
break;
|
|
|
|
|
|
|
|
n += h->slices[i].n;
|
|
|
|
|
|
|
|
if( --i == -1 ) i = h->sliceCount - 1; /* circular history */
|
|
|
|
if( i == h->newest ) break; /* we've come all the way around */
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_recentHistory *
|
2010-11-16 15:17:34 +00:00
|
|
|
tr_historyNew( unsigned int seconds, unsigned int precision )
|
2010-03-08 04:29:58 +00:00
|
|
|
{
|
|
|
|
tr_recentHistory * h;
|
|
|
|
|
2010-11-16 15:17:34 +00:00
|
|
|
assert( precision <= seconds );
|
|
|
|
|
2010-03-08 04:29:58 +00:00
|
|
|
h = tr_new0( tr_recentHistory, 1 );
|
2010-11-16 15:17:34 +00:00
|
|
|
h->precision = precision;
|
|
|
|
h->sliceCount = seconds / precision;
|
2010-03-08 04:29:58 +00:00
|
|
|
h->slices = tr_new0( struct history_slice, h->sliceCount );
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_historyFree( tr_recentHistory * h )
|
|
|
|
{
|
|
|
|
tr_free( h->slices );
|
|
|
|
tr_free( h );
|
|
|
|
}
|