2010-03-08 04:29:58 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2010-03-08 04:29:58 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2012-12-05 17:29:46 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2 (b)
|
2010-03-08 04:29:58 +00:00
|
|
|
* 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>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <string.h> /* memset () */
|
2010-11-16 15:17:34 +00:00
|
|
|
|
2010-03-08 04:29:58 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "history.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_historyAdd (tr_recentHistory * h, time_t now, unsigned int n)
|
2010-03-08 04:29:58 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (h->slices[h->newest].date == now)
|
|
|
|
{
|
|
|
|
h->slices[h->newest].n += n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (++h->newest == TR_RECENT_HISTORY_PERIOD_SEC)
|
|
|
|
h->newest = 0;
|
|
|
|
h->slices[h->newest].date = now;
|
|
|
|
h->slices[h->newest].n = n;
|
2010-03-08 04:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-30 06:03:55 +00:00
|
|
|
unsigned int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_historyGet (const tr_recentHistory * h, time_t now, unsigned int sec)
|
2010-03-08 04:29:58 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
unsigned int n = 0;
|
|
|
|
const time_t cutoff = (now?now:tr_time ()) - sec;
|
|
|
|
int i = h->newest;
|
2010-03-08 04:29:58 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (;;)
|
2010-03-08 04:29:58 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (h->slices[i].date <= cutoff)
|
|
|
|
break;
|
|
|
|
|
|
|
|
n += h->slices[i].n;
|
2010-03-08 04:29:58 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (--i == -1)
|
|
|
|
i = TR_RECENT_HISTORY_PERIOD_SEC - 1; /* circular history */
|
2010-03-08 04:29:58 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (i == h->newest)
|
|
|
|
break; /* we've come all the way around */
|
2010-03-08 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return n;
|
2010-03-08 04:29:58 +00:00
|
|
|
}
|