1 /* $Id: filter_session_shared.cpp,v 1.11 2006-06-10 14:29:12 adam Exp $
2 Copyright (c) 2005-2006, Index Data.
4 See the LICENSE file for details
10 #include "package.hpp"
12 #include <boost/thread/mutex.hpp>
13 #include <boost/thread/condition.hpp>
14 #include <boost/shared_ptr.hpp>
17 #include "filter_session_shared.hpp"
21 #include <yaz/otherinfo.h>
22 #include <yaz/diagbib1.h>
27 namespace mp = metaproxy_1;
28 namespace yf = metaproxy_1::filter;
30 namespace metaproxy_1 {
34 class SessionShared::InitKey {
36 bool operator < (const SessionShared::InitKey &k) const;
37 InitKey(Z_InitRequest *req);
39 char *m_idAuthentication_buf;
40 int m_idAuthentication_size;
41 char *m_otherInfo_buf;
45 std::list<std::string> m_targets;
47 class SessionShared::BackendClass {
48 yazpp_1::GDU m_init_response;
50 struct SessionShared::Frontend {
51 void init(Package &package, Z_GDU *gdu);
54 mp::Session m_session;
58 void close(Package &package);
61 class SessionShared::Rep {
62 friend class SessionShared;
63 friend struct Frontend;
65 FrontendPtr get_frontend(Package &package);
66 void release_frontend(Package &package);
69 boost::condition m_cond_session_ready;
70 std::map<mp::Session, FrontendPtr> m_clients;
72 typedef std::map<InitKey,BackendClass> BackendClassMap;
73 BackendClassMap m_backend_map;
79 yf::SessionShared::InitKey::InitKey(Z_InitRequest *req)
81 Z_IdAuthentication *t = req->idAuthentication;
83 z_IdAuthentication(m_odr, &t, 1, 0);
84 m_idAuthentication_buf =
85 odr_getbuf(m_odr, &m_idAuthentication_size, 0);
87 Z_OtherInformation *o = req->otherInfo;
88 z_OtherInformation(m_odr, &o, 1, 0);
89 m_otherInfo_buf = odr_getbuf(m_odr, &m_otherInfo_size, 0);
92 bool yf::SessionShared::InitKey::operator < (const SessionShared::InitKey &k)
96 c = mp::util::memcmp2(
97 (void*) m_idAuthentication_buf, m_idAuthentication_size,
98 (void*) k.m_idAuthentication_buf, k.m_idAuthentication_size);
104 c = mp::util::memcmp2((void*) m_otherInfo_buf, m_otherInfo_size,
105 (void*) k.m_otherInfo_buf, k.m_otherInfo_size);
113 void yf::SessionShared::Frontend::init(mp::Package &package, Z_GDU *gdu)
115 Z_InitRequest *req = gdu->u.z3950->u.initRequest;
117 std::list<std::string> targets;
119 mp::util::get_vhost_otherinfo(&req->otherInfo, false, targets);
121 // std::cout << "SessionShared::Frontend::init\n";
122 if (targets.size() < 1)
124 // no targets given, just relay this one and don't deal with it
131 yf::SessionShared::SessionShared() : m_p(new SessionShared::Rep)
135 yf::SessionShared::~SessionShared() {
139 yf::SessionShared::Frontend::Frontend(Rep *rep) : m_is_virtual(false), m_p(rep)
143 void yf::SessionShared::Frontend::close(mp::Package &package)
146 std::list<BackendPtr>::const_iterator b_it;
148 for (b_it = m_backend_list.begin(); b_it != m_backend_list.end(); b_it++)
150 (*b_it)->m_backend_session.close();
151 Package close_package((*b_it)->m_backend_session, package.origin());
152 close_package.copy_filter(package);
153 close_package.move((*b_it)->m_route);
155 m_backend_list.clear();
160 yf::SessionShared::Frontend::~Frontend()
164 yf::SessionShared::FrontendPtr yf::SessionShared::Rep::get_frontend(mp::Package &package)
166 boost::mutex::scoped_lock lock(m_mutex);
168 std::map<mp::Session,yf::SessionShared::FrontendPtr>::iterator it;
172 it = m_clients.find(package.session());
173 if (it == m_clients.end())
176 if (!it->second->m_in_use)
178 it->second->m_in_use = true;
181 m_cond_session_ready.wait(lock);
183 FrontendPtr f(new Frontend(this));
184 m_clients[package.session()] = f;
189 void yf::SessionShared::Rep::release_frontend(mp::Package &package)
191 boost::mutex::scoped_lock lock(m_mutex);
192 std::map<mp::Session,yf::SessionShared::FrontendPtr>::iterator it;
194 it = m_clients.find(package.session());
195 if (it != m_clients.end())
197 if (package.session().is_closed())
199 it->second->close(package);
204 it->second->m_in_use = false;
206 m_cond_session_ready.notify_all();
211 void yf::SessionShared::process(mp::Package &package) const
213 FrontendPtr f = m_p->get_frontend(package);
215 Z_GDU *gdu = package.request().get();
217 if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
218 Z_APDU_initRequest && !f->m_is_virtual)
220 f->init(package, gdu);
222 else if (!f->m_is_virtual)
224 else if (gdu && gdu->which == Z_GDU_Z3950)
226 Z_APDU *apdu = gdu->u.z3950;
227 if (apdu->which == Z_APDU_initRequest)
231 package.response() = odr.create_close(
233 Z_Close_protocolError,
236 package.session().close();
242 package.response() = odr.create_close(
243 apdu, Z_Close_protocolError,
244 "unsupported APDU in filter_session_shared");
246 package.session().close();
249 m_p->release_frontend(package);
252 static mp::filter::Base* filter_creator()
254 return new mp::filter::SessionShared;
258 struct metaproxy_1_filter_struct metaproxy_1_filter_session_shared = {
268 * indent-tabs-mode: nil
269 * c-file-style: "stroustrup"
271 * vim: shiftwidth=4 tabstop=8 expandtab