From: Adam Dickmeiss Date: Mon, 24 Oct 2005 14:33:30 +0000 (+0000) Subject: Began work on virtual database filter X-Git-Tag: YP2.0.0.2~189 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=d732b2b2ea19a0669f9a42ca6fd7bc14a3845fc5;p=metaproxy-moved-to-github.git Began work on virtual database filter --- diff --git a/src/.cvsignore b/src/.cvsignore index 018c9f6..9ad5db0 100644 --- a/src/.cvsignore +++ b/src/.cvsignore @@ -9,6 +9,7 @@ Makefile Makefile.in config.hpp.in ex_filter_frontend_net +ex_libxml2_conf test_boost_threads test_boost_time test_filter1 @@ -20,3 +21,4 @@ test_thread_pool_observer test_session1 test_session2 test_filter_z3950_client +test_filter_virt_db diff --git a/src/Makefile.am b/src/Makefile.am index 0060716..76c620a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.22 2005-10-24 13:31:36 marc Exp $ +## $Id: Makefile.am,v 1.23 2005-10-24 14:33:30 adam Exp $ MAINTAINERCLEANFILES = Makefile.in config.in config.hpp @@ -14,6 +14,7 @@ libyp2_la_SOURCES = \ thread_pool_observer.cpp thread_pool_observer.hpp \ filter_frontend_net.cpp filter_frontend_net.hpp \ filter_log.cpp filter_log.hpp \ + filter_virt_db.cpp filter_virt_db.hpp \ filter_z3950_client.cpp filter_z3950_client.hpp # Rules for programs.. @@ -36,7 +37,8 @@ check_PROGRAMS = \ test_boost_threads test_boost_time \ test_filter_frontend_net \ test_filter_log \ - test_filter_z3950_client + test_filter_z3950_client \ + test_filter_virt_db TESTS=$(check_PROGRAMS) @@ -51,6 +53,7 @@ test_thread_pool_observer_SOURCES = test_thread_pool_observer.cpp test_filter_frontend_net_SOURCES = test_filter_frontend_net.cpp test_filter_log_SOURCES = test_filter_log.cpp test_filter_z3950_client_SOURCES = test_filter_z3950_client.cpp +test_filter_virt_db_SOURCES = test_filter_virt_db.cpp TESTLDADD = $(LDADD) -lboost_unit_test_framework @@ -65,6 +68,7 @@ test_package1_LDADD = $(TESTLDADD) test_filter_frontend_net_LDADD = $(TESTLDADD) test_filter_log_LDADD = $(TESTLDADD) test_filter_z3950_client_LDADD = $(TESTLDADD) +test_filter_virt_db_LDADD = $(TESTLDADD) # doxygen target dox: diff --git a/src/filter_virt_db.cpp b/src/filter_virt_db.cpp new file mode 100644 index 0000000..e70a791 --- /dev/null +++ b/src/filter_virt_db.cpp @@ -0,0 +1,202 @@ +/* $Id: filter_virt_db.cpp,v 1.1 2005-10-24 14:33:30 adam Exp $ + Copyright (c) 2005, Index Data. + +%LICENSE% + */ + +#include "config.hpp" + +#include "filter.hpp" +#include "router.hpp" +#include "package.hpp" + +#include + +#include "filter_virt_db.hpp" + +#include +#include +#include +#include + +#include +#include + +namespace yf = yp2::filter; + +namespace yp2 { + namespace filter { + struct Virt_db_session { + Virt_db_session(yp2::Session &id, bool use_vhost); + yp2::Session m_session; + bool m_use_vhost; + }; + struct Virt_db_map { + Virt_db_map(std::string database, std::string vhost); + std::string m_database; + std::string m_vhost; + }; + class Virt_db::Rep { + friend class Virt_db; + + void release_session(Package &package); + void init(Package &package, Z_APDU *apdu); + void search(Package &package, Z_APDU *apdu); + private: + std::listm_sessions; + std::listm_maps; + }; + } +} + +yf::Virt_db_map::Virt_db_map(std::string database, std::string vhost) + : m_database(database), m_vhost(vhost) +{ +} + +yf::Virt_db_session::Virt_db_session(yp2::Session &id, + bool use_vhost) : + m_session(id) , m_use_vhost(use_vhost) +{ + +} + +yf::Virt_db::Virt_db() { + m_p = new Virt_db::Rep; +} + +yf::Virt_db::~Virt_db() { + delete m_p; +} + +void yf::Virt_db::Rep::release_session(Package &package) +{ + +} + +void yf::Virt_db::Rep::search(Package &package, Z_APDU *apdu) +{ + Z_SearchRequest *req = apdu->u.searchRequest; + + std::list::iterator it; + for (it = m_sessions.begin(); it != m_sessions.end(); it++) + { + if (package.session() == (*it).m_session) + break; + } + if (it == m_sessions.end()) + return; + if ((*it).m_use_vhost) + package.move(); + else + { + if (req->num_databaseNames != 1) + { + ODR odr = odr_createmem(ODR_ENCODE); + Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse); + + Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records)); + apdu->u.searchResponse->records = rec; + rec->which = Z_Records_NSD; + rec->u.nonSurrogateDiagnostic = + zget_DefaultDiagFormat( + odr, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0); + package.response() = apdu; + + odr_destroy(odr); + } + const char *database = req->databaseNames[0]; + } +} + +void yf::Virt_db::Rep::init(Package &package, Z_APDU *apdu) +{ + std::list::iterator it; + + for (it = m_sessions.begin(); it != m_sessions.end(); it++) + { + if (package.session() == (*it).m_session) + break; + } + if (it != m_sessions.end()) + m_sessions.erase(it); + + Z_InitRequest *req = apdu->u.initRequest; + + const char *vhost = + yaz_oi_get_string_oidval(&req->otherInfo, VAL_PROXY, 1, 0); + if (!vhost) + { + ODR odr = odr_createmem(ODR_ENCODE); + + Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse); + Z_InitResponse *resp = apdu->u.initResponse; + + int i; + static const int masks[] = { + Z_Options_search, Z_Options_present, 0 + }; + for (i = 0; masks[i]; i++) + if (ODR_MASK_GET(req->options, masks[i])) + ODR_MASK_SET(resp->options, masks[i]); + + package.response() = apdu; + + odr_destroy(odr); + + m_sessions.push_back(Virt_db_session(package.session(), false)); + } + else + { + m_sessions.push_back(Virt_db_session(package.session(), true)); + package.move(); + } +} + +void yf::Virt_db::add_map_db2vhost(std::string db, std::string vhost) +{ + m_p->m_maps.push_back(Virt_db_map(db, vhost)); +} + +void yf::Virt_db::process(Package &package) const +{ + Z_GDU *gdu = package.request().get(); + + if (!gdu || gdu->which != Z_GDU_Z3950) + package.move(); + else + { + Z_APDU *apdu = gdu->u.z3950; + if (apdu->which == Z_APDU_initRequest) + { + m_p->init(package, apdu); + } + else if (apdu->which == Z_APDU_searchRequest) + { + m_p->search(package, apdu); + } + else + { + ODR odr = odr_createmem(ODR_ENCODE); + + Z_APDU *apdu = zget_APDU(odr, Z_APDU_close); + + *apdu->u.close->closeReason = Z_Close_protocolError; + + package.response() = apdu; + package.session().close(); + odr_destroy(odr); + } + } + m_p->release_session(package); +} + + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/filter_virt_db.hpp b/src/filter_virt_db.hpp new file mode 100644 index 0000000..cd06081 --- /dev/null +++ b/src/filter_virt_db.hpp @@ -0,0 +1,38 @@ +/* $Id: filter_virt_db.hpp,v 1.1 2005-10-24 14:33:30 adam Exp $ + Copyright (c) 2005, Index Data. + +%LICENSE% + */ + +#ifndef FILTER_VIRT_DB_HPP +#define FILTER_VIRT_DB_HPP + +#include +#include + +#include "filter.hpp" + +namespace yp2 { + namespace filter { + class Virt_db : public Base { + class Rep; + public: + ~Virt_db(); + Virt_db(); + void process(yp2::Package & package) const; + void add_map_db2vhost(std::string db, std::string vhost); + private: + Rep *m_p; + }; + } +} + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/test_filter_virt_db.cpp b/src/test_filter_virt_db.cpp new file mode 100644 index 0000000..72aa647 --- /dev/null +++ b/src/test_filter_virt_db.cpp @@ -0,0 +1,84 @@ +/* $Id: test_filter_virt_db.cpp,v 1.1 2005-10-24 14:33:30 adam Exp $ + Copyright (c) 2005, Index Data. + +%LICENSE% + */ + +#include "config.hpp" +#include +#include + +#include "filter_virt_db.hpp" + +#include "router.hpp" +#include "session.hpp" +#include "package.hpp" + +#define BOOST_AUTO_TEST_MAIN +#include + +#include +#include +using namespace boost::unit_test; + + +BOOST_AUTO_TEST_CASE( test_filter_virt_db_1 ) +{ + try + { + yp2::filter::Virt_db vdb; + } + catch ( ... ) { + BOOST_CHECK (false); + } +} + +BOOST_AUTO_TEST_CASE( test_filter_virt_db_2 ) +{ + try + { + yp2::RouterChain router; + + yp2::filter::Virt_db vdb; + + router.rule(vdb); + + // Create package with Z39.50 init request in it + yp2::Package pack; + + ODR odr = odr_createmem(ODR_ENCODE); + Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest); + + BOOST_CHECK(apdu); + + pack.request() = apdu; + odr_destroy(odr); + + // Put it in router + pack.router(router).move(); + + // Inspect that we got Z39.50 init Response OK. + yazpp_1::GDU *gdu = &pack.response(); + + BOOST_CHECK(!pack.session().is_closed()); + + Z_GDU *z_gdu = gdu->get(); + BOOST_CHECK(z_gdu); + if (z_gdu) { + BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950); + BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse); + } + } + catch ( ... ) { + BOOST_CHECK (false); + } +} + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/test_filter_z3950_client.cpp b/src/test_filter_z3950_client.cpp index cb07173..7090d4c 100644 --- a/src/test_filter_z3950_client.cpp +++ b/src/test_filter_z3950_client.cpp @@ -1,4 +1,4 @@ -/* $Id: test_filter_z3950_client.cpp,v 1.2 2005-10-16 16:09:58 adam Exp $ +/* $Id: test_filter_z3950_client.cpp,v 1.3 2005-10-24 14:33:30 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -26,9 +26,7 @@ BOOST_AUTO_TEST_CASE( test_filter_z3950_client_1 ) { try { - { - yp2::filter::Z3950Client zc; - } + yp2::filter::Z3950Client zc; // can we construct OK? } catch ( ... ) { BOOST_CHECK (false); @@ -59,7 +57,7 @@ BOOST_AUTO_TEST_CASE( test_filter_z3950_client_2 ) // Put it in router pack.router(router).move(); - // Inspect that we got Z39.50 init Response - a Z39.50 session + // Inspect that we got Z39.50 init Response - a Z39.50 session MUST // specify a virtual host yazpp_1::GDU *gdu = &pack.response();