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
20 #include "filter_zoom.hpp"
22 #include <metaproxy/package.hpp>
23 #include <metaproxy/util.hpp>
25 #include <boost/thread/mutex.hpp>
26 #include <boost/thread/condition.hpp>
30 namespace mp = metaproxy_1;
31 namespace yf = mp::filter;
33 namespace metaproxy_1 {
38 ZOOM_connection m_connection;
39 ZOOM_resultset m_resultset;
41 class Zoom::Frontend {
46 yazpp_1::GDU m_init_gdu;
55 void process(metaproxy_1::Package & package);
56 void configure(const xmlNode * ptr);
58 FrontendPtr get_frontend(mp::Package &package);
59 void release_frontend(mp::Package &package);
61 std::map<mp::Session, FrontendPtr> m_clients;
63 boost::condition m_cond_session_ready;
68 // define Pimpl wrapper forwarding to Impl
70 yf::Zoom::Zoom() : m_p(new Impl)
75 { // must have a destructor because of boost::scoped_ptr
78 void yf::Zoom::configure(const xmlNode *xmlnode, bool test_only)
80 m_p->configure(xmlnode);
83 void yf::Zoom::process(mp::Package &package) const
85 m_p->process(package);
89 // define Implementation stuff
91 yf::Zoom::Frontend::Frontend(Impl *impl) :
92 m_p(impl), m_is_virtual(false), m_in_use(true)
96 yf::Zoom::Frontend::~Frontend()
100 yf::Zoom::FrontendPtr yf::Zoom::Impl::get_frontend(mp::Package &package)
102 boost::mutex::scoped_lock lock(m_mutex);
104 std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
108 it = m_clients.find(package.session());
109 if (it == m_clients.end())
112 if (!it->second->m_in_use)
114 it->second->m_in_use = true;
117 m_cond_session_ready.wait(lock);
119 FrontendPtr f(new Frontend(this));
120 m_clients[package.session()] = f;
125 void yf::Zoom::Impl::release_frontend(mp::Package &package)
127 boost::mutex::scoped_lock lock(m_mutex);
128 std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
130 it = m_clients.find(package.session());
131 if (it != m_clients.end())
133 if (package.session().is_closed())
139 it->second->m_in_use = false;
141 m_cond_session_ready.notify_all();
145 yf::Zoom::Impl::Impl()
149 yf::Zoom::Impl::~Impl()
153 void yf::Zoom::Impl::configure(const xmlNode *xmlnode)
157 void yf::Zoom::Impl::process(mp::Package &package)
159 FrontendPtr f = get_frontend(package);
160 Z_GDU *gdu = package.request().get();
164 if (gdu->which == Z_GDU_Z3950)
166 Z_APDU *apdu = gdu->u.z3950;
169 package.response() = odr.create_close(
171 Z_Close_protocolError,
174 package.session().close();
176 else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
179 Z_InitRequest *req = gdu->u.z3950->u.initRequest;
183 Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
184 Z_InitResponse *resp = apdu->u.initResponse;
187 static const int masks[] = {
192 for (i = 0; masks[i] != -1; i++)
193 if (ODR_MASK_GET(req->options, masks[i]))
194 ODR_MASK_SET(resp->options, masks[i]);
196 static const int versions[] = {
202 for (i = 0; versions[i] != -1; i++)
203 if (ODR_MASK_GET(req->protocolVersion, versions[i]))
204 ODR_MASK_SET(resp->protocolVersion, versions[i]);
208 *resp->preferredMessageSize = *req->preferredMessageSize;
209 *resp->maximumRecordSize = *req->maximumRecordSize;
211 package.response() = apdu;
212 f->m_is_virtual = true;
217 release_frontend(package);
221 static mp::filter::Base* filter_creator()
223 return new mp::filter::Zoom;
227 struct metaproxy_1_filter_struct metaproxy_1_filter_zoom = {
238 * c-file-style: "Stroustrup"
239 * indent-tabs-mode: nil
241 * vim: shiftwidth=4 tabstop=8 expandtab