2010-03-08 04:29:58 +00:00
|
|
|
/*
|
2014-01-18 20:56:57 +00:00
|
|
|
* This file Copyright (C) 2010-2014 Mnemosyne LLC
|
2010-03-08 04:29:58 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2010-03-08 04:29:58 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-04-21 07:40:57 +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"
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_historyAdd(tr_recentHistory* h, time_t now, unsigned int n)
|
2010-03-08 04:29:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (h->slices[h->newest].date == now)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
h->slices[h->newest].n += n;
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned int tr_historyGet(tr_recentHistory const* h, time_t now, unsigned int sec)
|
2010-03-08 04:29:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
unsigned int n = 0;
|
2017-04-30 16:25:26 +00:00
|
|
|
time_t const cutoff = (now != 0 ? now : tr_time()) - sec;
|
2017-04-19 12:04:45 +00:00
|
|
|
int i = h->newest;
|
2010-03-08 04:29:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2010-03-08 04:29:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (h->slices[i].date <= cutoff)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n += h->slices[i].n;
|
|
|
|
|
|
|
|
if (--i == -1)
|
|
|
|
{
|
|
|
|
i = TR_RECENT_HISTORY_PERIOD_SEC - 1; /* circular history */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == h->newest)
|
|
|
|
{
|
|
|
|
break; /* we've come all the way around */
|
|
|
|
}
|
2010-03-08 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return n;
|
2010-03-08 04:29:58 +00:00
|
|
|
}
|