added first attempt to read xml config files
[metaproxy-moved-to-github.git] / src / p2_modules.cpp
1
2 #include "config.hpp"
3 #include <dlfcn.h>
4
5 #include "p2_modules.h"
6
7 class P2_ModuleDLEntry {
8 public:
9     void *m_dl_handle;
10     P2_ModuleEntry *m_entry;
11     P2_ModuleDLEntry();
12     ~P2_ModuleDLEntry();
13 };
14
15 P2_ModuleDLEntry::P2_ModuleDLEntry()
16 {
17     m_dl_handle = 0;
18     m_entry = 0;
19 }
20     
21 P2_ModuleDLEntry::~P2_ModuleDLEntry()
22 {
23     if (m_dl_handle)
24         dlclose(m_dl_handle);
25 }
26     
27 P2_ModuleFactory::P2_ModuleFactory()
28 {
29 }
30
31 P2_ModuleFactory::~P2_ModuleFactory()
32 {
33 }
34
35 bool P2_ModuleFactory::add(P2_ModuleEntry *entry)
36 {
37     P2_ModuleDLEntry *m = new P2_ModuleDLEntry();
38     m->m_entry = entry;
39     m_modules.push_back(m);
40     return true;
41 }
42
43 bool P2_ModuleFactory::add(const char *fname)
44 {
45     void *dl_handle = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
46     if (!dl_handle)
47         return false;
48
49     P2_ModuleEntry *entry =
50         reinterpret_cast<P2_ModuleEntry *> 
51         (dlsym(dl_handle, "p2_module_entry"));
52     if (!entry)
53     {
54         dlclose(dl_handle);
55         return false;
56     }
57     P2_ModuleDLEntry *m = new P2_ModuleDLEntry();
58     m->m_dl_handle = dl_handle;
59     m->m_entry = entry;
60     m_modules.push_back(m);
61     return true;
62 }
63
64 void *P2_ModuleFactory::get_interface(const char *name, int version)
65 {
66     std::list<P2_ModuleDLEntry *>::const_iterator it;
67     for (it = m_modules.begin();  it != m_modules.end(); it++)
68     {
69         P2_ModuleDLEntry *ent = *it;
70         if (!strcmp(ent->m_entry->name, name) &&
71             ent->m_entry->version == version)
72         {
73             return ent->m_entry->interface_ptr;
74         }
75     }
76     return 0;
77 }
78