X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ffilter_limit.cpp;h=a3b3add40ffad654991d3ad04d60253bb70090ea;hb=586d78659d671683f33ec55f4a7d32b28e345ccd;hp=f2c7399b76e13924d9717b73e8f2ae0f51487cad;hpb=0ffe3085807575717988cf9ccbac846f43f6a46a;p=metaproxy-moved-to-github.git diff --git a/src/filter_limit.cpp b/src/filter_limit.cpp index f2c7399..a3b3add 100644 --- a/src/filter_limit.cpp +++ b/src/filter_limit.cpp @@ -1,5 +1,5 @@ /* This file is part of Metaproxy. - Copyright (C) 2005-2009 Index Data + Copyright (C) Index Data Metaproxy is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -22,8 +22,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include -#include "package.hpp" -#include "util.hpp" +#include +#include +#ifdef WIN32 +#include +#endif namespace mp = metaproxy_1; namespace yf = mp::filter; @@ -56,7 +59,7 @@ namespace metaproxy_1 { } // define Pimpl wrapper forwarding to Impl - + yf::Limit::Limit() : m_p(new Impl) { } @@ -65,7 +68,8 @@ yf::Limit::~Limit() { // must have a destructor because of boost::scoped_ptr } -void yf::Limit::configure(const xmlNode *xmlnode, bool test_only) +void yf::Limit::configure(const xmlNode *xmlnode, bool test_only, + const char *path) { m_p->configure(xmlnode); } @@ -84,7 +88,7 @@ yf::Limit::Impl::Impl() : m_bw_max(0), m_pdu_max(0), m_search_max(0), } yf::Limit::Impl::~Impl() -{ +{ } void yf::Limit::Impl::configure(const xmlNode *ptr) @@ -115,7 +119,7 @@ void yf::Limit::Impl::configure(const xmlNode *ptr) } else { - throw mp::filter::FilterException("Bad element " + throw mp::filter::FilterException("Bad element " + std::string((const char *) ptr->name)); } @@ -124,15 +128,13 @@ void yf::Limit::Impl::configure(const xmlNode *ptr) void yf::Limit::Impl::process(mp::Package &package) { - package.move(); - int reduce = 0; - + int sz = 0; { boost::mutex::scoped_lock scoped_lock(m_session_mutex); yf::Limit::Ses *ses = 0; - std::map::iterator it = + std::map::iterator it = m_sessions.find(package.session()); if (it != m_sessions.end()) ses = it->second; @@ -142,14 +144,11 @@ void yf::Limit::Impl::process(mp::Package &package) m_sessions[package.session()] = ses; } - int sz = package.request().get_size() + package.response().get_size(); - - ses->bw_stat.add_bytes(sz); - ses->pdu_stat.add_bytes(1); - + Z_GDU *gdu = package.request().get(); if (gdu && gdu->which == Z_GDU_Z3950) { + sz += package.request().get_size(); // we're getting a Z39.50 package Z_APDU *apdu = gdu->u.z3950; if (apdu->which == Z_APDU_searchRequest) @@ -165,11 +164,33 @@ void yf::Limit::Impl::process(mp::Package &package) } } } - + } + package.move(); + int reduce = 0; + { + boost::mutex::scoped_lock scoped_lock(m_session_mutex); + + yf::Limit::Ses *ses = 0; + + std::map::iterator it = + m_sessions.find(package.session()); + if (it != m_sessions.end()) + ses = it->second; + else + { + ses = new yf::Limit::Ses; + m_sessions[package.session()] = ses; + } + + sz += package.response().get_size(); + + ses->bw_stat.add_bytes(sz); + ses->pdu_stat.add_bytes(1); + int bw_total = ses->bw_stat.get_total(); int pdu_total = ses->pdu_stat.get_total(); int search_total = ses->search_stat.get_total(); - + if (m_search_max) reduce += search_total / m_search_max; if (m_bw_max) @@ -183,12 +204,19 @@ void yf::Limit::Impl::process(mp::Package &package) } } if (package.session().is_closed()) + { m_sessions.erase(package.session()); + delete ses; + } } if (reduce) { yaz_log(YLOG_LOG, "sleeping %d seconds", reduce); +#ifdef WIN32 + Sleep(reduce * 1000); +#else sleep(reduce); +#endif } }