Example config uses backendelementset
[yazproxy-moved-to-github.git] / src / yaz-bw.cpp
1 /* This file is part of YAZ proxy
2    Copyright (C) 1998-2011 Index Data
3
4 YAZ proxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include <time.h>
20 #include <yaz/log.h>
21 #include <yazproxy/bw.h>
22
23 Yaz_bw::Yaz_bw(int sz)
24 {
25     m_sec = 0;
26     m_size = sz;
27     m_bucket = new int[m_size];
28     m_ptr = 0;
29 }
30
31 Yaz_bw::~Yaz_bw()
32 {
33     delete [] m_bucket;
34 }
35
36 int Yaz_bw::get_total()
37 {
38     add_bytes(0);
39     int bw = 0;
40     int i;
41     for (i = 0; i<m_size; i++)
42         bw += m_bucket[i];
43     return bw;
44 }
45
46 void Yaz_bw::add_bytes(int b)
47 {
48     long now = time(0);
49
50     int d = now - m_sec;
51     if (d > m_size)
52         d = m_size;
53     while (--d >= 0)
54     {
55         if (++m_ptr == m_size)
56             m_ptr = 0;
57         m_bucket[m_ptr] = 0;
58     }
59     m_bucket[m_ptr] += b;
60     m_sec = now;
61 }
62
63 /*
64  * Local variables:
65  * c-basic-offset: 4
66  * c-file-style: "Stroustrup"
67  * indent-tabs-mode: nil
68  * End:
69  * vim: shiftwidth=4 tabstop=8 expandtab
70  */
71