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 <boost/thread/mutex.hpp>
25 #include <boost/thread/condition.hpp>
26 #include <boost/thread/thread.hpp>
27 #include <boost/thread/xtime.hpp>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/format.hpp>
32 #include "filter_session_shared.hpp"
36 #include <yaz/otherinfo.h>
37 #include <yaz/diagbib1.h>
38 #include <yazpp/z-query.h>
43 namespace mp = metaproxy_1;
44 namespace yf = metaproxy_1::filter;
46 namespace metaproxy_1 {
49 // key for session.. We'll only share sessions with same InitKey
50 class SessionShared::InitKey {
52 bool operator < (const SessionShared::InitKey &k) const;
53 InitKey(Z_InitRequest *req);
54 InitKey(const InitKey &);
57 char *m_idAuthentication_buf;
58 int m_idAuthentication_size;
59 char *m_otherInfo_buf;
63 // worker thread .. for expiry of sessions
64 class SessionShared::Worker {
66 Worker(SessionShared::Rep *rep);
67 void operator() (void);
69 SessionShared::Rep *m_p;
72 class SessionShared::BackendSet {
74 std::string m_result_set_id;
75 Databases m_databases;
76 int m_result_set_size;
77 yazpp_1::Yaz_Z_Query m_query;
78 time_t m_time_last_use;
81 const std::string &result_set_id,
82 const Databases &databases,
83 const yazpp_1::Yaz_Z_Query &query);
85 Package &frontend_package,
86 const Z_APDU *apdu_req,
87 const BackendInstancePtr bp);
89 // backend connection instance
90 class SessionShared::BackendInstance {
92 friend class BackendClass;
93 friend class BackendSet;
95 mp::Session m_session;
96 BackendSetList m_sets;
99 int m_result_set_sequence;
100 time_t m_time_last_use;
101 mp::Package * m_close_package;
104 // backends of some class (all with same InitKey)
105 class SessionShared::BackendClass : boost::noncopyable {
107 friend struct Frontend;
108 bool m_named_result_sets;
109 BackendInstanceList m_backend_list;
110 BackendInstancePtr create_backend(const Package &package);
111 void remove_backend(BackendInstancePtr b);
112 BackendInstancePtr get_backend(const Package &package);
113 void use_backend(BackendInstancePtr b);
114 void release_backend(BackendInstancePtr b);
116 yazpp_1::GDU m_init_request;
117 yazpp_1::GDU m_init_response;
118 boost::mutex m_mutex_backend_class;
120 time_t m_backend_set_ttl;
121 time_t m_backend_expiry_ttl;
122 size_t m_backend_set_max;
124 BackendClass(const yazpp_1::GDU &init_request,
130 // frontend result set
131 class SessionShared::FrontendSet {
132 Databases m_databases;
133 yazpp_1::Yaz_Z_Query m_query;
135 const Databases &get_databases();
136 const yazpp_1::Yaz_Z_Query &get_query();
138 const Databases &databases,
139 const yazpp_1::Yaz_Z_Query &query);
143 struct SessionShared::Frontend {
148 Z_Options m_init_options;
149 void search(Package &package, Z_APDU *apdu);
150 void present(Package &package, Z_APDU *apdu);
151 void scan(Package &package, Z_APDU *apdu);
153 void get_set(mp::Package &package,
154 const Z_APDU *apdu_req,
155 const Databases &databases,
156 yazpp_1::Yaz_Z_Query &query,
157 BackendInstancePtr &found_backend,
158 BackendSetPtr &found_set);
159 void override_set(BackendInstancePtr &found_backend,
160 std::string &result_set_id);
163 BackendClassPtr m_backend_class;
164 FrontendSets m_frontend_sets;
167 class SessionShared::Rep {
168 friend class SessionShared;
169 friend struct Frontend;
171 FrontendPtr get_frontend(Package &package);
172 void release_frontend(Package &package);
177 void init(Package &package, const Z_GDU *gdu,
178 FrontendPtr frontend);
179 boost::mutex m_mutex;
180 boost::condition m_cond_session_ready;
181 std::map<mp::Session, FrontendPtr> m_clients;
183 BackendClassMap m_backend_map;
184 boost::mutex m_mutex_backend_map;
185 boost::thread_group m_thrds;
193 yf::SessionShared::FrontendSet::FrontendSet(
194 const Databases &databases,
195 const yazpp_1::Yaz_Z_Query &query)
196 : m_databases(databases), m_query(query)
200 const yf::SessionShared::Databases &
201 yf::SessionShared::FrontendSet::get_databases()
206 const yazpp_1::Yaz_Z_Query& yf::SessionShared::FrontendSet::get_query()
211 yf::SessionShared::InitKey::InitKey(const InitKey &k)
213 m_odr = odr_createmem(ODR_ENCODE);
215 m_idAuthentication_size = k.m_idAuthentication_size;
216 m_idAuthentication_buf = (char*)odr_malloc(m_odr, m_idAuthentication_size);
217 memcpy(m_idAuthentication_buf, k.m_idAuthentication_buf,
218 m_idAuthentication_size);
220 m_otherInfo_size = k.m_otherInfo_size;
221 m_otherInfo_buf = (char*)odr_malloc(m_odr, m_otherInfo_size);
222 memcpy(m_otherInfo_buf, k.m_otherInfo_buf,
226 yf::SessionShared::InitKey::InitKey(Z_InitRequest *req)
228 m_odr = odr_createmem(ODR_ENCODE);
230 Z_IdAuthentication *t = req->idAuthentication;
231 z_IdAuthentication(m_odr, &t, 1, 0);
232 m_idAuthentication_buf =
233 odr_getbuf(m_odr, &m_idAuthentication_size, 0);
235 Z_OtherInformation *o = req->otherInfo;
236 z_OtherInformation(m_odr, &o, 1, 0);
237 m_otherInfo_buf = odr_getbuf(m_odr, &m_otherInfo_size, 0);
240 yf::SessionShared::InitKey::~InitKey()
245 bool yf::SessionShared::InitKey::operator < (const SessionShared::InitKey &k)
249 c = mp::util::memcmp2(
250 (void*) m_idAuthentication_buf, m_idAuthentication_size,
251 (void*) k.m_idAuthentication_buf, k.m_idAuthentication_size);
257 c = mp::util::memcmp2((void*) m_otherInfo_buf, m_otherInfo_size,
258 (void*) k.m_otherInfo_buf, k.m_otherInfo_size);
266 void yf::SessionShared::BackendClass::release_backend(BackendInstancePtr b)
268 boost::mutex::scoped_lock lock(m_mutex_backend_class);
273 void yf::SessionShared::BackendClass::remove_backend(BackendInstancePtr b)
275 BackendInstanceList::iterator it = m_backend_list.begin();
277 while (it != m_backend_list.end())
280 it = m_backend_list.erase(it);
288 yf::SessionShared::BackendInstancePtr
289 yf::SessionShared::BackendClass::get_backend(
290 const mp::Package &frontend_package)
293 boost::mutex::scoped_lock lock(m_mutex_backend_class);
295 BackendInstanceList::const_iterator it = m_backend_list.begin();
297 BackendInstancePtr backend1; // null
299 for (; it != m_backend_list.end(); it++)
301 if (!(*it)->m_in_use)
304 || (*it)->m_sequence_this < backend1->m_sequence_this)
310 use_backend(backend1);
314 return create_backend(frontend_package);
317 void yf::SessionShared::BackendClass::use_backend(BackendInstancePtr backend)
319 backend->m_in_use = true;
320 time(&backend->m_time_last_use);
321 backend->m_sequence_this = m_sequence_top++;
324 yf::SessionShared::BackendInstance::~BackendInstance()
326 delete m_close_package;
329 yf::SessionShared::BackendInstancePtr yf::SessionShared::BackendClass::create_backend(
330 const mp::Package &frontend_package)
332 BackendInstancePtr bp(new BackendInstance);
333 BackendInstancePtr null;
335 bp->m_close_package =
336 new mp::Package(bp->m_session, frontend_package.origin());
337 bp->m_close_package->copy_filter(frontend_package);
339 Package init_package(bp->m_session, frontend_package.origin());
341 init_package.copy_filter(frontend_package);
343 yazpp_1::GDU actual_init_request = m_init_request;
344 Z_GDU *init_pdu = actual_init_request.get();
346 assert(init_pdu->which == Z_GDU_Z3950);
347 assert(init_pdu->u.z3950->which == Z_APDU_initRequest);
349 Z_InitRequest *req = init_pdu->u.z3950->u.initRequest;
350 ODR_MASK_ZERO(req->options);
352 ODR_MASK_SET(req->options, Z_Options_search);
353 ODR_MASK_SET(req->options, Z_Options_present);
354 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
355 ODR_MASK_SET(req->options, Z_Options_scan);
357 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
358 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
359 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
361 init_package.request() = init_pdu;
365 boost::mutex::scoped_lock lock(m_mutex_backend_class);
367 m_named_result_sets = false;
368 Z_GDU *gdu = init_package.response().get();
369 if (!init_package.session().is_closed()
370 && gdu && gdu->which == Z_GDU_Z3950
371 && gdu->u.z3950->which == Z_APDU_initResponse)
373 Z_InitResponse *res = gdu->u.z3950->u.initResponse;
376 m_init_response = gdu->u.z3950;
377 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
379 m_named_result_sets = true;
384 // did not receive an init response or closed
388 time(&bp->m_time_last_use);
389 bp->m_sequence_this = 0;
390 bp->m_result_set_sequence = 0;
391 m_backend_list.push_back(bp);
397 yf::SessionShared::BackendClass::BackendClass(const yazpp_1::GDU &init_request,
401 : m_named_result_sets(false), m_init_request(init_request),
402 m_sequence_top(0), m_backend_set_ttl(resultset_ttl),
403 m_backend_expiry_ttl(session_ttl), m_backend_set_max(resultset_max)
406 yf::SessionShared::BackendClass::~BackendClass()
409 void yf::SessionShared::Rep::init(mp::Package &package, const Z_GDU *gdu,
410 FrontendPtr frontend)
412 Z_InitRequest *req = gdu->u.z3950->u.initRequest;
414 frontend->m_is_virtual = true;
415 frontend->m_init_options = *req->options;
418 boost::mutex::scoped_lock lock(m_mutex_backend_map);
419 BackendClassMap::const_iterator it;
420 it = m_backend_map.find(k);
421 if (it == m_backend_map.end())
423 BackendClassPtr b(new BackendClass(gdu->u.z3950,
427 m_backend_map[k] = b;
428 frontend->m_backend_class = b;
432 frontend->m_backend_class = it->second;
435 BackendClassPtr bc = frontend->m_backend_class;
436 BackendInstancePtr backend = bc->get_backend(package);
441 Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
442 *apdu->u.initResponse->result = 0;
443 package.response() = apdu;
444 package.session().close();
448 boost::mutex::scoped_lock lock(bc->m_mutex_backend_class);
449 yazpp_1::GDU init_response = bc->m_init_response;
450 Z_GDU *response_gdu = init_response.get();
451 mp::util::transfer_referenceId(odr, gdu->u.z3950,
452 response_gdu->u.z3950);
454 Z_Options *server_options =
455 response_gdu->u.z3950->u.initResponse->options;
456 Z_Options *client_options = &frontend->m_init_options;
459 for (i = 0; i<30; i++)
460 if (!ODR_MASK_GET(client_options, i))
461 ODR_MASK_CLEAR(server_options, i);
462 package.response() = init_response;
465 bc->release_backend(backend);
468 void yf::SessionShared::BackendSet::timestamp()
470 time(&m_time_last_use);
473 yf::SessionShared::BackendSet::BackendSet(
474 const std::string &result_set_id,
475 const Databases &databases,
476 const yazpp_1::Yaz_Z_Query &query) :
477 m_result_set_id(result_set_id),
478 m_databases(databases), m_result_set_size(0), m_query(query)
483 bool yf::SessionShared::BackendSet::search(
484 mp::Package &frontend_package,
485 const Z_APDU *frontend_apdu,
486 const BackendInstancePtr bp)
488 Package search_package(bp->m_session, frontend_package.origin());
490 search_package.copy_filter(frontend_package);
493 Z_APDU *apdu_req = zget_APDU(odr, Z_APDU_searchRequest);
494 Z_SearchRequest *req = apdu_req->u.searchRequest;
496 req->resultSetName = odr_strdup(odr, m_result_set_id.c_str());
497 req->query = m_query.get_Z_Query();
499 req->num_databaseNames = m_databases.size();
500 req->databaseNames = (char**)
501 odr_malloc(odr, req->num_databaseNames * sizeof(char *));
502 Databases::const_iterator it = m_databases.begin();
504 for (; it != m_databases.end(); it++)
505 req->databaseNames[i++] = odr_strdup(odr, it->c_str());
507 search_package.request() = apdu_req;
509 search_package.move();
511 Z_Records *z_records_diag = 0;
512 Z_GDU *gdu = search_package.response().get();
513 if (!search_package.session().is_closed()
514 && gdu && gdu->which == Z_GDU_Z3950
515 && gdu->u.z3950->which == Z_APDU_searchResponse)
517 Z_SearchResponse *b_resp = gdu->u.z3950->u.searchResponse;
520 if (b_resp->records->which == Z_Records_NSD
521 || b_resp->records->which == Z_Records_multipleNSD)
522 z_records_diag = b_resp->records;
526 if (frontend_apdu->which == Z_APDU_searchRequest)
528 Z_APDU *f_apdu = odr.create_searchResponse(frontend_apdu,
530 Z_SearchResponse *f_resp = f_apdu->u.searchResponse;
531 f_resp->records = z_records_diag;
532 frontend_package.response() = f_apdu;
535 if (frontend_apdu->which == Z_APDU_presentRequest)
537 Z_APDU *f_apdu = odr.create_presentResponse(frontend_apdu,
539 Z_PresentResponse *f_resp = f_apdu->u.presentResponse;
540 f_resp->records = z_records_diag;
541 frontend_package.response() = f_apdu;
545 m_result_set_size = *b_resp->resultCount;
549 if (frontend_apdu->which == Z_APDU_searchRequest)
550 f_apdu = odr.create_searchResponse(
551 frontend_apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, 0);
552 else if (frontend_apdu->which == Z_APDU_presentRequest)
553 f_apdu = odr.create_presentResponse(
554 frontend_apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, 0);
556 f_apdu = odr.create_close(
557 frontend_apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, 0);
558 frontend_package.response() = f_apdu;
562 void yf::SessionShared::Frontend::override_set(
563 BackendInstancePtr &found_backend,
564 std::string &result_set_id)
566 BackendClassPtr bc = m_backend_class;
567 BackendInstanceList::const_iterator it = bc->m_backend_list.begin();
571 for (; it != bc->m_backend_list.end(); it++)
573 if (!(*it)->m_in_use)
575 BackendSetList::iterator set_it = (*it)->m_sets.begin();
576 for (; set_it != (*it)->m_sets.end(); set_it++)
578 if (now >= (*set_it)->m_time_last_use &&
579 now - (*set_it)->m_time_last_use > bc->m_backend_set_ttl)
582 result_set_id = (*set_it)->m_result_set_id;
583 found_backend->m_sets.erase(set_it);
589 size_t max_sets = bc->m_named_result_sets ? bc->m_backend_set_max : 1;
590 for (it = bc->m_backend_list.begin(); it != bc->m_backend_list.end(); it++)
592 if (!(*it)->m_in_use && (*it)->m_sets.size() < max_sets)
595 if (bc->m_named_result_sets)
597 result_set_id = boost::io::str(
598 boost::format("%1%") %
599 found_backend->m_result_set_sequence);
600 found_backend->m_result_set_sequence++;
603 result_set_id = "default";
609 void yf::SessionShared::Frontend::get_set(mp::Package &package,
610 const Z_APDU *apdu_req,
611 const Databases &databases,
612 yazpp_1::Yaz_Z_Query &query,
613 BackendInstancePtr &found_backend,
614 BackendSetPtr &found_set)
616 std::string result_set_id;
617 BackendClassPtr bc = m_backend_class;
619 boost::mutex::scoped_lock lock(bc->m_mutex_backend_class);
621 // look at each backend and see if we have a similar search
622 BackendInstanceList::const_iterator it = bc->m_backend_list.begin();
624 for (; it != bc->m_backend_list.end(); it++)
626 if (!(*it)->m_in_use)
628 BackendSetList::const_iterator set_it = (*it)->m_sets.begin();
629 for (; set_it != (*it)->m_sets.end(); set_it++)
631 if ((*set_it)->m_databases == databases
632 && query.match(&(*set_it)->m_query))
636 bc->use_backend(found_backend);
637 found_set->timestamp();
638 // found matching set. No need to search again
644 override_set(found_backend, result_set_id);
646 bc->use_backend(found_backend);
650 // create a new backend set (and new set)
651 found_backend = bc->create_backend(package);
657 if (apdu_req->which == Z_APDU_searchRequest)
659 f_apdu = odr.create_searchResponse(
660 apdu_req, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, 0);
662 else if (apdu_req->which == Z_APDU_presentRequest)
664 f_apdu = odr.create_presentResponse(
665 apdu_req, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, 0);
669 f_apdu = odr.create_close(
670 apdu_req, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, 0);
672 package.response() = f_apdu;
675 if (bc->m_named_result_sets)
677 result_set_id = boost::io::str(
678 boost::format("%1%") % found_backend->m_result_set_sequence);
679 found_backend->m_result_set_sequence++;
682 result_set_id = "default";
684 // we must search ...
685 BackendSetPtr new_set(new BackendSet(result_set_id,
687 if (!new_set->search(package, apdu_req, found_backend))
689 bc->remove_backend(found_backend);
690 return; // search error
693 found_set->timestamp();
694 found_backend->m_sets.push_back(found_set);
697 void yf::SessionShared::Frontend::search(mp::Package &package,
700 Z_SearchRequest *req = apdu_req->u.searchRequest;
701 FrontendSets::iterator fset_it =
702 m_frontend_sets.find(req->resultSetName);
703 if (fset_it != m_frontend_sets.end())
705 // result set already exist
706 // if replace indicator is off: we return diagnostic if
707 // result set already exist.
708 if (*req->replaceIndicator == 0)
712 odr.create_searchResponse(
714 YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF,
716 package.response() = apdu;
720 m_frontend_sets.erase(fset_it);
723 yazpp_1::Yaz_Z_Query query;
724 query.set_Z_Query(req->query);
727 for (i = 0; i<req->num_databaseNames; i++)
728 databases.push_back(req->databaseNames[i]);
730 BackendSetPtr found_set; // null
731 BackendInstancePtr found_backend; // null
733 get_set(package, apdu_req, databases, query, found_backend, found_set);
738 Z_APDU *f_apdu = odr.create_searchResponse(apdu_req, 0, 0);
739 Z_SearchResponse *f_resp = f_apdu->u.searchResponse;
740 *f_resp->resultCount = found_set->m_result_set_size;
741 package.response() = f_apdu;
743 FrontendSetPtr fset(new FrontendSet(databases, query));
744 m_frontend_sets[req->resultSetName] = fset;
746 m_backend_class->release_backend(found_backend);
749 void yf::SessionShared::Frontend::present(mp::Package &package,
753 Z_PresentRequest *req = apdu_req->u.presentRequest;
755 FrontendSets::iterator fset_it =
756 m_frontend_sets.find(req->resultSetId);
758 if (fset_it == m_frontend_sets.end())
761 odr.create_presentResponse(
763 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
765 package.response() = apdu;
768 FrontendSetPtr fset = fset_it->second;
770 Databases databases = fset->get_databases();
771 yazpp_1::Yaz_Z_Query query = fset->get_query();
773 BackendClassPtr bc = m_backend_class;
774 BackendSetPtr found_set; // null
775 BackendInstancePtr found_backend;
777 get_set(package, apdu_req, databases, query, found_backend, found_set);
781 Z_APDU *p_apdu = zget_APDU(odr, Z_APDU_presentRequest);
782 Z_PresentRequest *p_req = p_apdu->u.presentRequest;
783 p_req->preferredRecordSyntax = req->preferredRecordSyntax;
784 p_req->resultSetId = odr_strdup(odr, found_set->m_result_set_id.c_str());
785 *p_req->resultSetStartPoint = *req->resultSetStartPoint;
786 *p_req->numberOfRecordsRequested = *req->numberOfRecordsRequested;
787 p_req->preferredRecordSyntax = req->preferredRecordSyntax;
788 p_req->recordComposition = req->recordComposition;
790 Package present_package(found_backend->m_session, package.origin());
791 present_package.copy_filter(package);
793 present_package.request() = p_apdu;
795 present_package.move();
797 Z_GDU *gdu = present_package.response().get();
798 if (!present_package.session().is_closed()
799 && gdu && gdu->which == Z_GDU_Z3950
800 && gdu->u.z3950->which == Z_APDU_presentResponse)
802 Z_PresentResponse *b_resp = gdu->u.z3950->u.presentResponse;
803 Z_APDU *f_apdu_res = odr.create_presentResponse(apdu_req, 0, 0);
804 Z_PresentResponse *f_resp = f_apdu_res->u.presentResponse;
806 f_resp->numberOfRecordsReturned = b_resp->numberOfRecordsReturned;
807 f_resp->nextResultSetPosition = b_resp->nextResultSetPosition;
808 f_resp->presentStatus= b_resp->presentStatus;
809 f_resp->records = b_resp->records;
810 f_resp->otherInfo = b_resp->otherInfo;
811 package.response() = f_apdu_res;
812 bc->release_backend(found_backend);
816 bc->remove_backend(found_backend);
818 odr.create_presentResponse(
819 apdu_req, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, 0);
820 package.response() = f_apdu_res;
824 void yf::SessionShared::Frontend::scan(mp::Package &frontend_package,
827 BackendClassPtr bc = m_backend_class;
828 BackendInstancePtr backend = bc->get_backend(frontend_package);
832 Z_APDU *apdu = odr.create_scanResponse(
833 apdu_req, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, 0);
834 frontend_package.response() = apdu;
838 Package scan_package(backend->m_session, frontend_package.origin());
839 scan_package.copy_filter(frontend_package);
840 scan_package.request() = apdu_req;
842 frontend_package.response() = scan_package.response();
843 if (scan_package.session().is_closed())
845 frontend_package.session().close();
846 bc->remove_backend(backend);
849 bc->release_backend(backend);
853 yf::SessionShared::Worker::Worker(SessionShared::Rep *rep) : m_p(rep)
857 void yf::SessionShared::Worker::operator() (void)
862 void yf::SessionShared::BackendClass::expire_class()
866 boost::mutex::scoped_lock lock(m_mutex_backend_class);
867 BackendInstanceList::iterator bit = m_backend_list.begin();
868 while (bit != m_backend_list.end())
870 time_t last_use = (*bit)->m_time_last_use;
872 if ((*bit)->m_in_use)
876 else if ((now >= last_use && now - last_use > m_backend_expiry_ttl)
880 (*bit)->m_close_package->response() = odr.create_close(
881 0, Z_Close_lackOfActivity, 0);
882 (*bit)->m_close_package->session().close();
883 (*bit)->m_close_package->move();
885 bit = m_backend_list.erase(bit);
894 void yf::SessionShared::Rep::expire()
899 boost::xtime_get(&xt, boost::TIME_UTC);
901 boost::thread::sleep(xt);
903 BackendClassMap::const_iterator b_it = m_backend_map.begin();
904 for (; b_it != m_backend_map.end(); b_it++)
905 b_it->second->expire_class();
909 yf::SessionShared::Rep::Rep()
911 m_resultset_ttl = 30;
912 m_resultset_max = 10;
914 yf::SessionShared::Worker w(this);
915 m_thrds.add_thread(new boost::thread(w));
918 yf::SessionShared::SessionShared() : m_p(new SessionShared::Rep)
922 yf::SessionShared::~SessionShared() {
926 yf::SessionShared::Frontend::Frontend(Rep *rep) : m_is_virtual(false), m_p(rep)
930 yf::SessionShared::Frontend::~Frontend()
934 yf::SessionShared::FrontendPtr yf::SessionShared::Rep::get_frontend(mp::Package &package)
936 boost::mutex::scoped_lock lock(m_mutex);
938 std::map<mp::Session,yf::SessionShared::FrontendPtr>::iterator it;
942 it = m_clients.find(package.session());
943 if (it == m_clients.end())
946 if (!it->second->m_in_use)
948 it->second->m_in_use = true;
951 m_cond_session_ready.wait(lock);
953 FrontendPtr f(new Frontend(this));
954 m_clients[package.session()] = f;
959 void yf::SessionShared::Rep::release_frontend(mp::Package &package)
961 boost::mutex::scoped_lock lock(m_mutex);
962 std::map<mp::Session,yf::SessionShared::FrontendPtr>::iterator it;
964 it = m_clients.find(package.session());
965 if (it != m_clients.end())
967 if (package.session().is_closed())
973 it->second->m_in_use = false;
975 m_cond_session_ready.notify_all();
980 void yf::SessionShared::process(mp::Package &package) const
982 FrontendPtr f = m_p->get_frontend(package);
984 Z_GDU *gdu = package.request().get();
986 if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
987 Z_APDU_initRequest && !f->m_is_virtual)
989 m_p->init(package, gdu, f);
991 else if (!f->m_is_virtual)
993 else if (gdu && gdu->which == Z_GDU_Z3950)
995 Z_APDU *apdu = gdu->u.z3950;
996 if (apdu->which == Z_APDU_initRequest)
1000 package.response() = odr.create_close(
1002 Z_Close_protocolError,
1005 package.session().close();
1007 else if (apdu->which == Z_APDU_close)
1011 package.response() = odr.create_close(
1013 Z_Close_peerAbort, "received close from client");
1014 package.session().close();
1016 else if (apdu->which == Z_APDU_searchRequest)
1018 f->search(package, apdu);
1020 else if (apdu->which == Z_APDU_presentRequest)
1022 f->present(package, apdu);
1024 else if (apdu->which == Z_APDU_scanRequest)
1026 f->scan(package, apdu);
1032 package.response() = odr.create_close(
1033 apdu, Z_Close_protocolError,
1034 "unsupported APDU in filter_session_shared");
1036 package.session().close();
1039 m_p->release_frontend(package);
1042 void yf::SessionShared::configure(const xmlNode *ptr, bool test_only)
1044 for (ptr = ptr->children; ptr; ptr = ptr->next)
1046 if (ptr->type != XML_ELEMENT_NODE)
1048 if (!strcmp((const char *) ptr->name, "resultset"))
1050 const struct _xmlAttr *attr;
1051 for (attr = ptr->properties; attr; attr = attr->next)
1053 if (!strcmp((const char *) attr->name, "ttl"))
1054 m_p->m_resultset_ttl =
1055 mp::xml::get_int(attr->children, 30);
1056 else if (!strcmp((const char *) attr->name, "max"))
1058 m_p->m_resultset_max =
1059 mp::xml::get_int(attr->children, 10);
1062 throw mp::filter::FilterException(
1063 "Bad attribute " + std::string((const char *)
1067 else if (!strcmp((const char *) ptr->name, "session"))
1069 const struct _xmlAttr *attr;
1070 for (attr = ptr->properties; attr; attr = attr->next)
1072 if (!strcmp((const char *) attr->name, "ttl"))
1073 m_p->m_session_ttl =
1074 mp::xml::get_int(attr->children, 120);
1076 throw mp::filter::FilterException(
1077 "Bad attribute " + std::string((const char *)
1083 throw mp::filter::FilterException("Bad element "
1084 + std::string((const char *)
1090 static mp::filter::Base* filter_creator()
1092 return new mp::filter::SessionShared;
1096 struct metaproxy_1_filter_struct metaproxy_1_filter_session_shared = {
1106 * indent-tabs-mode: nil
1107 * c-file-style: "stroustrup"
1109 * vim: shiftwidth=4 tabstop=8 expandtab