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"
25 #include <boost/concept_check.hpp>
26 //#include <json-glib/json-glib.h>
31 /********************************************
32 * Example JSON config:
34 * sources: [ path1, path2, path3 ]
35 * sinks: [ path1, path2, path3 ]
38 **********************************************/
40 std::string get_file_contents(const char *filename)
42 //FILE *in = fopen(filename,"r");
44 std::ifstream in(filename, std::ios::in);
54 PluginLoader::PluginLoader(string configFile, AbstractRoutingEngine* re, int argc, char** argv): f_create(NULL), routingEngine(re), mMainLoop(nullptr)
57 DebugOut()<<"Loading config file: "<<configFile<<endl;
58 json_object *rootobject;
59 json_tokener *tokener = json_tokener_new();
60 std::string configBuffer = get_file_contents(configFile.c_str());
61 enum json_tokener_error err;
64 rootobject = json_tokener_parse_ex(tokener, configBuffer.c_str(),configBuffer.length());
65 } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue);
66 if (err != json_tokener_success)
68 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(err));
69 // Handle errors, as appropriate for your application.
71 if (tokener->char_offset < configFile.length()) // XXX shouldn't access internal fields
73 // Handle extra characters after parsed object as desired.
74 // e.g. issue an error, parse another object from that point, etc...
77 //DebugOut()<<"Config members: "<<json_reader_count_members(reader)<<endl;
78 json_object *mainloopobject = json_object_object_get(rootobject,"mainloop");
81 /// there is a mainloop entry. Load the plugin:
83 string mainloopstr = string(json_object_get_string(mainloopobject));
85 mMainLoop = loadMainLoop(mainloopstr,argc, argv);
89 DebugOut(0)<<"Failed to load main loop plugin."<<endl;
94 /// there is no mainloop entry, use default glib
95 DebugOut()<<"No mainloop specified in config. Using glib by default."<<endl;
96 mMainLoop = new GlibMainLoop(argc,argv);
99 json_object *sourcesobject = json_object_object_get(rootobject,"sources");
103 DebugOut()<<"Error getting sources member: "<<endl;
104 throw std::runtime_error("Error getting sources member");
107 //g_assert(json_reader_is_array(reader));
108 g_assert(json_object_get_type(sourcesobject)==json_type_array);
111 array_list *sourceslist = json_object_get_array(sourcesobject);
114 DebugOut() << "Error getting source list" << endl;
115 throw std::runtime_error("Error getting sources list");
118 for(int i=0; i < array_list_length(sourceslist); i++)
120 json_object *obj = (json_object*)array_list_get_idx(sourceslist,i); //This is an object
122 std::map<std::string, std::string> configurationMap;
123 json_object_object_foreach(obj, key, val)
125 string valstr = json_object_get_string(val);
126 DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;
127 configurationMap[key] = valstr;
129 json_object *pathobject = json_object_object_get(obj,"path");
130 string path = string(json_object_get_string(pathobject));
132 AbstractSource* plugin = loadPlugin<AbstractSource*>(path,configurationMap);
134 if(plugin != nullptr)
136 mSources.push_back(plugin);
138 json_object_put(pathobject);
140 DebugOut() << "Trying to free list" << endl;
141 array_list_free(sourceslist);
142 DebugOut() << "Trying to free obj" << endl;
143 //json_object_put(sourcesobject);
144 DebugOut() << "Done first" << endl;
147 json_object *sinksobject = json_object_object_get(rootobject,"sinks");
151 DebugOut() << "Error getting sink object" << endl;
152 throw std::runtime_error("Error getting sink object");
157 array_list *sinkslist = json_object_get_array(sinksobject);
162 DebugOut() << "Error getting sink list" << endl;
163 throw std::runtime_error("Error getting sink list");
167 for(int i=0; i < array_list_length(sinkslist); i++)
169 json_object *obj = (json_object*)array_list_get_idx(sinkslist,i);
171 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;
181 json_object *pathobject = json_object_object_get(obj,"path");
182 string path = string(json_object_get_string(pathobject));
184 AbstractSinkManager* plugin = loadPlugin<AbstractSinkManager*>(path, configurationMap);
186 if(plugin == nullptr)
188 throw std::runtime_error("plugin is not a SinkManager");
190 json_object_put(pathobject);
191 //json_object_put(obj);
194 DebugOut() << "Trying to free list" << endl;
195 array_list_free(sinkslist);
196 DebugOut() << "Trying to free obj" << endl;
197 //json_object_put(sinksobject);
198 DebugOut() << "Done" << endl;
201 ///TODO: this will probably explode:
203 //if(error) g_error_free(error);
205 //g_object_unref(reader);
206 //g_object_unref(parser);
210 PluginLoader::~PluginLoader()
212 for(auto itr = mSinks.begin(); itr != mSinks.end(); itr++)
217 for(auto itr = mSources.begin(); itr != mSources.end(); itr++)
223 SinkList PluginLoader::sinks()
228 IMainLoop *PluginLoader::mainloop()
233 SourceList PluginLoader::sources()
240 std::string PluginLoader::errorString()