Introduce filter method 'start'
[metaproxy-moved-to-github.git] / src / filter_session_shared.cpp
index cd6a237..f0f84f7 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Metaproxy.
-   Copyright (C) 2005-2009 Index Data
+   Copyright (C) 2005-2010 Index Data
 
 Metaproxy is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -18,8 +18,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 #include "config.hpp"
 
-#include "filter.hpp"
-#include "package.hpp"
+#include <metaproxy/filter.hpp>
+#include <metaproxy/package.hpp>
 
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/condition.hpp>
@@ -28,7 +28,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <boost/shared_ptr.hpp>
 #include <boost/format.hpp>
 
-#include "util.hpp"
+#include <metaproxy/util.hpp>
 #include "filter_session_shared.hpp"
 
 #include <yaz/log.h>
@@ -179,6 +179,7 @@ namespace metaproxy_1 {
         private:
             void init(Package &package, const Z_GDU *gdu,
                       FrontendPtr frontend);
+            void start();
             boost::mutex m_mutex;
             boost::condition m_cond_session_ready;
             std::map<mp::Session, FrontendPtr> m_clients;
@@ -377,13 +378,17 @@ yf::SessionShared::BackendInstancePtr yf::SessionShared::BackendClass::create_ba
 
     m_named_result_sets = false;
     Z_GDU *gdu = init_package.response().get();
-    if (!init_package.session().is_closed()
-        && gdu && gdu->which == Z_GDU_Z3950 
-        && gdu->u.z3950->which == Z_APDU_initResponse)
+    if (init_package.session().is_closed())
     {
+        /* already closed. We don't know why */
+        return null;
+    }
+    else if (gdu && gdu->which == Z_GDU_Z3950 
+             && gdu->u.z3950->which == Z_APDU_initResponse
+             && *gdu->u.z3950->u.initResponse->result)
+    {
+        /* successful init response */
         Z_InitResponse *res = gdu->u.z3950->u.initResponse;
-        if (!*res->result)
-            return null;
         m_init_response = gdu->u.z3950;
         if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
         {
@@ -392,7 +397,10 @@ yf::SessionShared::BackendInstancePtr yf::SessionShared::BackendClass::create_ba
     }
     else
     {
-        // did not receive an init response or closed
+        /* not init or init rejected */
+        init_package.copy_filter(frontend_package);
+        init_package.session().close();
+        init_package.move();
         return null;
     }
     bp->m_in_use = true;
@@ -610,7 +618,6 @@ restart:
     BackendClassPtr bc = m_backend_class;
     {
         boost::mutex::scoped_lock lock(bc->m_mutex_backend_class);
-     
         // look at each backend and see if we have a similar search
         BackendInstanceList::const_iterator it = bc->m_backend_list.begin();
         
@@ -846,7 +853,8 @@ void yf::SessionShared::Frontend::present(mp::Package &package,
         Z_APDU *f_apdu_res = odr.create_presentResponse(apdu_req, 0, 0);
         Z_PresentResponse *f_resp = f_apdu_res->u.presentResponse;
 
-        yaz_log(YLOG_LOG, "Found %d+%d records in cache %p",
+        yaz_log(YLOG_LOG, "Found " ODR_INT_PRINTF "+" ODR_INT_PRINTF 
+                " records in cache %p",
                 *req->resultSetStartPoint,                      
                 *req->numberOfRecordsRequested,
                 &found_set->m_record_cache);        
@@ -897,7 +905,8 @@ void yf::SessionShared::Frontend::present(mp::Package &package,
 
         if (b_resp->records && b_resp->records->which ==  Z_Records_DBOSD)
         {
-            yaz_log(YLOG_LOG, "Adding %d+%d records to cache %p",
+            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);        
@@ -1009,6 +1018,10 @@ yf::SessionShared::Rep::Rep()
     m_resultset_ttl = 30;
     m_resultset_max = 10;
     m_session_ttl = 90;
+}
+
+void yf::SessionShared::Rep::start()
+{
     yf::SessionShared::Worker w(this);
     m_thrds.add_thread(new boost::thread(w));
 }
@@ -1020,6 +1033,10 @@ 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)
 {