RouterFleXML now stores routes and filters internally.
[metaproxy-moved-to-github.git] / src / factory_filter.cpp
1 /* $Id: factory_filter.cpp,v 1.2 2006-01-05 16:39:37 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8
9 #include "factory_filter.hpp"
10
11 #if HAVE_DLFCN_H
12 #include <dlfcn.h>
13 #endif
14 #include <stdexcept>
15 #include <iostream>
16 #include <string>
17 #include <map>
18
19 namespace yp2 {
20     class FactoryFilter::Rep {
21         typedef std::map<std::string, CreateFilterCallback> CallbackMap;
22         typedef std::map<std::string, CreateFilterCallback>::iterator 
23             CallbackMapIt;
24     public:
25         friend class FactoryFilter;
26         CallbackMap m_fcm;
27         Rep();
28         ~Rep();
29     };
30 }
31
32 yp2::FactoryFilter::NotFound::NotFound(const std::string message)
33     : std::runtime_error(message)
34 {
35 }
36
37 yp2::FactoryFilter::Rep::Rep()
38 {
39 }
40
41 yp2::FactoryFilter::Rep::~Rep()
42 {
43 }
44
45 yp2::FactoryFilter::FactoryFilter() : m_p(new yp2::FactoryFilter::Rep)
46 {
47
48 }
49
50 yp2::FactoryFilter::~FactoryFilter()
51 {
52
53 }
54
55 bool yp2::FactoryFilter::add_creator(std::string fi,
56                                      CreateFilterCallback cfc)
57 {
58     return m_p->m_fcm.insert(Rep::CallbackMap::value_type(fi, cfc)).second;
59 }
60
61
62 bool yp2::FactoryFilter::drop_creator(std::string fi)
63 {
64     return m_p->m_fcm.erase(fi) == 1;
65 }
66
67 yp2::filter::Base* yp2::FactoryFilter::create(std::string fi)
68 {
69     Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
70     
71     if (it == m_p->m_fcm.end()){
72         std::string msg = "filter type '" + fi + "' not found";
73             throw NotFound(msg);
74     }
75     // call create function
76     return (it->second());
77 }
78
79 #if HAVE_DLFCN_H
80 bool yp2::FactoryFilter::add_creator_dyn(const std::string &fi,
81                                          const std::string &path)
82 {
83     if (m_p->m_fcm.find(fi) != m_p->m_fcm.end())
84     {
85         return true;
86     }
87
88     std::string full_path = path + "/yp2_filter_" + fi + ".so";
89     void *dl_handle = dlopen(full_path.c_str(), RTLD_GLOBAL|RTLD_NOW);
90     if (!dl_handle)
91     {
92         const char *dl = dlerror();
93         std::cout << "dlopen " << full_path << " failed. dlerror=" << dl << 
94             std::endl;
95         return false;
96     }
97
98     std::string full_name = "yp2_filter_" + fi;
99     
100     void *dlsym_ptr = dlsym(dl_handle, full_name.c_str());
101     if (!dlsym_ptr)
102     {
103         std::cout << "dlsym " << full_name << " failed\n";
104         return false;
105     }
106     struct yp2_filter_struct *s = (struct yp2_filter_struct *) dlsym_ptr;
107     return add_creator(fi, s->creator);
108 }
109 #endif
110
111 /*
112  * Local variables:
113  * c-basic-offset: 4
114  * indent-tabs-mode: nil
115  * c-file-style: "stroustrup"
116  * End:
117  * vim: shiftwidth=4 tabstop=8 expandtab
118  */