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 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;
73 int m_time_connect_max;
77 class Z3950Client::Rep {
79 // number of seconds to wait before we give up request
83 std::string m_default_target;
84 std::string m_force_target;
86 boost::condition m_cond_session_ready;
87 std::map<mp::Session,Z3950Client::Assoc *> m_clients;
88 Z3950Client::Assoc *get_assoc(Package &package);
89 void send_and_receive(Package &package,
90 yf::Z3950Client::Assoc *c);
91 void release_assoc(Package &package);
98 yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager,
99 yazpp_1::IPDU_Observable *PDU_Observable,
100 std::string host, int timeout_sec)
101 : Z_Assoc(PDU_Observable),
102 m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable),
103 m_package(0), m_in_use(true), m_waiting(false),
104 m_destroyed(false), m_connected(false), m_has_closed(false),
106 m_time_elapsed(0), m_time_max(timeout_sec), m_time_connect_max(10),
109 // std::cout << "create assoc " << this << "\n";
112 yf::Z3950Client::Assoc::~Assoc()
114 // std::cout << "destroy assoc " << this << "\n";
117 void yf::Z3950Client::Assoc::connectNotify()
124 void yf::Z3950Client::Assoc::failNotify()
132 Z_GDU *gdu = m_package->request().get();
134 if (gdu && gdu->which == Z_GDU_Z3950)
137 m_package->response() = odr.create_close(apdu, Z_Close_peerAbort, 0);
138 m_package->session().close();
142 void yf::Z3950Client::Assoc::timeoutNotify()
145 if ((m_connected && m_time_elapsed >= m_time_max)
146 || (!m_connected && m_time_elapsed >= m_time_connect_max))
154 Z_GDU *gdu = m_package->request().get();
156 if (gdu && gdu->which == Z_GDU_Z3950)
160 m_package->response() =
161 odr.create_close(apdu, Z_Close_lackOfActivity, 0);
163 m_package->response() =
164 odr.create_close(apdu, Z_Close_peerAbort, 0);
166 m_package->session().close();
171 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
176 m_package->response() = gdu;
179 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
180 yazpp_1::IPDU_Observable *the_PDU_Observable,
187 yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep)
189 m_p->m_timeout_sec = 30;
190 m_p->m_max_sockets = 0;
191 m_p->m_force_close = false;
194 yf::Z3950Client::~Z3950Client() {
197 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
199 // only one thread messes with the clients list at a time
200 boost::mutex::scoped_lock lock(m_mutex);
202 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
204 Z_GDU *gdu = package.request().get();
206 int max_sockets = package.origin().get_max_sockets();
207 if (max_sockets == 0)
208 max_sockets = m_max_sockets;
212 it = m_clients.find(package.session());
213 if (it != m_clients.end())
215 it->second->m_queue_len++;
219 // double init .. NOT working yet
220 if (gdu && gdu->which == Z_GDU_Z3950 &&
221 gdu->u.z3950->which == Z_APDU_initRequest)
223 yazpp_1::SocketManager *s = it->second->m_socket_manager;
224 delete it->second; // destroy Z_Assoc
225 delete s; // then manager
230 if (!it->second->m_in_use)
232 it->second->m_in_use = true;
235 m_cond_session_ready.wait(lock);
238 if (!gdu || gdu->which != Z_GDU_Z3950)
243 // new Z39.50 session ..
244 Z_APDU *apdu = gdu->u.z3950;
245 // check that it is init. If not, close
246 if (apdu->which != Z_APDU_initRequest)
250 package.response() = odr.create_close(apdu,
251 Z_Close_protocolError,
252 "First PDU was not an "
253 "Initialize Request");
254 package.session().close();
257 std::string target = m_force_target;
258 if (!target.length())
260 target = m_default_target;
261 std::list<std::string> vhosts;
262 mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
264 size_t no_vhosts = vhosts.size();
267 std::list<std::string>::const_iterator v_it = vhosts.begin();
270 else if (no_vhosts == 0)
272 if (!target.length())
274 // no default target. So we don't know where to connect
276 package.response() = odr.create_initResponse(
278 YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
279 "z3950_client: No vhost given");
281 package.session().close();
285 else if (no_vhosts > 1)
288 package.response() = odr.create_initResponse(
290 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
291 "z3950_client: Can not cope with multiple vhosts");
292 package.session().close();
297 std::list<std::string> dblist;
298 mp::util::split_zurl(target, host, dblist);
302 ; // z3950_client: Databases in vhost ignored
305 // see if we have reached max number of clients (max-sockets)
309 int no_not_in_use = 0;
311 it = m_clients.begin();
312 for (; it != m_clients.end(); it++)
314 yf::Z3950Client::Assoc *as = it->second;
315 if (!strcmp(as->m_host.c_str(), host.c_str()))
322 yaz_log(YLOG_LOG, "Found %d/%d connections for %s", number, max_sockets,
324 if (number < max_sockets)
326 if (no_not_in_use == 0) // all in use..
330 package.response() = odr.create_initResponse(
331 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions");
332 package.session().close();
336 xtime_get(&xt, boost::TIME_UTC);
339 if (!m_cond_session_ready.timed_wait(lock, xt))
343 package.response() = odr.create_initResponse(
344 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "max sessions");
345 package.session().close();
350 yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
351 yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
352 yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as,
355 m_clients[package.session()] = as;
359 void yf::Z3950Client::Rep::send_and_receive(Package &package,
360 yf::Z3950Client::Assoc *c)
365 c->m_package = &package;
367 if (package.session().is_closed() && c->m_connected && !c->m_has_closed
372 package.request() = odr.create_close(
373 0, Z_Close_finished, "z3950_client");
374 c->m_package = 0; // don't inspect response
376 Z_GDU *gdu = package.request().get();
378 if (!gdu || gdu->which != Z_GDU_Z3950)
381 if (gdu->u.z3950->which == Z_APDU_close)
382 c->m_has_closed = true;
385 c->m_time_elapsed = 0;
389 c->client(c->m_host.c_str());
390 c->timeout(1); // so timeoutNotify gets called once per second
392 while (!c->m_destroyed && c->m_waiting
393 && c->m_socket_manager->processEvent() > 0)
402 c->m_time_elapsed = 0;
405 // relay the package ..
407 c->send_GDU(gdu, &len);
409 switch (gdu->u.z3950->which)
411 case Z_APDU_triggerResourceControlRequest:
415 // for the rest: wait for a response PDU
416 while (!c->m_destroyed && c->m_waiting
417 && c->m_socket_manager->processEvent() > 0)
423 void yf::Z3950Client::Rep::release_assoc(Package &package)
425 boost::mutex::scoped_lock lock(m_mutex);
426 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
428 it = m_clients.find(package.session());
429 if (it != m_clients.end())
431 it->second->m_in_use = false;
432 it->second->m_queue_len--;
434 if (package.session().is_closed())
436 // destroy hint (send_and_receive)
437 it->second->m_destroyed = true;
438 if (it->second->m_queue_len == 0)
440 yazpp_1::SocketManager *s = it->second->m_socket_manager;
441 delete it->second; // destroy Z_Assoc
442 delete s; // then manager
446 m_cond_session_ready.notify_all();
450 void yf::Z3950Client::process(Package &package) const
452 yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
455 m_p->send_and_receive(package, c);
456 m_p->release_assoc(package);
460 void yf::Z3950Client::configure(const xmlNode *ptr, bool test_only)
462 for (ptr = ptr->children; ptr; ptr = ptr->next)
464 if (ptr->type != XML_ELEMENT_NODE)
466 if (!strcmp((const char *) ptr->name, "timeout"))
468 m_p->m_timeout_sec = mp::xml::get_int(ptr, 30);
470 else if (!strcmp((const char *) ptr->name, "default_target"))
472 m_p->m_default_target = mp::xml::get_text(ptr);
474 else if (!strcmp((const char *) ptr->name, "force_target"))
476 m_p->m_force_target = mp::xml::get_text(ptr);
478 else if (!strcmp((const char *) ptr->name, "max-sockets"))
480 m_p->m_max_sockets = mp::xml::get_int(ptr, 0);
482 else if (!strcmp((const char *) ptr->name, "force_close"))
484 m_p->m_force_close = mp::xml::get_bool(ptr, 0);
488 throw mp::filter::FilterException("Bad element "
489 + std::string((const char *)
495 static mp::filter::Base* filter_creator()
497 return new mp::filter::Z3950Client;
501 struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
511 * c-file-style: "Stroustrup"
512 * indent-tabs-mode: nil
514 * vim: shiftwidth=4 tabstop=8 expandtab