2 Copyright (C) 2012 Intel Corporation
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.
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.
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
20 #include "pluginloader.h"
21 #include "glibmainloop.h"
26 #include <boost/concept_check.hpp>
27 //#include <json-glib/json-glib.h>
32 std::string get_file_contents(const char *filename)
34 //FILE *in = fopen(filename,"r");
36 std::ifstream in(filename, std::ios::in);
46 PluginLoader::PluginLoader(string configFile, int argc, char** argv): f_create(NULL), routingEngine(nullptr), mMainLoop(nullptr)
48 DebugOut()<<"Loading config file: "<<configFile<<endl;
49 json_object *rootobject;
50 json_tokener *tokener = json_tokener_new();
51 std::string configBuffer = get_file_contents(configFile.c_str());
52 if(configBuffer == "")
54 throw std::runtime_error("No config or config empty");
56 enum json_tokener_error err;
59 rootobject = json_tokener_parse_ex(tokener, configBuffer.c_str(),configBuffer.length());
60 } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue);
61 if (err != json_tokener_success)
63 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(err));
64 // Handle errors, as appropriate for your application.
65 throw std::runtime_error("Invalid config");
67 if (tokener->char_offset < configFile.length()) // XXX shouldn't access internal fields
69 // Handle extra characters after parsed object as desired.
70 // e.g. issue an error, parse another object from that point, etc...
73 json_object *coreobject = json_object_object_get(rootobject,"routingEngine");
76 /// there is a mainloop entry. Load the plugin:
78 string restr = string(json_object_get_string(coreobject));
80 routingEngine = loadRoutingEngine(restr);
84 DebugOut(DebugOut::Warning)<<"Failed to load routing engine plugin: "<<restr<<endl;
90 /// there is no mainloop entry, use default glib
91 DebugOut()<<"No routing engine specified in config. Using built-in 'core' routing engine by default."<<endl;
93 /// core wants some specific configuration settings:
94 std::map<std::string,std::string> settings;
96 json_object *lpq = json_object_object_get(rootobject,"lowPriorityQueueSize");
99 /// there is a mainloop entry. Load the plugin:
101 string restr = string(json_object_get_string(lpq));
102 settings["lowPriorityQueueSize"] = restr;
105 json_object *npq = json_object_object_get(rootobject,"normalPriorityQueueSize");
108 /// there is a mainloop entry. Load the plugin:
110 string restr = string(json_object_get_string(npq));
111 settings["normalPriorityQueueSize"] = restr;
114 json_object *hpq = json_object_object_get(rootobject,"highPriorityQueueSize");
117 /// there is a mainloop entry. Load the plugin:
119 string restr = string(json_object_get_string(hpq));
120 settings["highPriorityQueueSize"] = restr;
123 routingEngine = new Core(settings);
128 json_object *mainloopobject = json_object_object_get(rootobject,"mainloop");
131 /// there is a mainloop entry. Load the plugin:
133 string mainloopstr = string(json_object_get_string(mainloopobject));
135 mMainLoop = loadMainLoop(mainloopstr,argc, argv);
139 DebugOut(DebugOut::Warning)<<"Failed to load main loop plugin."<<endl;
144 /// there is no mainloop entry, use default glib
145 DebugOut()<<"No mainloop specified in config. Using glib by default."<<endl;
146 mMainLoop = new GlibMainLoop(argc,argv);
149 json_object *sourcesobject = json_object_object_get(rootobject,"sources");
153 DebugOut()<<"Error getting sources member: "<<endl;
154 throw std::runtime_error("Error getting sources member");
157 //g_assert(json_reader_is_array(reader));
158 g_assert(json_object_get_type(sourcesobject)==json_type_array);
161 array_list *sourceslist = json_object_get_array(sourcesobject);
164 DebugOut() << "Error getting source list" << endl;
165 throw std::runtime_error("Error getting sources list");
168 for(int i=0; i < array_list_length(sourceslist); i++)
170 json_object *obj = (json_object*)array_list_get_idx(sourceslist,i); //This is an object
172 std::map<std::string, std::string> configurationMap;
173 json_object_object_foreach(obj, key, val)
175 string valstr = json_object_get_string(val);
176 DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;
177 configurationMap[key] = valstr;
180 string path = configurationMap["path"];
182 AbstractSource* plugin = loadPlugin<AbstractSource*>(path,configurationMap);
184 if(plugin != nullptr)
186 mSources.push_back(plugin);
190 //json_object_put(sourcesobject);
193 json_object *sinksobject = json_object_object_get(rootobject,"sinks");
197 DebugOut() << "Error getting sink object" << endl;
198 throw std::runtime_error("Error getting sink object");
201 array_list *sinkslist = json_object_get_array(sinksobject);
206 DebugOut() << "Error getting sink list" << endl;
207 throw std::runtime_error("Error getting sink list");
211 for(int i=0; i < array_list_length(sinkslist); i++)
213 json_object *obj = (json_object*)array_list_get_idx(sinkslist,i);
215 std::map<std::string, std::string> configurationMap;
217 json_object_object_foreach(obj, key, val)
219 string valstr = json_object_get_string(val);
220 DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;
221 configurationMap[key] = valstr;
225 string path = configurationMap["path"];
227 AbstractSinkManager* plugin = loadPlugin<AbstractSinkManager*>(path, configurationMap);
229 if(plugin == nullptr)
231 DebugOut()<<"plugin is not a SinkManager"<<endl;
233 AbstractSink* sink = loadPlugin<AbstractSink*>(path, configurationMap);
237 DebugOut(DebugOut::Warning)<<"plugin seems to be invalid: "<<path<<endl;
242 mSinkManagers.push_back(plugin);
246 //json_object_put(sinksobject);
247 json_object_put(rootobject);
248 json_tokener_free(tokener);
250 Core* core = static_cast<Core*>(routingEngine);
251 if( core != nullptr )
253 core->inspectSupported();
257 PluginLoader::~PluginLoader()
259 for(auto i :mSinkManagers)
264 for(auto handle : openHandles)
268 IMainLoop *PluginLoader::mainloop()
273 SourceList PluginLoader::sources()
278 std::string PluginLoader::errorString()