Added support for dynamic load support for RouterFlexXML. The filter path
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 19 Jan 2006 09:41:01 +0000 (09:41 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 19 Jan 2006 09:41:01 +0000 (09:41 +0000)
can be specified as CDATA in element dlpath in XML config, i.e.
<yp2 xmlns="http://indexdata.dk/yp2/config/1">
  <dlpath>../src/.libs</dlpath>
  <start route="start"/>
  ..

src/factory_filter.cpp
src/factory_filter.hpp
src/router_flexml.cpp
src/test_filter_factory.cpp

index d48d2dd..8b69ae1 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: factory_filter.cpp,v 1.2 2006-01-05 16:39:37 adam Exp $
+/* $Id: factory_filter.cpp,v 1.3 2006-01-19 09:41:01 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -64,6 +64,17 @@ bool yp2::FactoryFilter::drop_creator(std::string fi)
     return m_p->m_fcm.erase(fi) == 1;
 }
 
+bool yp2::FactoryFilter::exist(std::string fi)
+{
+    Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
+    
+    if (it == m_p->m_fcm.end())
+    {
+        return false;
+    }
+    return true;
+}
+
 yp2::filter::Base* yp2::FactoryFilter::create(std::string fi)
 {
     Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
@@ -76,10 +87,19 @@ yp2::filter::Base* yp2::FactoryFilter::create(std::string fi)
     return (it->second());
 }
 
+bool yp2::FactoryFilter::have_dl_support()
+{
 #if HAVE_DLFCN_H
-bool yp2::FactoryFilter::add_creator_dyn(const std::string &fi,
-                                         const std::string &path)
+    return true;
+#else
+    return false;
+#endif
+}
+
+bool yp2::FactoryFilter::add_creator_dl(const std::string &fi,
+                                        const std::string &path)
 {
+#if HAVE_DLFCN_H
     if (m_p->m_fcm.find(fi) != m_p->m_fcm.end())
     {
         return true;
@@ -105,8 +125,10 @@ bool yp2::FactoryFilter::add_creator_dyn(const std::string &fi,
     }
     struct yp2_filter_struct *s = (struct yp2_filter_struct *) dlsym_ptr;
     return add_creator(fi, s->creator);
-}
+#else
+    return false;
 #endif
+}
 
 /*
  * Local variables:
index 7ecb706..006ac1d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: factory_filter.hpp,v 1.2 2006-01-05 16:39:37 adam Exp $
+/* $Id: factory_filter.hpp,v 1.3 2006-01-19 09:41:01 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -34,9 +34,11 @@ namespace yp2 {
         bool drop_creator(std::string fi);
         
         yp2::filter::Base* create(std::string fi);
+        bool exist(std::string fi);
+    
+        bool add_creator_dl(const std::string &fi, const std::string &path);
 
-        bool add_creator_dyn(const std::string &fi, const std::string &path);
-
+        bool have_dl_support();
 
         class NotFound : public std::runtime_error {
         public:
index 027d2ae..69953e0 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: router_flexml.cpp,v 1.15 2006-01-18 10:30:58 adam Exp $
+/* $Id: router_flexml.cpp,v 1.16 2006-01-19 09:41:01 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -45,6 +45,8 @@ namespace yp2 {
 
         std::string m_start_route;
 
+        std::string m_dl_path;
+
         void parse_xml_config_dom(xmlDocPtr doc);
 
         void parse_xml_filters(xmlDocPtr doc, const xmlNode *node);
@@ -96,6 +98,12 @@ void yp2::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
                                     " in filter element. Got " + name);
         }
 
+        if (!m_factory->exist(type_value))
+        {
+            std::cout << "about to load " << type_value << ", path=" << 
+                m_dl_path << "\n";
+            m_factory->add_creator_dl(type_value, m_dl_path);
+        }
         yp2::filter::Base* filter_base = m_factory->create(type_value);
 
         filter_base->configure(node);
@@ -134,8 +142,8 @@ void yp2::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
                 id_value = value;
             else
                 throw yp2::XMLError("Only attribute 'id' allowed for"
-                                         " element 'route'."
-                                         " Got " + name);
+                                    " element 'route'."
+                                    " Got " + name);
         }
 
         Route route;
@@ -181,6 +189,12 @@ void yp2::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
             }
             else if (type_value.length())
             {
+                if (!m_factory->exist(type_value))
+                {
+                    std::cout << "about to load " << type_value << ", path=" << 
+                        m_dl_path << "\n";
+                    m_factory->add_creator_dl(type_value, m_dl_path);
+                }
                 yp2::filter::Base* filter_base = m_factory->create(type_value);
 
                 filter_base->configure(node3);
@@ -211,8 +225,14 @@ void yp2::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc)
     
     yp2::xml::check_element_yp2(root,  "yp2");
 
-    // process <start> node which is expected first element node
     const xmlNode* node = yp2::xml::jump_to_children(root, XML_ELEMENT_NODE);
+
+    if (yp2::xml::is_element_yp2(node, "dlpath"))
+    {
+        m_dl_path = yp2::xml::get_text(node);
+        node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
+    }
+    // process <start> node which is expected first element node
     if (yp2::xml::check_element_yp2(node, "start"))
     {
         const struct _xmlAttr *attr;
index 81012d4..3b6be2c 100644 (file)
@@ -1,8 +1,7 @@
-/* $Id: test_filter_factory.cpp,v 1.10 2006-01-04 14:30:51 adam Exp $
+/* $Id: test_filter_factory.cpp,v 1.11 2006-01-19 09:41:01 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
-
 */
 
 #include <iostream>
@@ -97,10 +96,10 @@ BOOST_AUTO_UNIT_TEST( test_filter_factory_2 )
         const std::string id = "dl";
         
         // first load
-        BOOST_CHECK(ffactory.add_creator_dyn(id, ".libs"));
+        BOOST_CHECK(ffactory.add_creator_dl(id, ".libs"));
 
         // test double load
-        BOOST_CHECK(ffactory.add_creator_dyn(id, ".libs"));
+        BOOST_CHECK(ffactory.add_creator_dl(id, ".libs"));
                 
         yp2::filter::Base* filter = 0;
         filter = ffactory.create(id);