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>
28 std::string get_file_contents(const char *filename)
30 //FILE *in = fopen(filename,"r");
32 std::ifstream in(filename, std::ios::in);
43 PluginLoader::PluginLoader(string configFile, int argc, char** argv): f_create(NULL), routingEngine(nullptr), mMainLoop(nullptr)
45 DebugOut()<<"Loading config file: "<<configFile<<endl;
46 json_object *rootobject;
47 json_tokener *tokener = json_tokener_new();
48 std::string configBuffer = get_file_contents(configFile.c_str());
49 if(configBuffer == "")
51 throw std::runtime_error("No config or config empty");
53 enum json_tokener_error err;
56 rootobject = json_tokener_parse_ex(tokener, configBuffer.c_str(),configBuffer.length());
57 } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue);
58 if (err != json_tokener_success)
60 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(err));
61 // Handle errors, as appropriate for your application.
62 throw std::runtime_error("Invalid config");
64 if (tokener->char_offset < configFile.length()) // XXX shouldn't access internal fields
66 // Handle extra characters after parsed object as desired.
67 // e.g. issue an error, parse another object from that point, etc...
70 json_object *coreobject = json_object_object_get(rootobject,"routingEngine");
73 /// there is a mainloop entry. Load the plugin:
75 string restr = string(json_object_get_string(coreobject));
77 routingEngine = loadRoutingEngine(restr);
81 DebugOut(DebugOut::Warning)<<"Failed to load routing engine plugin: "<<restr<<endl;
87 /// there is no mainloop entry, use default glib
88 DebugOut()<<"No routing engine specified in config. Using built-in 'core' routing engine by default."<<endl;
90 /// core wants some specific configuration settings:
91 std::map<std::string,std::string> settings;
93 json_object *lpq = json_object_object_get(rootobject,"lowPriorityQueueSize");
96 /// there is a mainloop entry. Load the plugin:
98 string restr = string(json_object_get_string(lpq));
99 settings["lowPriorityQueueSize"] = restr;
102 json_object *npq = json_object_object_get(rootobject,"normalPriorityQueueSize");
105 /// there is a mainloop entry. Load the plugin:
107 string restr = string(json_object_get_string(npq));
108 settings["normalPriorityQueueSize"] = restr;
111 json_object *hpq = json_object_object_get(rootobject,"highPriorityQueueSize");
114 /// there is a mainloop entry. Load the plugin:
116 string restr = string(json_object_get_string(hpq));
117 settings["highPriorityQueueSize"] = restr;
120 routingEngine = new Core(settings);
125 json_object *mainloopobject = json_object_object_get(rootobject,"mainloop");
128 /// there is a mainloop entry. Load the plugin:
130 string mainloopstr = string(json_object_get_string(mainloopobject));
132 mMainLoop = loadMainLoop(mainloopstr,argc, argv);
136 DebugOut(DebugOut::Warning)<<"Failed to load main loop plugin."<<endl;
141 /// there is no mainloop entry, use default glib
142 DebugOut()<<"No mainloop specified in config. Using glib by default."<<endl;
143 mMainLoop = new GlibMainLoop(argc,argv);
146 json_object *sourcesobject = json_object_object_get(rootobject,"sources");
150 DebugOut()<<"Error getting sources member: "<<endl;
151 throw std::runtime_error("Error getting sources member");
154 //g_assert(json_reader_is_array(reader));
155 g_assert(json_object_get_type(sourcesobject)==json_type_array);
158 array_list *sourceslist = json_object_get_array(sourcesobject);
161 DebugOut() << "Error getting source list" << endl;
162 throw std::runtime_error("Error getting sources list");
165 for(int i=0; i < array_list_length(sourceslist); i++)
167 json_object *obj = (json_object*)array_list_get_idx(sourceslist,i); //This is an object
169 std::map<std::string, std::string> configurationMap;
170 json_object_object_foreach(obj, key, val)
172 string valstr = json_object_get_string(val);
173 DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;
174 configurationMap[key] = valstr;
177 string path = configurationMap["path"];
179 if(!loadPlugin(path,configurationMap))
180 DebugOut(DebugOut::Warning) << "Failed to load plugin: " << path <<endl;
183 //json_object_put(sourcesobject);
186 json_object *sinksobject = json_object_object_get(rootobject,"sinks");
190 DebugOut() << "Error getting sink object" << endl;
191 throw std::runtime_error("Error getting sink object");
194 array_list *sinkslist = json_object_get_array(sinksobject);
199 DebugOut() << "Error getting sink list" << endl;
200 throw std::runtime_error("Error getting sink list");
204 for(int i=0; i < array_list_length(sinkslist); i++)
206 json_object *obj = (json_object*)array_list_get_idx(sinkslist,i);
208 std::map<std::string, std::string> configurationMap;
210 json_object_object_foreach(obj, key, val)
212 string valstr = json_object_get_string(val);
213 DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;
214 configurationMap[key] = valstr;
218 string path = configurationMap["path"];
220 loadPlugin(path, configurationMap);
223 //json_object_put(sinksobject);
224 json_object_put(rootobject);
225 json_tokener_free(tokener);
227 Core* core = static_cast<Core*>(routingEngine);
228 if( core != nullptr )
230 core->inspectSupported();
234 PluginLoader::~PluginLoader()
236 for(auto handle : openHandles)
240 IMainLoop *PluginLoader::mainloop()
245 std::string PluginLoader::errorString()