period-connect which specifies the period we do connect statistics
[yazproxy-moved-to-github.git] / src / limit-connect.cpp
1 /* $Id: limit-connect.cpp,v 1.2 2006-04-06 16:25:21 adam Exp $
2    Copyright (c) 1998-2006, Index Data.
3
4 This file is part of the yazproxy.
5
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20 */
21
22 #include <yazproxy/limit-connect.h>
23
24 #include <time.h>
25 #include <string.h>
26 #include <yaz/xmalloc.h>
27
28 struct LimitConnect::Peer {
29     friend class LimitConnect;
30     
31     Peer(int sz, const char *peername);
32     ~Peer();
33     void add_connect();
34     
35     char *m_peername;
36     Yaz_bw m_bw;
37     Peer *m_next;
38 };
39
40 LimitConnect::LimitConnect()
41 {
42     m_period = 60;
43     m_peers = 0;
44 }
45
46
47 LimitConnect::~LimitConnect()
48 {
49     cleanup(true);
50 }
51
52 void LimitConnect::set_period(int sec)
53 {
54     m_period = sec;
55 }
56
57 LimitConnect::Peer::Peer(int sz, const char *peername) : m_bw(sz)
58 {
59     m_peername = xstrdup(peername);
60     m_next = 0;
61 }
62
63 LimitConnect::Peer::~Peer()
64 {
65     xfree(m_peername);
66 }
67
68 void LimitConnect::Peer::add_connect()
69 {
70     m_bw.add_bytes(1);
71 }
72
73 LimitConnect::Peer **LimitConnect::lookup(const char *peername)
74 {
75     Peer **p = &m_peers;
76     while (*p)
77     {
78         if (!strcmp((*p)->m_peername, peername))
79             break;
80         p = &(*p)->m_next;
81     }
82     return p;
83 }
84
85 void LimitConnect::add_connect(const char *peername)
86 {
87     Peer **p = lookup(peername);
88     if (!*p)
89         *p = new Peer(m_period, peername);
90     (*p)->add_connect();
91 }
92
93 int LimitConnect::get_total(const char *peername)
94 {
95     Peer **p = lookup(peername);
96     if (!*p)
97         return 0;
98     return (*p)->m_bw.get_total();
99 }
100
101 void LimitConnect::cleanup(bool all)
102 {
103     Peer **p = &m_peers;
104     while (*p)
105     {
106         Peer *tp = *p;
107         if (all || (tp->m_bw.get_total() == 0))
108         {
109             *p = tp->m_next;
110             delete tp;
111         }
112         else
113             p = &tp->m_next;
114     }
115 }
116
117 /*
118  * Local variables:
119  * c-basic-offset: 4
120  * indent-tabs-mode: nil
121  * End:
122  * vim: shiftwidth=4 tabstop=8 expandtab
123  */