1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2008 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>
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;
75 class Z3950Client::Rep {
77 // number of seconds to wait before we give up request
79 std::string m_default_target;
80 std::string m_force_target;
82 boost::condition m_cond_session_ready;
83 std::map<mp::Session,Z3950Client::Assoc *> m_clients;
84 Z3950Client::Assoc *get_assoc(Package &package);
85 void send_and_receive(Package &package,
86 yf::Z3950Client::Assoc *c);
87 void release_assoc(Package &package);
94 yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager,
95 yazpp_1::IPDU_Observable *PDU_Observable,
96 std::string host, int timeout_sec)
97 : Z_Assoc(PDU_Observable),
98 m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable),
99 m_package(0), m_in_use(true), m_waiting(false),
100 m_destroyed(false), m_connected(false), m_queue_len(1),
101 m_time_elapsed(0), m_time_max(timeout_sec),
104 // std::cout << "create assoc " << this << "\n";
107 yf::Z3950Client::Assoc::~Assoc()
109 // std::cout << "destroy assoc " << this << "\n";
112 void yf::Z3950Client::Assoc::connectNotify()
119 void yf::Z3950Client::Assoc::failNotify()
127 Z_GDU *gdu = m_package->request().get();
129 if (gdu && gdu->which == Z_GDU_Z3950)
132 m_package->response() = odr.create_close(apdu, Z_Close_peerAbort, 0);
133 m_package->session().close();
137 void yf::Z3950Client::Assoc::timeoutNotify()
140 if (m_time_elapsed >= m_time_max)
148 Z_GDU *gdu = m_package->request().get();
150 if (gdu && gdu->which == Z_GDU_Z3950)
154 m_package->response() =
155 odr.create_close(apdu, Z_Close_lackOfActivity, 0);
157 m_package->response() =
158 odr.create_close(apdu, Z_Close_peerAbort, 0);
160 m_package->session().close();
165 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
170 m_package->response() = gdu;
173 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
174 yazpp_1::IPDU_Observable *the_PDU_Observable,
181 yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep)
183 m_p->m_timeout_sec = 30;
186 yf::Z3950Client::~Z3950Client() {
189 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
191 // only one thread messes with the clients list at a time
192 boost::mutex::scoped_lock lock(m_mutex);
194 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
196 Z_GDU *gdu = package.request().get();
197 // only deal with Z39.50
198 if (!gdu || gdu->which != Z_GDU_Z3950)
203 it = m_clients.find(package.session());
204 if (it != m_clients.end())
206 it->second->m_queue_len++;
210 // double init .. NOT working yet
211 if (gdu && gdu->which == Z_GDU_Z3950 &&
212 gdu->u.z3950->which == Z_APDU_initRequest)
214 yazpp_1::SocketManager *s = it->second->m_socket_manager;
215 delete it->second; // destroy Z_Assoc
216 delete s; // then manager
221 if (!it->second->m_in_use)
223 it->second->m_in_use = true;
226 m_cond_session_ready.wait(lock);
229 // new Z39.50 session ..
230 Z_APDU *apdu = gdu->u.z3950;
231 // check that it is init. If not, close
232 if (apdu->which != Z_APDU_initRequest)
236 package.response() = odr.create_close(apdu,
237 Z_Close_protocolError,
238 "First PDU was not an "
239 "Initialize Request");
240 package.session().close();
243 std::string target = m_force_target;
244 if (!target.length())
246 target = m_default_target;
247 std::list<std::string> vhosts;
248 mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
250 size_t no_vhosts = vhosts.size();
253 std::list<std::string>::const_iterator v_it = vhosts.begin();
256 else if (no_vhosts == 0)
258 if (!target.length())
260 // no default target. So we don't know where to connect
262 package.response() = odr.create_initResponse(
264 YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
265 "z3950_client: No virtal host given");
267 package.session().close();
271 else if (no_vhosts > 1)
274 package.response() = odr.create_initResponse(
276 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
277 "z3950_client: Can not cope with multiple vhosts");
278 package.session().close();
282 std::list<std::string> dblist;
284 mp::util::split_zurl(target, host, dblist);
288 ; // z3950_client: Databases in vhost ignored
291 yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
292 yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
293 yf::Z3950Client::Assoc *as = new yf::Z3950Client::Assoc(sm, pdu_as,
296 m_clients[package.session()] = as;
300 void yf::Z3950Client::Rep::send_and_receive(Package &package,
301 yf::Z3950Client::Assoc *c)
303 Z_GDU *gdu = package.request().get();
308 if (!gdu || gdu->which != Z_GDU_Z3950)
311 c->m_time_elapsed = 0;
312 c->m_package = &package;
316 c->client(c->m_host.c_str());
317 c->timeout(1); // so timeoutNotify gets called once per second
319 while (!c->m_destroyed && c->m_waiting
320 && c->m_socket_manager->processEvent() > 0)
329 c->m_time_elapsed = 0;
332 // relay the package ..
334 c->send_GDU(gdu, &len);
336 switch(gdu->u.z3950->which)
338 case Z_APDU_triggerResourceControlRequest:
342 // for the rest: wait for a response PDU
343 while (!c->m_destroyed && c->m_waiting
344 && c->m_socket_manager->processEvent() > 0)
350 void yf::Z3950Client::Rep::release_assoc(Package &package)
352 boost::mutex::scoped_lock lock(m_mutex);
353 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
355 it = m_clients.find(package.session());
356 if (it != m_clients.end())
358 Z_GDU *gdu = package.request().get();
359 if (gdu && gdu->which == Z_GDU_Z3950)
360 { // only Z39.50 packages lock in get_assoc.. release it
361 it->second->m_in_use = false;
362 it->second->m_queue_len--;
365 if (package.session().is_closed())
367 // destroy hint (send_and_receive)
368 it->second->m_destroyed = true;
370 // wait until no one is waiting for it.
371 while (it->second->m_queue_len)
372 m_cond_session_ready.wait(lock);
374 // the Z_Assoc and PDU_Assoc must be destroyed before
375 // the socket manager.. so pull that out.. first..
376 yazpp_1::SocketManager *s = it->second->m_socket_manager;
377 delete it->second; // destroy Z_Assoc
378 delete s; // then manager
381 m_cond_session_ready.notify_all();
385 void yf::Z3950Client::process(Package &package) const
387 yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
390 m_p->send_and_receive(package, c);
392 m_p->release_assoc(package);
395 void yf::Z3950Client::configure(const xmlNode *ptr, bool test_only)
397 for (ptr = ptr->children; ptr; ptr = ptr->next)
399 if (ptr->type != XML_ELEMENT_NODE)
401 if (!strcmp((const char *) ptr->name, "timeout"))
403 m_p->m_timeout_sec = mp::xml::get_int(ptr->children, 30);
405 else if (!strcmp((const char *) ptr->name, "default_target"))
407 m_p->m_default_target = mp::xml::get_text(ptr);
409 else if (!strcmp((const char *) ptr->name, "force_target"))
411 m_p->m_force_target = mp::xml::get_text(ptr);
415 throw mp::filter::FilterException("Bad element "
416 + std::string((const char *)
422 static mp::filter::Base* filter_creator()
424 return new mp::filter::Z3950Client;
428 struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
438 * indent-tabs-mode: nil
439 * c-file-style: "stroustrup"
441 * vim: shiftwidth=4 tabstop=8 expandtab