1 /***************************************************************************
3 * Copyright (C) 2006 David Brodsky *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation and appearing *
8 * in the file LICENSE.GPL included in the packaging of this file. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * General Public License for more details. *
15 ***************************************************************************/
17 #include <tairon/net/timer.h>
19 #include "ratemeasurer.h"
27 /* {{{ RateMeasurer::RateMeasurer(double) */
28 RateMeasurer::RateMeasurer(double p
) : period(p
)
30 lastTime
= (double) Tairon::Net::Timer::currentTime() / 1000;
31 startTime
= lastTime
- 1;
36 /* {{{ RateMeasurer::~RateMeasurer() */
37 RateMeasurer::~RateMeasurer()
42 /* {{{ RateMeasurer::getRate() */
43 double RateMeasurer::getRate()
45 double t
= (double) Tairon::Net::Timer::currentTime() / 1000;
46 if (lastTime
+ (period
/ 2) < t
)
52 /* {{{ RateMeasurer::update(unsigned int) */
53 void RateMeasurer::update(unsigned int amount
)
55 double t
= (double) Tairon::Net::Timer::currentTime() / 1000;
56 rate
= ((rate
* (lastTime
- startTime
)) + amount
) / (t
- startTime
);
58 if (startTime
< t
- period
)
59 startTime
= t
- period
;
65 }; // namespace Tairent
67 // vim: ai sw=4 ts=4 noet fdm=marker