1 /* $Id: filter_session_shared.cpp,v 1.1 2005-11-14 23:35:22 adam Exp $
2 Copyright (c) 2005, Index Data.
11 #include "package.hpp"
13 #include <boost/thread/mutex.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 yf = yp2::filter;
31 class Session_shared::Rep {
32 friend class Session_shared;
33 void handle_init(Z_InitRequest *req, Package &package);
34 void handle_search(Z_SearchRequest *req, Package &package);
36 typedef boost::shared_ptr<Session_shared::List> SharedList;
38 typedef std::map<Session_shared::InitKey,SharedList> InitListMap;
39 InitListMap m_init_list_map;
41 typedef std::map<Session,SharedList> SessionListMap;
42 SessionListMap m_session_list_map;
45 class Session_shared::InitKey {
46 friend class Session_shared;
47 friend class Session_shared::Rep;
52 std::string m_password;
54 bool operator < (const Session_shared::InitKey &k) const;
56 class Session_shared::List {
58 yazpp_1::GDU m_init_response; // init response for backend
59 Session m_session; // session for backend
68 bool yf::Session_shared::InitKey::operator < (const Session_shared::InitKey &k) const {
69 if (m_vhost < k.m_vhost)
71 else if (m_vhost < k.m_vhost)
74 if (m_open < k.m_open)
76 else if (m_open > k.m_open)
79 if (m_user < k.m_user)
81 else if (m_user > k.m_user)
84 if (m_group < k.m_group)
86 else if (m_group > k.m_group)
89 if (m_password < k.m_password)
91 else if (m_password > k.m_password)
96 yf::Session_shared::Session_shared() : m_p(new Rep)
100 yf::Session_shared::~Session_shared()
104 void yf::Session_shared::Rep::handle_search(Z_SearchRequest *req,
107 yaz_log(YLOG_LOG, "Got search");
109 SessionListMap::iterator it = m_session_list_map.find(package.session());
110 if (it == m_session_list_map.end())
113 package.response() = odr.create_close(
114 Z_Close_protocolError,
115 "no session for search request in session_shared");
116 package.session().close();
120 Package search_package(it->second->m_session, package.origin());
121 search_package.copy_filter(package);
122 search_package.request() = package.request();
124 search_package.move();
126 // transfer to frontend
127 package.response() = search_package.response();
130 void yf::Session_shared::Rep::handle_init(Z_InitRequest *req, Package &package)
132 yaz_log(YLOG_LOG, "Got init");
134 Session_shared::InitKey key;
136 yaz_oi_get_string_oidval(&req->otherInfo, VAL_PROXY, 1, 0);
140 if (!req->idAuthentication)
142 yaz_log(YLOG_LOG, "No authentication");
146 Z_IdAuthentication *auth = req->idAuthentication;
149 case Z_IdAuthentication_open:
150 yaz_log(YLOG_LOG, "open auth open=%s", auth->u.open);
151 key.m_open = auth->u.open;
153 case Z_IdAuthentication_idPass:
154 yaz_log(YLOG_LOG, "idPass user=%s group=%s pass=%s",
155 auth->u.idPass->userId, auth->u.idPass->groupId,
156 auth->u.idPass->password);
157 if (auth->u.idPass->userId)
158 key.m_user = auth->u.idPass->userId;
159 if (auth->u.idPass->groupId)
160 key.m_group = auth->u.idPass->groupId;
161 if (auth->u.idPass->password)
162 key.m_password = auth->u.idPass->password;
164 case Z_IdAuthentication_anonymous:
165 yaz_log(YLOG_LOG, "anonymous");
168 yaz_log(YLOG_LOG, "other");
171 InitListMap::iterator it = m_init_list_map.find(key);
172 if (it == m_init_list_map.end())
174 yaz_log(YLOG_LOG, "New KEY");
176 // building new package with original init and new session
177 SharedList l(new Session_shared::List); // new session for backend
179 Package init_package(l->m_session, package.origin());
180 init_package.copy_filter(package);
181 init_package.request() = package.request();
185 // transfer to frontend
186 package.response() = init_package.response();
188 // check that we really got Z39.50 Init Response
189 Z_GDU *gdu = init_package.response().get();
190 if (gdu && gdu->which == Z_GDU_Z3950
191 && gdu->u.z3950->which == Z_APDU_initResponse)
193 // save the init response
194 l->m_init_response = init_package.response();
196 // save session and init response for later
197 m_init_list_map[key] = l;
199 m_session_list_map[package.session()] = l;
204 yaz_log(YLOG_LOG, "Existing KEY");
205 package.response() = it->second->m_init_response;
207 m_session_list_map[package.session()] = it->second;
211 void yf::Session_shared::process(Package &package) const
213 // don't tell the backend if the "fronent" filter closes..
214 // we want to keep them alive
215 if (package.session().is_closed())
217 m_p->m_session_list_map.erase(package.session());
221 Z_GDU *gdu = package.request().get();
223 if (gdu && gdu->which == Z_GDU_Z3950)
225 Z_APDU *apdu = gdu->u.z3950;
229 case Z_APDU_initRequest:
230 m_p->handle_init(apdu->u.initRequest, package);
232 case Z_APDU_searchRequest:
233 m_p->handle_search(apdu->u.searchRequest, package);
237 package.response() = odr.create_close(
238 Z_Close_protocolError,
239 "cannot handle a package of this type");
240 package.session().close();
244 if (package.session().is_closed()) {
245 m_p->m_session_list_map.erase(package.session());
249 package.move(); // Not Z39.50 or not Init
256 * indent-tabs-mode: nil
257 * c-file-style: "stroustrup"
259 * vim: shiftwidth=4 tabstop=8 expandtab