From c93d8c8db5ae24e01775060677f8e8e9c2afff34 Mon Sep 17 00:00:00 2001 From: Marc Cromme Date: Fri, 29 Sep 2006 08:42:47 +0000 Subject: [PATCH] code cleanup, replaced class name Virt_db with VirtualDB moved general helper functions from sru filter to util.cpp and util.hpp --- src/ex_filter_frontend_net.cpp | 4 +- src/filter_sru_to_z3950.cpp | 49 +++++----------------- src/filter_virt_db.cpp | 90 ++++++++++++++++++++-------------------- src/filter_virt_db.hpp | 8 ++-- src/test_filter_virt_db.cpp | 8 ++-- src/util.cpp | 23 ++++++++-- src/util.hpp | 27 ++++++++++-- 7 files changed, 108 insertions(+), 101 deletions(-) diff --git a/src/ex_filter_frontend_net.cpp b/src/ex_filter_frontend_net.cpp index 0b856e3..02383e1 100644 --- a/src/ex_filter_frontend_net.cpp +++ b/src/ex_filter_frontend_net.cpp @@ -1,4 +1,4 @@ -/* $Id: ex_filter_frontend_net.cpp,v 1.27 2006-06-10 14:29:11 adam Exp $ +/* $Id: ex_filter_frontend_net.cpp,v 1.28 2006-09-29 08:42:47 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -105,7 +105,7 @@ int main(int argc, char **argv) router.append(filter_log_front); // put Virt db filter in router - mp::filter::Virt_db filter_virt_db; + mp::filter::VirtualDB filter_virt_db; filter_virt_db.add_map_db2target("gils", "indexdata.dk/gils", ""); filter_virt_db.add_map_db2target("Default", "localhost:9999/Default", diff --git a/src/filter_sru_to_z3950.cpp b/src/filter_sru_to_z3950.cpp index 2d7bcb9..76c5df8 100644 --- a/src/filter_sru_to_z3950.cpp +++ b/src/filter_sru_to_z3950.cpp @@ -1,4 +1,4 @@ -/* $Id: filter_sru_to_z3950.cpp,v 1.14 2006-09-26 13:15:33 marc Exp $ +/* $Id: filter_sru_to_z3950.cpp,v 1.15 2006-09-29 08:42:47 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -25,38 +25,9 @@ namespace mp = metaproxy_1; +namespace mp_util = metaproxy_1::util; namespace yf = mp::filter; -namespace metaproxy_1 -{ - - template - std::string to_string(const T& t) - { - std::ostringstream o; - if(o << t) - return o.str(); - - return std::string(); - } - - std::string http_header_value(const Z_HTTP_Header* header, - const std::string name) - { - while (header && header->name - && std::string(header->name) != name) - header = header->next; - - if (header && header->name && std::string(header->name) == name - && header->value) - return std::string(header->value); - - return std::string(); - } - - -} - namespace metaproxy_1 { namespace filter { @@ -292,18 +263,18 @@ bool yf::SRUtoZ3950::Rep::build_simple_explain(mp::Package &package, // building SRU explain record std::string explain_xml - = mp::to_string( + = mp_util::to_string( "\n" " \n" " ") + package.origin().server_host() - + mp::to_string("\n" + + mp_util::to_string("\n" " ") - + mp::to_string(package.origin().server_port()) - + mp::to_string("\n" + + mp_util::to_string(package.origin().server_port()) + + mp_util::to_string("\n" " ") + database - + mp::to_string("\n" + + mp_util::to_string("\n" " \n" "\n"); @@ -991,7 +962,7 @@ yf::SRUtoZ3950::Rep::sru_protocol(const Z_HTTP_Request &http_req) const const std::string http_method(http_req.method); const std::string http_type - = http_header_value(http_req.headers, "Content-Type"); + = mp_util::http_header_value(http_req.headers, "Content-Type"); if (http_method == "GET") return "SRU GET"; @@ -1022,10 +993,10 @@ yf::SRUtoZ3950::Rep::debug_http(const Z_HTTP_Request &http_req) const message += "Path: " + std::string(http_req.path) + "
\n"; message += "Content-Type:" - + http_header_value(http_req.headers, "Content-Type") + + mp_util::http_header_value(http_req.headers, "Content-Type") + "
\n"; message += "Content-Length:" - + http_header_value(http_req.headers, "Content-Length") + + mp_util::http_header_value(http_req.headers, "Content-Length") + "
\n"; message += "

\n"; diff --git a/src/filter_virt_db.cpp b/src/filter_virt_db.cpp index 692d361..5d4d749 100644 --- a/src/filter_virt_db.cpp +++ b/src/filter_virt_db.cpp @@ -1,4 +1,4 @@ -/* $Id: filter_virt_db.cpp,v 1.45 2006-08-30 12:27:34 adam Exp $ +/* $Id: filter_virt_db.cpp,v 1.46 2006-09-29 08:42:47 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -29,7 +29,7 @@ namespace yf = mp::filter; namespace metaproxy_1 { namespace filter { - struct Virt_db::Set { + struct VirtualDB::Set { Set(BackendPtr b, std::string setname); Set(); ~Set(); @@ -37,13 +37,13 @@ namespace metaproxy_1 { BackendPtr m_backend; std::string m_setname; }; - struct Virt_db::Map { + struct VirtualDB::Map { Map(std::list targets, std::string route); Map(); std::list m_targets; std::string m_route; }; - struct Virt_db::Backend { + struct VirtualDB::Backend { mp::Session m_backend_session; std::list m_frontend_databases; std::list m_targets; @@ -51,7 +51,7 @@ namespace metaproxy_1 { bool m_named_result_sets; int m_number_of_sets; }; - struct Virt_db::Frontend { + struct VirtualDB::Frontend { Frontend(Rep *rep); ~Frontend(); mp::Session m_session; @@ -59,14 +59,14 @@ namespace metaproxy_1 { bool m_in_use; yazpp_1::GDU m_init_gdu; std::list m_backend_list; - std::map m_sets; + std::map m_sets; void search(Package &package, Z_APDU *apdu); void present(Package &package, Z_APDU *apdu); void scan(Package &package, Z_APDU *apdu); void close(Package &package); - typedef std::map::iterator Sets_it; + typedef std::map::iterator Sets_it; void fixup_package(Package &p, BackendPtr b); void fixup_npr_record(ODR odr, Z_NamePlusRecord *npr, @@ -86,15 +86,15 @@ namespace metaproxy_1 { int &error_code, std::string &addinfo); Rep *m_p; }; - class Virt_db::Rep { - friend class Virt_db; + class VirtualDB::Rep { + friend class VirtualDB; friend struct Frontend; FrontendPtr get_frontend(Package &package); void release_frontend(Package &package); private: - std::mapm_maps; - typedef std::map::iterator Sets_it; + std::mapm_maps; + typedef std::map::iterator Sets_it; boost::mutex m_mutex; boost::condition m_cond_session_ready; std::map m_clients; @@ -102,7 +102,7 @@ namespace metaproxy_1 { } } -yf::Virt_db::BackendPtr yf::Virt_db::Frontend::lookup_backend_from_databases( +yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::lookup_backend_from_databases( std::list databases) { std::list::const_iterator map_it; @@ -114,7 +114,7 @@ yf::Virt_db::BackendPtr yf::Virt_db::Frontend::lookup_backend_from_databases( return null; } -yf::Virt_db::BackendPtr yf::Virt_db::Frontend::create_backend_from_databases( +yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::create_backend_from_databases( std::list databases, int &error_code, std::string &addinfo) { BackendPtr b(new Backend); @@ -129,7 +129,7 @@ yf::Virt_db::BackendPtr yf::Virt_db::Frontend::create_backend_from_databases( std::map targets_dedup; for (; db_it != databases.end(); db_it++) { - std::map::iterator map_it; + std::map::iterator map_it; map_it = m_p->m_maps.find(mp::util::database_name_normalize(*db_it)); if (map_it == m_p->m_maps.end()) // database not found { @@ -161,7 +161,7 @@ yf::Virt_db::BackendPtr yf::Virt_db::Frontend::create_backend_from_databases( return b; } -yf::Virt_db::BackendPtr yf::Virt_db::Frontend::init_backend( +yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::init_backend( std::list databases, mp::Package &package, int &error_code, std::string &addinfo) { @@ -238,7 +238,7 @@ yf::Virt_db::BackendPtr yf::Virt_db::Frontend::init_backend( return b; } -void yf::Virt_db::Frontend::search(mp::Package &package, Z_APDU *apdu_req) +void yf::VirtualDB::Frontend::search(mp::Package &package, Z_APDU *apdu_req) { Z_SearchRequest *req = apdu_req->u.searchRequest; std::string vhost; @@ -357,18 +357,18 @@ void yf::Virt_db::Frontend::search(mp::Package &package, Z_APDU *apdu_req) } b->m_number_of_sets++; - m_sets[resultSetId] = Virt_db::Set(b, backend_setname); + m_sets[resultSetId] = VirtualDB::Set(b, backend_setname); fixup_package(search_package, b); package.response() = search_package.response(); } -yf::Virt_db::Frontend::Frontend(Rep *rep) +yf::VirtualDB::Frontend::Frontend(Rep *rep) { m_p = rep; m_is_virtual = false; } -void yf::Virt_db::Frontend::close(mp::Package &package) +void yf::VirtualDB::Frontend::close(mp::Package &package) { std::list::const_iterator b_it; @@ -382,15 +382,15 @@ void yf::Virt_db::Frontend::close(mp::Package &package) m_backend_list.clear(); } -yf::Virt_db::Frontend::~Frontend() +yf::VirtualDB::Frontend::~Frontend() { } -yf::Virt_db::FrontendPtr yf::Virt_db::Rep::get_frontend(mp::Package &package) +yf::VirtualDB::FrontendPtr yf::VirtualDB::Rep::get_frontend(mp::Package &package) { boost::mutex::scoped_lock lock(m_mutex); - std::map::iterator it; + std::map::iterator it; while(true) { @@ -411,10 +411,10 @@ yf::Virt_db::FrontendPtr yf::Virt_db::Rep::get_frontend(mp::Package &package) return f; } -void yf::Virt_db::Rep::release_frontend(mp::Package &package) +void yf::VirtualDB::Rep::release_frontend(mp::Package &package) { boost::mutex::scoped_lock lock(m_mutex); - std::map::iterator it; + std::map::iterator it; it = m_clients.find(package.session()); if (it != m_clients.end()) @@ -432,38 +432,38 @@ void yf::Virt_db::Rep::release_frontend(mp::Package &package) } } -yf::Virt_db::Set::Set(BackendPtr b, std::string setname) +yf::VirtualDB::Set::Set(BackendPtr b, std::string setname) : m_backend(b), m_setname(setname) { } -yf::Virt_db::Set::Set() +yf::VirtualDB::Set::Set() { } -yf::Virt_db::Set::~Set() +yf::VirtualDB::Set::~Set() { } -yf::Virt_db::Map::Map(std::list targets, std::string route) +yf::VirtualDB::Map::Map(std::list targets, std::string route) : m_targets(targets), m_route(route) { } -yf::Virt_db::Map::Map() +yf::VirtualDB::Map::Map() { } -yf::Virt_db::Virt_db() : m_p(new Virt_db::Rep) +yf::VirtualDB::VirtualDB() : m_p(new VirtualDB::Rep) { } -yf::Virt_db::~Virt_db() { +yf::VirtualDB::~VirtualDB() { } -void yf::Virt_db::Frontend::fixup_npr_record(ODR odr, Z_NamePlusRecord *npr, +void yf::VirtualDB::Frontend::fixup_npr_record(ODR odr, Z_NamePlusRecord *npr, BackendPtr b) { if (npr->databaseName) @@ -476,11 +476,11 @@ void yf::Virt_db::Frontend::fixup_npr_record(ODR odr, Z_NamePlusRecord *npr, db_it != b->m_frontend_databases.end(); db_it++) { // see which target it corresponds to.. (if any) - std::map::const_iterator map_it; + std::map::const_iterator map_it; map_it = m_p->m_maps.find(*db_it); if (map_it != m_p->m_maps.end()) { - Virt_db::Map m = map_it->second; + VirtualDB::Map m = map_it->second; std::list::const_iterator t; for (t = m.m_targets.begin(); t != m.m_targets.end(); t++) @@ -503,7 +503,7 @@ void yf::Virt_db::Frontend::fixup_npr_record(ODR odr, Z_NamePlusRecord *npr, } } -void yf::Virt_db::Frontend::fixup_npr_records(ODR odr, Z_Records *records, +void yf::VirtualDB::Frontend::fixup_npr_records(ODR odr, Z_Records *records, BackendPtr b) { if (records && records->which == Z_Records_DBOSD) @@ -517,7 +517,7 @@ void yf::Virt_db::Frontend::fixup_npr_records(ODR odr, Z_Records *records, } } -void yf::Virt_db::Frontend::fixup_package(mp::Package &p, BackendPtr b) +void yf::VirtualDB::Frontend::fixup_package(mp::Package &p, BackendPtr b) { Z_GDU *gdu = p.response().get(); mp::odr odr; @@ -538,7 +538,7 @@ void yf::Virt_db::Frontend::fixup_package(mp::Package &p, BackendPtr b) } } -void yf::Virt_db::Frontend::present(mp::Package &package, Z_APDU *apdu_req) +void yf::VirtualDB::Frontend::present(mp::Package &package, Z_APDU *apdu_req) { Z_PresentRequest *req = apdu_req->u.presentRequest; std::string resultSetId = req->resultSetId; @@ -583,7 +583,7 @@ void yf::Virt_db::Frontend::present(mp::Package &package, Z_APDU *apdu_req) delete id; } -void yf::Virt_db::Frontend::scan(mp::Package &package, Z_APDU *apdu_req) +void yf::VirtualDB::Frontend::scan(mp::Package &package, Z_APDU *apdu_req) { Z_ScanRequest *req = apdu_req->u.scanRequest; std::string vhost; @@ -649,16 +649,16 @@ void yf::Virt_db::Frontend::scan(mp::Package &package, Z_APDU *apdu_req) } -void yf::Virt_db::add_map_db2targets(std::string db, +void yf::VirtualDB::add_map_db2targets(std::string db, std::list targets, std::string route) { m_p->m_maps[mp::util::database_name_normalize(db)] - = Virt_db::Map(targets, route); + = VirtualDB::Map(targets, route); } -void yf::Virt_db::add_map_db2target(std::string db, +void yf::VirtualDB::add_map_db2target(std::string db, std::string target, std::string route) { @@ -666,10 +666,10 @@ void yf::Virt_db::add_map_db2target(std::string db, targets.push_back(target); m_p->m_maps[mp::util::database_name_normalize(db)] - = Virt_db::Map(targets, route); + = VirtualDB::Map(targets, route); } -void yf::Virt_db::process(mp::Package &package) const +void yf::VirtualDB::process(mp::Package &package) const { FrontendPtr f = m_p->get_frontend(package); @@ -767,7 +767,7 @@ void yf::Virt_db::process(mp::Package &package) const } -void mp::filter::Virt_db::configure(const xmlNode * ptr) +void mp::filter::VirtualDB::configure(const xmlNode * ptr) { for (ptr = ptr->children; ptr; ptr = ptr->next) { @@ -809,7 +809,7 @@ void mp::filter::Virt_db::configure(const xmlNode * ptr) static mp::filter::Base* filter_creator() { - return new mp::filter::Virt_db; + return new mp::filter::VirtualDB; } extern "C" { diff --git a/src/filter_virt_db.hpp b/src/filter_virt_db.hpp index 766d61e..49faeba 100644 --- a/src/filter_virt_db.hpp +++ b/src/filter_virt_db.hpp @@ -1,4 +1,4 @@ -/* $Id: filter_virt_db.hpp,v 1.16 2006-06-10 14:29:12 adam Exp $ +/* $Id: filter_virt_db.hpp,v 1.17 2006-09-29 08:42:47 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -16,7 +16,7 @@ namespace metaproxy_1 { namespace filter { - class Virt_db : public Base { + class VirtualDB : public Base { class Rep; struct Frontend; struct Map; @@ -25,8 +25,8 @@ namespace metaproxy_1 { typedef boost::shared_ptr BackendPtr; typedef boost::shared_ptr FrontendPtr; public: - ~Virt_db(); - Virt_db(); + ~VirtualDB(); + VirtualDB(); void process(metaproxy_1::Package & package) const; void configure(const xmlNode * ptr); void add_map_db2targets(std::string db, diff --git a/src/test_filter_virt_db.cpp b/src/test_filter_virt_db.cpp index a3cef92..9141beb 100644 --- a/src/test_filter_virt_db.cpp +++ b/src/test_filter_virt_db.cpp @@ -1,4 +1,4 @@ -/* $Id: test_filter_virt_db.cpp,v 1.14 2006-09-28 11:56:54 marc Exp $ +/* $Id: test_filter_virt_db.cpp,v 1.15 2006-09-29 08:42:47 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -31,7 +31,7 @@ BOOST_AUTO_UNIT_TEST( test_filter_virt_db_1 ) { try { - mp::filter::Virt_db vdb; + mp::filter::VirtualDB vdb; } catch ( ... ) { BOOST_CHECK (false); @@ -44,7 +44,7 @@ BOOST_AUTO_UNIT_TEST( test_filter_virt_db_2 ) { mp::RouterChain router; - mp::filter::Virt_db vdb; + mp::filter::VirtualDB vdb; router.append(vdb); @@ -201,7 +201,7 @@ BOOST_AUTO_UNIT_TEST( test_filter_virt_db_3 ) router.append(filter_log1); #endif - mp::filter::Virt_db vdb; + mp::filter::VirtualDB vdb; router.append(vdb); vdb.add_map_db2target("Default", "localhost:210", ""); mp::filter::Log filter_log2("BACK"); diff --git a/src/util.cpp b/src/util.cpp index e177410..4615347 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,24 +1,41 @@ -/* $Id: util.cpp,v 1.19 2006-08-30 12:27:34 adam Exp $ +/* $Id: util.cpp,v 1.20 2006-09-29 08:42:47 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details */ #include "config.hpp" +#include "util.hpp" #include #include #include #include // for yaz_query_to_wrbuf() -#include "util.hpp" -//#include +#include namespace mp = metaproxy_1; // Doxygen doesn't like mp::util, so we use this instead namespace mp_util = metaproxy_1::util; + +std::string mp_util::http_header_value(const Z_HTTP_Header* header, + const std::string name) +{ + while (header && header->name + && std::string(header->name) != name) + header = header->next; + + if (header && header->name && std::string(header->name) == name + && header->value) + return std::string(header->value); + + return std::string(); +} + + + int mp_util::memcmp2(const void *buf1, int len1, const void *buf2, int len2) { diff --git a/src/util.hpp b/src/util.hpp index ba89bfd..fbdd367 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -1,4 +1,4 @@ -/* $Id: util.hpp,v 1.18 2006-08-30 12:27:34 adam Exp $ +/* $Id: util.hpp,v 1.19 2006-09-29 08:42:47 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -7,17 +7,36 @@ #ifndef YP2_UTIL_HPP #define YP2_UTIL_HPP +#include "package.hpp" + #include + +#include +#include + #include #include #include +#include +#include -#include -#include -#include "package.hpp" namespace metaproxy_1 { namespace util { + + + template + std::string to_string(const T& t) + { + std::ostringstream o; + if(o << t) + return o.str(); + return std::string(); + }; + + std::string http_header_value(const Z_HTTP_Header* header, + const std::string name); + int memcmp2(const void *buf1, int len1, const void *buf2, int len2); std::string database_name_normalize(const std::string &s); -- 1.7.10.4