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
22 #include "package.hpp"
24 #include "filter_z3950_client.hpp"
31 #include <boost/thread/mutex.hpp>
32 #include <boost/thread/condition.hpp>
33 #include <boost/thread/xtime.hpp>
37 #include <yaz/otherinfo.h>
38 #include <yaz/diagbib1.h>
40 #include <yazpp/socket-manager.h>
41 #include <yazpp/pdu-assoc.h>
42 #include <yazpp/z-assoc.h>
44 namespace mp = metaproxy_1;
45 namespace yf = mp::filter;
47 namespace metaproxy_1 {
49 class Z3950Client::Assoc : public yazpp_1::Z_Assoc{
51 Assoc(yazpp_1::SocketManager *socket_manager,
52 yazpp_1::IPDU_Observable *PDU_Observable,
53 std::string host, int timeout);
58 void recv_GDU(Z_GDU *gdu, int len);
59 yazpp_1::IPDU_Observer* sessionNotify(
60 yazpp_1::IPDU_Observable *the_PDU_Observable,
63 yazpp_1::SocketManager *m_socket_manager;
64 yazpp_1::IPDU_Observable *m_PDU_Observable;
76 class Z3950Client::Rep {
78 // number of seconds to wait before we give up request
81 std::string m_default_target;
82 std::string m_force_target;
84 boost::condition m_cond_session_ready;
85 std::map<mp::Session,Z3950Client::Assoc *> m_clients;
86 Z3950Client::Assoc *get_assoc(Package &package);
87 void send_and_receive(Package &package,
88 yf::Z3950Client::Assoc *c);
89 void release_assoc(Package &package);
96 yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager,
97 yazpp_1::IPDU_Observable *PDU_Observable,
98 std::string host, int timeout_sec)
99 : Z_Assoc(PDU_Observable),
100 m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable),
101 m_package(0), m_in_use(true), m_waiting(false),
102 m_destroyed(false), m_connected(false), m_queue_len(1),
103 m_time_elapsed(0), m_time_max(timeout_sec),
106 // std::cout << "create assoc " << this << "\n";
109 yf::Z3950Client::Assoc::~Assoc()
111 // std::cout << "destroy assoc " << this << "\n";
114 void yf::Z3950Client::Assoc::connectNotify()
121 void yf::Z3950Client::Assoc::failNotify()
129 Z_GDU *gdu = m_package->request().get();
131 if (gdu && gdu->which == Z_GDU_Z3950)
134 m_package->response() = odr.create_close(apdu, Z_Close_peerAbort, 0);
135 m_package->session().close();
139 void yf::Z3950Client::Assoc::timeoutNotify()
142 if (m_time_elapsed >= m_time_max)
150 Z_GDU *gdu = m_package->request().get();
152 if (gdu && gdu->which == Z_GDU_Z3950)
156 m_package->response() =
157 odr.create_close(apdu, Z_Close_lackOfActivity, 0);
159 m_package->response() =
160 odr.create_close(apdu, Z_Close_peerAbort, 0);
162 m_package->session().close();
167 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
172 m_package->response() = gdu;
175 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
176 yazpp_1::IPDU_Observable *the_PDU_Observable,
183 yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep)
185 m_p->m_timeout_sec = 30;
186 m_p->m_max_sockets = 0;
189 yf::Z3950Client::~Z3950Client() {
192 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
194 // only one thread messes with the clients list at a time
195 boost::mutex::scoped_lock lock(m_mutex);
197 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
199 Z_GDU *gdu = package.request().get();
200 // only deal with Z39.50
201 if (!gdu || gdu->which != Z_GDU_Z3950)
206 it = m_clients.find(package.session());
207 if (it != m_clients.end())
209 it->second->m_queue_len++;
213 // double init .. NOT working yet
214 if (gdu && gdu->which == Z_GDU_Z3950 &&
215 gdu->u.z3950->which == Z_APDU_initRequest)
217 yazpp_1::SocketManager *s = it->second->m_socket_manager;
218 delete it->second; // destroy Z_Assoc
219 delete s; // then manager
224 if (!it->second->m_in_use)
226 it->second->m_in_use = true;
229 m_cond_session_ready.wait(lock);
232 // new Z39.50 session ..
233 Z_APDU *apdu = gdu->u.z3950;
234 // check that it is init. If not, close
235 if (apdu->which != Z_APDU_initRequest)
239 package.response() = odr.create_close(apdu,
240 Z_Close_protocolError,
241 "First PDU was not an "
242 "Initialize Request");
243 package.session().close();
246 std::string target = m_force_target;
247 if (!target.length())
249 target = m_default_target;
250 std::list<std::string> vhosts;
251 mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
253 size_t no_vhosts = vhosts.size();
256 std::list<std::string>::const_iterator v_it = vhosts.begin();
259 else if (no_vhosts == 0)
261 if (!target.length())
263 // no default target. So we don't know where to connect
265 package.response() = odr.create_initResponse(
267 YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
268 "z3950_client: No virtal host given");
270 package.session().close();
274 else if (no_vhosts > 1)
277 package.response() = odr.create_initResponse(
279 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
280 "z3950_client: Can not cope with multiple vhosts");
281 package.session().close();
286 std::list<std::string> dblist;
288 mp::util::split_zurl(target, host, dblist);
292 ; // z3950_client: Databases in vhost ignored
295 while (m_max_sockets)
298 it = m_clients.begin();
299 for (; it != m_clients.end(); it++)
301 yf::Z3950Client::Assoc *as = it->second;
302 if (!strcmp(as->get_hostname(), host.c_str()))
305 if (number < m_max_sockets)
308 xtime_get(&xt, boost::TIME_UTC);
311 if (!m_cond_session_ready.timed_wait(lock, xt))
315 package.response() = odr.create_initResponse(
316 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions");
317 package.session().close();
322 yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
323 yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
324 yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as,
327 m_clients[package.session()] = as;
331 void yf::Z3950Client::Rep::send_and_receive(Package &package,
332 yf::Z3950Client::Assoc *c)
334 Z_GDU *gdu = package.request().get();
339 if (!gdu || gdu->which != Z_GDU_Z3950)
342 c->m_time_elapsed = 0;
343 c->m_package = &package;
347 c->client(c->m_host.c_str());
348 c->timeout(1); // so timeoutNotify gets called once per second
350 while (!c->m_destroyed && c->m_waiting
351 && c->m_socket_manager->processEvent() > 0)
360 c->m_time_elapsed = 0;
363 // relay the package ..
365 c->send_GDU(gdu, &len);
367 switch(gdu->u.z3950->which)
369 case Z_APDU_triggerResourceControlRequest:
373 // for the rest: wait for a response PDU
374 while (!c->m_destroyed && c->m_waiting
375 && c->m_socket_manager->processEvent() > 0)
381 void yf::Z3950Client::Rep::release_assoc(Package &package)
383 boost::mutex::scoped_lock lock(m_mutex);
384 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
386 it = m_clients.find(package.session());
387 if (it != m_clients.end())
389 Z_GDU *gdu = package.request().get();
390 if (gdu && gdu->which == Z_GDU_Z3950)
391 { // only Z39.50 packages lock in get_assoc.. release it
392 it->second->m_in_use = false;
393 it->second->m_queue_len--;
396 if (package.session().is_closed())
398 // destroy hint (send_and_receive)
399 it->second->m_destroyed = true;
401 // wait until no one is waiting for it.
402 while (it->second->m_queue_len)
403 m_cond_session_ready.wait(lock);
405 // the Z_Assoc and PDU_Assoc must be destroyed before
406 // the socket manager.. so pull that out.. first..
407 yazpp_1::SocketManager *s = it->second->m_socket_manager;
408 delete it->second; // destroy Z_Assoc
409 delete s; // then manager
412 m_cond_session_ready.notify_all();
416 void yf::Z3950Client::process(Package &package) const
418 yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
421 m_p->send_and_receive(package, c);
423 m_p->release_assoc(package);
426 void yf::Z3950Client::configure(const xmlNode *ptr, bool test_only)
428 for (ptr = ptr->children; ptr; ptr = ptr->next)
430 if (ptr->type != XML_ELEMENT_NODE)
432 if (!strcmp((const char *) ptr->name, "timeout"))
434 m_p->m_timeout_sec = mp::xml::get_int(ptr->children, 30);
436 else if (!strcmp((const char *) ptr->name, "default_target"))
438 m_p->m_default_target = mp::xml::get_text(ptr);
440 else if (!strcmp((const char *) ptr->name, "force_target"))
442 m_p->m_force_target = mp::xml::get_text(ptr);
444 else if (!strcmp((const char *) ptr->name, "max-sockets"))
446 m_p->m_max_sockets = mp::xml::get_int(ptr->children, 0);
450 throw mp::filter::FilterException("Bad element "
451 + std::string((const char *)
457 static mp::filter::Base* filter_creator()
459 return new mp::filter::Z3950Client;
463 struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
473 * indent-tabs-mode: nil
474 * c-file-style: "stroustrup"
476 * vim: shiftwidth=4 tabstop=8 expandtab