Started work on shared session filter.
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 14 Nov 2005 23:35:22 +0000 (23:35 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 14 Nov 2005 23:35:22 +0000 (23:35 +0000)
src/Makefile.am
src/ex_filter_frontend_net.cpp
src/filter_session_shared.cpp [new file with mode: 0644]
src/filter_session_shared.hpp [new file with mode: 0644]
src/filter_template.cpp [new file with mode: 0644]
src/filter_template.hpp [new file with mode: 0644]
src/filter_virt_db.cpp
src/router_flexml.cpp

index 88dc408..b05f05b 100644 (file)
@@ -1,4 +1,4 @@
-## $Id: Makefile.am,v 1.35 2005-11-11 15:00:25 adam Exp $
+## $Id: Makefile.am,v 1.36 2005-11-14 23:35:22 adam Exp $
 
 MAINTAINERCLEANFILES = Makefile.in config.in config.hpp
 
@@ -23,6 +23,8 @@ libyp2_la_SOURCES = \
        filter_virt_db.cpp filter_virt_db.hpp \
        filter_z3950_client.cpp filter_z3950_client.hpp \
        filter_backend_test.cpp filter_backend_test.hpp \
+       filter_session_shared.cpp filter_session_shared.hpp \
+       filter_template.cpp filter_template.hpp \
        pipe.cpp pipe.hpp \
        util.cpp util.hpp
 
index 3edde7b..a124e6a 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: ex_filter_frontend_net.cpp,v 1.17 2005-10-31 09:40:18 marc Exp $
+/* $Id: ex_filter_frontend_net.cpp,v 1.18 2005-11-14 23:35:22 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -17,6 +17,7 @@ namespace po = boost::program_options;
 #include "filter_frontend_net.hpp"
 #include "filter_z3950_client.hpp"
 #include "filter_virt_db.hpp"
+#include "filter_session_shared.hpp"
 #include "filter_log.hpp"
 
 #include "router_chain.hpp"
@@ -102,10 +103,12 @@ int main(int argc, char **argv)
 
             // put Virt db filter in router
             yp2::filter::Virt_db filter_virt_db;
-            filter_virt_db.add_map_db2vhost("Default", "indexdata.dk/gils");
-            filter_virt_db.add_map_db2vhost("Local", "localhost:9100/Default");
+            filter_virt_db.add_map_db2vhost("gils", "indexdata.dk/gils");
+            filter_virt_db.add_map_db2vhost("Default", "localhost:9100/Default");
+//         router.append(filter_virt_db);
 
-           router.append(filter_virt_db);
+            yp2::filter::Session_shared filter_session_shared;
+           router.append(filter_session_shared);
 
             yp2::filter::Log filter_log_back("BACK");
             router.append(filter_log_back);
diff --git a/src/filter_session_shared.cpp b/src/filter_session_shared.cpp
new file mode 100644 (file)
index 0000000..ee06d30
--- /dev/null
@@ -0,0 +1,260 @@
+/* $Id: filter_session_shared.cpp,v 1.1 2005-11-14 23:35:22 adam Exp $
+   Copyright (c) 2005, Index Data.
+
+%LICENSE%
+ */
+
+#include "config.hpp"
+
+#include "filter.hpp"
+#include "router.hpp"
+#include "package.hpp"
+
+#include <boost/thread/mutex.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include "util.hpp"
+#include "filter_session_shared.hpp"
+
+#include <yaz/log.h>
+#include <yaz/zgdu.h>
+#include <yaz/otherinfo.h>
+#include <yaz/diagbib1.h>
+
+#include <map>
+#include <iostream>
+
+namespace yf = yp2::filter;
+
+namespace yp2 {
+    namespace filter {
+        class Session_shared::Rep {
+            friend class Session_shared;
+            void handle_init(Z_InitRequest *req, Package &package);
+            void handle_search(Z_SearchRequest *req, Package &package);
+        public:
+            typedef boost::shared_ptr<Session_shared::List> SharedList;
+
+            typedef std::map<Session_shared::InitKey,SharedList> InitListMap;
+            InitListMap m_init_list_map;
+
+            typedef std::map<Session,SharedList> SessionListMap;
+            SessionListMap m_session_list_map;
+
+        };
+        class Session_shared::InitKey {
+            friend class Session_shared;
+            friend class Session_shared::Rep;
+            std::string m_vhost;
+            std::string m_open;
+            std::string m_user;
+            std::string m_group;
+            std::string m_password;
+        public:
+            bool operator < (const Session_shared::InitKey &k) const;
+        };
+        class Session_shared::List {
+        public:
+            yazpp_1::GDU m_init_response;  // init response for backend 
+            Session m_session;             // session for backend
+        };
+    }
+    
+}
+
+
+using namespace yp2;
+
+bool yf::Session_shared::InitKey::operator < (const Session_shared::InitKey &k) const {
+    if (m_vhost < k.m_vhost)
+        return true;
+    else if (m_vhost < k.m_vhost)
+        return false;
+
+    if (m_open < k.m_open)
+        return true;
+    else if (m_open > k.m_open)
+        return false;
+
+    if (m_user < k.m_user)
+        return true;
+    else if (m_user > k.m_user)
+        return false;
+    if (m_group < k.m_group)
+        return true;
+    else if (m_group > k.m_group)
+        return false;
+
+    if (m_password < k.m_password)
+        return true;
+    else if (m_password > k.m_password)
+        return false;
+    return false;
+}
+
+yf::Session_shared::Session_shared() : m_p(new Rep)
+{
+}
+
+yf::Session_shared::~Session_shared()
+{
+}
+
+void yf::Session_shared::Rep::handle_search(Z_SearchRequest *req,
+                                            Package &package)
+{
+    yaz_log(YLOG_LOG, "Got search");
+
+    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.session().close();
+        
+        return;
+    }
+    Package search_package(it->second->m_session, package.origin());
+    search_package.copy_filter(package);
+    search_package.request() = package.request();
+    
+    search_package.move();
+        
+    // transfer to frontend
+    package.response() = search_package.response();
+}
+
+void yf::Session_shared::Rep::handle_init(Z_InitRequest *req, Package &package)
+{
+    yaz_log(YLOG_LOG, "Got init");
+
+    Session_shared::InitKey key;
+    const char *vhost =
+        yaz_oi_get_string_oidval(&req->otherInfo, VAL_PROXY, 1, 0);
+    if (vhost)
+        key.m_vhost = vhost;
+
+    if (!req->idAuthentication)
+    {
+        yaz_log(YLOG_LOG, "No authentication");
+    }
+    else
+    {
+        Z_IdAuthentication *auth = req->idAuthentication;
+        switch(auth->which)
+        {
+        case Z_IdAuthentication_open:
+            yaz_log(YLOG_LOG, "open auth open=%s", auth->u.open);
+            key.m_open = auth->u.open;
+            break;
+        case Z_IdAuthentication_idPass:
+            yaz_log(YLOG_LOG, "idPass user=%s group=%s pass=%s",
+                    auth->u.idPass->userId, auth->u.idPass->groupId,
+                    auth->u.idPass->password);
+            if (auth->u.idPass->userId)
+                key.m_user = auth->u.idPass->userId;
+            if (auth->u.idPass->groupId)
+                key.m_group  = auth->u.idPass->groupId;
+            if (auth->u.idPass->password)
+                key.m_password  = auth->u.idPass->password;
+            break;
+        case Z_IdAuthentication_anonymous:
+            yaz_log(YLOG_LOG, "anonymous");
+            break;
+        default:
+            yaz_log(YLOG_LOG, "other");
+        } 
+    }
+    InitListMap::iterator it = m_init_list_map.find(key);
+    if (it == m_init_list_map.end())
+    {
+        yaz_log(YLOG_LOG, "New KEY");
+
+        // building new package with original init and new session 
+        SharedList l(new Session_shared::List);  // new session for backend
+
+        Package init_package(l->m_session, package.origin());
+        init_package.copy_filter(package);
+        init_package.request() = package.request();
+
+        init_package.move();
+        
+        // transfer to frontend
+        package.response() = init_package.response();
+         
+        // check that we really got Z39.50 Init Response
+        Z_GDU *gdu = init_package.response().get();
+        if (gdu && gdu->which == Z_GDU_Z3950
+           && gdu->u.z3950->which == Z_APDU_initResponse)
+        {
+            // save the init response
+            l->m_init_response = init_package.response();
+            
+            // save session and init response for later
+            m_init_list_map[key] = l;
+
+            m_session_list_map[package.session()] = l;
+        }
+    }
+    else
+    {
+        yaz_log(YLOG_LOG, "Existing KEY");
+        package.response() = it->second->m_init_response;
+
+        m_session_list_map[package.session()] = it->second;
+    }
+}
+
+void yf::Session_shared::process(Package &package) const
+{
+    // don't tell the backend if the "fronent" filter closes..
+    // we want to keep them alive
+    if (package.session().is_closed())
+    {
+        m_p->m_session_list_map.erase(package.session());
+        return;
+    }
+
+    Z_GDU *gdu = package.request().get();
+
+    if (gdu && gdu->which == Z_GDU_Z3950)
+    {
+        Z_APDU *apdu = gdu->u.z3950;
+
+        switch(apdu->which)
+        {
+        case Z_APDU_initRequest:
+            m_p->handle_init(apdu->u.initRequest, package);
+            break;
+        case Z_APDU_searchRequest:
+            m_p->handle_search(apdu->u.searchRequest, package);
+            break;
+        default:
+            yp2::odr odr;
+            package.response() = odr.create_close(
+                Z_Close_protocolError,
+                "cannot handle a package of this type");
+            package.session().close();
+            break;
+            
+        }
+        if (package.session().is_closed()) {
+            m_p->m_session_list_map.erase(package.session());
+        }
+    }
+    else
+        package.move();  // Not Z39.50 or not Init
+}
+
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * c-file-style: "stroustrup"
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
diff --git a/src/filter_session_shared.hpp b/src/filter_session_shared.hpp
new file mode 100644 (file)
index 0000000..ce79af1
--- /dev/null
@@ -0,0 +1,38 @@
+/* $Id: filter_session_shared.hpp,v 1.1 2005-11-14 23:35:22 adam Exp $
+   Copyright (c) 2005, Index Data.
+
+%LICENSE%
+ */
+
+#ifndef FILTER_SESSION_SHARED_HPP
+#define FILTER_SESSION_SHARED_HPP
+
+#include <boost/scoped_ptr.hpp>
+
+#include "filter.hpp"
+
+namespace yp2 {
+    namespace filter {
+        class Session_shared : public Base {
+            class Rep;
+            class InitKey;
+            class List;
+        public:
+            ~Session_shared();
+            Session_shared();
+            void process(yp2::Package & package) const;
+        private:
+            boost::scoped_ptr<Rep> m_p;
+        };
+    }
+}
+
+#endif
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * c-file-style: "stroustrup"
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
diff --git a/src/filter_template.cpp b/src/filter_template.cpp
new file mode 100644 (file)
index 0000000..1c3bf86
--- /dev/null
@@ -0,0 +1,55 @@
+/* $Id: filter_template.cpp,v 1.1 2005-11-14 23:35:22 adam Exp $
+   Copyright (c) 2005, Index Data.
+
+%LICENSE%
+ */
+
+#include "config.hpp"
+
+#include "filter.hpp"
+#include "router.hpp"
+#include "package.hpp"
+
+#include <boost/thread/mutex.hpp>
+
+#include "util.hpp"
+#include "filter_template.hpp"
+
+#include <yaz/zgdu.h>
+
+namespace yf = yp2::filter;
+
+namespace yp2 {
+    namespace filter {
+        class Template::Rep {
+            friend class Template;
+            int dummy;
+        };
+    }
+}
+
+using namespace yp2;
+
+yf::Template::Template() : m_p(new Rep)
+{
+}
+
+yf::Template::~Template()
+{
+}
+
+void yf::Template::process(Package &package) const
+{
+    // Z_GDU *gdu = package.request().get();
+    package.move();
+}
+
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * c-file-style: "stroustrup"
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
diff --git a/src/filter_template.hpp b/src/filter_template.hpp
new file mode 100644 (file)
index 0000000..ac44a95
--- /dev/null
@@ -0,0 +1,36 @@
+/* $Id: filter_template.hpp,v 1.1 2005-11-14 23:35:22 adam Exp $
+   Copyright (c) 2005, Index Data.
+
+%LICENSE%
+ */
+
+#ifndef FILTER_TEMPLATE_HPP
+#define FILTER_TEMPLATE_HPP
+
+#include <boost/scoped_ptr.hpp>
+
+#include "filter.hpp"
+
+namespace yp2 {
+    namespace filter {
+        class Template : public Base {
+            class Rep;
+        public:
+            ~Template();
+            Template();
+            void process(yp2::Package & package) const;
+        private:
+            boost::scoped_ptr<Rep> m_p;
+        };
+    }
+}
+
+#endif
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * c-file-style: "stroustrup"
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
index 4190cfa..663c1be 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_virt_db.cpp,v 1.13 2005-11-03 14:45:16 adam Exp $
+/* $Id: filter_virt_db.cpp,v 1.14 2005-11-14 23:35:22 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
 #include "filter_virt_db.hpp"
 
 #include <yaz/zgdu.h>
-#include <yaz/log.h>
 #include <yaz/otherinfo.h>
 #include <yaz/diagbib1.h>
 
-#include <list>
 #include <map>
 #include <iostream>
 
index aa4257a..944ec0c 100644 (file)
@@ -1,10 +1,11 @@
-/* $Id: router_flexml.cpp,v 1.2 2005-11-10 23:10:42 adam Exp $
+/* $Id: router_flexml.cpp,v 1.3 2005-11-14 23:35:22 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
  */
 
 
+#include "config.hpp"
 #include "router_flexml.hpp"
 
 #include <iostream>