Further work on filter registration in RouterFleXML
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 4 Jan 2006 14:15:45 +0000 (14:15 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 4 Jan 2006 14:15:45 +0000 (14:15 +0000)
src/ex_router_flexml.cpp
src/factory_static.cpp
src/factory_static.hpp
src/router_flexml.cpp
src/router_flexml.hpp
src/test_router_flexml.cpp

index 013aa32..c2e58f6 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: ex_router_flexml.cpp,v 1.3 2006-01-04 11:19:04 adam Exp $
+/* $Id: ex_router_flexml.cpp,v 1.4 2006-01-04 14:15:45 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -34,8 +34,9 @@ int main(int argc, char **argv)
            "</route>\n"
             "</routes>\n"
            "</yp2>\n";
-       
-       yp2::RouterFleXML rflexml(xmlconf);
+
+       yp2::FilterFactory factory;
+       yp2::RouterFleXML rflexml(xmlconf, factory);
        
        
        
index 1f684f6..f988789 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: factory_static.cpp,v 1.2 2006-01-04 11:55:31 adam Exp $
+/* $Id: factory_static.cpp,v 1.3 2006-01-04 14:15:45 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -24,7 +24,7 @@
 #include "filter_virt_db.hpp"
 #include "filter_z3950_client.hpp"
 
-yp2::FactoryStatic::FactoryStatic(yp2::FilterFactory &factory)
+yp2::FactoryStatic::FactoryStatic()
 {
     struct yp2_filter_struct *buildins[] = {
         &yp2_filter_backend_test,
@@ -39,7 +39,7 @@ yp2::FactoryStatic::FactoryStatic(yp2::FilterFactory &factory)
     int i;
 
     for (i = 0; buildins[i]; i++)
-        factory.add_creator(buildins[i]->type, buildins[i]->creator);
+        add_creator(buildins[i]->type, buildins[i]->creator);
 }
 
 
index 9433979..d01d13c 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: factory_static.hpp,v 1.1 2006-01-04 11:19:04 adam Exp $
+/* $Id: factory_static.hpp,v 1.2 2006-01-04 14:15:45 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -10,9 +10,9 @@
 #include "filter_factory.hpp"
 
 namespace yp2 {
-    class FactoryStatic {
+    class FactoryStatic : public FilterFactory {
     public:
-        FactoryStatic(yp2::FilterFactory &factory);
+        FactoryStatic();
     };
 }
 
index fc3c6f0..483990e 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: router_flexml.cpp,v 1.7 2006-01-04 11:19:04 adam Exp $
+/* $Id: router_flexml.cpp,v 1.8 2006-01-04 14:15:45 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -25,15 +25,11 @@ namespace yp2 {
         friend class RouterFleXML;
         Rep();
 
-        typedef std::map<std::string, boost::shared_ptr<const yp2::filter::Base> >
-                IdFilterMap ;
-        typedef std::list<std::string> FilterIdList;
-        typedef std::map<std::string, FilterIdList > IdRouteMap ;
+        typedef std::map<std::string,
+                         boost::shared_ptr<const yp2::filter::Base > >
+                         IdFilterMap ;
 
-        bool m_xinclude;
         IdFilterMap m_id_filter_map;
-        FilterIdList m_filter_id_list;
-        IdRouteMap m_id_route_map;
 
         void create_filter(std::string type, 
                            const xmlDoc * xmldoc,
@@ -56,7 +52,9 @@ namespace yp2 {
         const xmlNode* jump_to_next(const xmlNode* node, int xml_node_type);
         
         const xmlNode* jump_to_children(const xmlNode* node, int xml_node_type);
-        FilterFactory m_factory;
+        bool m_xinclude;
+    private:
+        FilterFactory *m_factory; // TODO shared_ptr
     };
 }
 
@@ -166,7 +164,15 @@ void yp2::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc)
             //const xmlNode *val;
         }
 
-        yp2::filter::Base* filter_base = m_factory.create(type_value);
+        yp2::filter::Base* filter_base = m_factory->create(type_value);
+
+        filter_base->configure(node2);
+
+        if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
+            throw XMLError("Filter " + id_value + " already defined");
+
+        m_id_filter_map[id_value] =
+            boost::shared_ptr<yp2::filter::Base>(filter_base);
 
         node2 = jump_to_next(node2, XML_ELEMENT_NODE);
     }
@@ -219,12 +225,11 @@ yp2::RouterFleXML::Rep::Rep() :
 {
 }
 
-yp2::RouterFleXML::RouterFleXML(std::string xmlconf) 
+yp2::RouterFleXML::RouterFleXML(std::string xmlconf, yp2::FilterFactory &factory) 
     : m_p(new Rep)
 {            
-    {
-        yp2::FactoryStatic fs(m_p->m_factory);
-    }
+
+    m_p->m_factory = &factory;
 
     LIBXML_TEST_VERSION;
     
index 14e368c..9cce474 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: router_flexml.hpp,v 1.7 2005-12-08 15:34:08 adam Exp $
+/* $Id: router_flexml.hpp,v 1.8 2006-01-04 14:15:45 adam Exp $
    Copyright (c) 2005, Index Data.
 
    %LICENSE%
@@ -6,6 +6,8 @@
 
 #include "router.hpp"
 
+#include "filter_factory.hpp"
+
 #include <stdexcept>
 
 #include <boost/scoped_ptr.hpp>
@@ -16,7 +18,7 @@ namespace yp2
     {
         class Rep;
     public:
-        RouterFleXML(std::string xmlconf);
+        RouterFleXML(std::string xmlconf, yp2::FilterFactory &factory);
         
         ~RouterFleXML();
         
index 8cfbf87..05d7b84 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: test_router_flexml.cpp,v 1.9 2006-01-04 11:55:32 adam Exp $
+/* $Id: test_router_flexml.cpp,v 1.10 2006-01-04 14:15:45 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
 
 #include "filter.hpp"
 #include "router_flexml.hpp"
-#include "filter_factory.hpp"
+#include "factory_static.hpp"
 
 #define BOOST_AUTO_TEST_MAIN
 #include <boost/test/auto_unit_test.hpp>
 
 using namespace boost::unit_test;
 
+static bool tfilter_destroyed = false;
 class TFilter: public yp2::filter::Base {
 public:
     void process(yp2::Package & package) const {};
+    ~TFilter() { tfilter_destroyed = true; };
 };
-    
+
+static yp2::filter::Base* filter_creator()
+{
+    return new TFilter;
+}
 
 BOOST_AUTO_UNIT_TEST( test_router_flexml_1 )
 {
@@ -37,6 +43,9 @@ BOOST_AUTO_UNIT_TEST( test_router_flexml_1 )
             "    <filter id=\"log_cout1\" type=\"log\">\n"
             "      <logfile>mylog1.log</logfile>\n"
             "    </filter>\n"
+            "    <filter id=\"tfilter_id\" type=\"tfilter\">\n"
+            "      <someelement/>\n"
+            "    </filter>\n"
             "    <filter id=\"log_cout2\" type=\"log\">\n"
             "      <logfile>mylog2.log</logfile>\n"
             "    </filter>\n"
@@ -48,19 +57,19 @@ BOOST_AUTO_UNIT_TEST( test_router_flexml_1 )
             "    </route>\n"
             "  </routes>\n"
             "</yp2>\n";
-        yp2::RouterFleXML rflexml(xmlconf);
-    }
-    catch ( yp2::RouterFleXML::XMLError &e) {
-        std::cout << "XMLError: " << e.what() << "\n";
-        BOOST_CHECK (false);
+
+        yp2::FactoryStatic factory;
+        factory.add_creator("tfilter", filter_creator);
+        yp2::RouterFleXML rflexml(xmlconf, factory);
     }
-    catch ( yp2::FilterFactoryException &e) {
-        std::cout << "FilterFactoryException: " << e.what() << "\n";
+    catch ( std::runtime_error &e) {
+        std::cout << "std::runtime error: " << e.what() << "\n";
         BOOST_CHECK (false);
     }
     catch ( ... ) {
         BOOST_CHECK (false);
     }
+    BOOST_CHECK(tfilter_destroyed == true);
 }
 
 BOOST_AUTO_UNIT_TEST( test_router_flexml_2 )
@@ -75,11 +84,16 @@ BOOST_AUTO_UNIT_TEST( test_router_flexml_2 )
             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
             "      <port>210</port>\n";
         
-        yp2::RouterFleXML rflexml(xmlconf_invalid);
+        yp2::FilterFactory factory;
+        yp2::RouterFleXML rflexml(xmlconf_invalid, factory);
     }
     catch ( yp2::RouterFleXML::XMLError &e) {
         got_xml_error = true;
     }
+    catch ( std::runtime_error &e) {
+        std::cout << "std::runtime error: " << e.what() << "\n";
+        BOOST_CHECK (false);
+    }
     catch ( ... ) {
         ;
     }
@@ -109,7 +123,12 @@ BOOST_AUTO_UNIT_TEST( test_router_flexml_3 )
             "  </y:routes>\n"
             "</y:yp2>\n";
        
-        yp2::RouterFleXML rflexml(xmlconf);
+        yp2::FactoryStatic factory;
+        yp2::RouterFleXML rflexml(xmlconf, factory);
+    }
+    catch ( std::runtime_error &e) {
+        std::cout << "std::runtime error: " << e.what() << "\n";
+        BOOST_CHECK (false);
     }
     catch ( ... ) {
         BOOST_CHECK (false);