1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2008 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 /* $Id: filter_z3950_client.cpp,v 1.32 2008-02-20 15:07:52 adam Exp $
19 Copyright (c) 2005-2007, Index Data.
21 See the LICENSE file for details
27 #include "package.hpp"
29 #include "filter_z3950_client.hpp"
36 #include <boost/thread/mutex.hpp>
37 #include <boost/thread/condition.hpp>
41 #include <yaz/otherinfo.h>
42 #include <yaz/diagbib1.h>
44 #include <yazpp/socket-manager.h>
45 #include <yazpp/pdu-assoc.h>
46 #include <yazpp/z-assoc.h>
48 namespace mp = metaproxy_1;
49 namespace yf = mp::filter;
51 namespace metaproxy_1 {
53 class Z3950Client::Assoc : public yazpp_1::Z_Assoc{
55 Assoc(yazpp_1::SocketManager *socket_manager,
56 yazpp_1::IPDU_Observable *PDU_Observable,
57 std::string host, int timeout);
62 void recv_GDU(Z_GDU *gdu, int len);
63 yazpp_1::IPDU_Observer* sessionNotify(
64 yazpp_1::IPDU_Observable *the_PDU_Observable,
67 yazpp_1::SocketManager *m_socket_manager;
68 yazpp_1::IPDU_Observable *m_PDU_Observable;
80 class Z3950Client::Rep {
82 // number of seconds to wait before we give up request
84 std::string m_default_target;
85 std::string m_force_target;
87 boost::condition m_cond_session_ready;
88 std::map<mp::Session,Z3950Client::Assoc *> m_clients;
89 Z3950Client::Assoc *get_assoc(Package &package);
90 void send_and_receive(Package &package,
91 yf::Z3950Client::Assoc *c);
92 void release_assoc(Package &package);
99 yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager,
100 yazpp_1::IPDU_Observable *PDU_Observable,
101 std::string host, int timeout_sec)
102 : Z_Assoc(PDU_Observable),
103 m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable),
104 m_package(0), m_in_use(true), m_waiting(false),
105 m_destroyed(false), m_connected(false), m_queue_len(1),
106 m_time_elapsed(0), m_time_max(timeout_sec),
109 // std::cout << "create assoc " << this << "\n";
112 yf::Z3950Client::Assoc::~Assoc()
114 // std::cout << "destroy assoc " << this << "\n";
117 void yf::Z3950Client::Assoc::connectNotify()
124 void yf::Z3950Client::Assoc::failNotify()
132 Z_GDU *gdu = m_package->request().get();
134 if (gdu && gdu->which == Z_GDU_Z3950)
137 m_package->response() = odr.create_close(apdu, Z_Close_peerAbort, 0);
138 m_package->session().close();
142 void yf::Z3950Client::Assoc::timeoutNotify()
145 if (m_time_elapsed >= m_time_max)
153 Z_GDU *gdu = m_package->request().get();
155 if (gdu && gdu->which == Z_GDU_Z3950)
159 m_package->response() =
160 odr.create_close(apdu, Z_Close_lackOfActivity, 0);
162 m_package->response() =
163 odr.create_close(apdu, Z_Close_peerAbort, 0);
165 m_package->session().close();
170 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
175 m_package->response() = gdu;
178 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
179 yazpp_1::IPDU_Observable *the_PDU_Observable,
186 yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep)
188 m_p->m_timeout_sec = 30;
191 yf::Z3950Client::~Z3950Client() {
194 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
196 // only one thread messes with the clients list at a time
197 boost::mutex::scoped_lock lock(m_mutex);
199 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
201 Z_GDU *gdu = package.request().get();
202 // only deal with Z39.50
203 if (!gdu || gdu->which != Z_GDU_Z3950)
208 it = m_clients.find(package.session());
209 if (it != m_clients.end())
211 it->second->m_queue_len++;
215 // double init .. NOT working yet
216 if (gdu && gdu->which == Z_GDU_Z3950 &&
217 gdu->u.z3950->which == Z_APDU_initRequest)
219 yazpp_1::SocketManager *s = it->second->m_socket_manager;
220 delete it->second; // destroy Z_Assoc
221 delete s; // then manager
226 if (!it->second->m_in_use)
228 it->second->m_in_use = true;
231 m_cond_session_ready.wait(lock);
234 // new Z39.50 session ..
235 Z_APDU *apdu = gdu->u.z3950;
236 // check that it is init. If not, close
237 if (apdu->which != Z_APDU_initRequest)
241 package.response() = odr.create_close(apdu,
242 Z_Close_protocolError,
243 "First PDU was not an "
244 "Initialize Request");
245 package.session().close();
248 std::string target = m_force_target;
249 if (!target.length())
251 target = m_default_target;
252 std::list<std::string> vhosts;
253 mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
255 size_t no_vhosts = vhosts.size();
258 std::list<std::string>::const_iterator v_it = vhosts.begin();
261 else if (no_vhosts == 0)
263 if (!target.length())
265 // no default target. So we don't know where to connect
267 package.response() = odr.create_initResponse(
269 YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
270 "z3950_client: No virtal host given");
272 package.session().close();
276 else if (no_vhosts > 1)
279 package.response() = odr.create_initResponse(
281 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
282 "z3950_client: Can not cope with multiple vhosts");
283 package.session().close();
287 std::list<std::string> dblist;
289 mp::util::split_zurl(target, host, dblist);
293 ; // z3950_client: Databases in vhost ignored
296 yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
297 yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
298 yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as,
301 m_clients[package.session()] = as;
305 void yf::Z3950Client::Rep::send_and_receive(Package &package,
306 yf::Z3950Client::Assoc *c)
308 Z_GDU *gdu = package.request().get();
313 if (!gdu || gdu->which != Z_GDU_Z3950)
316 c->m_time_elapsed = 0;
317 c->m_package = &package;
321 c->client(c->m_host.c_str());
322 c->timeout(1); // so timeoutNotify gets called once per second
324 while (!c->m_destroyed && c->m_waiting
325 && c->m_socket_manager->processEvent() > 0)
334 c->m_time_elapsed = 0;
337 // relay the package ..
339 c->send_GDU(gdu, &len);
341 switch(gdu->u.z3950->which)
343 case Z_APDU_triggerResourceControlRequest:
347 // for the rest: wait for a response PDU
348 while (!c->m_destroyed && c->m_waiting
349 && c->m_socket_manager->processEvent() > 0)
355 void yf::Z3950Client::Rep::release_assoc(Package &package)
357 boost::mutex::scoped_lock lock(m_mutex);
358 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
360 it = m_clients.find(package.session());
361 if (it != m_clients.end())
363 Z_GDU *gdu = package.request().get();
364 if (gdu && gdu->which == Z_GDU_Z3950)
365 { // only Z39.50 packages lock in get_assoc.. release it
366 it->second->m_in_use = false;
367 it->second->m_queue_len--;
370 if (package.session().is_closed())
372 // destroy hint (send_and_receive)
373 it->second->m_destroyed = true;
375 // wait until no one is waiting for it.
376 while (it->second->m_queue_len)
377 m_cond_session_ready.wait(lock);
379 // the Z_Assoc and PDU_Assoc must be destroyed before
380 // the socket manager.. so pull that out.. first..
381 yazpp_1::SocketManager *s = it->second->m_socket_manager;
382 delete it->second; // destroy Z_Assoc
383 delete s; // then manager
386 m_cond_session_ready.notify_all();
390 void yf::Z3950Client::process(Package &package) const
392 yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
395 m_p->send_and_receive(package, c);
397 m_p->release_assoc(package);
400 void yf::Z3950Client::configure(const xmlNode *ptr, bool test_only)
402 for (ptr = ptr->children; ptr; ptr = ptr->next)
404 if (ptr->type != XML_ELEMENT_NODE)
406 if (!strcmp((const char *) ptr->name, "timeout"))
408 m_p->m_timeout_sec = mp::xml::get_int(ptr->children, 30);
410 else if (!strcmp((const char *) ptr->name, "default_target"))
412 m_p->m_default_target = mp::xml::get_text(ptr);
414 else if (!strcmp((const char *) ptr->name, "force_target"))
416 m_p->m_force_target = mp::xml::get_text(ptr);
420 throw mp::filter::FilterException("Bad element "
421 + std::string((const char *)
427 static mp::filter::Base* filter_creator()
429 return new mp::filter::Z3950Client;
433 struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
443 * indent-tabs-mode: nil
444 * c-file-style: "stroustrup"
446 * vim: shiftwidth=4 tabstop=8 expandtab