Happy new year
[metaproxy-moved-to-github.git] / src / filter_limit.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 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     int sz = 0;
131     {
132         boost::mutex::scoped_lock scoped_lock(m_session_mutex);
133
134         yf::Limit::Ses *ses = 0;
135
136         std::map<mp::Session,yf::Limit::Ses *>::iterator it = 
137             m_sessions.find(package.session());
138         if (it != m_sessions.end())
139             ses = it->second;
140         else
141         {
142             ses = new yf::Limit::Ses;
143             m_sessions[package.session()] = ses;
144         }
145
146
147         Z_GDU *gdu = package.request().get();
148         if (gdu && gdu->which == Z_GDU_Z3950)
149         {
150             sz += package.request().get_size();
151             // we're getting a Z39.50 package
152             Z_APDU *apdu = gdu->u.z3950;
153             if (apdu->which == Z_APDU_searchRequest)
154                 ses->search_stat.add_bytes(1);
155             if (m_max_record_retrieve)
156             {
157                 if (apdu->which == Z_APDU_presentRequest)
158                 {
159                     Z_PresentRequest *pr = apdu->u.presentRequest;
160                     if (pr->numberOfRecordsRequested &&
161                         *pr->numberOfRecordsRequested > m_max_record_retrieve)
162                         *pr->numberOfRecordsRequested = m_max_record_retrieve;
163                 }
164             }
165         }
166     }
167     package.move();
168     int reduce = 0;
169     {
170         boost::mutex::scoped_lock scoped_lock(m_session_mutex);
171
172         yf::Limit::Ses *ses = 0;
173
174         std::map<mp::Session,yf::Limit::Ses *>::iterator it = 
175             m_sessions.find(package.session());
176         if (it != m_sessions.end())
177             ses = it->second;
178         else
179         {
180             ses = new yf::Limit::Ses;
181             m_sessions[package.session()] = ses;
182         }
183
184         sz += package.response().get_size();
185         
186         ses->bw_stat.add_bytes(sz);
187         ses->pdu_stat.add_bytes(1);
188         
189         int bw_total = ses->bw_stat.get_total();
190         int pdu_total = ses->pdu_stat.get_total();
191         int search_total = ses->search_stat.get_total();
192         
193         if (m_search_max)
194             reduce += search_total / m_search_max;
195         if (m_bw_max)
196             reduce += (bw_total/m_bw_max);
197         if (m_pdu_max)
198         {
199             if (pdu_total > m_pdu_max)
200             {
201                 int nreduce = (m_pdu_max >= 60) ? 1 : 60/m_pdu_max;
202                 reduce = (reduce > nreduce) ? reduce : nreduce;
203             }
204         }
205         if (package.session().is_closed())
206         {
207             m_sessions.erase(package.session());
208             delete ses;
209         }
210     }
211     if (reduce)
212     {
213         yaz_log(YLOG_LOG, "sleeping %d seconds", reduce);
214 #ifdef WIN32
215         Sleep(reduce * 1000);
216 #else
217         sleep(reduce);
218 #endif
219     }
220 }
221
222
223 static mp::filter::Base* filter_creator()
224 {
225     return new mp::filter::Limit;
226 }
227
228 extern "C" {
229     struct metaproxy_1_filter_struct metaproxy_1_filter_limit = {
230         0,
231         "limit",
232         filter_creator
233     };
234 }
235
236
237 /*
238  * Local variables:
239  * c-basic-offset: 4
240  * c-file-style: "Stroustrup"
241  * indent-tabs-mode: nil
242  * End:
243  * vim: shiftwidth=4 tabstop=8 expandtab
244  */
245