1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2011 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
21 #include "filter_z3950_client.hpp"
22 #include <metaproxy/package.hpp>
23 #include <metaproxy/util.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <boost/thread/condition.hpp>
32 #include <boost/thread/xtime.hpp>
36 #include <yaz/otherinfo.h>
37 #include <yaz/diagbib1.h>
39 #include <yazpp/socket-manager.h>
40 #include <yazpp/pdu-assoc.h>
41 #include <yazpp/z-assoc.h>
43 namespace mp = metaproxy_1;
44 namespace yf = mp::filter;
46 namespace metaproxy_1 {
48 class Z3950Client::Assoc : public yazpp_1::Z_Assoc{
50 Assoc(yazpp_1::SocketManager *socket_manager,
51 yazpp_1::IPDU_Observable *PDU_Observable,
52 std::string host, int timeout);
57 void recv_GDU(Z_GDU *gdu, int len);
58 void fixup_nsd(ODR odr, Z_Records *records);
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;
74 int m_time_connect_max;
78 class Z3950Client::Rep {
80 // 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_has_closed(false),
107 m_time_elapsed(0), m_time_max(timeout_sec), m_time_connect_max(10),
110 // std::cout << "create assoc " << this << "\n";
113 yf::Z3950Client::Assoc::~Assoc()
115 // std::cout << "destroy assoc " << this << "\n";
118 void yf::Z3950Client::Assoc::connectNotify()
125 void yf::Z3950Client::Assoc::failNotify()
133 Z_GDU *gdu = m_package->request().get();
135 if (gdu && gdu->which == Z_GDU_Z3950)
138 m_package->response() = odr.create_close(apdu, Z_Close_peerAbort, 0);
139 m_package->session().close();
143 void yf::Z3950Client::Assoc::timeoutNotify()
146 if ((m_connected && m_time_elapsed >= m_time_max)
147 || (!m_connected && m_time_elapsed >= m_time_connect_max))
155 Z_GDU *gdu = m_package->request().get();
157 if (gdu && gdu->which == Z_GDU_Z3950)
161 m_package->response() =
162 odr.create_close(apdu, Z_Close_lackOfActivity, 0);
164 m_package->response() =
165 odr.create_close(apdu, Z_Close_peerAbort, 0);
167 m_package->session().close();
172 void yf::Z3950Client::Assoc::fixup_nsd(ODR odr, Z_Records *records)
174 if (records && records->which == Z_Records_NSD)
176 Z_DefaultDiagFormat *nsd = records->u.nonSurrogateDiagnostic;
177 if (nsd->which == Z_DiagRec_defaultFormat)
181 if (nsd->u.v2Addinfo)
183 addinfo.assign(nsd->u.v2Addinfo);
186 addinfo += "(backend=" + m_host + ")";
187 nsd->u.v2Addinfo = odr_strdup(odr, addinfo.c_str());
192 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
198 mp::odr odr; // must be in scope for response() = assignment
199 if (gdu && gdu->which == Z_GDU_Z3950)
201 Z_APDU *apdu = gdu->u.z3950;
204 case Z_APDU_searchResponse:
205 fixup_nsd(odr, apdu->u.searchResponse->records);
207 case Z_APDU_presentResponse:
208 fixup_nsd(odr, apdu->u.presentResponse->records);
212 m_package->response() = gdu;
216 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
217 yazpp_1::IPDU_Observable *the_PDU_Observable,
224 yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep)
226 m_p->m_timeout_sec = 30;
227 m_p->m_max_sockets = 0;
228 m_p->m_force_close = false;
231 yf::Z3950Client::~Z3950Client() {
234 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
236 // only one thread messes with the clients list at a time
237 boost::mutex::scoped_lock lock(m_mutex);
239 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
241 Z_GDU *gdu = package.request().get();
243 int max_sockets = package.origin().get_max_sockets();
244 if (max_sockets == 0)
245 max_sockets = m_max_sockets;
249 it = m_clients.find(package.session());
250 if (it != m_clients.end())
252 it->second->m_queue_len++;
256 // double init .. NOT working yet
257 if (gdu && gdu->which == Z_GDU_Z3950 &&
258 gdu->u.z3950->which == Z_APDU_initRequest)
260 yazpp_1::SocketManager *s = it->second->m_socket_manager;
261 delete it->second; // destroy Z_Assoc
262 delete s; // then manager
267 if (!it->second->m_in_use)
269 it->second->m_in_use = true;
272 m_cond_session_ready.wait(lock);
275 if (!gdu || gdu->which != Z_GDU_Z3950)
280 // new Z39.50 session ..
281 Z_APDU *apdu = gdu->u.z3950;
282 // check that it is init. If not, close
283 if (apdu->which != Z_APDU_initRequest)
287 package.response() = odr.create_close(apdu,
288 Z_Close_protocolError,
289 "First PDU was not an "
290 "Initialize Request");
291 package.session().close();
294 std::string target = m_force_target;
295 if (!target.length())
297 target = m_default_target;
298 std::list<std::string> vhosts;
299 mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
301 size_t no_vhosts = vhosts.size();
304 std::list<std::string>::const_iterator v_it = vhosts.begin();
307 else if (no_vhosts == 0)
309 if (!target.length())
311 // no default target. So we don't know where to connect
313 package.response() = odr.create_initResponse(
315 YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
316 "z3950_client: No vhost given");
318 package.session().close();
322 else if (no_vhosts > 1)
325 package.response() = odr.create_initResponse(
327 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
328 "z3950_client: Can not cope with multiple vhosts");
329 package.session().close();
334 std::list<std::string> dblist;
335 mp::util::split_zurl(target, host, dblist);
339 ; // z3950_client: Databases in vhost ignored
342 // see if we have reached max number of clients (max-sockets)
346 int no_not_in_use = 0;
348 it = m_clients.begin();
349 for (; it != m_clients.end(); it++)
351 yf::Z3950Client::Assoc *as = it->second;
352 if (!strcmp(as->m_host.c_str(), host.c_str()))
359 yaz_log(YLOG_LOG, "Found %d/%d connections for %s", number, max_sockets,
361 if (number < max_sockets)
363 if (no_not_in_use == 0) // all in use..
367 package.response() = odr.create_initResponse(
368 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions");
369 package.session().close();
373 xtime_get(&xt, boost::TIME_UTC);
376 if (!m_cond_session_ready.timed_wait(lock, xt))
380 package.response() = odr.create_initResponse(
381 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions");
382 package.session().close();
387 yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
388 yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
389 yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as,
392 m_clients[package.session()] = as;
396 void yf::Z3950Client::Rep::send_and_receive(Package &package,
397 yf::Z3950Client::Assoc *c)
402 c->m_package = &package;
404 if (package.session().is_closed() && c->m_connected && !c->m_has_closed
409 package.request() = odr.create_close(
410 0, Z_Close_finished, "z3950_client");
411 c->m_package = 0; // don't inspect response
413 Z_GDU *gdu = package.request().get();
415 if (!gdu || gdu->which != Z_GDU_Z3950)
418 if (gdu->u.z3950->which == Z_APDU_close)
419 c->m_has_closed = true;
422 c->m_time_elapsed = 0;
426 if (c->client(c->m_host.c_str()))
430 odr.create_close(gdu->u.z3950, Z_Close_peerAbort, 0);
431 package.session().close();
434 c->timeout(1); // so timeoutNotify gets called once per second
437 while (!c->m_destroyed && c->m_waiting
438 && c->m_socket_manager->processEvent() > 0)
447 c->m_time_elapsed = 0;
450 // relay the package ..
452 c->send_GDU(gdu, &len);
454 switch (gdu->u.z3950->which)
456 case Z_APDU_triggerResourceControlRequest:
460 // for the rest: wait for a response PDU
461 while (!c->m_destroyed && c->m_waiting
462 && c->m_socket_manager->processEvent() > 0)
468 void yf::Z3950Client::Rep::release_assoc(Package &package)
470 boost::mutex::scoped_lock lock(m_mutex);
471 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
473 it = m_clients.find(package.session());
474 if (it != m_clients.end())
476 it->second->m_in_use = false;
477 it->second->m_queue_len--;
479 if (package.session().is_closed())
481 // destroy hint (send_and_receive)
482 it->second->m_destroyed = true;
483 if (it->second->m_queue_len == 0)
485 yazpp_1::SocketManager *s = it->second->m_socket_manager;
486 delete it->second; // destroy Z_Assoc
487 delete s; // then manager
491 m_cond_session_ready.notify_all();
495 void yf::Z3950Client::process(Package &package) const
497 yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
500 m_p->send_and_receive(package, c);
501 m_p->release_assoc(package);
505 void yf::Z3950Client::configure(const xmlNode *ptr, bool test_only,
508 for (ptr = ptr->children; ptr; ptr = ptr->next)
510 if (ptr->type != XML_ELEMENT_NODE)
512 if (!strcmp((const char *) ptr->name, "timeout"))
514 m_p->m_timeout_sec = mp::xml::get_int(ptr, 30);
516 else if (!strcmp((const char *) ptr->name, "default_target"))
518 m_p->m_default_target = mp::xml::get_text(ptr);
520 else if (!strcmp((const char *) ptr->name, "force_target"))
522 m_p->m_force_target = mp::xml::get_text(ptr);
524 else if (!strcmp((const char *) ptr->name, "max-sockets"))
526 m_p->m_max_sockets = mp::xml::get_int(ptr, 0);
528 else if (!strcmp((const char *) ptr->name, "force_close"))
530 m_p->m_force_close = mp::xml::get_bool(ptr, 0);
534 throw mp::filter::FilterException("Bad element "
535 + std::string((const char *)
541 static mp::filter::Base* filter_creator()
543 return new mp::filter::Z3950Client;
547 struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
557 * c-file-style: "Stroustrup"
558 * indent-tabs-mode: nil
560 * vim: shiftwidth=4 tabstop=8 expandtab