1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2010 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 yazpp_1::IPDU_Observer* sessionNotify(
59 yazpp_1::IPDU_Observable *the_PDU_Observable,
62 yazpp_1::SocketManager *m_socket_manager;
63 yazpp_1::IPDU_Observable *m_PDU_Observable;
72 int m_time_connect_max;
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), m_time_connect_max(10),
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_connected && m_time_elapsed >= m_time_max)
143 || (!m_connected && m_time_elapsed >= m_time_connect_max))
151 Z_GDU *gdu = m_package->request().get();
153 if (gdu && gdu->which == Z_GDU_Z3950)
157 m_package->response() =
158 odr.create_close(apdu, Z_Close_lackOfActivity, 0);
160 m_package->response() =
161 odr.create_close(apdu, Z_Close_peerAbort, 0);
163 m_package->session().close();
168 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
173 m_package->response() = gdu;
176 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
177 yazpp_1::IPDU_Observable *the_PDU_Observable,
184 yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep)
186 m_p->m_timeout_sec = 30;
187 m_p->m_max_sockets = 0;
190 yf::Z3950Client::~Z3950Client() {
193 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
195 // only one thread messes with the clients list at a time
196 boost::mutex::scoped_lock lock(m_mutex);
198 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
200 Z_GDU *gdu = package.request().get();
201 // only deal with Z39.50
202 if (!gdu || gdu->which != Z_GDU_Z3950)
208 int max_sockets = package.origin().get_max_sockets();
209 if (max_sockets == 0)
210 max_sockets = m_max_sockets;
214 it = m_clients.find(package.session());
215 if (it != m_clients.end())
217 it->second->m_queue_len++;
221 // double init .. NOT working yet
222 if (gdu && gdu->which == Z_GDU_Z3950 &&
223 gdu->u.z3950->which == Z_APDU_initRequest)
225 yazpp_1::SocketManager *s = it->second->m_socket_manager;
226 delete it->second; // destroy Z_Assoc
227 delete s; // then manager
232 if (!it->second->m_in_use)
234 it->second->m_in_use = true;
237 m_cond_session_ready.wait(lock);
240 // new Z39.50 session ..
241 Z_APDU *apdu = gdu->u.z3950;
242 // check that it is init. If not, close
243 if (apdu->which != Z_APDU_initRequest)
247 package.response() = odr.create_close(apdu,
248 Z_Close_protocolError,
249 "First PDU was not an "
250 "Initialize Request");
251 package.session().close();
254 std::string target = m_force_target;
255 if (!target.length())
257 target = m_default_target;
258 std::list<std::string> vhosts;
259 mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
261 size_t no_vhosts = vhosts.size();
264 std::list<std::string>::const_iterator v_it = vhosts.begin();
267 else if (no_vhosts == 0)
269 if (!target.length())
271 // no default target. So we don't know where to connect
273 package.response() = odr.create_initResponse(
275 YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
276 "z3950_client: No vhost given");
278 package.session().close();
282 else if (no_vhosts > 1)
285 package.response() = odr.create_initResponse(
287 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
288 "z3950_client: Can not cope with multiple vhosts");
289 package.session().close();
294 std::list<std::string> dblist;
295 mp::util::split_zurl(target, host, dblist);
299 ; // z3950_client: Databases in vhost ignored
302 // see if we have reached max number of clients (max-sockets)
306 int no_not_in_use = 0;
308 it = m_clients.begin();
309 for (; it != m_clients.end(); it++)
311 yf::Z3950Client::Assoc *as = it->second;
312 if (!strcmp(as->get_hostname(), host.c_str()))
319 yaz_log(YLOG_LOG, "Found %d/%d connections for %s", number, max_sockets,
321 if (number < max_sockets)
323 if (no_not_in_use == 0) // all in use..
327 package.response() = odr.create_initResponse(
328 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions");
329 package.session().close();
333 xtime_get(&xt, boost::TIME_UTC);
336 if (!m_cond_session_ready.timed_wait(lock, xt))
340 package.response() = odr.create_initResponse(
341 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions");
342 package.session().close();
347 yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
348 yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
349 yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as,
352 m_clients[package.session()] = as;
356 void yf::Z3950Client::Rep::send_and_receive(Package &package,
357 yf::Z3950Client::Assoc *c)
359 Z_GDU *gdu = package.request().get();
364 if (!gdu || gdu->which != Z_GDU_Z3950)
367 c->m_time_elapsed = 0;
368 c->m_package = &package;
372 c->client(c->m_host.c_str());
373 c->timeout(1); // so timeoutNotify gets called once per second
375 while (!c->m_destroyed && c->m_waiting
376 && c->m_socket_manager->processEvent() > 0)
385 c->m_time_elapsed = 0;
388 // relay the package ..
390 c->send_GDU(gdu, &len);
392 switch(gdu->u.z3950->which)
394 case Z_APDU_triggerResourceControlRequest:
398 // for the rest: wait for a response PDU
399 while (!c->m_destroyed && c->m_waiting
400 && c->m_socket_manager->processEvent() > 0)
406 void yf::Z3950Client::Rep::release_assoc(Package &package)
408 boost::mutex::scoped_lock lock(m_mutex);
409 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
411 it = m_clients.find(package.session());
412 if (it != m_clients.end())
414 Z_GDU *gdu = package.request().get();
415 if (gdu && gdu->which == Z_GDU_Z3950)
416 { // only Z39.50 packages lock in get_assoc.. release it
417 it->second->m_in_use = false;
418 it->second->m_queue_len--;
421 if (package.session().is_closed())
423 // destroy hint (send_and_receive)
424 it->second->m_destroyed = true;
425 if (it->second->m_queue_len == 0)
427 yazpp_1::SocketManager *s = it->second->m_socket_manager;
428 delete it->second; // destroy Z_Assoc
429 delete s; // then manager
433 m_cond_session_ready.notify_all();
437 void yf::Z3950Client::process(Package &package) const
439 yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
442 m_p->send_and_receive(package, c);
444 m_p->release_assoc(package);
447 void yf::Z3950Client::configure(const xmlNode *ptr, bool test_only)
449 for (ptr = ptr->children; ptr; ptr = ptr->next)
451 if (ptr->type != XML_ELEMENT_NODE)
453 if (!strcmp((const char *) ptr->name, "timeout"))
455 m_p->m_timeout_sec = mp::xml::get_int(ptr->children, 30);
457 else if (!strcmp((const char *) ptr->name, "default_target"))
459 m_p->m_default_target = mp::xml::get_text(ptr);
461 else if (!strcmp((const char *) ptr->name, "force_target"))
463 m_p->m_force_target = mp::xml::get_text(ptr);
465 else if (!strcmp((const char *) ptr->name, "max-sockets"))
467 m_p->m_max_sockets = mp::xml::get_int(ptr->children, 0);
471 throw mp::filter::FilterException("Bad element "
472 + std::string((const char *)
478 static mp::filter::Base* filter_creator()
480 return new mp::filter::Z3950Client;
484 struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
494 * c-file-style: "Stroustrup"
495 * indent-tabs-mode: nil
497 * vim: shiftwidth=4 tabstop=8 expandtab