Fix configuration loading in tairentctl.
[tairent.git] / src / main / ratemeasurer.cpp
blobcaa2fcddec43a2aeb127b0572b12e0883191ab9d
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 David Brodsky *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
17 #include <tairon/net/timer.h>
19 #include "ratemeasurer.h"
21 namespace Tairent
24 namespace Main
27 /* {{{ RateMeasurer::RateMeasurer(double) */
28 RateMeasurer::RateMeasurer(double p) : period(p)
30 lastTime = (double) Tairon::Net::Timer::currentTime() / 1000;
31 startTime = lastTime - 1;
32 rate = 0;
34 /* }}} */
36 /* {{{ RateMeasurer::~RateMeasurer() */
37 RateMeasurer::~RateMeasurer()
40 /* }}} */
42 /* {{{ RateMeasurer::getRate() */
43 double RateMeasurer::getRate()
45 double t = (double) Tairon::Net::Timer::currentTime() / 1000;
46 if (lastTime + (period / 2) < t)
47 update(0);
48 return rate;
50 /* }}} */
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);
57 lastTime = t;
58 if (startTime < t - period)
59 startTime = t - period;
61 /* }}} */
63 }; // namespace Main
65 }; // namespace Tairent
67 // vim: ai sw=4 ts=4 noet fdm=marker