client: Add Object Detection & Washer_Shot
[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 typedef void create_t(AbstractRoutingEngine*, map<string, string> );
34 typedef void* create_mainloop_t(int argc, char** argv);
35 typedef void* createRoutingEngine(void);
36
37 class PluginLoader
38 {
39
40 public:
41         PluginLoader(std::string configFile, int argc, char** argv);
42         ~PluginLoader();
43
44         IMainLoop* mainloop();
45
46         std::string errorString();
47
48         void scanPluginDir(const std::string &);
49
50         bool readPluginConfig(const std::string & configData);
51
52 private: ///methods:
53
54
55         bool loadPlugin(string pluginName, std::map<std::string, std::string> config)
56         {
57                 DebugOut()<<"Loading plugin: "<<pluginName<<endl;
58
59                 void* handle = dlopen(pluginName.c_str(), RTLD_LAZY);
60
61                 if(!handle)
62                 {
63                         mErrorString = dlerror();
64                         DebugOut(DebugOut::Error)<<"error opening plugin: "<<pluginName<<" in "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<" "<<mErrorString<<endl;
65                         return false;
66                 }
67
68                 openHandles.push_back(handle);
69
70                 f_create = (create_t *)dlsym(handle, "create");
71
72                 if(f_create)
73                 {
74                         f_create(routingEngine, config);
75                         return true;
76                 }
77
78                 return false;
79         }
80
81         IMainLoop* loadMainLoop(string pluginName, int argc, char** argv)
82         {
83                 DebugOut()<<"Loading plugin: "<<pluginName<<endl;
84
85                 void* handle = dlopen(pluginName.c_str(), RTLD_LAZY);
86
87                 if(!handle)
88                 {
89                         mErrorString = dlerror();
90                         DebugOut(DebugOut::Error)<<"error opening plugin: "<<pluginName<<" in "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<" "<<mErrorString<<endl;
91                         return nullptr;
92                 }
93
94                 openHandles.push_back(handle);
95
96                 m_create = (create_mainloop_t *)dlsym(handle, "create");
97
98                 if(m_create)
99                 {
100                         void* obj = m_create(argc, argv);
101                         return static_cast<IMainLoop*>( obj );
102                 }
103
104                 return nullptr;
105         }
106         AbstractRoutingEngine* loadRoutingEngine(string pluginName)
107         {
108                 DebugOut()<<"Loading plugin: "<<pluginName<<endl;
109
110                 void* handle = dlopen(pluginName.c_str(), RTLD_LAZY);
111
112                 if(!handle)
113                 {
114                         mErrorString = dlerror();
115                         cerr<<"error opening plugin: "<<pluginName<<" in "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<" "<<mErrorString<<endl;
116                         return nullptr;
117                 }
118
119                 openHandles.push_back(handle);
120
121                 r_create = (createRoutingEngine *)dlsym(handle, "create");
122
123                 if(r_create)
124                 {
125                         void* obj = r_create();
126                         return static_cast<AbstractRoutingEngine*>( obj );
127                 }
128
129                 return nullptr;
130         }
131
132 private:
133
134         std::string mPluginPath;
135         std::string mErrorString;
136
137         AbstractRoutingEngine* routingEngine;
138
139         create_t * f_create;
140         create_mainloop_t * m_create;
141         createRoutingEngine * r_create;
142
143         IMainLoop* mMainLoop;
144
145         std::vector<void*> openHandles;
146 };
147
148 #endif // PLUGINLOADER_H