1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2009 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;
73 int m_time_connect_max;
77 class Z3950Client::Rep {
79 // number of seconds to wait before we give up request
82 std::string m_default_target;
83 std::string m_force_target;
85 boost::condition m_cond_session_ready;
86 std::map<mp::Session,Z3950Client::Assoc *> m_clients;
87 Z3950Client::Assoc *get_assoc(Package &package);
88 void send_and_receive(Package &package,
89 yf::Z3950Client::Assoc *c);
90 void release_assoc(Package &package);
97 yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager,
98 yazpp_1::IPDU_Observable *PDU_Observable,
99 std::string host, int timeout_sec)
100 : Z_Assoc(PDU_Observable),
101 m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable),
102 m_package(0), m_in_use(true), m_waiting(false),
103 m_destroyed(false), m_connected(false), m_queue_len(1),
104 m_time_elapsed(0), m_time_max(timeout_sec), m_time_connect_max(10),
107 // std::cout << "create assoc " << this << "\n";
110 yf::Z3950Client::Assoc::~Assoc()
112 // std::cout << "destroy assoc " << this << "\n";
115 void yf::Z3950Client::Assoc::connectNotify()
122 void yf::Z3950Client::Assoc::failNotify()
130 Z_GDU *gdu = m_package->request().get();
132 if (gdu && gdu->which == Z_GDU_Z3950)
135 m_package->response() = odr.create_close(apdu, Z_Close_peerAbort, 0);
136 m_package->session().close();
140 void yf::Z3950Client::Assoc::timeoutNotify()
143 if ((m_connected && m_time_elapsed >= m_time_max)
144 || (!m_connected && m_time_elapsed >= m_time_connect_max))
152 Z_GDU *gdu = m_package->request().get();
154 if (gdu && gdu->which == Z_GDU_Z3950)
158 m_package->response() =
159 odr.create_close(apdu, Z_Close_lackOfActivity, 0);
161 m_package->response() =
162 odr.create_close(apdu, Z_Close_peerAbort, 0);
164 m_package->session().close();
169 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
174 m_package->response() = gdu;
177 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
178 yazpp_1::IPDU_Observable *the_PDU_Observable,
185 yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep)
187 m_p->m_timeout_sec = 30;
188 m_p->m_max_sockets = 0;
191 yf::Z3950Client::~Z3950Client() {
194 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
196 // only one thread messes with the clients list at a time
197 boost::mutex::scoped_lock lock(m_mutex);
199 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
201 Z_GDU *gdu = package.request().get();
202 // only deal with Z39.50
203 if (!gdu || gdu->which != Z_GDU_Z3950)
209 int max_sockets = package.origin().get_max_sockets();
210 if (max_sockets == 0)
211 max_sockets = m_max_sockets;
215 it = m_clients.find(package.session());
216 if (it != m_clients.end())
218 it->second->m_queue_len++;
222 // double init .. NOT working yet
223 if (gdu && gdu->which == Z_GDU_Z3950 &&
224 gdu->u.z3950->which == Z_APDU_initRequest)
226 yazpp_1::SocketManager *s = it->second->m_socket_manager;
227 delete it->second; // destroy Z_Assoc
228 delete s; // then manager
233 if (!it->second->m_in_use)
235 it->second->m_in_use = true;
238 m_cond_session_ready.wait(lock);
241 // new Z39.50 session ..
242 Z_APDU *apdu = gdu->u.z3950;
243 // check that it is init. If not, close
244 if (apdu->which != Z_APDU_initRequest)
248 package.response() = odr.create_close(apdu,
249 Z_Close_protocolError,
250 "First PDU was not an "
251 "Initialize Request");
252 package.session().close();
255 std::string target = m_force_target;
256 if (!target.length())
258 target = m_default_target;
259 std::list<std::string> vhosts;
260 mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
262 size_t no_vhosts = vhosts.size();
265 std::list<std::string>::const_iterator v_it = vhosts.begin();
268 else if (no_vhosts == 0)
270 if (!target.length())
272 // no default target. So we don't know where to connect
274 package.response() = odr.create_initResponse(
276 YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
277 "z3950_client: No vhost given");
279 package.session().close();
283 else if (no_vhosts > 1)
286 package.response() = odr.create_initResponse(
288 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
289 "z3950_client: Can not cope with multiple vhosts");
290 package.session().close();
295 std::list<std::string> dblist;
296 mp::util::split_zurl(target, host, dblist);
300 ; // z3950_client: Databases in vhost ignored
303 // see if we have reached max number of clients (max-sockets)
307 int no_not_in_use = 0;
309 it = m_clients.begin();
310 for (; it != m_clients.end(); it++)
312 yf::Z3950Client::Assoc *as = it->second;
313 if (!strcmp(as->get_hostname(), host.c_str()))
320 yaz_log(YLOG_LOG, "Found %d/%d connections for %s", number, max_sockets,
322 if (number < max_sockets)
324 if (no_not_in_use == 0) // all in use..
328 package.response() = odr.create_initResponse(
329 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions");
330 package.session().close();
334 xtime_get(&xt, boost::TIME_UTC);
337 if (!m_cond_session_ready.timed_wait(lock, xt))
341 package.response() = odr.create_initResponse(
342 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions");
343 package.session().close();
348 yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
349 yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
350 yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as,
353 m_clients[package.session()] = as;
357 void yf::Z3950Client::Rep::send_and_receive(Package &package,
358 yf::Z3950Client::Assoc *c)
360 Z_GDU *gdu = package.request().get();
365 if (!gdu || gdu->which != Z_GDU_Z3950)
368 c->m_time_elapsed = 0;
369 c->m_package = &package;
373 c->client(c->m_host.c_str());
374 c->timeout(1); // so timeoutNotify gets called once per second
376 while (!c->m_destroyed && c->m_waiting
377 && c->m_socket_manager->processEvent() > 0)
386 c->m_time_elapsed = 0;
389 // relay the package ..
391 c->send_GDU(gdu, &len);
393 switch(gdu->u.z3950->which)
395 case Z_APDU_triggerResourceControlRequest:
399 // for the rest: wait for a response PDU
400 while (!c->m_destroyed && c->m_waiting
401 && c->m_socket_manager->processEvent() > 0)
407 void yf::Z3950Client::Rep::release_assoc(Package &package)
409 boost::mutex::scoped_lock lock(m_mutex);
410 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
412 it = m_clients.find(package.session());
413 if (it != m_clients.end())
415 Z_GDU *gdu = package.request().get();
416 if (gdu && gdu->which == Z_GDU_Z3950)
417 { // only Z39.50 packages lock in get_assoc.. release it
418 it->second->m_in_use = false;
419 it->second->m_queue_len--;
422 if (package.session().is_closed())
424 // destroy hint (send_and_receive)
425 it->second->m_destroyed = true;
426 if (it->second->m_queue_len == 0)
428 yazpp_1::SocketManager *s = it->second->m_socket_manager;
429 delete it->second; // destroy Z_Assoc
430 delete s; // then manager
434 m_cond_session_ready.notify_all();
438 void yf::Z3950Client::process(Package &package) const
440 yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
443 m_p->send_and_receive(package, c);
445 m_p->release_assoc(package);
448 void yf::Z3950Client::configure(const xmlNode *ptr, bool test_only)
450 for (ptr = ptr->children; ptr; ptr = ptr->next)
452 if (ptr->type != XML_ELEMENT_NODE)
454 if (!strcmp((const char *) ptr->name, "timeout"))
456 m_p->m_timeout_sec = mp::xml::get_int(ptr->children, 30);
458 else if (!strcmp((const char *) ptr->name, "default_target"))
460 m_p->m_default_target = mp::xml::get_text(ptr);
462 else if (!strcmp((const char *) ptr->name, "force_target"))
464 m_p->m_force_target = mp::xml::get_text(ptr);
466 else if (!strcmp((const char *) ptr->name, "max-sockets"))
468 m_p->m_max_sockets = mp::xml::get_int(ptr->children, 0);
472 throw mp::filter::FilterException("Bad element "
473 + std::string((const char *)
479 static mp::filter::Base* filter_creator()
481 return new mp::filter::Z3950Client;
485 struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
495 * c-file-style: "Stroustrup"
496 * indent-tabs-mode: nil
498 * vim: shiftwidth=4 tabstop=8 expandtab