From: Adam Dickmeiss Date: Tue, 25 Oct 2005 21:32:01 +0000 (+0000) Subject: Experimented with template for session management in filter X-Git-Tag: YP2.0.0.2~178 X-Git-Url: http://jsfdemo.indexdata.com/?a=commitdiff_plain;h=b3a5a3535ff97f71581c9b496f95e0820120f743;p=metaproxy-moved-to-github.git Experimented with template for session management in filter backend_test_cpp. Removed p2.cpp. --- diff --git a/src/.cvsignore b/src/.cvsignore index 9ad5db0..f28f845 100644 --- a/src/.cvsignore +++ b/src/.cvsignore @@ -21,4 +21,5 @@ test_thread_pool_observer test_session1 test_session2 test_filter_z3950_client +test_filter_backend_test test_filter_virt_db diff --git a/src/Makefile.am b/src/Makefile.am index 021dc3d..e85054a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.24 2005-10-25 11:48:30 adam Exp $ +## $Id: Makefile.am,v 1.25 2005-10-25 21:32:01 adam Exp $ MAINTAINERCLEANFILES = Makefile.in config.in config.hpp @@ -16,7 +16,8 @@ libyp2_la_SOURCES = \ filter_log.cpp filter_log.hpp \ filter_virt_db.cpp filter_virt_db.hpp \ filter_z3950_client.cpp filter_z3950_client.hpp \ - filter_backend_test.cpp filter_backend_test.hpp + filter_backend_test.cpp filter_backend_test.hpp \ + test_util.cpp test_util.hpp # Rules for programs.. @@ -39,6 +40,7 @@ check_PROGRAMS = \ test_filter_frontend_net \ test_filter_log \ test_filter_z3950_client \ + test_filter_backend_test \ test_filter_virt_db TESTS=$(check_PROGRAMS) @@ -54,6 +56,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_backend_test_SOURCES = test_filter_backend_test.cpp test_filter_virt_db_SOURCES = test_filter_virt_db.cpp TESTLDADD = $(LDADD) -lboost_unit_test_framework @@ -69,6 +72,7 @@ test_package1_LDADD = $(TESTLDADD) test_filter_frontend_net_LDADD = $(TESTLDADD) test_filter_log_LDADD = $(TESTLDADD) test_filter_z3950_client_LDADD = $(TESTLDADD) +test_filter_backend_test_LDADD = $(TESTLDADD) test_filter_virt_db_LDADD = $(TESTLDADD) # doxygen target diff --git a/src/filter_backend_test.cpp b/src/filter_backend_test.cpp index dda5135..d8e9cdb 100644 --- a/src/filter_backend_test.cpp +++ b/src/filter_backend_test.cpp @@ -1,4 +1,4 @@ -/* $Id: filter_backend_test.cpp,v 1.3 2005-10-25 16:01:36 adam Exp $ +/* $Id: filter_backend_test.cpp,v 1.4 2005-10-25 21:32:01 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -27,11 +27,16 @@ namespace yf = yp2::filter; namespace yp2 { namespace filter { + class Session_info { + int dummy; + }; class Backend_test::Rep { friend class Backend_test; private: bool m_support_named_result_sets; + + session_map m_sessions; }; } } @@ -56,7 +61,18 @@ void yf::Backend_test::process(Package &package) const Z_APDU *apdu_req = gdu->u.z3950; Z_APDU *apdu_res = 0; ODR odr = odr_createmem(ODR_ENCODE); - if (apdu_req->which == Z_APDU_initRequest) + + if (apdu_req->which != Z_APDU_initRequest && + !m_p->m_sessions.active(package.session())) + { + apdu_res = zget_APDU(odr, Z_APDU_close); + *apdu_res->u.close->closeReason = Z_Close_protocolError; + apdu_res->u.close->diagnosticInformation = + odr_strdup(odr, "no init for filter_backend_test"); + + package.session().close(); + } + else if (apdu_req->which == Z_APDU_initRequest) { apdu_res = zget_APDU(odr, Z_APDU_initResponse); Z_InitRequest *req = apdu_req->u.initRequest; @@ -88,13 +104,15 @@ void yf::Backend_test::process(Package &package) const else break; + Session_info info; + m_p->m_sessions.create(info, package.session()); } else if (apdu_req->which == Z_APDU_searchRequest) - { + { apdu_res = zget_APDU(odr, Z_APDU_searchResponse); Z_SearchRequest *req = apdu_req->u.searchRequest; Z_SearchResponse *resp = apdu_res->u.searchResponse; - + if (!m_p->m_support_named_result_sets && strcmp(req->resultSetName, "default")) { @@ -126,6 +144,8 @@ void yf::Backend_test::process(Package &package) const package.response() = apdu_res; odr_destroy(odr); } + if (package.session().is_closed()) + m_p->m_sessions.release(package.session()); } diff --git a/src/filter_virt_db.cpp b/src/filter_virt_db.cpp index c12fc19..9b61465 100644 --- a/src/filter_virt_db.cpp +++ b/src/filter_virt_db.cpp @@ -1,4 +1,4 @@ -/* $Id: filter_virt_db.cpp,v 1.5 2005-10-25 16:00:58 adam Exp $ +/* $Id: filter_virt_db.cpp,v 1.6 2005-10-25 21:32:01 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -117,10 +117,7 @@ void yf::Virt_db::Rep::release_session(Package &package) { boost::mutex::scoped_lock lock(m_sessions_mutex); - Ses_it it = m_sessions.find(package.session()); - - if (it != m_sessions.end()) - m_sessions.erase(it); + m_sessions.erase(package.session()); } void yf::Virt_db::Rep::present(Package &package, Z_APDU *apdu, bool &move_later){ diff --git a/src/filter_z3950_client.cpp b/src/filter_z3950_client.cpp index db7a9fa..8a5c30c 100644 --- a/src/filter_z3950_client.cpp +++ b/src/filter_z3950_client.cpp @@ -1,4 +1,4 @@ -/* $Id: filter_z3950_client.cpp,v 1.4 2005-10-25 16:01:14 adam Exp $ +/* $Id: filter_z3950_client.cpp,v 1.5 2005-10-25 21:32:01 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -6,6 +6,7 @@ #include "config.hpp" +#include #include "filter.hpp" #include "router.hpp" #include "package.hpp" @@ -32,7 +33,7 @@ namespace yp2 { class Z3950Client::Assoc : public yazpp_1::Z_Assoc{ friend class Pimpl; public: - Assoc(yp2::Session id, yazpp_1::SocketManager *socket_manager, + Assoc(yazpp_1::SocketManager *socket_manager, yazpp_1::IPDU_Observable *PDU_Observable, std::string host); ~Assoc(); @@ -44,7 +45,7 @@ namespace yp2 { yazpp_1::IPDU_Observable *the_PDU_Observable, int fd); private: - yp2::Session m_session_id; + // yp2::Session m_session_id; yazpp_1::SocketManager *m_socket_manager; yazpp_1::IPDU_Observable *m_PDU_Observable; Package *m_package; @@ -56,7 +57,7 @@ namespace yp2 { class Z3950Client::Pimpl { public: boost::mutex m_mutex; - std::list m_clients; + std::map m_clients; Z3950Client::Assoc *get_assoc(Package &package); void send_and_receive(Package &package, yf::Z3950Client::Assoc *c); @@ -67,11 +68,10 @@ namespace yp2 { } -yf::Z3950Client::Assoc::Assoc(yp2::Session id, - yazpp_1::SocketManager *socket_manager, +yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager, yazpp_1::IPDU_Observable *PDU_Observable, std::string host) - : Z_Assoc(PDU_Observable), m_session_id(id), + : Z_Assoc(PDU_Observable), m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable), m_package(0), m_waiting(false), m_connected(false), m_host(host) @@ -157,15 +157,11 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Pimpl::get_assoc(Package &package) // only one thread messes with the clients list at a time boost::mutex::scoped_lock lock(m_mutex); - std::list::iterator it; - - for (it = m_clients.begin(); it != m_clients.end(); it++) - { - if ((*it)->m_session_id == package.session()) - break; - } + std::map::iterator it; + + it = m_clients.find(package.session()); if (it != m_clients.end()) - return *it; + return it->second; // only deal with Z39.50 Z_GDU *gdu = package.request().get(); @@ -217,10 +213,8 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Pimpl::get_assoc(Package &package) yazpp_1::SocketManager *sm = new yazpp_1::SocketManager; yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm); - yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(package.session(), - sm, pdu_as, - vhost); - m_clients.push_back(as); + yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as, vhost); + m_clients[package.session()] = as; return as; } @@ -265,19 +259,15 @@ void yf::Z3950Client::Pimpl::release_assoc(Package &package, if (package.session().is_closed()) { boost::mutex::scoped_lock lock(m_mutex); - std::list::iterator it; + std::map::iterator it; - for (it = m_clients.begin(); it != m_clients.end(); it++) - { - if ((*it)->m_session_id == package.session()) - break; - } + it = m_clients.find(package.session()); if (it != m_clients.end()) { // the Z_Assoc and PDU_Assoc must be destroyed before // the socket manager.. so pull that out.. first.. - yazpp_1::SocketManager *s = (*it)->m_socket_manager; - delete *it; // destroy Z_Assoc + yazpp_1::SocketManager *s = it->second->m_socket_manager; + delete it->second; // destroy Z_Assoc delete s; // then manager m_clients.erase(it); } diff --git a/src/p2.cpp b/src/p2.cpp deleted file mode 100644 index 038afcd..0000000 --- a/src/p2.cpp +++ /dev/null @@ -1,154 +0,0 @@ -/* $Id: p2.cpp,v 1.5 2005-10-15 14:09:09 adam Exp $ - Copyright (c) 2005, Index Data. - -%LICENSE% - */ -/* $Id: p2.cpp,v 1.5 2005-10-15 14:09:09 adam Exp $ - Copyright (c) 1998-2005, Index Data. - -This file is part of the yaz-proxy. - -YAZ proxy 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. - -YAZ proxy 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 YAZ proxy; see the file LICENSE. If not, write to the -Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA -02111-1307, USA. - */ - -#include -#include -#include -#include -#include - -#include "config.hpp" -#include -#include "p2_config.h" -#include "p2_frontend.h" -#include "p2_xmlerror.h" -#include "p2_modules.h" - -using namespace yazpp_1; - -extern P2_ModuleEntry *p2_backend_dummy; - -/* - frontend result set map - resultset -> db,query - - backend result set map - db,query -> resultset, target - resultset, target -*/ -class P2_Frontend; - -P2_Config *P2_Server::lockConfig() -{ - pthread_mutex_lock(&m_mutex_config); - return m_config; -} - -void P2_Server::unlockConfig() -{ - pthread_mutex_unlock(&m_mutex_config); -} - -P2_Server::P2_Server(IPDU_Observable *the_PDU_Observable, - yp2::ThreadPoolSocketObserver *my_thread, - P2_Config *config, - P2_ModuleFactory *modules) - : Z_Assoc(the_PDU_Observable) -{ - m_my_thread = my_thread; - m_modules = modules; - m_config = config; - - pthread_mutex_init(&m_mutex_config, 0); - - yaz_log(YLOG_LOG, "Construct P2_Server=%p", this); -} - -IPDU_Observer *P2_Server::sessionNotify(IPDU_Observable - *the_PDU_Observable, int fd) -{ - P2_Frontend *my = new P2_Frontend(the_PDU_Observable, m_my_thread, this); - yaz_log(YLOG_LOG, "New session %s", the_PDU_Observable->getpeername()); - return my; -} - -P2_Server::~P2_Server() -{ - yaz_log(YLOG_LOG, "Destroy P2_server=%p", this); - pthread_mutex_destroy(&m_mutex_config); -} - -void P2_Server::recv_GDU(Z_GDU *apdu, int len) -{ -} - -void P2_Server::failNotify() -{ -} - -void P2_Server::timeoutNotify() -{ -} - -void P2_Server::connectNotify() -{ -} - -int main(int argc, char **argv) -{ - p2_xmlerror_setup(); - - P2_Config config; - - if (!config.parse_options(argc, argv)) - { - yaz_log(YLOG_FATAL, "Configuration incorrect. Exiting"); - exit(1); - } - - SocketManager mySocketManager; - - PDU_Assoc *my_PDU_Assoc = 0; - - yp2::ThreadPoolSocketObserver my_thread(&mySocketManager, - config.m_no_threads); - - my_PDU_Assoc = new PDU_Assoc(&mySocketManager); - - P2_ModuleFactory modules; - - modules.add(p2_backend_dummy); - - std::list::const_iterator it; - for (it = config.m_modules.begin(); it != config.m_modules.end(); it++) - modules.add((*it)->m_fname.c_str()); - - P2_Server z(my_PDU_Assoc, &my_thread, &config, &modules); - z.server(config.m_listen_address.c_str()); - - while (mySocketManager.processEvent() > 0) - ; - return 0; -} -/* - * 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/session.hpp b/src/session.hpp index 148749d..b4d8916 100644 --- a/src/session.hpp +++ b/src/session.hpp @@ -1,4 +1,4 @@ -/* $Id: session.hpp,v 1.9 2005-10-25 11:48:30 adam Exp $ +/* $Id: session.hpp,v 1.10 2005-10-25 21:32:01 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -8,7 +8,7 @@ #define SESSION_HPP //#include - +#include #include namespace yp2 { @@ -70,6 +70,27 @@ namespace yp2 { static unsigned long int m_global_id; }; + + template class session_map { + public: + void create(T &t, const yp2::Session &s) { + boost::mutex::scoped_lock lock(m_mutex); + m_map[s] = t; + }; + void release(const yp2::Session &s) { + boost::mutex::scoped_lock lock(m_mutex); + + m_map.erase(s); + }; + bool active(const yp2::Session &s) { + typename std::map::const_iterator it; + it = m_map.find(s); + return it == m_map.end() ? false : true; + } + private: + boost::mutex m_mutex; + std::mapm_map; + }; } diff --git a/src/test_filter_virt_db.cpp b/src/test_filter_virt_db.cpp index 68056f2..4c14649 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.3 2005-10-25 16:01:36 adam Exp $ +/* $Id: test_filter_virt_db.cpp,v 1.4 2005-10-25 21:32:01 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -8,6 +8,7 @@ #include #include +#include "test_util.hpp" #include "filter_virt_db.hpp" #include "filter_backend_test.hpp" #include "filter_log.hpp" @@ -110,27 +111,17 @@ static void init(yp2::Package &pack, yp2::Router &router) } static void search(yp2::Package &pack, yp2::Router &router, - const char *pqf_query, const char *db, + const std::string &query, const char *db, const char *setname) { // Create package with Z39.50 search request in it ODR odr = odr_createmem(ODR_ENCODE); Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest); - - YAZ_PQF_Parser pqf_parser = yaz_pqf_create(); - - Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, pqf_query); - BOOST_CHECK(rpn); - if (!rpn) - return; - Z_Query query; - query.which = Z_Query_type_1; - query.u.type_1 = rpn; - - apdu->u.searchRequest->resultSetName = odr_strdup(odr, setname); - apdu->u.searchRequest->query = &query; + yp2::util::pqf(odr, apdu, query); + + apdu->u.searchRequest->resultSetName = odr_strdup(odr, setname); apdu->u.searchRequest->num_databaseNames = 1; apdu->u.searchRequest->databaseNames = (char**) diff --git a/src/test_util.cpp b/src/test_util.cpp new file mode 100644 index 0000000..0731cf3 --- /dev/null +++ b/src/test_util.cpp @@ -0,0 +1,36 @@ +/* $Id: test_util.cpp,v 1.1 2005-10-25 21:32:01 adam Exp $ + Copyright (c) 2005, Index Data. + +%LICENSE% + */ + +#include "config.hpp" + +#include +#include "test_util.hpp" + +bool yp2::util::pqf(ODR odr, Z_APDU *apdu, const std::string &q) { + YAZ_PQF_Parser pqf_parser = yaz_pqf_create(); + + Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, q.c_str()); + if (!rpn) + { + yaz_pqf_destroy(pqf_parser); + return false; + } + yaz_pqf_destroy(pqf_parser); + Z_Query *query = (Z_Query *) odr_malloc(odr, sizeof(Z_Query)); + query->which = Z_Query_type_1; + query->u.type_1 = rpn; + + apdu->u.searchRequest->query = query; + return true; +} +/* + * 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_util.hpp b/src/test_util.hpp new file mode 100644 index 0000000..edfd9ad --- /dev/null +++ b/src/test_util.hpp @@ -0,0 +1,25 @@ +/* $Id: test_util.hpp,v 1.1 2005-10-25 21:32:01 adam Exp $ + Copyright (c) 2005, Index Data. + +%LICENSE% + */ + +#ifndef TEST_UTIL_HPP +#define TEST_UTIL_HPP + +#include +#include +namespace yp2 { + struct util { + static bool pqf(ODR odr, Z_APDU *apdu, const std::string &q); + }; +} +#endif +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */