X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=src%2Ffilter_virt_db.cpp;h=e6f23383d597420c59e398508c1e983885621ddb;hb=8d19ec229f799ca5f13fdf4e64ec9d877d95a92a;hp=a7d0ae8c2dcabb8236efdfbdd5b6bbc455ed9b4e;hpb=0d323a3f455c0a0a669fa33e77b670d1af23fec5;p=metaproxy-moved-to-github.git diff --git a/src/filter_virt_db.cpp b/src/filter_virt_db.cpp index a7d0ae8..e6f2338 100644 --- a/src/filter_virt_db.cpp +++ b/src/filter_virt_db.cpp @@ -1,5 +1,5 @@ /* This file is part of Metaproxy. - Copyright (C) 2005-2009 Index Data + Copyright (C) 2005-2011 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 @@ -18,20 +18,21 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "config.hpp" -#include "filter.hpp" -#include "package.hpp" +#include "filter_virt_db.hpp" +#include #include #include #include -#include "util.hpp" -#include "filter_virt_db.hpp" +#include +#include "torus.hpp" #include #include #include #include +#include #include #include @@ -79,6 +80,7 @@ namespace metaproxy_1 { void search(Package &package, Z_APDU *apdu); void present(Package &package, Z_APDU *apdu); void scan(Package &package, Z_APDU *apdu); + int relay_apdu(Package &package, Z_APDU *apdu); void close(Package &package); typedef std::map::iterator Sets_it; @@ -114,6 +116,7 @@ namespace metaproxy_1 { boost::condition m_cond_session_ready; std::map m_clients; bool pass_vhosts; + mp::Torus torus; }; } } @@ -212,6 +215,8 @@ yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::init_backend( req->implementationId = org_init->implementationId; req->implementationName = org_init->implementationName; req->implementationVersion = org_init->implementationVersion; + *req->preferredMessageSize = *org_init->preferredMessageSize; + *req->maximumRecordSize = *org_init->maximumRecordSize; ODR_MASK_SET(req->options, Z_Options_search); ODR_MASK_SET(req->options, Z_Options_present); @@ -228,21 +233,21 @@ yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::init_backend( Z_GDU *gdu = init_package.response().get(); // we hope to get an init response - if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which == - Z_APDU_initResponse) + error_code = YAZ_BIB1_DATABASE_UNAVAILABLE; + if (gdu && gdu->which == Z_GDU_Z3950 + && gdu->u.z3950->which == Z_APDU_initResponse + && *gdu->u.z3950->u.initResponse->result) { Z_InitResponse *res = gdu->u.z3950->u.initResponse; if (ODR_MASK_GET(res->options, Z_Options_namedResultSets)) { b->m_named_result_sets = true; } - if (*res->result) + if (*res->result && !init_package.session().is_closed()) { m_backend_list.push_back(b); return b; - } - error_code = YAZ_BIB1_DATABASE_UNAVAILABLE; mp::util::get_init_diagnostics(res, error_code, addinfo); } if (!init_package.session().is_closed()) @@ -268,7 +273,6 @@ void yf::VirtualDB::Frontend::search(mp::Package &package, Z_APDU *apdu_req) for (i = 0; inum_databaseNames; i++) databases.push_back(req->databaseNames[i]); - BackendPtr b; // null for now Sets_it sets_it = m_sets.find(req->resultSetName); if (sets_it != m_sets.end()) { @@ -287,37 +291,22 @@ void yf::VirtualDB::Frontend::search(mp::Package &package, Z_APDU *apdu_req) return; } sets_it->second.m_backend->m_number_of_sets--; - - // pick up any existing backend with a database match - std::list::const_iterator map_it; - map_it = m_backend_list.begin(); - for (; map_it != m_backend_list.end(); map_it++) - { - BackendPtr tmp = *map_it; - if (tmp->m_frontend_databases == databases) - break; - } - if (map_it != m_backend_list.end()) - b = *map_it; } - else + // pick up any existing database with named result sets .. + // or one which has no result sets.. yet. + BackendPtr b; // null for now + std::list::const_iterator map_it; + map_it = m_backend_list.begin(); + for (; map_it != m_backend_list.end(); map_it++) { - // new result set. - - // pick up any existing database with named result sets .. - // or one which has no result sets.. yet. - std::list::const_iterator map_it; - map_it = m_backend_list.begin(); - for (; map_it != m_backend_list.end(); map_it++) + BackendPtr tmp = *map_it; + if (tmp->m_frontend_databases == databases && + (tmp->m_named_result_sets || + tmp->m_number_of_sets == 0)) { - BackendPtr tmp = *map_it; - if (tmp->m_frontend_databases == databases && - (tmp->m_named_result_sets || - tmp->m_number_of_sets == 0)) - break; - } - if (map_it != m_backend_list.end()) b = *map_it; + break; + } } if (!b) // no backend yet. Must create a new one { @@ -450,6 +439,59 @@ void yf::VirtualDB::Rep::release_frontend(mp::Package &package) } } +void yf::VirtualDB::refresh_torus(void) +{ + xmlDoc *doc = m_p->torus.get_doc(); + xmlNode *ptr1 = 0; + if (doc && (ptr1 = xmlDocGetRootElement(doc))) + { + for (ptr1 = ptr1->children; ptr1; ptr1 = ptr1->next) + { + if (ptr1->type != XML_ELEMENT_NODE) + continue; + if (!strcmp((const char *) ptr1->name, "record")) + { + xmlNode *ptr2 = ptr1; + for (ptr2 = ptr2->children; ptr2; ptr2 = ptr2->next) + { + if (ptr2->type != XML_ELEMENT_NODE) + continue; + if (!strcmp((const char *) ptr2->name, "layer")) + { + std::string database; + std::string target; + std::string route; + std::string solr; + xmlNode *ptr3 = ptr2; + for (ptr3 = ptr3->children; ptr3; ptr3 = ptr3->next) + { + if (ptr3->type != XML_ELEMENT_NODE) + continue; + if (!strcmp((const char *) ptr3->name, "id")) + { + database = mp::xml::get_text(ptr3); + } + else if (!strcmp((const char *) ptr3->name, "zurl")) + { + target = mp::xml::get_text(ptr3); + } + else if (!strcmp((const char *) ptr3->name, "sru")) + { + solr = mp::xml::get_text(ptr3); + } + } + if (solr.length() == 0 && + database.length() && target.length()) + { + add_map_db2target(database, target, route); + } + } + } + } + } + } +} + yf::VirtualDB::Set::Set(BackendPtr b, std::string setname) : m_backend(b), m_setname(setname) { @@ -617,6 +659,27 @@ void yf::VirtualDB::Frontend::present(mp::Package &package, Z_APDU *apdu_req) delete id; } +int yf::VirtualDB::Frontend::relay_apdu(mp::Package &package, Z_APDU *apdu_req) +{ + int no = 0; + std::list::const_iterator map_it; + map_it = m_backend_list.begin(); + for (; map_it != m_backend_list.end(); map_it++) + { + BackendPtr b = *map_it; + + Package relay_package(b->m_backend_session, package.origin()); + relay_package.copy_filter(package); + + relay_package.request() = yazpp_1::GDU(apdu_req); + + relay_package.move(b->m_route); + package.response() = relay_package.response(); + no++; + } + return no; +} + void yf::VirtualDB::Frontend::scan(mp::Package &package, Z_APDU *apdu_req) { Z_ScanRequest *req = apdu_req->u.scanRequest; @@ -684,8 +747,8 @@ void yf::VirtualDB::Frontend::scan(mp::Package &package, Z_APDU *apdu_req) void yf::VirtualDB::add_map_db2targets(std::string db, - std::list targets, - std::string route) + std::list targets, + std::string route) { m_p->m_maps.push_back( VirtualDB::Map(mp::util::database_name_normalize(db), targets, route)); @@ -693,8 +756,8 @@ void yf::VirtualDB::add_map_db2targets(std::string db, void yf::VirtualDB::add_map_db2target(std::string db, - std::string target, - std::string route) + std::string target, + std::string route) { std::list targets; targets.push_back(target); @@ -752,6 +815,9 @@ void yf::VirtualDB::process(mp::Package &package) const else break; + *resp->preferredMessageSize = *req->preferredMessageSize; + *resp->maximumRecordSize = *req->maximumRecordSize; + package.response() = apdu; f->m_is_virtual = true; } @@ -786,7 +852,15 @@ void yf::VirtualDB::process(mp::Package &package) const } else if (apdu->which == Z_APDU_close) { - package.session().close(); + if (f->relay_apdu(package, apdu) == 0) + { + mp::odr odr; + + package.response() = odr.create_close( + apdu, Z_Close_finished, "virt_db"); + + package.session().close(); + } } else { @@ -802,7 +876,6 @@ void yf::VirtualDB::process(mp::Package &package) const m_p->release_frontend(package); } - void mp::filter::VirtualDB::configure(const xmlNode * ptr, bool test_only) { for (ptr = ptr->children; ptr; ptr = ptr->next) @@ -837,6 +910,22 @@ void mp::filter::VirtualDB::configure(const xmlNode * ptr, bool test_only) std::string route = mp::xml::get_route(ptr); add_map_db2targets(database, targets, route); } + else if (!strcmp((const char *) ptr->name, "torus")) + { + std::string url; + const struct _xmlAttr *attr; + for (attr = ptr->properties; attr; attr = attr->next) + { + if (!strcmp((const char *) attr->name, "url")) + url = mp::xml::get_text(attr->children); + else + throw mp::filter::FilterException( + "Bad attribute " + std::string((const char *) + attr->name)); + } + m_p->torus.read_searchables(url); + refresh_torus(); + } else { throw mp::filter::FilterException