Use Win32 Sleep
[metaproxy-moved-to-github.git] / src / filter_limit.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2010 Index Data
3
4 Metaproxy 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 Metaproxy 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 "config.hpp"
20 #include "filter_limit.hpp"
21
22 #include <time.h>
23 #include <yaz/log.h>
24 #include <yazpp/timestat.h>
25 #include <metaproxy/package.hpp>
26 #include <metaproxy/util.hpp>
27 #ifdef WIN32
28 #include <windows.h>
29 #endif
30
31 namespace mp = metaproxy_1;
32 namespace yf = mp::filter;
33
34 namespace metaproxy_1 {
35     namespace filter {
36         class Limit::Ses {
37         public:
38             yazpp_1::TimeStat bw_stat;
39             yazpp_1::TimeStat pdu_stat;
40             yazpp_1::TimeStat search_stat;
41             Ses() : bw_stat(60), pdu_stat(60), search_stat(60) {};
42         };
43
44         class Limit::Impl {
45         public:
46             Impl();
47             ~Impl();
48             void process(metaproxy_1::Package & package);
49             void configure(const xmlNode * ptr);
50         private:
51             boost::mutex m_session_mutex;
52             std::map<mp::Session,Limit::Ses *> m_sessions;
53             int m_bw_max;
54             int m_pdu_max;
55             int m_search_max;
56             int m_max_record_retrieve;
57         };
58     }
59 }
60
61 // define Pimpl wrapper forwarding to Impl
62  
63 yf::Limit::Limit() : m_p(new Impl)
64 {
65 }
66
67 yf::Limit::~Limit()
68 {  // must have a destructor because of boost::scoped_ptr
69 }
70
71 void yf::Limit::configure(const xmlNode *xmlnode, bool test_only)
72 {
73     m_p->configure(xmlnode);
74 }
75
76 void yf::Limit::process(mp::Package &package) const
77 {
78     m_p->process(package);
79 }
80
81
82 // define Implementation stuff
83
84 yf::Limit::Impl::Impl() : m_bw_max(0), m_pdu_max(0), m_search_max(0),
85                           m_max_record_retrieve(0)
86 {
87 }
88
89 yf::Limit::Impl::~Impl()
90
91 }
92
93 void yf::Limit::Impl::configure(const xmlNode *ptr)
94 {
95     for (ptr = ptr->children; ptr; ptr = ptr->next)
96     {
97         if (ptr->type != XML_ELEMENT_NODE)
98             continue;
99         if (!strcmp((const char *) ptr->name, "limit"))
100         {
101             const struct _xmlAttr *attr;
102             for (attr = ptr->properties; attr; attr = attr->next)
103             {
104                 if (!strcmp((const char *) attr->name, "bandwidth"))
105                     m_bw_max = mp::xml::get_int(attr->children, 0);
106                 else if (!strcmp((const char *) attr->name, "pdu"))
107                     m_pdu_max = mp::xml::get_int(attr->children, 0);
108                 else if (!strcmp((const char *) attr->name, "search"))
109                     m_search_max = mp::xml::get_int(attr->children, 0);
110                 else if (!strcmp((const char *) attr->name, "retrieve"))
111                     m_max_record_retrieve =
112                         mp::xml::get_int(attr->children, 0);
113                 else
114                     throw mp::filter::FilterException(
115                         "Bad attribute " + std::string((const char *)
116                                                        attr->name));
117             }
118         }
119         else
120         {
121             throw mp::filter::FilterException("Bad element " 
122                                                + std::string((const char *)
123                                                              ptr->name));
124         }
125     }
126 }
127
128 void yf::Limit::Impl::process(mp::Package &package)
129 {
130     package.move();
131     int reduce = 0;
132
133     {
134         boost::mutex::scoped_lock scoped_lock(m_session_mutex);
135
136         yf::Limit::Ses *ses = 0;
137
138         std::map<mp::Session,yf::Limit::Ses *>::iterator it = 
139             m_sessions.find(package.session());
140         if (it != m_sessions.end())
141             ses = it->second;
142         else
143         {
144             ses = new yf::Limit::Ses;
145             m_sessions[package.session()] = ses;
146         }
147
148         int sz = package.request().get_size() + package.response().get_size();
149         
150         ses->bw_stat.add_bytes(sz);
151         ses->pdu_stat.add_bytes(1);
152         
153         Z_GDU *gdu = package.request().get();
154         if (gdu && gdu->which == Z_GDU_Z3950)
155         {
156             // we're getting a Z39.50 package
157             Z_APDU *apdu = gdu->u.z3950;
158             if (apdu->which == Z_APDU_searchRequest)
159                 ses->search_stat.add_bytes(1);
160             if (m_max_record_retrieve)
161             {
162                 if (apdu->which == Z_APDU_presentRequest)
163                 {
164                     Z_PresentRequest *pr = apdu->u.presentRequest;
165                     if (pr->numberOfRecordsRequested &&
166                         *pr->numberOfRecordsRequested > m_max_record_retrieve)
167                         *pr->numberOfRecordsRequested = m_max_record_retrieve;
168                 }
169             }
170         }
171         
172         int bw_total = ses->bw_stat.get_total();
173         int pdu_total = ses->pdu_stat.get_total();
174         int search_total = ses->search_stat.get_total();
175         
176         if (m_search_max)
177             reduce += search_total / m_search_max;
178         if (m_bw_max)
179             reduce += (bw_total/m_bw_max);
180         if (m_pdu_max)
181         {
182             if (pdu_total > m_pdu_max)
183             {
184                 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
185                 reduce = (reduce > nreduce) ? reduce : nreduce;
186             }
187         }
188         if (package.session().is_closed())
189         {
190             m_sessions.erase(package.session());
191             delete ses;
192         }
193     }
194     if (reduce)
195     {
196         yaz_log(YLOG_LOG, "sleeping %d seconds", reduce);
197 #ifdef WIN32
198         Sleep(reduce * 1000);
199 #else
200         sleep(reduce);
201 #endif
202     }
203 }
204
205
206 static mp::filter::Base* filter_creator()
207 {
208     return new mp::filter::Limit;
209 }
210
211 extern "C" {
212     struct metaproxy_1_filter_struct metaproxy_1_filter_limit = {
213         0,
214         "limit",
215         filter_creator
216     };
217 }
218
219
220 /*
221  * Local variables:
222  * c-basic-offset: 4
223  * c-file-style: "Stroustrup"
224  * indent-tabs-mode: nil
225  * End:
226  * vim: shiftwidth=4 tabstop=8 expandtab
227  */
228