X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ffilter_session_shared.cpp;h=67f13e76dc41a333f4233e3358c840af51baab46;hb=fcce42024d4c6fa3ddaefc1a140ee22016db8ab7;hp=b1351bd369d3462e97c82cfa6b985467bbca8709;hpb=b35155fac04ac744e267e00a62d3db9cab23dcd7;p=metaproxy-moved-to-github.git diff --git a/src/filter_session_shared.cpp b/src/filter_session_shared.cpp index b1351bd..67f13e7 100644 --- a/src/filter_session_shared.cpp +++ b/src/filter_session_shared.cpp @@ -1,8 +1,20 @@ -/* $Id: filter_session_shared.cpp,v 1.13 2006-06-20 22:27:45 adam Exp $ - Copyright (c) 2005-2006, Index Data. +/* This file is part of Metaproxy. + Copyright (C) 2005-2008 Index Data - See the LICENSE file for details - */ +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 +Software Foundation; either version 2, or (at your option) any later +version. + +Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #include "config.hpp" @@ -34,6 +46,7 @@ namespace yf = metaproxy_1::filter; namespace metaproxy_1 { namespace filter { + // key for session.. We'll only share sessions with same InitKey class SessionShared::InitKey { public: bool operator < (const SessionShared::InitKey &k) const; @@ -41,13 +54,13 @@ namespace metaproxy_1 { InitKey(const InitKey &); ~InitKey(); private: - InitKey &operator = (const InitKey &k); char *m_idAuthentication_buf; int m_idAuthentication_size; char *m_otherInfo_buf; int m_otherInfo_size; ODR m_odr; }; + // worker thread .. for expiry of sessions class SessionShared::Worker { public: Worker(SessionShared::Rep *rep); @@ -55,6 +68,7 @@ namespace metaproxy_1 { private: SessionShared::Rep *m_p; }; + // backend result set class SessionShared::BackendSet { public: std::string m_result_set_id; @@ -72,6 +86,7 @@ namespace metaproxy_1 { const Z_APDU *apdu_req, const BackendInstancePtr bp); }; + // backend connection instance class SessionShared::BackendInstance { friend class Rep; friend class BackendClass; @@ -86,6 +101,7 @@ namespace metaproxy_1 { mp::Package * m_close_package; ~BackendInstance(); }; + // backends of some class (all with same InitKey) class SessionShared::BackendClass : boost::noncopyable { friend class Rep; friend struct Frontend; @@ -96,7 +112,7 @@ namespace metaproxy_1 { BackendInstancePtr get_backend(const Package &package); void use_backend(BackendInstancePtr b); void release_backend(BackendInstancePtr b); - void expire(); + void expire_class(); yazpp_1::GDU m_init_request; yazpp_1::GDU m_init_response; boost::mutex m_mutex_backend_class; @@ -105,9 +121,13 @@ namespace metaproxy_1 { time_t m_backend_expiry_ttl; size_t m_backend_set_max; public: - BackendClass(const yazpp_1::GDU &init_request); + BackendClass(const yazpp_1::GDU &init_request, + int resultset_ttl, + int resultset_max, + int session_ttl); ~BackendClass(); }; + // frontend result set class SessionShared::FrontendSet { Databases m_databases; yazpp_1::Yaz_Z_Query m_query; @@ -119,6 +139,7 @@ namespace metaproxy_1 { const yazpp_1::Yaz_Z_Query &query); FrontendSet(); }; + // frontend session struct SessionShared::Frontend { Frontend(Rep *rep); ~Frontend(); @@ -142,6 +163,7 @@ namespace metaproxy_1 { BackendClassPtr m_backend_class; FrontendSets m_frontend_sets; }; + // representation class SessionShared::Rep { friend class SessionShared; friend struct Frontend; @@ -161,6 +183,9 @@ namespace metaproxy_1 { BackendClassMap m_backend_map; boost::mutex m_mutex_backend_map; boost::thread_group m_thrds; + int m_resultset_ttl; + int m_resultset_max; + int m_session_ttl; }; } } @@ -369,10 +394,13 @@ yf::SessionShared::BackendInstancePtr yf::SessionShared::BackendClass::create_ba } -yf::SessionShared::BackendClass::BackendClass(const yazpp_1::GDU &init_request) +yf::SessionShared::BackendClass::BackendClass(const yazpp_1::GDU &init_request, + int resultset_ttl, + int resultset_max, + int session_ttl) : m_named_result_sets(false), m_init_request(init_request), - m_sequence_top(0), m_backend_set_ttl(30), - m_backend_expiry_ttl(30), m_backend_set_max(10) + m_sequence_top(0), m_backend_set_ttl(resultset_ttl), + m_backend_expiry_ttl(session_ttl), m_backend_set_max(resultset_max) {} yf::SessionShared::BackendClass::~BackendClass() @@ -392,17 +420,16 @@ void yf::SessionShared::Rep::init(mp::Package &package, const Z_GDU *gdu, it = m_backend_map.find(k); if (it == m_backend_map.end()) { - BackendClassPtr b(new BackendClass(gdu->u.z3950)); + BackendClassPtr b(new BackendClass(gdu->u.z3950, + m_resultset_ttl, + m_resultset_max, + m_session_ttl)); m_backend_map[k] = b; frontend->m_backend_class = b; - std::cout << "SessionShared::Rep::init new session " - << frontend->m_backend_class << "\n"; } else { frontend->m_backend_class = it->second; - std::cout << "SessionShared::Rep::init existing session " - << frontend->m_backend_class << "\n"; } } BackendClassPtr bc = frontend->m_backend_class; @@ -454,7 +481,7 @@ yf::SessionShared::BackendSet::BackendSet( } bool yf::SessionShared::BackendSet::search( - Package &frontend_package, + mp::Package &frontend_package, const Z_APDU *frontend_apdu, const BackendInstancePtr bp) { @@ -554,7 +581,6 @@ void yf::SessionShared::Frontend::override_set( found_backend = *it; result_set_id = (*set_it)->m_result_set_id; found_backend->m_sets.erase(set_it); - std::cout << "REUSE TTL SET: " << result_set_id << "\n"; return; } } @@ -575,7 +601,6 @@ void yf::SessionShared::Frontend::override_set( } else result_set_id = "default"; - std::cout << "AVAILABLE SET: " << result_set_id << "\n"; return; } } @@ -610,8 +635,6 @@ void yf::SessionShared::Frontend::get_set(mp::Package &package, found_backend = *it; bc->use_backend(found_backend); found_set->timestamp(); - std::cout << "MATCH SET: " << - found_set->m_result_set_id << "\n"; // found matching set. No need to search again return; } @@ -649,8 +672,6 @@ void yf::SessionShared::Frontend::get_set(mp::Package &package, package.response() = f_apdu; return; } - std::cout << "NEW " << found_backend << "\n"; - if (bc->m_named_result_sets) { result_set_id = boost::io::str( @@ -659,14 +680,12 @@ void yf::SessionShared::Frontend::get_set(mp::Package &package, } else result_set_id = "default"; - std::cout << "NEW SET: " << result_set_id << "\n"; } // we must search ... BackendSetPtr new_set(new BackendSet(result_set_id, databases, query)); if (!new_set->search(package, apdu_req, found_backend)) { - std::cout << "search error\n"; bc->remove_backend(found_backend); return; // search error } @@ -840,7 +859,7 @@ void yf::SessionShared::Worker::operator() (void) m_p->expire(); } -void yf::SessionShared::BackendClass::expire() +void yf::SessionShared::BackendClass::expire_class() { time_t now; time(&now); @@ -848,12 +867,10 @@ void yf::SessionShared::BackendClass::expire() BackendInstanceList::iterator bit = m_backend_list.begin(); while (bit != m_backend_list.end()) { - std::cout << "expiry "; time_t last_use = (*bit)->m_time_last_use; if ((*bit)->m_in_use) { - std::cout << "inuse"; bit++; } else if ((now >= last_use && now - last_use > m_backend_expiry_ttl) @@ -866,14 +883,11 @@ void yf::SessionShared::BackendClass::expire() (*bit)->m_close_package->move(); bit = m_backend_list.erase(bit); - std::cout << "erase"; } else { - std::cout << "keep"; bit++; } - std::cout << std::endl; } } @@ -885,16 +899,18 @@ void yf::SessionShared::Rep::expire() boost::xtime_get(&xt, boost::TIME_UTC); xt.sec += 30; boost::thread::sleep(xt); - std::cout << "." << std::endl; BackendClassMap::const_iterator b_it = m_backend_map.begin(); for (; b_it != m_backend_map.end(); b_it++) - b_it->second->expire(); + b_it->second->expire_class(); } } yf::SessionShared::Rep::Rep() { + m_resultset_ttl = 30; + m_resultset_max = 10; + m_session_ttl = 90; yf::SessionShared::Worker w(this); m_thrds.add_thread(new boost::thread(w)); } @@ -1023,6 +1039,54 @@ void yf::SessionShared::process(mp::Package &package) const m_p->release_frontend(package); } +void yf::SessionShared::configure(const xmlNode *ptr, bool test_only) +{ + for (ptr = ptr->children; ptr; ptr = ptr->next) + { + if (ptr->type != XML_ELEMENT_NODE) + continue; + if (!strcmp((const char *) ptr->name, "resultset")) + { + const struct _xmlAttr *attr; + for (attr = ptr->properties; attr; attr = attr->next) + { + if (!strcmp((const char *) attr->name, "ttl")) + m_p->m_resultset_ttl = + mp::xml::get_int(attr->children, 30); + else if (!strcmp((const char *) attr->name, "max")) + { + m_p->m_resultset_max = + mp::xml::get_int(attr->children, 10); + } + else + throw mp::filter::FilterException( + "Bad attribute " + std::string((const char *) + attr->name)); + } + } + else if (!strcmp((const char *) ptr->name, "session")) + { + const struct _xmlAttr *attr; + for (attr = ptr->properties; attr; attr = attr->next) + { + if (!strcmp((const char *) attr->name, "ttl")) + m_p->m_session_ttl = + mp::xml::get_int(attr->children, 120); + else + throw mp::filter::FilterException( + "Bad attribute " + std::string((const char *) + attr->name)); + } + } + else + { + throw mp::filter::FilterException("Bad element " + + std::string((const char *) + ptr->name)); + } + } +} + static mp::filter::Base* filter_creator() { return new mp::filter::SessionShared;