1 /* $Id: filter_z3950_client.cpp,v 1.31 2007-05-09 21:23:09 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 This file is part of Metaproxy.
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 /* $Id: filter_z3950_client.cpp,v 1.31 2007-05-09 21:23:09 adam Exp $
22 Copyright (c) 2005-2007, Index Data.
24 See the LICENSE file for details
30 #include "package.hpp"
32 #include "filter_z3950_client.hpp"
39 #include <boost/thread/mutex.hpp>
40 #include <boost/thread/condition.hpp>
44 #include <yaz/otherinfo.h>
45 #include <yaz/diagbib1.h>
47 #include <yazpp/socket-manager.h>
48 #include <yazpp/pdu-assoc.h>
49 #include <yazpp/z-assoc.h>
51 namespace mp = metaproxy_1;
52 namespace yf = mp::filter;
54 namespace metaproxy_1 {
56 class Z3950Client::Assoc : public yazpp_1::Z_Assoc{
58 Assoc(yazpp_1::SocketManager *socket_manager,
59 yazpp_1::IPDU_Observable *PDU_Observable,
60 std::string host, int timeout);
65 void recv_GDU(Z_GDU *gdu, int len);
66 yazpp_1::IPDU_Observer* sessionNotify(
67 yazpp_1::IPDU_Observable *the_PDU_Observable,
70 yazpp_1::SocketManager *m_socket_manager;
71 yazpp_1::IPDU_Observable *m_PDU_Observable;
83 class Z3950Client::Rep {
85 // number of seconds to wait before we give up request
87 std::string m_default_target;
88 std::string m_force_target;
90 boost::condition m_cond_session_ready;
91 std::map<mp::Session,Z3950Client::Assoc *> m_clients;
92 Z3950Client::Assoc *get_assoc(Package &package);
93 void send_and_receive(Package &package,
94 yf::Z3950Client::Assoc *c);
95 void release_assoc(Package &package);
102 yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager,
103 yazpp_1::IPDU_Observable *PDU_Observable,
104 std::string host, int timeout_sec)
105 : Z_Assoc(PDU_Observable),
106 m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable),
107 m_package(0), m_in_use(true), m_waiting(false),
108 m_destroyed(false), m_connected(false), m_queue_len(1),
109 m_time_elapsed(0), m_time_max(timeout_sec),
112 // std::cout << "create assoc " << this << "\n";
115 yf::Z3950Client::Assoc::~Assoc()
117 // std::cout << "destroy assoc " << this << "\n";
120 void yf::Z3950Client::Assoc::connectNotify()
127 void yf::Z3950Client::Assoc::failNotify()
135 Z_GDU *gdu = m_package->request().get();
137 if (gdu && gdu->which == Z_GDU_Z3950)
140 m_package->response() = odr.create_close(apdu, Z_Close_peerAbort, 0);
141 m_package->session().close();
145 void yf::Z3950Client::Assoc::timeoutNotify()
148 if (m_time_elapsed >= m_time_max)
156 Z_GDU *gdu = m_package->request().get();
158 if (gdu && gdu->which == Z_GDU_Z3950)
162 m_package->response() =
163 odr.create_close(apdu, Z_Close_lackOfActivity, 0);
165 m_package->response() =
166 odr.create_close(apdu, Z_Close_peerAbort, 0);
168 m_package->session().close();
173 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
178 m_package->response() = gdu;
181 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
182 yazpp_1::IPDU_Observable *the_PDU_Observable,
189 yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep)
191 m_p->m_timeout_sec = 30;
194 yf::Z3950Client::~Z3950Client() {
197 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
199 // only one thread messes with the clients list at a time
200 boost::mutex::scoped_lock lock(m_mutex);
202 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
204 Z_GDU *gdu = package.request().get();
205 // only deal with Z39.50
206 if (!gdu || gdu->which != Z_GDU_Z3950)
211 it = m_clients.find(package.session());
212 if (it != m_clients.end())
214 it->second->m_queue_len++;
218 // double init .. NOT working yet
219 if (gdu && gdu->which == Z_GDU_Z3950 &&
220 gdu->u.z3950->which == Z_APDU_initRequest)
222 yazpp_1::SocketManager *s = it->second->m_socket_manager;
223 delete it->second; // destroy Z_Assoc
224 delete s; // then manager
229 if (!it->second->m_in_use)
231 it->second->m_in_use = true;
234 m_cond_session_ready.wait(lock);
237 // new Z39.50 session ..
238 Z_APDU *apdu = gdu->u.z3950;
239 // check that it is init. If not, close
240 if (apdu->which != Z_APDU_initRequest)
244 package.response() = odr.create_close(apdu,
245 Z_Close_protocolError,
246 "First PDU was not an "
247 "Initialize Request");
248 package.session().close();
251 std::string target = m_force_target;
252 if (!target.length())
254 target = m_default_target;
255 std::list<std::string> vhosts;
256 mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
258 size_t no_vhosts = vhosts.size();
261 std::list<std::string>::const_iterator v_it = vhosts.begin();
264 else if (no_vhosts == 0)
266 if (!target.length())
268 // no default target. So we don't know where to connect
270 package.response() = odr.create_initResponse(
272 YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
273 "z3950_client: No virtal host given");
275 package.session().close();
279 else if (no_vhosts > 1)
282 package.response() = odr.create_initResponse(
284 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
285 "z3950_client: Can not cope with multiple vhosts");
286 package.session().close();
290 std::list<std::string> dblist;
292 mp::util::split_zurl(target, host, dblist);
296 ; // z3950_client: Databases in vhost ignored
299 yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
300 yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
301 yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as,
304 m_clients[package.session()] = as;
308 void yf::Z3950Client::Rep::send_and_receive(Package &package,
309 yf::Z3950Client::Assoc *c)
311 Z_GDU *gdu = package.request().get();
316 if (!gdu || gdu->which != Z_GDU_Z3950)
319 c->m_time_elapsed = 0;
320 c->m_package = &package;
324 c->client(c->m_host.c_str());
325 c->timeout(1); // so timeoutNotify gets called once per second
327 while (!c->m_destroyed && c->m_waiting
328 && c->m_socket_manager->processEvent() > 0)
337 c->m_time_elapsed = 0;
340 // relay the package ..
342 c->send_GDU(gdu, &len);
344 switch(gdu->u.z3950->which)
346 case Z_APDU_triggerResourceControlRequest:
350 // for the rest: wait for a response PDU
351 while (!c->m_destroyed && c->m_waiting
352 && c->m_socket_manager->processEvent() > 0)
358 void yf::Z3950Client::Rep::release_assoc(Package &package)
360 boost::mutex::scoped_lock lock(m_mutex);
361 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
363 it = m_clients.find(package.session());
364 if (it != m_clients.end())
366 Z_GDU *gdu = package.request().get();
367 if (gdu && gdu->which == Z_GDU_Z3950)
368 { // only Z39.50 packages lock in get_assoc.. release it
369 it->second->m_in_use = false;
370 it->second->m_queue_len--;
373 if (package.session().is_closed())
375 // destroy hint (send_and_receive)
376 it->second->m_destroyed = true;
378 // wait until no one is waiting for it.
379 while (it->second->m_queue_len)
380 m_cond_session_ready.wait(lock);
382 // the Z_Assoc and PDU_Assoc must be destroyed before
383 // the socket manager.. so pull that out.. first..
384 yazpp_1::SocketManager *s = it->second->m_socket_manager;
385 delete it->second; // destroy Z_Assoc
386 delete s; // then manager
389 m_cond_session_ready.notify_all();
393 void yf::Z3950Client::process(Package &package) const
395 yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
398 m_p->send_and_receive(package, c);
400 m_p->release_assoc(package);
403 void yf::Z3950Client::configure(const xmlNode *ptr)
405 for (ptr = ptr->children; ptr; ptr = ptr->next)
407 if (ptr->type != XML_ELEMENT_NODE)
409 if (!strcmp((const char *) ptr->name, "timeout"))
411 m_p->m_timeout_sec = mp::xml::get_int(ptr->children, 30);
413 else if (!strcmp((const char *) ptr->name, "default_target"))
415 m_p->m_default_target = mp::xml::get_text(ptr);
417 else if (!strcmp((const char *) ptr->name, "force_target"))
419 m_p->m_force_target = mp::xml::get_text(ptr);
423 throw mp::filter::FilterException("Bad element "
424 + std::string((const char *)
430 static mp::filter::Base* filter_creator()
432 return new mp::filter::Z3950Client;
436 struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
446 * indent-tabs-mode: nil
447 * c-file-style: "stroustrup"
449 * vim: shiftwidth=4 tabstop=8 expandtab