AbstractSinkManagers deleted in ~PluginLoader
[profile/ivi/automotive-message-broker.git] / ambd / pluginloader.cpp
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
20 #include "pluginloader.h"
21 #include "glibmainloop.h"
22 #include <json.h>
23 #include <iostream>
24 #include <stdexcept>
25 #include <boost/concept_check.hpp>
26 //#include <json-glib/json-glib.h>
27
28
29 using namespace std;
30
31 /********************************************
32  * Example JSON config:
33  * {
34  *      sources: [ path1, path2, path3 ]
35  *      sinks: [ path1, path2, path3 ]
36  * }
37  * 
38 **********************************************/
39
40 std::string get_file_contents(const char *filename)
41 {
42   //FILE *in = fopen(filename,"r");
43   
44   std::ifstream in(filename, std::ios::in);
45   std::string output;
46   std::string line;
47   while(in.good())
48   {
49     getline(in,line);
50     output.append(line);
51   }
52   return output;
53 }
54 PluginLoader::PluginLoader(string configFile, AbstractRoutingEngine* re, int argc, char** argv): f_create(NULL), routingEngine(re), mMainLoop(nullptr)
55 {
56         if(lt_dlinit())
57         {
58                 cerr<<"error initializing libtool: "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<" "<<lt_dlerror()<<endl;
59                 throw std::runtime_error("Error initializing libtool. Aborting");
60         }
61
62         DebugOut()<<"Loading config file: "<<configFile<<endl;
63         json_object *rootobject;
64         json_tokener *tokener = json_tokener_new();
65         std::string configBuffer = get_file_contents(configFile.c_str());
66         if(configBuffer == "")
67         {
68                 throw std::runtime_error("No config or config empty");
69         }
70         enum json_tokener_error err;
71         do
72         {
73                 rootobject = json_tokener_parse_ex(tokener, configBuffer.c_str(),configBuffer.length());
74         } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue);
75         if (err != json_tokener_success)
76         {
77                 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(err));
78                 // Handle errors, as appropriate for your application.
79                 throw std::runtime_error("Invalid config");
80         }
81         if (tokener->char_offset < configFile.length()) // XXX shouldn't access internal fields
82         {
83                 // Handle extra characters after parsed object as desired.
84                 // e.g. issue an error, parse another object from that point, etc...
85         }
86         
87         //DebugOut()<<"Config members: "<<json_reader_count_members(reader)<<endl;
88         json_object *mainloopobject = json_object_object_get(rootobject,"mainloop");
89         if (mainloopobject)
90         {
91                 /// there is a mainloop entry.  Load the plugin:
92
93                 string mainloopstr = string(json_object_get_string(mainloopobject));
94
95                 mMainLoop = loadMainLoop(mainloopstr,argc, argv);
96
97                 if(!mMainLoop)
98                 {
99                         DebugOut(0)<<"Failed to load main loop plugin."<<endl;
100                 }
101         }
102         else if(!mMainLoop)
103         {
104                 /// there is no mainloop entry, use default glib
105                 DebugOut()<<"No mainloop specified in config.  Using glib by default."<<endl;
106                 mMainLoop = new GlibMainLoop(argc,argv);
107         }
108         
109         json_object *sourcesobject = json_object_object_get(rootobject,"sources");
110
111         if(!sourcesobject)
112         {
113                 DebugOut()<<"Error getting sources member: "<<endl;
114                 throw std::runtime_error("Error getting sources member");
115         }
116         
117         //g_assert(json_reader_is_array(reader));
118         g_assert(json_object_get_type(sourcesobject)==json_type_array);
119         
120         
121         array_list *sourceslist = json_object_get_array(sourcesobject);
122         if (!sourceslist)
123         {
124           DebugOut() << "Error getting source list" << endl;
125           throw std::runtime_error("Error getting sources list");
126         }
127         
128         for(int i=0; i < array_list_length(sourceslist); i++)
129         {
130                 json_object *obj = (json_object*)array_list_get_idx(sourceslist,i); //This is an object
131                 
132                 std::map<std::string, std::string> configurationMap;
133                 json_object_object_foreach(obj, key, val)
134                 {
135                         string valstr = json_object_get_string(val);
136                         DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;
137                         configurationMap[key] = valstr;
138                 }
139
140                 string path = configurationMap["path"];
141
142                 AbstractSource* plugin = loadPlugin<AbstractSource*>(path,configurationMap);
143                 
144                 if(plugin != nullptr)
145                 {
146                         mSources.push_back(plugin);
147                 }
148         }
149
150         //json_object_put(sourcesobject);
151         ///read the sinks:
152         
153         json_object *sinksobject = json_object_object_get(rootobject,"sinks");
154         
155         if (!sinksobject)
156         {
157           DebugOut() << "Error getting sink object" << endl;
158           throw std::runtime_error("Error getting sink object");
159         }
160         
161         
162         
163         array_list *sinkslist = json_object_get_array(sinksobject);
164         
165         
166         if (!sinkslist)
167         {
168           DebugOut() << "Error getting sink list" << endl;
169           throw std::runtime_error("Error getting sink list");
170         }
171         
172         
173         for(int i=0; i < array_list_length(sinkslist); i++)
174         {
175                 json_object *obj = (json_object*)array_list_get_idx(sinkslist,i);
176
177                 std::map<std::string, std::string> configurationMap;
178
179                 json_object_object_foreach(obj, key, val)
180                 {
181                         string valstr = json_object_get_string(val);
182                         DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;
183                         configurationMap[key] = valstr;
184                 }
185
186                 
187                 string path = configurationMap["path"];
188
189                 AbstractSinkManager* plugin = loadPlugin<AbstractSinkManager*>(path, configurationMap);
190
191                 if(plugin == nullptr)
192                 {
193                         throw std::runtime_error("plugin is not a SinkManager");
194                 }
195                 else
196                 {
197                         mSinkManagers.push_back(plugin);
198                 }
199         }
200
201         //json_object_put(sinksobject);
202         json_object_put(rootobject);
203         json_tokener_free(tokener);
204 }
205
206 PluginLoader::~PluginLoader()
207 {
208         for(auto itr = mSinkManagers.begin(); itr != mSinkManagers.end(); itr++)
209         {
210                 delete *itr;
211         }
212
213         auto handle = openHandles.begin();
214         while(handle != openHandles.end())
215                 lt_dlclose(*handle++);
216         lt_dlexit();
217 }
218
219 IMainLoop *PluginLoader::mainloop()
220 {
221         return mMainLoop;
222 }
223
224 SourceList PluginLoader::sources()
225 {
226         return mSources;
227 }
228
229
230
231 std::string PluginLoader::errorString()
232 {
233         return mErrorString;
234 }
235