added multisource test script in examples and added --log option
[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 <ltdl.h>
25 #include <iostream>
26
27 #include "abstractsource.h"
28 #include "abstractsink.h"
29 #include "abstractroutingengine.h"
30 #include "debugout.h"
31
32
33
34 using namespace std;
35
36 typedef void* create_t(AbstractRoutingEngine*, map<string, string> );
37
38
39 class PluginLoader
40 {
41
42 public:
43         PluginLoader(string configFile, AbstractRoutingEngine* routingEngine);
44         ~PluginLoader();
45
46         SourceList sources();
47         SinkList sinks();
48
49         std::string errorString();
50         
51         
52 private: ///methods:
53         
54         template<class T>
55         T loadPlugin(string pluginName, map<string, string> config)
56         {
57                 DebugOut()<<"Loading plugin: "<<pluginName<<endl;
58                 
59                 if(lt_dlinit())
60                 {
61                         mErrorString = lt_dlerror();
62                         cerr<<"error initializing libtool: "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<" "<<mErrorString<<endl;
63                         return nullptr;
64                 }
65                 
66                 lt_dlerror();
67                 
68                 lt_dlhandle handle = lt_dlopenext(pluginName.c_str());
69                 
70                 if(!handle)
71                 {
72                         mErrorString = lt_dlerror();
73                         cerr<<"error opening plugin: "<<pluginName<<" in "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<" "<<mErrorString<<endl;
74                         return nullptr;
75                 }
76                 
77                 f_create = (create_t *)lt_dlsym(handle, "create");
78                 
79                 //mErrorString = lt_dlerror();
80                 
81                 if(f_create) 
82                 {
83                         void* obj = f_create(routingEngine, config);
84                         return static_cast<T>( obj );
85                 }
86                 
87                 return nullptr;
88         }
89         
90 private:
91         
92         std::string mPluginPath;
93         std::string mErrorString;
94         
95         AbstractRoutingEngine* routingEngine;
96         
97         SourceList mSources;
98         SinkList mSinks;
99         
100         create_t * f_create;
101 };
102
103 #endif // PLUGINLOADER_H