X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Ffilter_z3950_client.cpp;h=f64a4c5ef8ee1b20a46564648035427610552d9f;hb=f26cca046b4879a78261ea436c3e21bb3b851b52;hp=8a5c30c9358cbe55299380567c5b304f00cb539c;hpb=b3a5a3535ff97f71581c9b496f95e0820120f743;p=metaproxy-moved-to-github.git diff --git a/src/filter_z3950_client.cpp b/src/filter_z3950_client.cpp index 8a5c30c..f64a4c5 100644 --- a/src/filter_z3950_client.cpp +++ b/src/filter_z3950_client.cpp @@ -1,4 +1,4 @@ -/* $Id: filter_z3950_client.cpp,v 1.5 2005-10-25 21:32:01 adam Exp $ +/* $Id: filter_z3950_client.cpp,v 1.8 2005-10-30 17:13:36 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -12,7 +12,9 @@ #include "package.hpp" #include +#include +#include "util.hpp" #include "filter_z3950_client.hpp" #include @@ -31,7 +33,7 @@ namespace yf = yp2::filter; namespace yp2 { namespace filter { class Z3950Client::Assoc : public yazpp_1::Z_Assoc{ - friend class Pimpl; + friend class Rep; public: Assoc(yazpp_1::SocketManager *socket_manager, yazpp_1::IPDU_Observable *PDU_Observable, @@ -49,20 +51,21 @@ namespace yp2 { yazpp_1::SocketManager *m_socket_manager; yazpp_1::IPDU_Observable *m_PDU_Observable; Package *m_package; + bool m_in_use; bool m_waiting; bool m_connected; std::string m_host; }; - class Z3950Client::Pimpl { + class Z3950Client::Rep { public: boost::mutex m_mutex; + boost::condition m_cond_session_ready; std::map m_clients; Z3950Client::Assoc *get_assoc(Package &package); void send_and_receive(Package &package, yf::Z3950Client::Assoc *c); - void release_assoc(Package &package, - yf::Z3950Client::Assoc *c); + void release_assoc(Package &package); }; } } @@ -73,7 +76,7 @@ yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager, std::string host) : Z_Assoc(PDU_Observable), m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable), - m_package(0), m_waiting(false), m_connected(false), + m_package(0), m_in_use(true), m_waiting(false), m_connected(false), m_host(host) { // std::cout << "create assoc " << this << "\n"; @@ -95,7 +98,7 @@ void yf::Z3950Client::Assoc::failNotify() { m_waiting = false; - ODR odr = odr_createmem(ODR_ENCODE); + yp2::odr odr; Z_APDU *apdu = zget_APDU(odr, Z_APDU_close); @@ -106,15 +109,13 @@ void yf::Z3950Client::Assoc::failNotify() m_package->response() = apdu; m_package->session().close(); } - - odr_destroy(odr); } void yf::Z3950Client::Assoc::timeoutNotify() { m_waiting = false; - ODR odr = odr_createmem(ODR_ENCODE); + yp2::odr odr; Z_APDU *apdu = zget_APDU(odr, Z_APDU_close); @@ -125,7 +126,6 @@ void yf::Z3950Client::Assoc::timeoutNotify() m_package->response() = apdu; m_package->session().close(); } - odr_destroy(odr); } void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len) @@ -144,24 +144,33 @@ yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify( } -yf::Z3950Client::Z3950Client() { - m_p = new yf::Z3950Client::Pimpl; +yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep) +{ } yf::Z3950Client::~Z3950Client() { - delete m_p; } -yf::Z3950Client::Assoc *yf::Z3950Client::Pimpl::get_assoc(Package &package) +yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package) { // only one thread messes with the clients list at a time boost::mutex::scoped_lock lock(m_mutex); std::map::iterator it; - it = m_clients.find(package.session()); - if (it != m_clients.end()) - return it->second; + while(true) + { + it = m_clients.find(package.session()); + if (it == m_clients.end()) + break; + + if (!it->second->m_in_use) + { + it->second->m_in_use = true; + return it->second; + } + m_cond_session_ready.wait(lock); + } // only deal with Z39.50 Z_GDU *gdu = package.request().get(); @@ -178,7 +187,7 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Pimpl::get_assoc(Package &package) // check that it is init. If not, close if (apdu->which != Z_APDU_initRequest) { - ODR odr = odr_createmem(ODR_ENCODE); + yp2::odr odr; Z_APDU *apdu = zget_APDU(odr, Z_APDU_close); *apdu->u.close->closeReason = Z_Close_protocolError; @@ -188,7 +197,6 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Pimpl::get_assoc(Package &package) package.response() = apdu; package.session().close(); - odr_destroy(odr); return 0; } // check virtual host @@ -197,7 +205,7 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Pimpl::get_assoc(Package &package) VAL_PROXY, 1, 0); if (!vhost) { - ODR odr = odr_createmem(ODR_ENCODE); + yp2::odr odr; Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse); apdu->u.initResponse->userInformationField = @@ -207,7 +215,6 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Pimpl::get_assoc(Package &package) package.response() = apdu; package.session().close(); - odr_destroy(odr); return 0; } @@ -218,16 +225,14 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Pimpl::get_assoc(Package &package) return as; } -void yf::Z3950Client::Pimpl::send_and_receive(Package &package, - yf::Z3950Client::Assoc *c) +void yf::Z3950Client::Rep::send_and_receive(Package &package, + yf::Z3950Client::Assoc *c) { Z_GDU *gdu = package.request().get(); if (!gdu || gdu->which != Z_GDU_Z3950) return; - // we should lock c! - c->m_package = &package; c->m_waiting = true; if (!c->m_connected) @@ -253,16 +258,15 @@ void yf::Z3950Client::Pimpl::send_and_receive(Package &package, ; } -void yf::Z3950Client::Pimpl::release_assoc(Package &package, - yf::Z3950Client::Assoc *c) +void yf::Z3950Client::Rep::release_assoc(Package &package) { - if (package.session().is_closed()) + boost::mutex::scoped_lock lock(m_mutex); + std::map::iterator it; + + it = m_clients.find(package.session()); + if (it != m_clients.end()) { - boost::mutex::scoped_lock lock(m_mutex); - std::map::iterator it; - - it = m_clients.find(package.session()); - if (it != m_clients.end()) + if (package.session().is_closed()) { // the Z_Assoc and PDU_Assoc must be destroyed before // the socket manager.. so pull that out.. first.. @@ -271,6 +275,11 @@ void yf::Z3950Client::Pimpl::release_assoc(Package &package, delete s; // then manager m_clients.erase(it); } + else + { + it->second->m_in_use = false; + } + m_cond_session_ready.notify_all(); } } @@ -280,8 +289,8 @@ void yf::Z3950Client::process(Package &package) const if (c) { m_p->send_and_receive(package, c); - m_p->release_assoc(package, c); } + m_p->release_assoc(package); }