cdc6e8f7040c780a342b9fde3ad941ec998907c1
[profile/ivi/automotive-message-broker.git] / ambd / pluginloader.h
1 /*
2 Copyright (C) 2012 Intel Corporation
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #ifndef PLUGINLOADER_H
20 #define PLUGINLOADER_H
21
22 #include <string>
23 #include <functional>
24 #include <dlfcn.h>
25 #include <iostream>
26
27 #include "abstractsource.h"
28 #include "abstractsink.h"
29 #include "abstractroutingengine.h"
30 #include "debugout.h"
31 #include "imainloop.h"
32
33
34 using namespace std;
35
36 typedef void* create_t(AbstractRoutingEngine*, map<string, string> );
37 typedef void* create_mainloop_t(int argc, char** argv);
38 typedef void* createRoutingEngine(void);
39
40 class PluginLoader
41 {
42
43 public:
44         PluginLoader(string configFile, int argc, char** argv);
45         ~PluginLoader();
46
47         SourceList sources();
48
49         IMainLoop* mainloop();
50
51         std::string errorString();
52
53
54 private: ///methods:
55
56         template<class T>
57         T loadPlugin(string pluginName, map<string, string> config)
58         {
59                 DebugOut()<<"Loading plugin: "<<pluginName<<endl;
60
61                 void* handle = dlopen(pluginName.c_str(), RTLD_LAZY);
62
63                 if(!handle)
64                 {
65                         mErrorString = dlerror();
66                         DebugOut(DebugOut::Error)<<"error opening plugin: "<<pluginName<<" in "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<" "<<mErrorString<<endl;
67                         return nullptr;
68                 }
69
70                 openHandles.push_back(handle);
71
72                 f_create = (create_t *)dlsym(handle, "create");
73
74                 if(f_create)
75                 {
76                         void* obj = f_create(routingEngine, config);
77                         return static_cast<T>(obj);
78                 }
79
80                 return nullptr;
81         }
82
83         IMainLoop* loadMainLoop(string pluginName, int argc, char** argv)
84         {
85                 DebugOut()<<"Loading plugin: "<<pluginName<<endl;
86
87                 void* handle = dlopen(pluginName.c_str(), RTLD_LAZY);
88
89                 if(!handle)
90                 {
91                         mErrorString = dlerror();
92                         DebugOut(DebugOut::Error)<<"error opening plugin: "<<pluginName<<" in "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<" "<<mErrorString<<endl;
93                         return nullptr;
94                 }
95
96                 openHandles.push_back(handle);
97
98                 m_create = (create_mainloop_t *)dlsym(handle, "create");
99
100                 if(m_create)
101                 {
102                         void* obj = m_create(argc, argv);
103                         return static_cast<IMainLoop*>( obj );
104                 }
105
106                 return nullptr;
107         }
108         AbstractRoutingEngine* loadRoutingEngine(string pluginName)
109         {
110                 DebugOut()<<"Loading plugin: "<<pluginName<<endl;
111
112                 void* handle = dlopen(pluginName.c_str(), RTLD_LAZY);
113
114                 if(!handle)
115                 {
116                         mErrorString = dlerror();
117                         cerr<<"error opening plugin: "<<pluginName<<" in "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<" "<<mErrorString<<endl;
118                         return nullptr;
119                 }
120
121                 openHandles.push_back(handle);
122
123                 r_create = (createRoutingEngine *)dlsym(handle, "create");
124
125                 if(r_create)
126                 {
127                         void* obj = r_create();
128                         return static_cast<AbstractRoutingEngine*>( obj );
129                 }
130
131                 return nullptr;
132         }
133
134 private:
135
136         std::string mPluginPath;
137         std::string mErrorString;
138
139         AbstractRoutingEngine* routingEngine;
140
141         SourceList mSources;
142         list<AbstractSinkManager*> mSinkManagers;
143
144         create_t * f_create;
145         create_mainloop_t * m_create;
146         createRoutingEngine * r_create;
147
148
149         IMainLoop* mMainLoop;
150
151         std::vector<void*> openHandles;
152 };
153
154 #endif // PLUGINLOADER_H