+ found_backend->timestamp();
+
+ Z_APDU *p_apdu = zget_APDU(odr, Z_APDU_presentRequest);
+ Z_PresentRequest *p_req = p_apdu->u.presentRequest;
+ p_req->preferredRecordSyntax = req->preferredRecordSyntax;
+ p_req->resultSetId = odr_strdup(odr, found_set->m_result_set_id.c_str());
+ *p_req->resultSetStartPoint = *req->resultSetStartPoint;
+ *p_req->numberOfRecordsRequested = *req->numberOfRecordsRequested;
+ p_req->preferredRecordSyntax = req->preferredRecordSyntax;
+ p_req->recordComposition = req->recordComposition;
+
+ Package present_package(found_backend->m_session, package.origin());
+ present_package.copy_filter(package);
+
+ present_package.request() = p_apdu;
+
+ present_package.move();
+
+ Z_GDU *gdu = present_package.response().get();
+ if (!present_package.session().is_closed()
+ && gdu && gdu->which == Z_GDU_Z3950
+ && gdu->u.z3950->which == Z_APDU_presentResponse)
+ {
+ Z_PresentResponse *b_resp = gdu->u.z3950->u.presentResponse;
+ Z_APDU *f_apdu_res = odr.create_presentResponse(apdu_req, 0, 0);
+ Z_PresentResponse *f_resp = f_apdu_res->u.presentResponse;
+
+ f_resp->numberOfRecordsReturned = b_resp->numberOfRecordsReturned;
+ f_resp->nextResultSetPosition = b_resp->nextResultSetPosition;
+ f_resp->presentStatus= b_resp->presentStatus;
+ f_resp->records = b_resp->records;
+ f_resp->otherInfo = b_resp->otherInfo;
+ package.response() = f_apdu_res;
+
+ if (b_resp->records && b_resp->records->which == Z_Records_DBOSD)
+ {
+ yaz_log(YLOG_LOG, "Adding " ODR_INT_PRINTF "+" ODR_INT_PRINTF
+ " records to cache %p",
+ *req->resultSetStartPoint,
+ *f_resp->numberOfRecordsReturned,
+ &found_set->m_record_cache);
+ found_set->m_record_cache.add(
+ odr,
+ b_resp->records->u.databaseOrSurDiagnostics,
+ *req->resultSetStartPoint,
+ *f_resp->numberOfRecordsReturned);
+ }
+ bc->release_backend(found_backend);
+ }
+ else
+ {
+ bc->remove_backend(found_backend);
+ Z_APDU *f_apdu_res =
+ odr.create_presentResponse(
+ apdu_req, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
+ "session_shared: target closed connection during present");
+ package.response() = f_apdu_res;
+ }
+}
+
+void yf::SessionShared::Frontend::scan(mp::Package &frontend_package,
+ Z_APDU *apdu_req)
+{
+ BackendClassPtr bc = m_backend_class;
+ BackendInstancePtr backend = bc->get_backend(frontend_package);
+ if (!backend)
+ {
+ mp::odr odr;
+ Z_APDU *apdu = odr.create_scanResponse(
+ apdu_req, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
+ "session_shared: could not create backend");
+ frontend_package.response() = apdu;
+ }
+ else
+ {
+ Package scan_package(backend->m_session, frontend_package.origin());
+ backend->timestamp();
+ scan_package.copy_filter(frontend_package);
+ scan_package.request() = apdu_req;
+ scan_package.move();
+ frontend_package.response() = scan_package.response();
+ if (scan_package.session().is_closed())
+ {
+ frontend_package.session().close();
+ bc->remove_backend(backend);
+ }
+ else
+ bc->release_backend(backend);
+ }
+}
+
+yf::SessionShared::Worker::Worker(SessionShared::Rep *rep) : m_p(rep)
+{
+}
+
+void yf::SessionShared::Worker::operator() (void)
+{
+ m_p->expire();
+}
+
+void yf::SessionShared::BackendClass::expire_class()
+{
+ time_t now;
+ time(&now);
+ boost::mutex::scoped_lock lock(m_mutex_backend_class);
+ BackendInstanceList::iterator bit = m_backend_list.begin();
+ while (bit != m_backend_list.end())
+ {
+ time_t last_use = (*bit)->m_time_last_use;
+
+ if ((*bit)->m_in_use)
+ {
+ bit++;
+ }
+ else if (now < last_use || now - last_use > m_backend_expiry_ttl)
+ {
+ mp::odr odr;
+ (*bit)->m_close_package->response() = odr.create_close(
+ 0, Z_Close_lackOfActivity, 0);
+ (*bit)->m_close_package->session().close();
+ (*bit)->m_close_package->move();
+
+ bit = m_backend_list.erase(bit);
+ }
+ else
+ {
+ bit++;
+ }
+ }
+}
+
+void yf::SessionShared::Rep::expire()
+{
+ while (true)
+ {
+ boost::xtime xt;
+ boost::xtime_get(&xt,
+#if BOOST_VERSION >= 105000
+ boost::TIME_UTC_
+#else
+ boost::TIME_UTC
+#endif
+ );
+ xt.sec += m_session_ttl / 3;
+ boost::thread::sleep(xt);
+
+ BackendClassMap::const_iterator b_it = m_backend_map.begin();
+ for (; b_it != m_backend_map.end(); b_it++)
+ b_it->second->expire_class();
+ }
+}
+
+yf::SessionShared::Rep::Rep()
+{
+ m_resultset_ttl = 30;
+ m_resultset_max = 10;
+ m_session_ttl = 90;
+ m_optimize_search = true;
+ m_restart = false;
+ m_session_max = 100;
+ m_preferredMessageSize = 0;
+ m_maximumRecordSize = 0;
+}
+
+void yf::SessionShared::Rep::start()
+{
+ yf::SessionShared::Worker w(this);
+ m_thrds.add_thread(new boost::thread(w));
+}
+
+yf::SessionShared::SessionShared() : m_p(new SessionShared::Rep)
+{
+}
+
+yf::SessionShared::~SessionShared() {
+}
+
+void yf::SessionShared::start() const
+{
+ m_p->start();
+}
+
+yf::SessionShared::Frontend::Frontend(Rep *rep) : m_is_virtual(false), m_p(rep)
+{
+}
+
+yf::SessionShared::Frontend::~Frontend()
+{
+}
+
+yf::SessionShared::FrontendPtr yf::SessionShared::Rep::get_frontend(mp::Package &package)
+{
+ boost::mutex::scoped_lock lock(m_mutex);
+
+ std::map<mp::Session,yf::SessionShared::FrontendPtr>::iterator it;
+
+ while(true)
+ {
+ it = m_clients.find(package.session());
+ if (it == m_clients.end())
+ break;
+
+ if (!it->second->m_in_use)
+ {
+ it->second->m_in_use = true;
+ return it->second;
+ }
+ m_cond_session_ready.wait(lock);
+ }
+ FrontendPtr f(new Frontend(this));
+ m_clients[package.session()] = f;
+ f->m_in_use = true;
+ return f;
+}
+
+void yf::SessionShared::Rep::release_frontend(mp::Package &package)
+{
+ boost::mutex::scoped_lock lock(m_mutex);
+ std::map<mp::Session,yf::SessionShared::FrontendPtr>::iterator it;
+
+ it = m_clients.find(package.session());
+ if (it != m_clients.end())
+ {
+ if (package.session().is_closed())
+ {
+ m_clients.erase(it);
+ }
+ else
+ {
+ it->second->m_in_use = false;
+ }
+ m_cond_session_ready.notify_all();
+ }
+}
+
+
+void yf::SessionShared::process(mp::Package &package) const
+{
+ FrontendPtr f = m_p->get_frontend(package);
+