1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2012 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;
179 // should really check for nsd->which.. But union has two members
180 // containing almost same data
182 // Z_InternationalString *v3Addinfo;
184 if (nsd->u.v2Addinfo)
186 addinfo.assign(nsd->u.v2Addinfo);
189 addinfo += "(backend=" + m_host + ")";
190 nsd->u.v2Addinfo = odr_strdup(odr, addinfo.c_str());
194 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
200 mp::odr odr; // must be in scope for response() = assignment
201 if (gdu && gdu->which == Z_GDU_Z3950)
203 Z_APDU *apdu = gdu->u.z3950;
206 case Z_APDU_searchResponse:
207 fixup_nsd(odr, apdu->u.searchResponse->records);
209 case Z_APDU_presentResponse:
210 fixup_nsd(odr, apdu->u.presentResponse->records);
214 m_package->response() = gdu;
218 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
219 yazpp_1::IPDU_Observable *the_PDU_Observable,
226 yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep)
228 m_p->m_timeout_sec = 30;
229 m_p->m_max_sockets = 0;
230 m_p->m_force_close = false;
233 yf::Z3950Client::~Z3950Client() {
236 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
238 // only one thread messes with the clients list at a time
239 boost::mutex::scoped_lock lock(m_mutex);
241 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
243 Z_GDU *gdu = package.request().get();
245 int max_sockets = package.origin().get_max_sockets();
246 if (max_sockets == 0)
247 max_sockets = m_max_sockets;
251 it = m_clients.find(package.session());
252 if (it != m_clients.end())
254 it->second->m_queue_len++;
258 // double init .. NOT working yet
259 if (gdu && gdu->which == Z_GDU_Z3950 &&
260 gdu->u.z3950->which == Z_APDU_initRequest)
262 yazpp_1::SocketManager *s = it->second->m_socket_manager;
263 delete it->second; // destroy Z_Assoc
264 delete s; // then manager
269 if (!it->second->m_in_use)
271 it->second->m_in_use = true;
274 m_cond_session_ready.wait(lock);
277 if (!gdu || gdu->which != Z_GDU_Z3950)
282 // new Z39.50 session ..
283 Z_APDU *apdu = gdu->u.z3950;
284 // check that it is init. If not, close
285 if (apdu->which != Z_APDU_initRequest)
289 package.response() = odr.create_close(apdu,
290 Z_Close_protocolError,
291 "First PDU was not an "
292 "Initialize Request");
293 package.session().close();
296 std::string target = m_force_target;
297 if (!target.length())
299 target = m_default_target;
300 std::list<std::string> vhosts;
301 mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
303 size_t no_vhosts = vhosts.size();
306 std::list<std::string>::const_iterator v_it = vhosts.begin();
309 else if (no_vhosts == 0)
311 if (!target.length())
313 // no default target. So we don't know where to connect
315 package.response() = odr.create_initResponse(
317 YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
318 "z3950_client: No vhost given");
320 package.session().close();
324 else if (no_vhosts > 1)
327 package.response() = odr.create_initResponse(
329 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
330 "z3950_client: Can not cope with multiple vhosts");
331 package.session().close();
336 std::list<std::string> dblist;
337 mp::util::split_zurl(target, host, dblist);
341 ; // z3950_client: Databases in vhost ignored
344 // see if we have reached max number of clients (max-sockets)
348 int no_not_in_use = 0;
350 it = m_clients.begin();
351 for (; it != m_clients.end(); it++)
353 yf::Z3950Client::Assoc *as = it->second;
354 if (!strcmp(as->m_host.c_str(), host.c_str()))
361 yaz_log(YLOG_LOG, "Found %d/%d connections for %s", number, max_sockets,
363 if (number < max_sockets)
365 if (no_not_in_use == 0) // all in use..
369 package.response() = odr.create_initResponse(
370 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions");
371 package.session().close();
375 xtime_get(&xt, boost::TIME_UTC);
378 if (!m_cond_session_ready.timed_wait(lock, xt))
382 package.response() = odr.create_initResponse(
383 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions");
384 package.session().close();
389 yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
390 yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
391 yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as,
394 m_clients[package.session()] = as;
398 void yf::Z3950Client::Rep::send_and_receive(Package &package,
399 yf::Z3950Client::Assoc *c)
404 c->m_package = &package;
406 if (package.session().is_closed() && c->m_connected && !c->m_has_closed
411 package.request() = odr.create_close(
412 0, Z_Close_finished, "z3950_client");
413 c->m_package = 0; // don't inspect response
415 Z_GDU *gdu = package.request().get();
417 if (!gdu || gdu->which != Z_GDU_Z3950)
420 if (gdu->u.z3950->which == Z_APDU_close)
421 c->m_has_closed = true;
424 c->m_time_elapsed = 0;
428 if (c->client(c->m_host.c_str()))
432 odr.create_close(gdu->u.z3950, Z_Close_peerAbort, 0);
433 package.session().close();
436 c->timeout(1); // so timeoutNotify gets called once per second
439 while (!c->m_destroyed && c->m_waiting
440 && c->m_socket_manager->processEvent() > 0)
449 c->m_time_elapsed = 0;
452 // relay the package ..
454 c->send_GDU(gdu, &len);
456 switch (gdu->u.z3950->which)
458 case Z_APDU_triggerResourceControlRequest:
462 // for the rest: wait for a response PDU
463 while (!c->m_destroyed && c->m_waiting
464 && c->m_socket_manager->processEvent() > 0)
470 void yf::Z3950Client::Rep::release_assoc(Package &package)
472 boost::mutex::scoped_lock lock(m_mutex);
473 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
475 it = m_clients.find(package.session());
476 if (it != m_clients.end())
478 it->second->m_in_use = false;
479 it->second->m_queue_len--;
481 if (package.session().is_closed())
483 // destroy hint (send_and_receive)
484 it->second->m_destroyed = true;
485 if (it->second->m_queue_len == 0)
487 yazpp_1::SocketManager *s = it->second->m_socket_manager;
488 delete it->second; // destroy Z_Assoc
489 delete s; // then manager
493 m_cond_session_ready.notify_all();
497 void yf::Z3950Client::process(Package &package) const
499 yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
502 m_p->send_and_receive(package, c);
503 m_p->release_assoc(package);
507 void yf::Z3950Client::configure(const xmlNode *ptr, bool test_only,
510 for (ptr = ptr->children; ptr; ptr = ptr->next)
512 if (ptr->type != XML_ELEMENT_NODE)
514 if (!strcmp((const char *) ptr->name, "timeout"))
516 m_p->m_timeout_sec = mp::xml::get_int(ptr, 30);
518 else if (!strcmp((const char *) ptr->name, "default_target"))
520 m_p->m_default_target = mp::xml::get_text(ptr);
522 else if (!strcmp((const char *) ptr->name, "force_target"))
524 m_p->m_force_target = mp::xml::get_text(ptr);
526 else if (!strcmp((const char *) ptr->name, "max-sockets"))
528 m_p->m_max_sockets = mp::xml::get_int(ptr, 0);
530 else if (!strcmp((const char *) ptr->name, "force_close"))
532 m_p->m_force_close = mp::xml::get_bool(ptr, 0);
536 throw mp::filter::FilterException("Bad element "
537 + std::string((const char *)
543 static mp::filter::Base* filter_creator()
545 return new mp::filter::Z3950Client;
549 struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
559 * c-file-style: "Stroustrup"
560 * indent-tabs-mode: nil
562 * vim: shiftwidth=4 tabstop=8 expandtab