1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 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 void fixup_nsd(ODR odr, Z_DefaultDiagFormat *nsd);
60 void fixup_init(ODR odr, Z_InitResponse *initrs);
61 yazpp_1::IPDU_Observer* sessionNotify(
62 yazpp_1::IPDU_Observable *the_PDU_Observable,
65 yazpp_1::SocketManager *m_socket_manager;
66 yazpp_1::IPDU_Observable *m_PDU_Observable;
76 int m_time_connect_max;
80 class Z3950Client::Rep {
82 // number of seconds to wait before we give up request
86 std::string m_default_target;
87 std::string m_force_target;
89 boost::condition m_cond_session_ready;
90 std::map<mp::Session,Z3950Client::Assoc *> m_clients;
91 Z3950Client::Assoc *get_assoc(Package &package);
92 void send_and_receive(Package &package,
93 yf::Z3950Client::Assoc *c);
94 void release_assoc(Package &package);
101 yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager,
102 yazpp_1::IPDU_Observable *PDU_Observable,
103 std::string host, int timeout_sec)
104 : Z_Assoc(PDU_Observable),
105 m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable),
106 m_package(0), m_in_use(true), m_waiting(false),
107 m_destroyed(false), m_connected(false), m_has_closed(false),
109 m_time_elapsed(0), m_time_max(timeout_sec), m_time_connect_max(10),
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_connected && m_time_elapsed >= m_time_max)
149 || (!m_connected && m_time_elapsed >= m_time_connect_max))
157 Z_GDU *gdu = m_package->request().get();
159 if (gdu && gdu->which == Z_GDU_Z3950)
163 m_package->response() =
164 odr.create_close(apdu, Z_Close_lackOfActivity, 0);
166 m_package->response() =
167 odr.create_close(apdu, Z_Close_peerAbort, 0);
169 m_package->session().close();
174 void yf::Z3950Client::Assoc::fixup_nsd(ODR odr, Z_DefaultDiagFormat *nsd)
178 // should really check for nsd->which.. But union has two members
179 // containing almost same data
180 const char *v2Addinfo = nsd->u.v2Addinfo;
181 // Z_InternationalString *v3Addinfo;
182 if (v2Addinfo && *v2Addinfo)
184 addinfo.assign(nsd->u.v2Addinfo);
187 addinfo += "(backend=" + m_host + ")";
188 nsd->u.v2Addinfo = odr_strdup(odr, addinfo.c_str());
191 void yf::Z3950Client::Assoc::fixup_nsd(ODR odr, Z_Records *records)
193 if (records && records->which == Z_Records_NSD)
195 fixup_nsd(odr, records->u.nonSurrogateDiagnostic);
197 if (records && records->which == Z_Records_multipleNSD)
199 Z_DiagRecs *drecs = records->u.multipleNonSurDiagnostics;
201 for (i = 0; i < drecs->num_diagRecs; i++)
203 Z_DiagRec *dr = drecs->diagRecs[i];
205 if (dr->which == Z_DiagRec_defaultFormat)
206 fixup_nsd(odr, dr->u.defaultFormat);
211 void yf::Z3950Client::Assoc::fixup_init(ODR odr, Z_InitResponse *initrs)
213 Z_External *uif = initrs->userInformationField;
215 if (uif && uif->which == Z_External_userInfo1)
217 Z_OtherInformation *ui = uif->u.userInfo1;
219 for (i = 0; i < ui->num_elements; i++)
221 Z_OtherInformationUnit *unit = ui->list[i];
222 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
223 unit->information.externallyDefinedInfo &&
224 unit->information.externallyDefinedInfo->which ==
227 Z_DiagnosticFormat *diag =
228 unit->information.externallyDefinedInfo->u.diag1;
230 for (j = 0; j < diag->num; j++)
232 Z_DiagnosticFormat_s *ds = diag->elements[j];
233 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
235 Z_DefaultDiagFormat *r = ds->u.defaultDiagRec;
236 char *oaddinfo = r->u.v2Addinfo;
237 char *naddinfo = (char *) odr_malloc(
239 (oaddinfo ? strlen(oaddinfo) : 0) + 20 +
242 if (oaddinfo && *oaddinfo)
244 strcat(naddinfo, oaddinfo);
245 strcat(naddinfo, " ");
247 strcat(naddinfo, "(backend=");
248 strcat(naddinfo, m_host.c_str());
249 strcat(naddinfo, ")");
251 r->u.v2Addinfo = naddinfo;
259 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
265 mp::odr odr; // must be in scope for response() = assignment
266 if (gdu && gdu->which == Z_GDU_Z3950)
268 Z_APDU *apdu = gdu->u.z3950;
271 case Z_APDU_searchResponse:
272 fixup_nsd(odr, apdu->u.searchResponse->records);
274 case Z_APDU_presentResponse:
275 fixup_nsd(odr, apdu->u.presentResponse->records);
277 case Z_APDU_initResponse:
278 fixup_init(odr, apdu->u.initResponse);
282 m_package->response() = gdu;
286 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
287 yazpp_1::IPDU_Observable *the_PDU_Observable,
294 yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep)
296 m_p->m_timeout_sec = 30;
297 m_p->m_max_sockets = 0;
298 m_p->m_force_close = false;
301 yf::Z3950Client::~Z3950Client() {
304 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
306 // only one thread messes with the clients list at a time
307 boost::mutex::scoped_lock lock(m_mutex);
309 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
311 Z_GDU *gdu = package.request().get();
313 int max_sockets = package.origin().get_max_sockets();
314 if (max_sockets == 0)
315 max_sockets = m_max_sockets;
317 it = m_clients.find(package.session());
318 if (it != m_clients.end())
320 it->second->m_queue_len++;
324 // double init .. NOT working yet
325 if (gdu && gdu->which == Z_GDU_Z3950 &&
326 gdu->u.z3950->which == Z_APDU_initRequest)
328 yazpp_1::SocketManager *s = it->second->m_socket_manager;
329 delete it->second; // destroy Z_Assoc
330 delete s; // then manager
335 if (!it->second->m_in_use)
337 it->second->m_in_use = true;
340 m_cond_session_ready.wait(lock);
343 if (!gdu || gdu->which != Z_GDU_Z3950)
348 // new Z39.50 session ..
349 Z_APDU *apdu = gdu->u.z3950;
350 // check that it is init. If not, close
351 if (apdu->which != Z_APDU_initRequest)
355 package.response() = odr.create_close(apdu,
356 Z_Close_protocolError,
357 "First PDU was not an "
358 "Initialize Request");
359 package.session().close();
362 std::string target = m_force_target;
363 if (!target.length())
365 target = m_default_target;
366 std::list<std::string> vhosts;
367 mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
369 size_t no_vhosts = vhosts.size();
372 std::list<std::string>::const_iterator v_it = vhosts.begin();
375 else if (no_vhosts == 0)
377 if (!target.length())
379 // no default target. So we don't know where to connect
381 package.response() = odr.create_initResponse(
383 YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
384 "z3950_client: No vhost given");
386 package.session().close();
390 else if (no_vhosts > 1)
393 package.response() = odr.create_initResponse(
395 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
396 "z3950_client: Can not cope with multiple vhosts");
397 package.session().close();
402 // see if we have reached max number of clients (max-sockets)
406 int no_not_in_use = 0;
408 it = m_clients.begin();
409 for (; it != m_clients.end(); it++)
411 yf::Z3950Client::Assoc *as = it->second;
412 if (!strcmp(as->m_host.c_str(), target.c_str()))
419 yaz_log(YLOG_LOG, "Found %d/%d connections for %s", number, max_sockets,
421 if (number < max_sockets)
423 if (no_not_in_use == 0) // all in use..
427 package.response() = odr.create_initResponse(
428 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
429 "z3950_client: max sessions");
430 package.session().close();
435 #if BOOST_VERSION >= 105000
443 if (!m_cond_session_ready.timed_wait(lock, xt))
447 package.response() = odr.create_initResponse(
448 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
449 "z3950_client: max sessions");
450 package.session().close();
455 yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
456 yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
457 yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as,
460 m_clients[package.session()] = as;
464 void yf::Z3950Client::Rep::send_and_receive(Package &package,
465 yf::Z3950Client::Assoc *c)
470 c->m_package = &package;
472 if (package.session().is_closed() && c->m_connected && !c->m_has_closed
477 package.request() = odr.create_close(
478 0, Z_Close_finished, "z3950_client");
479 c->m_package = 0; // don't inspect response
481 Z_GDU *gdu = package.request().get();
483 if (!gdu || gdu->which != Z_GDU_Z3950)
486 if (gdu->u.z3950->which == Z_APDU_close)
487 c->m_has_closed = true;
490 c->m_time_elapsed = 0;
494 if (c->client(c->m_host.c_str()))
498 odr.create_close(gdu->u.z3950, Z_Close_peerAbort, 0);
499 package.session().close();
502 c->timeout(1); // so timeoutNotify gets called once per second
505 while (!c->m_destroyed && c->m_waiting
506 && c->m_socket_manager->processEvent() > 0)
515 c->m_time_elapsed = 0;
518 // relay the package ..
520 c->send_GDU(gdu, &len);
522 switch (gdu->u.z3950->which)
524 case Z_APDU_triggerResourceControlRequest:
528 // for the rest: wait for a response PDU
529 while (!c->m_destroyed && c->m_waiting
530 && c->m_socket_manager->processEvent() > 0)
536 void yf::Z3950Client::Rep::release_assoc(Package &package)
538 boost::mutex::scoped_lock lock(m_mutex);
539 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
541 it = m_clients.find(package.session());
542 if (it != m_clients.end())
544 it->second->m_in_use = false;
545 it->second->m_queue_len--;
547 if (package.session().is_closed())
549 // destroy hint (send_and_receive)
550 it->second->m_destroyed = true;
551 if (it->second->m_queue_len == 0)
553 yazpp_1::SocketManager *s = it->second->m_socket_manager;
554 delete it->second; // destroy Z_Assoc
555 delete s; // then manager
559 m_cond_session_ready.notify_all();
563 void yf::Z3950Client::process(Package &package) const
565 yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
568 m_p->send_and_receive(package, c);
569 m_p->release_assoc(package);
573 void yf::Z3950Client::configure(const xmlNode *ptr, bool test_only,
576 for (ptr = ptr->children; ptr; ptr = ptr->next)
578 if (ptr->type != XML_ELEMENT_NODE)
580 if (!strcmp((const char *) ptr->name, "timeout"))
582 m_p->m_timeout_sec = mp::xml::get_int(ptr, 30);
584 else if (!strcmp((const char *) ptr->name, "default_target"))
586 m_p->m_default_target = mp::xml::get_text(ptr);
588 else if (!strcmp((const char *) ptr->name, "force_target"))
590 m_p->m_force_target = mp::xml::get_text(ptr);
592 else if (!strcmp((const char *) ptr->name, "max-sockets"))
594 m_p->m_max_sockets = mp::xml::get_int(ptr, 0);
596 else if (!strcmp((const char *) ptr->name, "force_close"))
598 m_p->m_force_close = mp::xml::get_bool(ptr, 0);
602 throw mp::filter::FilterException("Bad element "
603 + std::string((const char *)
609 static mp::filter::Base* filter_creator()
611 return new mp::filter::Z3950Client;
615 struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
625 * c-file-style: "Stroustrup"
626 * indent-tabs-mode: nil
628 * vim: shiftwidth=4 tabstop=8 expandtab