Put proper reference IDs in response PDUs properly.
authorAdam Dickmeiss <adam@indexdata.dk>
Fri, 13 Jan 2006 15:09:35 +0000 (15:09 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Fri, 13 Jan 2006 15:09:35 +0000 (15:09 +0000)
Re-use session in filter virt_db when that is possible.
Filter virt_db offers named result sets - even for backends
that don't support it.

src/ex_filter_frontend_net.cpp
src/filter_auth_simple.cpp
src/filter_backend_test.cpp
src/filter_session_shared.cpp
src/filter_virt_db.cpp
src/filter_virt_db.hpp
src/filter_z3950_client.cpp
src/util.cpp
src/util.hpp

index 9634e92..18df43d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: ex_filter_frontend_net.cpp,v 1.23 2006-01-12 14:09:08 adam Exp $
+/* $Id: ex_filter_frontend_net.cpp,v 1.24 2006-01-13 15:09:35 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -108,7 +108,7 @@ int main(int argc, char **argv)
                                             "");
             filter_virt_db.add_map_db2vhost("Default", "localhost:9999/Default",
                                             "");
-            filter_virt_db.add_map_db2vhost("2", "localhost:9999/2", "");
+            filter_virt_db.add_map_db2vhost("2", "localhost:9999/Slow", "");
            router.append(filter_virt_db);
 
             yp2::filter::SessionShared filter_session_shared;
index d5a4c82..b211e75 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_auth_simple.cpp,v 1.1 2006-01-12 10:04:34 adam Exp $
+/* $Id: filter_auth_simple.cpp,v 1.2 2006-01-13 15:09:35 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -50,7 +50,7 @@ void yf::AuthSimple::process(yp2::Package &package) const
         if (!init->idAuthentication)
         {
             yp2::odr odr;
-            Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
+            Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
             
             apdu->u.initResponse->implementationName = "YP2/YAZ";
             *apdu->u.initResponse->result = 0; // reject
index f0a6732..04ae7f2 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_backend_test.cpp,v 1.13 2006-01-09 21:20:15 adam Exp $
+/* $Id: filter_backend_test.cpp,v 1.14 2006-01-13 15:09:35 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -65,13 +65,14 @@ void yf::Backend_test::process(Package &package) const
         if (apdu_req->which != Z_APDU_initRequest && 
             !m_p->m_sessions.exist(package.session()))
         {
-            apdu_res = odr.create_close(Z_Close_protocolError,
+            apdu_res = odr.create_close(apdu_req,
+                                        Z_Close_protocolError,
                                         "no init for filter_backend_test");
             package.session().close();
         }
         else if (apdu_req->which == Z_APDU_initRequest)
         {
-            apdu_res = zget_APDU(odr, Z_APDU_initResponse);
+            apdu_res = odr.create_initResponse(apdu_req, 0, 0);
             Z_InitRequest *req = apdu_req->u.initRequest;
             Z_InitResponse *resp = apdu_res->u.initResponse;
             
@@ -106,7 +107,7 @@ void yf::Backend_test::process(Package &package) const
         }
         else if (apdu_req->which == Z_APDU_searchRequest)
         {
-            apdu_res = zget_APDU(odr, Z_APDU_searchResponse);
+            apdu_res = odr.create_searchResponse(apdu_req, 0, 0);
             Z_SearchRequest *req = apdu_req->u.searchRequest;
             Z_SearchResponse *resp = apdu_res->u.searchResponse;
                 
@@ -126,11 +127,15 @@ void yf::Backend_test::process(Package &package) const
         }
         else if (apdu_req->which == Z_APDU_presentRequest)
         { 
-            apdu_res = zget_APDU(odr, Z_APDU_presentResponse);
+            apdu_res =
+                odr.create_presentResponse(apdu_req,
+                                           YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
+                                           0);
         }
         else
         {
-            apdu_res = odr.create_close(Z_Close_protocolError,
+            apdu_res = odr.create_close(apdu_req,
+                                        Z_Close_protocolError,
                                         "bad APDU in filter_backend_test");
             package.session().close();
         }
index 70602ee..996bfe9 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_session_shared.cpp,v 1.5 2006-01-09 21:20:15 adam Exp $
+/* $Id: filter_session_shared.cpp,v 1.6 2006-01-13 15:09:35 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -30,7 +30,7 @@ namespace yp2 {
         class SessionShared::Rep {
             friend class SessionShared;
             void handle_init(Z_InitRequest *req, Package &package);
-            void handle_search(Z_SearchRequest *req, Package &package);
+            void handle_search(Z_APDU *req, Package &package);
         public:
             typedef boost::shared_ptr<SessionShared::List> SharedList;
 
@@ -102,18 +102,21 @@ yf::SessionShared::~SessionShared()
 {
 }
 
-void yf::SessionShared::Rep::handle_search(Z_SearchRequest *req,
-                                            Package &package)
+void yf::SessionShared::Rep::handle_search(Z_APDU *apdu_req,
+                                           Package &package)
 {
     yaz_log(YLOG_LOG, "Got search");
 
+    // Z_SearchRequest *req = apdu_req->u.searchRequest;
+
     SessionListMap::iterator it = m_session_list_map.find(package.session());
     if (it == m_session_list_map.end())
     {
         yp2::odr odr;
-        package.response() = odr.create_close(
-                Z_Close_protocolError,
-                "no session for search request in session_shared");
+        package.response() =
+            odr.create_close(apdu_req,
+                             Z_Close_protocolError,
+                             "no session for search request in session_shared");
         package.session().close();
         
         return;
@@ -231,13 +234,13 @@ void yf::SessionShared::process(Package &package) const
             m_p->handle_init(apdu->u.initRequest, package);
             break;
         case Z_APDU_searchRequest:
-            m_p->handle_search(apdu->u.searchRequest, package);
+            m_p->handle_search(apdu, package);
             break;
         default:
             yp2::odr odr;
-            package.response() = odr.create_close(
-                Z_Close_protocolError,
-                "cannot handle a package of this type");
+            package.response() =
+                odr.create_close(apdu, Z_Close_protocolError,
+                                 "cannot handle a package of this type");
             package.session().close();
             break;
             
index 59e1494..db26acd 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_virt_db.cpp,v 1.22 2006-01-12 14:45:04 adam Exp $
+/* $Id: filter_virt_db.cpp,v 1.23 2006-01-13 15:09:35 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -11,6 +11,7 @@
 
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/condition.hpp>
+#include <boost/shared_ptr.hpp>
 
 #include "util.hpp"
 #include "filter_virt_db.hpp"
@@ -26,18 +27,14 @@ namespace yf = yp2::filter;
 
 namespace yp2 {
     namespace filter {
+
         struct Virt_db::Set {
-            Set(yp2::Session &id, std::string setname,
-                std::string vhost, std::string route,
-                bool named_result_sets);
+            Set(BackendPtr b, std::string setname);
             Set();
             ~Set();
 
-            yp2::Session m_backend_session;
-            std::string m_backend_setname;
-            std::string m_vhost;
-            std::string m_route;
-            bool m_named_result_sets;
+            BackendPtr m_backend;
+            std::string m_setname;
         };
         struct Virt_db::Map {
             Map(std::string vhost, std::string route);
@@ -45,21 +42,41 @@ namespace yp2 {
             std::string m_vhost;
             std::string m_route;
         };
+        struct Virt_db::Backend {
+            yp2::Session m_backend_session;
+            std::string m_backend_database;
+            std::string m_frontend_database;
+            std::string m_vhost;
+            std::string m_route;
+            bool m_named_result_sets;
+            int m_number_of_sets;
+        };
         struct Virt_db::Frontend {
-            Frontend();
+            Frontend(Rep *rep);
             ~Frontend();
             yp2::Session m_session;
             bool m_is_virtual;
             bool m_in_use;
+            std::list<BackendPtr> m_backend_list;
             std::map<std::string,Virt_db::Set> m_sets;
-            void search(Package &package, Z_APDU *apdu,
-                        const std::map<std::string, Virt_db::Map> &maps);
+
+            void search(Package &package, Z_APDU *apdu);
             void present(Package &package, Z_APDU *apdu);
+            void scan(Package &package, Z_APDU *apdu);
+
             void close(Package &package);
             typedef std::map<std::string,Virt_db::Set>::iterator Sets_it;
+
+            BackendPtr lookup_backend_from_database(std::string database);
+            BackendPtr create_backend_from_database(std::string database);
+            
+            BackendPtr init_backend(std::string database, Package &package,
+                                    int &error_code, std::string &addinfo);
+            Rep *m_p;
         };            
         class Virt_db::Rep {
             friend class Virt_db;
+            friend class Frontend;
             
             Frontend *get_frontend(Package &package);
             void release_frontend(Package &package);
@@ -78,22 +95,258 @@ namespace yp2 {
 
 using namespace yp2;
 
-yf::Virt_db::Frontend::Frontend()
+yf::Virt_db::BackendPtr yf::Virt_db::Frontend::lookup_backend_from_database(
+    std::string database)
+{
+    std::list<BackendPtr>::const_iterator map_it;
+    map_it = m_backend_list.begin();
+    for (; map_it != m_backend_list.end(); map_it++)
+        if ((*map_it)->m_frontend_database == database)
+            return *map_it;
+    BackendPtr null;
+    return null;
+}
+
+yf::Virt_db::BackendPtr yf::Virt_db::Frontend::create_backend_from_database(
+    std::string database)
+{
+    std::map<std::string, Virt_db::Map>::iterator map_it;
+    map_it = m_p->m_maps.find(database);
+    if (map_it == m_p->m_maps.end())
+    {
+        BackendPtr ptr;
+        return ptr;
+    }
+    BackendPtr b(new Backend);
+
+    b->m_number_of_sets = 0;
+    b->m_frontend_database = database;
+    b->m_named_result_sets = false;
+    b->m_route = map_it->second.m_route;
+    b->m_vhost = map_it->second.m_vhost;
+    const char *sep = strchr(b->m_vhost.c_str(), '/');
+    std::string backend_database;
+    if (sep)
+        b->m_backend_database = std::string(sep+1);
+    else
+        b->m_backend_database = database;
+
+    return b;
+}
+
+yf::Virt_db::BackendPtr yf::Virt_db::Frontend::init_backend(
+    std::string database, Package &package,
+    int &error_code, std::string &addinfo)
+{
+    BackendPtr b = create_backend_from_database(database);
+    if (!b)
+    {
+        error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
+        addinfo = database;
+        return b;
+    }
+    Package init_package(b->m_backend_session, package.origin());
+    init_package.copy_filter(package);
+
+    yp2::odr odr;
+
+    Z_APDU *init_apdu = zget_APDU(odr, Z_APDU_initRequest);
+    
+    yaz_oi_set_string_oidval(&init_apdu->u.initRequest->otherInfo, odr,
+                             VAL_PROXY, 1, b->m_vhost.c_str());
+    
+    Z_InitRequest *req = init_apdu->u.initRequest;
+
+    ODR_MASK_SET(req->options, Z_Options_search);
+    ODR_MASK_SET(req->options, Z_Options_present);
+    ODR_MASK_SET(req->options, Z_Options_namedResultSets);
+    ODR_MASK_SET(req->options, Z_Options_scan);
+
+    ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
+    ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
+    ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
+
+    init_package.request() = init_apdu;
+    
+    init_package.move(b->m_route);  // sending init 
+    
+    if (init_package.session().is_closed())
+    {
+        error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
+        addinfo = database;
+        BackendPtr null;
+        return null;
+    }
+    Z_GDU *gdu = init_package.response().get();
+    // we hope to get an init response
+    if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
+        Z_APDU_initResponse)
+    {
+        if (ODR_MASK_GET(gdu->u.z3950->u.initResponse->options,
+                         Z_Options_namedResultSets))
+        {
+            b->m_named_result_sets = true;
+        }
+    }
+    else
+    {
+        error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
+        addinfo = database;
+        BackendPtr null;
+        return null;
+    }        
+    m_backend_list.push_back(b);
+    return b;
+}
+
+void yf::Virt_db::Frontend::search(Package &package, Z_APDU *apdu_req)
 {
+    Z_SearchRequest *req = apdu_req->u.searchRequest;
+    std::string vhost;
+    std::string resultSetId = req->resultSetName;
+    yp2::odr odr;
+
+    // only one datatabase for now
+    if (req->num_databaseNames != 1)
+    {   // exactly one database must be specified
+        Z_APDU *apdu =
+            odr.create_searchResponse(
+                apdu_req, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
+        package.response() = apdu;
+        
+        return;
+    }
+    std::string database = std::string(req->databaseNames[0]);
+
+    BackendPtr b; // null for now
+    Sets_it sets_it = m_sets.find(req->resultSetName);
+    if (sets_it != m_sets.end())
+    {
+        // result set already exist 
+        // if replace indicator is off: we return diagnostic if
+        // result set already exist.
+        if (*req->replaceIndicator == 0)
+        {
+            Z_APDU *apdu = 
+                odr.create_searchResponse(
+                    apdu_req,
+                    YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF,
+                    0);
+            package.response() = apdu;
+            
+            return;
+        } 
+        sets_it->second.m_backend->m_number_of_sets--;
+
+        // pick up any existing backend with a database match
+        std::list<BackendPtr>::const_iterator map_it;
+        map_it = m_backend_list.begin();
+        for (; map_it != m_backend_list.end(); map_it++)
+        {
+            BackendPtr tmp = *map_it;
+            if (tmp->m_frontend_database == database)
+                break;
+        }
+        if (map_it != m_backend_list.end()) 
+            b = *map_it;
+    }
+    else
+    {
+        // new result set.
+
+        // pick up any existing database with named result sets ..
+        // or one which has no result sets.. yet.
+        std::list<BackendPtr>::const_iterator map_it;
+        map_it = m_backend_list.begin();
+        for (; map_it != m_backend_list.end(); map_it++)
+        {
+            BackendPtr tmp = *map_it;
+            if (tmp->m_frontend_database == database &&
+                (tmp->m_named_result_sets ||
+                 tmp->m_number_of_sets == 0))
+                break;
+        }
+        if (map_it != m_backend_list.end()) 
+            b = *map_it;
+    }
+    if (!b)  // no backend yet. Must create a new one
+    {
+        int error_code;
+        std::string addinfo;
+        b = init_backend(database, package, error_code, addinfo);
+        if (!b)
+        {
+            // did not get a backend (unavailable somehow?)
+            
+            Z_APDU *apdu = 
+                odr.create_searchResponse(
+                    apdu_req, error_code, addinfo.c_str());
+            package.response() = apdu;
+            return;
+        }
+    }
+    m_sets.erase(req->resultSetName);
+    // sending search to backend
+    Package search_package(b->m_backend_session, package.origin());
+
+    search_package.copy_filter(package);
+
+    std::string backend_setname;
+    if (b->m_named_result_sets)
+    {
+        std::cout << "named_result_sets TRUE\n";
+        backend_setname = std::string(req->resultSetName);
+    }
+    else
+    {
+        std::cout << "named_result_sets FALSE\n";
+        backend_setname = "default";
+        req->resultSetName = odr_strdup(odr, backend_setname.c_str());
+    }
+
+    const char *backend_database = b->m_backend_database.c_str();
+    req->databaseNames[0] = odr_strdup(odr, backend_database);
+
+    *req->replaceIndicator = 1;
+
+    search_package.request() = yazpp_1::GDU(apdu_req);
+    
+    search_package.move(b->m_route);
+
+    if (search_package.session().is_closed())
+    {
+        Z_APDU *apdu = 
+            odr.create_searchResponse(
+                apdu_req,
+                YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
+        package.response() = apdu;
+        return;
+    }
+    package.response() = search_package.response();
+
+    b->m_number_of_sets++;
+
+    m_sets[resultSetId] = Virt_db::Set(b, backend_setname);
+}
+
+yf::Virt_db::Frontend::Frontend(Rep *rep)
+{
+    m_p = rep;
     m_is_virtual = false;
 }
 
 void yf::Virt_db::Frontend::close(Package &package)
 {
-    Sets_it sit = m_sets.begin();
-    for (; sit != m_sets.end(); sit++)
+    std::list<BackendPtr>::const_iterator b_it;
+    
+    for (b_it = m_backend_list.begin(); b_it != m_backend_list.end(); b_it++)
     {
-        sit->second.m_backend_session.close();
-        Package close_package(sit->second.m_backend_session, package.origin());
+        (*b_it)->m_backend_session.close();
+        Package close_package((*b_it)->m_backend_session, package.origin());
         close_package.copy_filter(package);
-        close_package.move(sit->second.m_route);
+        close_package.move((*b_it)->m_route);
     }
-    m_sets.clear();
+    m_backend_list.clear();
 }
 
 yf::Virt_db::Frontend::~Frontend()
@@ -119,7 +372,7 @@ yf::Virt_db::Frontend *yf::Virt_db::Rep::get_frontend(Package &package)
         }
         m_cond_session_ready.wait(lock);
     }
-    Frontend *f = new Frontend;
+    Frontend *f = new Frontend(this);
     m_clients[package.session()] = f;
     f->m_in_use = true;
     return f;
@@ -148,11 +401,8 @@ void yf::Virt_db::Rep::release_frontend(Package &package)
     }
 }
 
-yf::Virt_db::Set::Set(yp2::Session &id, std::string setname,
-                      std::string vhost, std::string route,
-                      bool named_result_sets)
-    :   m_backend_session(id), m_backend_setname(setname), m_vhost(vhost),
-    m_route(route), m_named_result_sets(named_result_sets)
+yf::Virt_db::Set::Set(BackendPtr b, std::string setname)
+    :  m_backend(b), m_setname(setname)
 {
 }
 
@@ -182,55 +432,44 @@ yf::Virt_db::Virt_db() : m_p(new Virt_db::Rep)
 yf::Virt_db::~Virt_db() {
 }
 
-void yf::Virt_db::Frontend::present(Package &package, Z_APDU *apdu)
+void yf::Virt_db::Frontend::present(Package &package, Z_APDU *apdu_req)
 {
-    Session *id = 0;
-    Z_PresentRequest *req = apdu->u.presentRequest;
+    Z_PresentRequest *req = apdu_req->u.presentRequest;
     std::string resultSetId = req->resultSetId;
     yp2::odr odr;
 
     Sets_it sets_it = m_sets.find(resultSetId);
     if (sets_it == m_sets.end())
     {
-        Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentResponse);
-        
-        Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
-        apdu->u.presentResponse->records = rec;
-        rec->which = Z_Records_NSD;
-        rec->u.nonSurrogateDiagnostic =
-            zget_DefaultDiagFormat(
-                odr,
+        Z_APDU *apdu = 
+            odr.create_presentResponse(
+                apdu_req,
                 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
                 resultSetId.c_str());
         package.response() = apdu;
         
         return;
     }
-    id = new yp2::Session(sets_it->second.m_backend_session);
+    Session *id =
+        new yp2::Session(sets_it->second.m_backend->m_backend_session);
     
     // sending present to backend
     Package present_package(*id, package.origin());
     present_package.copy_filter(package);
     
     req->resultSetId = odr_strdup(odr, "default");
-    present_package.request() = yazpp_1::GDU(apdu);
+    present_package.request() = yazpp_1::GDU(apdu_req);
 
     present_package.move();
 
     if (present_package.session().is_closed())
     {
-        Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentResponse);
-        
-        Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
-        apdu->u.presentResponse->records = rec;
-        rec->which = Z_Records_NSD;
-        rec->u.nonSurrogateDiagnostic =
-            zget_DefaultDiagFormat(
-                odr,
+        Z_APDU *apdu = 
+            odr.create_presentResponse(
+                apdu_req,
                 YAZ_BIB1_RESULT_SET_NO_LONGER_EXISTS_UNILATERALLY_DELETED_BY_,
                 resultSetId.c_str());
         package.response() = apdu;
-        
         m_sets.erase(resultSetId);
     }
     else
@@ -240,160 +479,76 @@ void yf::Virt_db::Frontend::present(Package &package, Z_APDU *apdu)
     delete id;
 }
 
-void yf::Virt_db::Frontend::search(Package &package, Z_APDU *apdu,
-                          const std::map<std::string, Virt_db::Map> &maps)
+void yf::Virt_db::Frontend::scan(Package &package, Z_APDU *apdu_req)
 {
-    Z_SearchRequest *req = apdu->u.searchRequest;
+    Z_ScanRequest *req = apdu_req->u.scanRequest;
     std::string vhost;
-    std::string database;
-    std::string resultSetId = req->resultSetName;
-    bool support_named_result_sets = false;  // whether backend supports it
     yp2::odr odr;
-    
+
+    // only one datatabase for now
     if (req->num_databaseNames != 1)
     {   // exactly one database must be specified
-        Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-        
-        Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
-        apdu->u.searchResponse->records = rec;
-        rec->which = Z_Records_NSD;
-        rec->u.nonSurrogateDiagnostic =
-            zget_DefaultDiagFormat(
-                odr, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
+        Z_APDU *apdu =
+            odr.create_scanResponse(
+                apdu_req,
+                YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
         package.response() = apdu;
-        
         return;
     }
-    database = req->databaseNames[0];
-    std::map<std::string, Virt_db::Map>::const_iterator map_it;
-    map_it = maps.find(database);
-    if (map_it == maps.end()) 
-    {   // no map for database: return diagnostic
-        Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-        
-        Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
-        apdu->u.searchResponse->records = rec;
-        rec->which = Z_Records_NSD;
-        rec->u.nonSurrogateDiagnostic =
-            zget_DefaultDiagFormat(
-                odr, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, database.c_str());
-        package.response() = apdu;
-        
-        return;
-    }
-    if (*req->replaceIndicator == 0)
+    std::string database = std::string(req->databaseNames[0]);
+    
+    BackendPtr b;
+    // pick up any existing backend with a database match
+    std::list<BackendPtr>::const_iterator map_it;
+    map_it = m_backend_list.begin();
+    for (; map_it != m_backend_list.end(); map_it++)
     {
-        Sets_it sets_it = m_sets.find(req->resultSetName);
-        if (sets_it != m_sets.end())
-        {
-            Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-            
-            Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
-            apdu->u.searchResponse->records = rec;
-            rec->which = Z_Records_NSD;
-            rec->u.nonSurrogateDiagnostic =
-                zget_DefaultDiagFormat(
-                    odr,
-                    YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF,
-                    0);
-            package.response() = apdu;
-            
-            return;
-        }
+        BackendPtr tmp = *map_it;
+        if (tmp->m_frontend_database == database)
+            break;
     }
-    m_sets.erase(req->resultSetName);
-    vhost = map_it->second.m_vhost;
-    std::string route = map_it->second.m_route;
-    // we might look for an existing session with same vhost
-    Session id;
-    const char *vhost_cstr = vhost.c_str();
-    if (true)
-    {  // sending init to backend
-        Package init_package(id, package.origin());
-        init_package.copy_filter(package);
-        
-        Z_APDU *init_apdu = zget_APDU(odr, Z_APDU_initRequest);
-        
-        yaz_oi_set_string_oidval(&init_apdu->u.initRequest->otherInfo, odr,
-                                 VAL_PROXY, 1, vhost_cstr);
-        
-        init_package.request() = init_apdu;
-
-        init_package.move(route);  // sending init 
-
-        if (init_package.session().is_closed())
-        {
-            Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-            
-            Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
-            apdu->u.searchResponse->records = rec;
-            rec->which = Z_Records_NSD;
-            rec->u.nonSurrogateDiagnostic =
-                zget_DefaultDiagFormat(
-                    odr, YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
-            package.response() = apdu;
-        }
-        Z_GDU *gdu = init_package.response().get();
-        // we hope to get an init response
-        if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
-            Z_APDU_initResponse)
-        {
-            if (ODR_MASK_GET(gdu->u.z3950->u.initResponse->options,
-                             Z_Options_namedResultSets))
-                support_named_result_sets = true;
-        }
-        else
+    if (map_it != m_backend_list.end()) 
+        b = *map_it;
+    if (!b)  // no backend yet. Must create a new one
+    {
+        int error_code;
+        std::string addinfo;
+        b = init_backend(database, package, error_code, addinfo);
+        if (!b)
         {
-            Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-            
-            Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
-            apdu->u.searchResponse->records = rec;
-            rec->which = Z_Records_NSD;
-            rec->u.nonSurrogateDiagnostic =
-                zget_DefaultDiagFormat(
-                    odr, YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
+            // did not get a backend (unavailable somehow?)
+            Z_APDU *apdu =
+                odr.create_scanResponse(
+                    apdu_req, error_code, addinfo.c_str());
             package.response() = apdu;
             
             return;
         }
     }
-    // sending search to backend
-    Package search_package(id, package.origin());
+    // sending scan to backend
+    Package scan_package(b->m_backend_session, package.origin());
 
-    search_package.copy_filter(package);
-    const char *sep = strchr(vhost_cstr, '/');
-    if (sep)
-        req->databaseNames[0] = odr_strdup(odr, sep+1);
+    scan_package.copy_filter(package);
 
-    *req->replaceIndicator = 1;
+    const char *backend_database = b->m_backend_database.c_str();
+    req->databaseNames[0] = odr_strdup(odr, backend_database);
 
-    std::string backend_resultSetId = "default";
-    req->resultSetName = odr_strdup(odr, backend_resultSetId.c_str());
-    search_package.request() = yazpp_1::GDU(apdu);
+    scan_package.request() = yazpp_1::GDU(apdu_req);
     
-    search_package.move(route);
+    scan_package.move(b->m_route);
 
-    if (search_package.session().is_closed())
+    if (scan_package.session().is_closed())
     {
-        Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-        
-        Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
-        apdu->u.searchResponse->records = rec;
-        rec->which = Z_Records_NSD;
-        rec->u.nonSurrogateDiagnostic =
-            zget_DefaultDiagFormat(
-                odr, YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
+        Z_APDU *apdu =
+            odr.create_scanResponse(
+                apdu_req, YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
         package.response() = apdu;
-        
         return;
     }
-    package.response() = search_package.response();
-    
-    m_sets[resultSetId] =
-        Virt_db::Set(id, backend_resultSetId, vhost, route,
-                     support_named_result_sets);
+    package.response() = scan_package.response();
 }
 
+
 void yf::Virt_db::add_map_db2vhost(std::string db, std::string vhost,
                                    std::string route)
 {
@@ -417,7 +572,7 @@ void yf::Virt_db::process(Package &package) const
             if (!vhost)
             {
                 yp2::odr odr;
-                Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
+                Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
                 Z_InitResponse *resp = apdu->u.initResponse;
                 
                 int i;
@@ -425,6 +580,7 @@ void yf::Virt_db::process(Package &package) const
                     Z_Options_search,
                     Z_Options_present,
                     Z_Options_namedResultSets,
+                    Z_Options_scan,
                     -1 
                 };
                 for (i = 0; masks[i] != -1; i++)
@@ -459,6 +615,7 @@ void yf::Virt_db::process(Package &package) const
                 yp2::odr odr;
                 
                 package.response() = odr.create_close(
+                    apdu,
                     Z_Close_protocolError,
                     "double init");
                 
@@ -466,18 +623,22 @@ void yf::Virt_db::process(Package &package) const
             }
             else if (apdu->which == Z_APDU_searchRequest)
             {
-                f->search(package, apdu, m_p->m_maps);
+                f->search(package, apdu);
             }
             else if (apdu->which == Z_APDU_presentRequest)
             {
                 f->present(package, apdu);
             }
+            else if (apdu->which == Z_APDU_scanRequest)
+            {
+                f->scan(package, apdu);
+            }
             else
             {
                 yp2::odr odr;
                 
                 package.response() = odr.create_close(
-                    Z_Close_protocolError,
+                    apdu, Z_Close_protocolError,
                     "unsupported APDU in filter_virt_db");
                 
                 package.session().close();
index 72065e5..00cb5b4 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_virt_db.hpp,v 1.9 2006-01-12 14:45:04 adam Exp $
+/* $Id: filter_virt_db.hpp,v 1.10 2006-01-13 15:09:35 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -10,6 +10,7 @@
 #include <stdexcept>
 #include <list>
 #include <boost/scoped_ptr.hpp>
+#include <boost/shared_ptr.hpp>
 
 #include "filter.hpp"
 
@@ -20,6 +21,8 @@ namespace yp2 {
             class Frontend;
             class Map;
             class Set;
+            class Backend;
+            typedef boost::shared_ptr<Virt_db::Backend> BackendPtr;
         public:
             ~Virt_db();
             Virt_db();
index 7c0d88f..9975aa6 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_z3950_client.cpp,v 1.19 2006-01-11 11:51:50 adam Exp $
+/* $Id: filter_z3950_client.cpp,v 1.20 2006-01-13 15:09:35 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -108,7 +108,12 @@ void yf::Z3950Client::Assoc::failNotify()
 
     if (m_package)
     {
-        m_package->response() = odr.create_close(Z_Close_peerAbort, 0);
+        Z_GDU *gdu = m_package->request().get();
+        Z_APDU *apdu = 0;
+        if (gdu && gdu->which == Z_GDU_Z3950)
+            apdu = gdu->u.z3950;
+        
+        m_package->response() = odr.create_close(apdu, Z_Close_peerAbort, 0);
         m_package->session().close();
     }
 }
@@ -124,10 +129,17 @@ void yf::Z3950Client::Assoc::timeoutNotify()
         
         if (m_package)
         {
+            Z_GDU *gdu = m_package->request().get();
+            Z_APDU *apdu = 0;
+            if (gdu && gdu->which == Z_GDU_Z3950)
+                apdu = gdu->u.z3950;
+        
             if (m_connected)
-                m_package->response() = odr.create_close(Z_Close_lackOfActivity, 0);
+                m_package->response() =
+                    odr.create_close(apdu, Z_Close_lackOfActivity, 0);
             else
-                m_package->response() = odr.create_close(Z_Close_peerAbort, 0);
+                m_package->response() = 
+                    odr.create_close(apdu, Z_Close_peerAbort, 0);
                 
             m_package->session().close();
         }
@@ -204,7 +216,8 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
     {
         yp2::odr odr;
         
-        package.response() = odr.create_close(Z_Close_protocolError,
+        package.response() = odr.create_close(apdu,
+                                              Z_Close_protocolError,
                                               "First PDU was not an "
                                               "Initialize Request");
         package.session().close();
@@ -219,6 +232,7 @@ yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
     {
         yp2::odr odr;
         package.response() = odr.create_initResponse(
+            apdu,
             YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
             "Virtual host not given");
         
index 479aebf..fc304ca 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: util.cpp,v 1.3 2005-10-30 18:51:21 adam Exp $
+/* $Id: util.cpp,v 1.4 2006-01-13 15:09:35 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -48,9 +48,10 @@ yp2::odr::operator ODR() const
     return m_odr;
 }
 
-Z_APDU *yp2::odr::create_close(int reason, const char *addinfo)
+Z_APDU *yp2::odr::create_close(Z_APDU *in_apdu,
+                               int reason, const char *addinfo)
 {
-    Z_APDU *apdu = zget_APDU(m_odr, Z_APDU_close);
+    Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
     
     *apdu->u.close->closeReason = reason;
     if (addinfo)
@@ -58,9 +59,37 @@ Z_APDU *yp2::odr::create_close(int reason, const char *addinfo)
     return apdu;
 }
 
-Z_APDU *yp2::odr::create_initResponse(int error, const char *addinfo)
+Z_APDU *yp2::odr::create_APDU(int type, Z_APDU *in_apdu)
 {
-    Z_APDU *apdu = zget_APDU(m_odr, Z_APDU_initResponse);
+    return yp2::util::create_APDU(m_odr, type, in_apdu);
+}
+
+Z_APDU *yp2::util::create_APDU(ODR odr, int type, Z_APDU *in_apdu)
+{
+    Z_APDU *out_apdu = zget_APDU(odr, type);
+
+    Z_ReferenceId **id_to = yp2::util::get_referenceId(out_apdu);
+    *id_to = 0;
+    if (in_apdu)
+    {
+        Z_ReferenceId **id_from = yp2::util::get_referenceId(in_apdu);
+        if (id_from && *id_from && id_to)
+        {
+            *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
+            (*id_to)->size = (*id_to)->len = (*id_from)->len;
+            (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
+            memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
+        }
+        else if (id_to)
+            *id_to = 0;
+    }
+    return out_apdu;
+}
+
+Z_APDU *yp2::odr::create_initResponse(Z_APDU *in_apdu,
+                                      int error, const char *addinfo)
+{
+    Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
     if (error)
     {
         apdu->u.initResponse->userInformationField =
@@ -70,6 +99,114 @@ Z_APDU *yp2::odr::create_initResponse(int error, const char *addinfo)
     return apdu;
 }
 
+Z_APDU *yp2::odr::create_searchResponse(Z_APDU *in_apdu,
+                                        int error, const char *addinfo)
+{
+    Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
+    if (error)
+    {
+        Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
+        *apdu->u.searchResponse->searchStatus = 0;
+        apdu->u.searchResponse->records = rec;
+        rec->which = Z_Records_NSD;
+        rec->u.nonSurrogateDiagnostic =
+            zget_DefaultDiagFormat(m_odr, error, addinfo);
+        
+    }
+    return apdu;
+}
+
+Z_APDU *yp2::odr::create_presentResponse(Z_APDU *in_apdu,
+                                         int error, const char *addinfo)
+{
+    Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
+    if (error)
+    {
+        Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
+        apdu->u.presentResponse->records = rec;
+        rec->which = Z_Records_NSD;
+        rec->u.nonSurrogateDiagnostic =
+            zget_DefaultDiagFormat(m_odr, error, addinfo);
+    }
+    return apdu;
+}
+
+Z_APDU *yp2::odr::create_scanResponse(Z_APDU *in_apdu,
+                                      int error, const char *addinfo)
+{
+    Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
+    if (error)
+    {
+        Z_ScanResponse *res = apdu->u.scanResponse;
+        res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
+        *res->scanStatus = Z_Scan_failure;
+
+        res->entries->num_entries = 0;
+        res->entries->entries = 0;
+        res->entries->num_nonsurrogateDiagnostics = 1;
+        res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
+            odr_malloc(m_odr, sizeof(Z_DiagRec *));
+        res->entries->nonsurrogateDiagnostics[0] = 
+            zget_DiagRec(m_odr, error, addinfo);
+    }
+    return apdu;
+}
+
+Z_ReferenceId **yp2::util::get_referenceId(Z_APDU *apdu)
+{
+    switch (apdu->which)
+    {
+    case  Z_APDU_initRequest:
+        return &apdu->u.initRequest->referenceId; 
+    case  Z_APDU_initResponse:
+        return &apdu->u.initResponse->referenceId;
+    case  Z_APDU_searchRequest:
+        return &apdu->u.searchRequest->referenceId;
+    case  Z_APDU_searchResponse:
+        return &apdu->u.searchResponse->referenceId;
+    case  Z_APDU_presentRequest:
+        return &apdu->u.presentRequest->referenceId;
+    case  Z_APDU_presentResponse:
+        return &apdu->u.presentResponse->referenceId;
+    case  Z_APDU_deleteResultSetRequest:
+        return &apdu->u.deleteResultSetRequest->referenceId;
+    case  Z_APDU_deleteResultSetResponse:
+        return &apdu->u.deleteResultSetResponse->referenceId;
+    case  Z_APDU_accessControlRequest:
+        return &apdu->u.accessControlRequest->referenceId;
+    case  Z_APDU_accessControlResponse:
+        return &apdu->u.accessControlResponse->referenceId;
+    case  Z_APDU_resourceControlRequest:
+        return &apdu->u.resourceControlRequest->referenceId;
+    case  Z_APDU_resourceControlResponse:
+        return &apdu->u.resourceControlResponse->referenceId;
+    case  Z_APDU_triggerResourceControlRequest:
+        return &apdu->u.triggerResourceControlRequest->referenceId;
+    case  Z_APDU_resourceReportRequest:
+        return &apdu->u.resourceReportRequest->referenceId;
+    case  Z_APDU_resourceReportResponse:
+        return &apdu->u.resourceReportResponse->referenceId;
+    case  Z_APDU_scanRequest:
+        return &apdu->u.scanRequest->referenceId;
+    case  Z_APDU_scanResponse:
+        return &apdu->u.scanResponse->referenceId;
+    case  Z_APDU_sortRequest:
+        return &apdu->u.sortRequest->referenceId;
+    case  Z_APDU_sortResponse:
+        return &apdu->u.sortResponse->referenceId;
+    case  Z_APDU_segmentRequest:
+        return &apdu->u.segmentRequest->referenceId;
+    case  Z_APDU_extendedServicesRequest:
+        return &apdu->u.extendedServicesRequest->referenceId;
+    case  Z_APDU_extendedServicesResponse:
+        return &apdu->u.extendedServicesResponse->referenceId;
+    case  Z_APDU_close:
+        return &apdu->u.close->referenceId;
+    }
+    return 0;
+}
+
+
 /*
  * Local variables:
  * c-basic-offset: 4
index 9918d45..2ab14a1 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: util.hpp,v 1.3 2005-10-30 18:51:21 adam Exp $
+/* $Id: util.hpp,v 1.4 2006-01-13 15:09:35 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
 #include <boost/utility.hpp>
 
 namespace yp2 {
-    struct util  {
-       static bool pqf(ODR odr, Z_APDU *apdu, const std::string &q);
+    namespace util  {
+       bool pqf(ODR odr, Z_APDU *apdu, const std::string &q);
+        Z_ReferenceId **get_referenceId(Z_APDU *apdu);
+        Z_APDU *create_APDU(ODR odr, int type, Z_APDU *in_apdu);
     };
 
     class odr : public boost::noncopyable {
@@ -23,8 +25,17 @@ namespace yp2 {
         odr();
         ~odr();
         operator ODR() const;
-        Z_APDU *create_close(int reason, const char *addinfo);
-        Z_APDU *create_initResponse(int error, const char *addinfo);
+        Z_APDU *create_close(Z_APDU *in_apdu, 
+                             int reason, const char *addinfo);
+        Z_APDU *create_initResponse(Z_APDU *in_apdu, 
+                                    int error, const char *addinfo);
+        Z_APDU *create_searchResponse(Z_APDU *in_apdu,
+                                      int error, const char *addinfo);
+        Z_APDU *create_presentResponse(Z_APDU *in_apdu,
+                                       int error, const char *addinfo);
+        Z_APDU *create_scanResponse(Z_APDU *in_apdu,
+                                    int error, const char *addinfo);
+        Z_APDU *create_APDU(int type, Z_APDU *in_apdu);
     private:
         ODR m_odr;
     };